-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimate.py
74 lines (60 loc) · 2.22 KB
/
animate.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
'''
Created on Apr 24, 2011
@author: erik
'''
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import math
import time
class Animation:
""" Class for animating a microGP experiment.
An Animation instance is initialized with a microGP experiment instance.
When called the instance plots the target function, and then successively
plots the best match from each generation.
"""
def __init__(self, experiment):
plt.ion()
self.target = experiment.target_func
self.domain = experiment.fitness_accuracy
self.populations = experiment.populations
self.pdf_out = None
def __call__(self):
best = []
for population in self.populations:
best.append(population[0])
plot_file = "plot_" + time.strftime('%Y_%m_%d_%H_%M_%S')
try:
self.pdf_out = PdfPages(plot_file + '.pdf')
except IOError:
print 'Could not create PDF file. File may be open'
y_values = []
for x in self.domain:
y_values.append(eval(self.target))
line, = plt.plot(self.domain, y_values, label = "Target")
plt.legend()
plt.draw()
gen_count = 0
line, = plt.plot(self.domain, self.domain)
for candidate in best:
gen_count = gen_count + 1
y_values = []
fitness = candidate[0]
function = candidate[1]
for x in self.domain:
y_values.append(function([x]))
line.set_ydata(y_values)
line.set_label('Fitness: %.5f' % (fitness))
plt.legend(loc=8, ncol=2, mode='expand')
plt.title('Generation %i' % gen_count)
plt.draw()
if self.pdf_out:
try:
self.pdf_out.savefig()
except IOError:
print 'Could not save to PDF file. File may be open.'
plt.show()
if self.pdf_out:
try:
self.pdf_out.close()
except IOError:
print 'Could not close PDF file.'