forked from Happy-Algorithms-League/hal-cgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cgp_graph.py
204 lines (144 loc) · 5.5 KB
/
test_cgp_graph.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import numpy as np
import matplotlib.pyplot as plt
import pickle
import pytest
import sympy
import sys
import torch
from itertools import product
sys.path.insert(0, '../')
import gp
SEED = np.random.randint(2 ** 31)
def test_direct_input_output():
params = {
'n_inputs': 1,
'n_outputs': 1,
'n_columns': 3,
'n_rows': 3,
'levels_back': 2,
}
primitives = [gp.CGPAdd, gp.CGPSub]
genome = gp.CGPGenome(params['n_inputs'], params['n_outputs'], params['n_columns'], params['n_rows'], params['levels_back'], primitives)
genome.randomize(np.random)
genome[-2:] = [0, None] # set inputs for output node to input node
graph = gp.CGPGraph(genome)
x = [2.14159]
y = graph(x)
assert abs(x[0] - y[0]) < 1e-15
def test_compile_simple():
primitives = [gp.CGPAdd]
genome = gp.CGPGenome(2, 1, 1, 1, 1, primitives)
genome.dna = [-1, None, None, -1, None, None, 0, 0, 1, -2, 2, None]
graph = gp.CGPGraph(genome)
f = graph.compile_func()
x = [5., 2.]
y = f(x)
assert abs(x[0] + x[1] - y[0]) < 1e-15
primitives = [gp.CGPSub]
genome = gp.CGPGenome(2, 1, 1, 1, 1, primitives)
genome.dna = [-1, None, None, -1, None, None, 0, 0, 1, -2, 2, None]
graph = gp.CGPGraph(genome)
f = graph.compile_func()
x = [5., 2.]
y = f(x)
assert abs(x[0] - x[1] - y[0]) < 1e-15
def test_compile_two_columns():
primitives = [gp.CGPAdd, gp.CGPSub]
genome = gp.CGPGenome(2, 1, 2, 1, 1, primitives)
genome.dna = [-1, None, None, -1, None, None, 0, 0, 1, 1, 0, 2, -2, 3, None]
graph = gp.CGPGraph(genome)
f = graph.compile_func()
x = [5., 2.]
y = f(x)
assert abs(x[0] - (x[0] + x[1]) - y[0]) < 1e-15
def test_compile_two_columns_two_rows():
primitives = [gp.CGPAdd, gp.CGPSub]
genome = gp.CGPGenome(2, 2, 2, 2, 1, primitives)
genome.dna = [-1, None, None, -1, None, None, 0, 0, 1, 1, 0, 1, 0, 0, 2, 0, 2, 3, -2, 4, None, -2, 5, None]
graph = gp.CGPGraph(genome)
f = graph.compile_func()
x = [5., 2.]
y = f(x)
assert abs(x[0] + (x[0] + x[1]) - y[0]) < 1e-15
assert abs((x[0] + x[1]) + (x[0] - x[1]) - y[1]) < 1e-15
def test_compile_addsubmul():
params = {
'n_inputs': 2,
'n_outputs': 1,
'n_columns': 2,
'n_rows': 2,
'levels_back': 1,
}
primitives = [gp.CGPAdd, gp.CGPSub, gp.CGPMul]
genome = gp.CGPGenome(params['n_inputs'], params['n_outputs'], params['n_columns'], params['n_rows'], params['levels_back'], primitives)
genome.dna = [
-1, None, None, -1, None, None,
2, 0, 1, 1, 0, 1,
1, 2, 3, 0, 0, 0,
-2, 4, None]
graph = gp.CGPGraph(genome)
f = graph.compile_func()
x = [5., 2.]
y = f(x)
assert(abs(((x[0] * x[1]) - (x[0] - x[1])) - y[0]) < 1e-15)
def test_compile_torch_and_backprop():
primitives = [gp.CGPMul, gp.CGPConstantFloat]
genome = gp.CGPGenome(1, 1, 2, 2, 1, primitives)
genome.dna = [-1, None, None, 1, None, None, 1, None, None, 0, 0, 1, 0, 0, 1, -2, 3, None]
graph = gp.CGPGraph(genome)
c = graph.compile_torch_class()
optimizer = torch.optim.SGD(c.parameters(), lr=1e-1)
criterion = torch.nn.MSELoss()
for i in range(200):
x = torch.Tensor(1, 1).normal_()
y = c(x)
y_target = -2.14159 * x
loss = criterion(y, y_target)
c.zero_grad()
loss.backward()
optimizer.step()
assert(loss.detach().numpy() < 1e-15)
x = [3.]
x_torch = torch.Tensor(x).view(1, 1)
assert(abs(c(x_torch)[0].detach().numpy() - graph(x))[0] > 1e-15)
graph.update_parameters_from_torch_class(c)
assert(abs(c(x_torch)[0].detach().numpy() - graph(x))[0] < 1e-15)
batch_sizes = [1, 10]
primitives = [gp.CGPMul, gp.CGPConstantFloat]
genomes = [gp.CGPGenome(1, 1, 2, 2, 1, primitives) for i in range(2)]
# Function: f(x) = 1*x
genomes[0].dna = [-1, None, None, 1, None, None, 1, None, None, 0, 0, 1, 0, 0, 1, -2, 3, None]
# Function: f(x) = 1
genomes[1].dna = [-1, None, None, 1, None, None, 1, None, None, 0, 0, 1, 0, 0, 1, -2, 1, None]
@pytest.mark.parametrize("genome, batch_size", product(genomes, batch_sizes))
def test_compile_torch_output_shape(genome, batch_size):
graph = gp.CGPGraph(genome)
c = graph.compile_torch_class()
x = torch.Tensor(batch_size, 1).normal_()
y = c(x)
assert(y.shape == (batch_size, 1))
def test_compile_sympy_expr():
primitives = [gp.CGPAdd, gp.CGPConstantFloat]
genome = gp.CGPGenome(1, 1, 2, 2, 1, primitives)
genome.dna = [-1, None, None, 1, None, None, 1, None, None, 0, 0, 1, 0, 0, 1, -2, 3, None]
graph = gp.CGPGraph(genome)
x_0_target, y_0_target = sympy.symbols('x_0_target y_0_target')
y_0_target = x_0_target + 1.
y_0 = graph.compile_sympy_expr()[0]
for x in np.random.normal(size=100):
assert abs(y_0_target.subs('x_0_target', x).evalf() - y_0.subs('x_0', x).evalf()) < 1e-12
def test_catch_invalid_sympy_expr():
primitives = [gp.CGPSub, gp.CGPDiv]
genome = gp.CGPGenome(1, 1, 2, 1, 1, primitives)
# x[0] / (x[0] - x[0])
genome.dna = [-1, None, None, 0, 0, 0, 1, 0, 1, -2, 2, None]
graph = gp.CGPGraph(genome)
with pytest.raises(gp.exceptions.InvalidSympyExpression):
graph.to_sympy(simplify=True)
def test_allow_powers_of_x_0():
primitives = [gp.CGPSub, gp.CGPMul]
genome = gp.CGPGenome(1, 1, 2, 1, 1, primitives)
# x[0] ** 2
genome.dna = [-1, None, None, 0, 0, 0, 1, 0, 0, -2, 2, None]
graph = gp.CGPGraph(genome)
graph.to_sympy(simplify=True)