-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.py
51 lines (39 loc) · 1.01 KB
/
ga.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import random
from ga_defs import *
from celery.task.sets import TaskSet
pop_size=50
genome_size=10
threshold=0.5
population=[genome(genome_size) for k in range(pop_size)]
dom=domain()
eval_concurrent=True
generations=5
generation=0
while generation<generations:
generation+=1
#evaluate sequential
if(not eval_concurrent):
for k in population:
k.fitness = dom.evaluate(k)
else:
#evaluate concurrent
tasks=[]
for ind in population:
tasks.append(evaltask.subtask((dom,ind)))
job = TaskSet(tasks=tasks)
result=job.apply_async()
fits=result.join()
for k in range(len(population)):
population[k].fitness=fits[k]
population.sort(key=lambda k:k.fitness)
#print best fitness
print "generation: %d, best fitness %0.3f" % (generation,population[-1].fitness)
#reproduce
newpop = []
cutoff = int(len(population)*threshold)
cutoff_pop = population[cutoff:]
for k in range(len(population)):
new=genome(0,random.choice(cutoff_pop).genes)
new.mutate()
newpop.append(new)
population=newpop