%PDF- %PDF-
| Direktori : /home/waritko/suave/EVO/ |
| Current File : //home/waritko/suave/EVO/evo.py |
#from Tkinter import *
from graphics import *
from collections import namedtuple
import random
import math
from PIL import Image as NewImage
CONST_COUNT = 50 #count of object on screen
CONST_SHAPE = "circle" #preferet shape circle, triangle, square, rectangle
CONST_ROUNDS = 5 #number of user evaluation
CONST_SAMPLES = 9 #count of samples to evaluate in one round
CONST_CANVAS = 500 #canvas size
CONST_MUTATE = 10 # 1/COUNT_MUTATA percent to mutate
#TODO spocitat bity aby se menily kdyz se zmeni velikost canvas a nesmi byt mene nez 255
CONST_BITS = 10 # MUST BE 8 or larger
SampleStruct = namedtuple("Sample", "Items fitness")
ItemStruct = namedtuple("ItemStruct", "x y z r g b")
population = []
def create_circle(self, x, y, r, **kwargs):
return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)
def printSampleToEvaluate(Sample):
win = GraphWin("User input", CONST_CANVAS, CONST_CANVAS)
#root = Tk("Userinput")
#cv = Canvas(root, width=CONST_CANVAS, height=CONST_CANVAS, bg='black')
win.setBackground("black")
Items = Sample.Items
i = 0
while i < CONST_COUNT:
color = color_rgb(Items[i].r, Items[i].g, Items[i].b) #'#%02x%02x%02x' % (Items[i].r, Items[i].g, Items[i].b) #
c = Circle(Point(Items[i].x, Items[i].y), Items[i].z)
#root.Canvas.create_circle = create_circle(Items[i].x, Items[i].y, Items[i].z, fill=color, outline="black", width=2)
c.setFill(color)
c.draw(win)
#root.mainloop()
i = i + 1
#the_input = raw_input("Enter input: ")
#imageToSave = Image(c)
#imageToSave.save('/savedImg.jpg')
editedSample = SampleStruct(Sample.Items, int(input("1 - worst, 5 - best: ")))
filemane = "img" + str(random.randint(0,255)) #str(Items[i].r) + str(Items[i].g) + str(Items[i].b) + str(Items[i].x) + str(Items[i].y) + str(Items[i].z)
win.postscript(file=filemane, colormode='color')
win.close()
img = NewImage.open("img" + str(random.randint(0,255)) + ".eps")
img.save("img" + str(random.randint(0,255)), "gif")
return editedSample
def generateSample():
Items = []
i = 0
while i < CONST_COUNT:
x = random.randint(0, CONST_CANVAS) # Create a random x value
y = random.randint(0, CONST_CANVAS) # Create a random y value
z = random.randint(0, math.floor(CONST_CANVAS/2)) # radius
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
Items.append(ItemStruct(x, y, z, r, g, b))
i = i + 1
CreatedSample = SampleStruct(Items, 0)
return CreatedSample
def weighted_choice(choices):
total = sum(w for c, w in choices)
r = random.uniform(0, total)
upto = 0
for c, w in choices:
if upto + w >= r:
return SampleStruct(c,w)
upto += w
def mutate(wade): #Wade Wilson
roll = random.randint(0, CONST_MUTATE)
if(roll == CONST_MUTATE):
where = random.randint(0, len(wade)-1)
deadpool = list(wade)
if(wade[where] == '0'):
deadpool[where] = '1'
else:
deadpool[where] = '0'
return ''.join(deadpool)
return wade
def crossing(pOne, pTwo): #one sample on input, is called CONST_SAMPLES times
#TODO crossin!!!
children = []
OneItems = []
TwoItems = []
#while sch < CONST_SAMPLES: # Pro kazdy z 9 obrazku
pItemsOne = pOne.Items
pItemsTwo = pTwo.Items
binaryOne = ""
binaryTwo = ""
circle = 0
while circle < CONST_COUNT: #Pro kazdou kruznici
binaryOne += ("{0:b}".format(pItemsOne[circle].x).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsOne[circle].y).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsOne[circle].z).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsOne[circle].r).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsOne[circle].g).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsOne[circle].b).zfill(CONST_BITS))
binaryTwo += ("{0:b}".format(pItemsTwo[circle].x).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsTwo[circle].y).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsTwo[circle].z).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsTwo[circle].r).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsTwo[circle].g).zfill(CONST_BITS)) + \
("{0:b}".format(pItemsTwo[circle].b).zfill(CONST_BITS))
circle += 1
binaryOne = mutate(binaryOne)
binaryTwo = mutate(binaryTwo)
crossingPoint = random.randint(1,CONST_BITS*6*CONST_COUNT-2)
newBinaryOne = binaryOne[0:crossingPoint] + binaryTwo[crossingPoint:CONST_BITS*6*CONST_COUNT]
newBinaryTwo = binaryTwo[0:crossingPoint] + binaryOne[crossingPoint:CONST_BITS*6*CONST_COUNT]
circle2 = 0
offset = 0
while circle2 < CONST_COUNT:
OneItems.append(ItemStruct(int(newBinaryOne[offset:offset + CONST_BITS], 2),
int(newBinaryOne[offset + CONST_BITS:offset + 2 * CONST_BITS], 2),
int(newBinaryOne[offset + 2 * CONST_BITS:offset + 3 * CONST_BITS], 2),
int(newBinaryOne[offset + 3 * CONST_BITS + CONST_BITS-8 : offset + 4 * CONST_BITS], 2), #r
int(newBinaryOne[offset + 4 * CONST_BITS + CONST_BITS-8 : offset + 5 * CONST_BITS], 2), #g
int(newBinaryOne[offset + 5 * CONST_BITS + CONST_BITS-8 : offset + 6 * CONST_BITS], 2))) #b
TwoItems.append(ItemStruct(int(newBinaryTwo[offset:offset + CONST_BITS], 2),
int(newBinaryTwo[offset + CONST_BITS:offset + 2 * CONST_BITS], 2),
int(newBinaryTwo[offset + 2 * CONST_BITS :offset + 3 * CONST_BITS], 2),
int(newBinaryTwo[offset + 3 * CONST_BITS + CONST_BITS-8 :offset + 4 * CONST_BITS], 2),
int(newBinaryTwo[offset + 4 * CONST_BITS + CONST_BITS-8 :offset + 5 * CONST_BITS], 2),
int(newBinaryTwo[offset + 5 * CONST_BITS + CONST_BITS-8:offset + 6 * CONST_BITS], 2)))
offset += CONST_BITS*6
circle2 +=1
#sch = sch+1
newPOne = SampleStruct(OneItems, 0)
newPTwo = SampleStruct(TwoItems, 0)
children.append(newPOne)
children.append(newPTwo)
return children
def selection(parentPopulation):
childPopulation = []
ss = 0
while ss < CONST_SAMPLES:
parentOne = weighted_choice(parentPopulation)
parentTwo = weighted_choice(parentPopulation)
crossedChildrens = crossing(parentOne, parentTwo)
childPopulation.append(crossedChildrens[0])
if(ss != CONST_SAMPLES-1):
childPopulation.append(crossedChildrens[1])
ss = ss+2
return childPopulation
r = 0
while r < CONST_ROUNDS:
if(r == 0):
sf = 0
while sf < CONST_SAMPLES:
newSample = generateSample()
newSample = printSampleToEvaluate(newSample)
population.append(newSample)
sf = sf+1
else:
print("\n Next genration!")
s = 0
while s < CONST_SAMPLES:
newSample = printSampleToEvaluate(population[s])
population[s] = newSample
s = s+1
population = selection(population)
r = r+1