/
multi.py
90 lines (54 loc) · 1.08 KB
/
multi.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# cython: profile=True
import sys
import time
import random
import copy
import solution
import topFront
def ktourn(pop,k):
best = None
for i in xrange(k):
obj = random.choice(pop)
if not best or obj<best:
best = obj
return best
mu = 100
k = 8
pop = []
i = 0
nodes = 35
m = 4
n = 6
top = topFront.topFront()
while i<mu:
x = solution.solution(nodes,m, n)
x.evaluate()
pop.append(x)
i+=1
top.push(x)
pop.sort()
maxEvals = 50000
cur = mu
children = 40
rate = .001
sk = 10
while cur<maxEvals:
childs = []
for i in xrange(children):
x = ktourn(pop+top.top,k).mate(ktourn(pop+top.top,k))
x.mutate(rate)
x.evaluate()
childs.append(x)
top.push(x)
cur += children
survive = []
for i in xrange(mu):
survive.append(ktourn(pop+childs,sk))
pop = survive
pop.sort()
s = 0.0
for i in pop:
s+= i.fitness
print cur, s/len(pop),pop[0].fitness, "(",pop[0].mFit,",",pop[0].nFit,")",len(top.top)
pop.sort()
pop[0].report()