-
Notifications
You must be signed in to change notification settings - Fork 6
/
symbolic_regression02.py
executable file
·66 lines (47 loc) · 1.44 KB
/
symbolic_regression02.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
#!/usr/bin/env python3
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
# First set up the figure, the axis, and the plot element we want to animate
fig, ax = plt.subplots()
ax.axis([-10.1,8.1,-10.5,10.0])
plt.suptitle("Guess the function")
plt.xlabel("X")
plt.ylabel("Y")
plt.xticks(np.arange(-10, 10, step=2))
plt.grid(True, which="major")
plt.grid(True, which="minor", linestyle=":")
plt.minorticks_on()
s = np.arange(-10, 10, 2)
plt.plot(s, s + np.sin(s), "ro")
np.seterr(divide='ignore')
populations = []
x = np.arange(-10, 8.1, .01)
def read_functions():
with open("pop.txt") as fp:
functions = []
for line in fp:
if line.startswith("----"):
populations.append(functions)
functions = []
else:
functions.append(np.vectorize(eval("lambda X1: " + line)))
read_functions()
graphs = []
def init():
ax.set_title("Generation 0")
global graphs
graphs = []
for f in populations[0]:
g, = ax.plot(x, f(x), "k", linewidth=0.4)
graphs.append(g)
return tuple(graphs)
def update(i):
ax.set_title("Generation " + str(i))
for j, g in enumerate(graphs):
f = populations[i][j]
g.set_data(x, f(x))
return tuple(graphs)
anim = animation.FuncAnimation(fig, update, init_func=init, frames=range(len(populations)), interval=1, blit=True, repeat=True)
plt.show()
#anim.save('line.gif', dpi=80, writer='imagemagick')