-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo_display.py
More file actions
91 lines (72 loc) · 1.88 KB
/
demo_display.py
File metadata and controls
91 lines (72 loc) · 1.88 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import cPickle
import math
import itertools
from matplotlib import pyplot
import sys
sys.path.append("tasks/")
import representations
def float_range(frm, to, step):
while frm < to:
yield frm
frm += step
def rastrigin(x):
return 10 + (x**2 - (10 * math.cos(2 * math.pi * x)))
def fc(cs, z):
f = 0
for j, c in enumerate(cs):
f += c * (z ** j)
return f
f1 = open("demo-out-1")
f2 = open("demo-out-2")
bounds = [x for x in float_range(-5.2, 5.2, 0.1)]
last_best_f1 = None
last_best_f2 = None
# self._values = [rastrigin(x) for x in self._range]
pyplot.ion()
generation = 1
done1 = done2 = False
first = True
while True:
try:
best1 = cPickle.load(f1)
except EOFError:
done1 = True
try:
best2 = cPickle.load(f2)
except EOFError:
done2 = True
if done1 and done2:
print "DONE"
break
g1 = "%i(%s)" % (best1.generation, "D")
if not done1:
g1 = best1.generation
g2 = "%i(%s)" % (best2.generation, "D")
if not done2:
g2 = best2.generation
print "%i %i %i" % (generation, g1, g2)
generation += 1
if last_best_f1 == best1 and last_best_f2 == best2:
continue
last_best_f1 = best1
last_best_f2 = best2
pyplot.gcf().clear()
pyplot.vlines([-1.2, 1.2], -1, 5)
pyplot.vlines([-1, 1], 1, 5)
pyplot.hlines(1, -1, 1)
pyplot.hlines(-1, -1.2, 1.2)
points = [fc(best1.genes, x) for x in bounds]
pyplot.plot(bounds, points, "green")
points = [fc(best2.genes, x) for x in bounds]
pyplot.plot(bounds, points, "red")
pyplot.ylim([-3, 3])
pyplot.xlim([-1.5, 1.5])
pyplot.axhline(0, color='black')
pyplot.axvline(0, color='black')
# pyplot.plot(genes, fitness, 'ro')
pyplot.pause(0.0001)
if first:
first = not first
raw_input()
# self._last_genes = genes
# self._last_fitness = fitness