-
Notifications
You must be signed in to change notification settings - Fork 0
/
genes.py
executable file
·276 lines (243 loc) · 12.7 KB
/
genes.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Create genes (links & nodes) for generating CPPN graphs
__author__ = "Joe Sarsfield"
__email__ = "joe.sarsfield@gmail.com"
"""
import activations
import random
from copy import deepcopy
from config import gauss_freq_range, func_amp_range, gauss_vshift_range, sin_freq_range, sin_vshift_range
import numpy as np
class GenePool:
"""
Handles the creation and mutation of all CPPN genes throughout the evolutionary process
"""
def __init__(self,
cppn_inputs,
load_genepool=False,
n_dims=2):
self.gene_nodes_in = [] # Nodes that represent input and must exist for every CPPN, these cannot be modified or disabled
self.gene_nodes = [] # Store all hidden and output node genes
self.gene_links = [] # Store all link genes
self._hist_marker_num = -1 # Keeps track of historical marker number
self.activation_functions = activations.ActivationFunctionSet()
self.node_functions = activations.NodeFunctionSet()
self.num_inputs = cppn_inputs
self.n_dims = n_dims # e.g. value of 2 = (2d) x1, y1, x2, y2
if load_genepool is False:
self.create_initial_genes()
print("initial genes created")
def create_initial_genes(self):
""" create initial input & output genes for minimal graph """
# Create input nodes with no activation function
for i in range(self.num_inputs):
self.create_initial_gene_node({"depth": 0,
"activation_func": None,
"node_func": None,
"can_modify": False})
# Create random output node
self.create_initial_gene_node({"depth": 1,
"activation_func": activations.tanh, # TODO is the genome going to encode the activation func?
"node_func": "dot",
"can_modify": False}, is_input=False)
# Add a single initial link for each input node
for i in range(self.num_inputs):
self.create_gene_link((None,
self.gene_nodes[0],
self.gene_nodes_in[i],
self.get_new_hist_marker()))
# Create initial LEO gaussian hidden nodes with bias towards locality
for i in range(self.n_dims):
self.create_initial_gene_node({"depth": 0.5,
"activation_func": activations.gaussian,
"node_func": "diff",
"can_modify": False}, is_input=False)
# Create LEO output node
self.create_initial_gene_node({"depth": 1,
"activation_func": activations.step,
"node_func": "dot",
"can_modify": False}, is_input=False)
# Create LEO input to gaussian links
offset = 1
for i in range(self.num_inputs):
in_ind = offset + (i % self.n_dims)
self.create_gene_link((1,
self.gene_nodes[in_ind],
self.gene_nodes_in[i],
self.get_new_hist_marker()))
# Create LEO gaussian to step output links
for i in range(self.n_dims):
self.create_gene_link((1,
self.gene_nodes[-1],
self.gene_nodes[offset + i],
self.get_new_hist_marker()))
self.gene_links.sort(key=lambda x: x.historical_marker)
"""
def create_minimal_graphs(self, n):
initial generation of n minimal CPPN graphs with random weights
Minimally connected graph with no hidden nodes, each input and output nodes should have at least one link.
Links can only go forwards.
for i in range(n):
act_func = self.activation_functions.get_random_activation_func()
"""
def create_initial_gene_node(self, gene_config, is_input=True):
""" Create input or output gene nodes, these nodes cannot be modified or disabled and are thus treated differently from hidden node"""
gene_config["historical_marker"] = self.get_new_hist_marker()
if is_input:
self.gene_nodes_in.append(GeneNode(**gene_config))
else:
self.gene_nodes.append(GeneNode(**gene_config))
def create_gene_node(self, gene_config):
""" Create a gene e.g. link or node
Must have a historical marker required for crossover of parents
"""
#gene_config["historical_marker"] = self.get_new_hist_marker()
self.gene_nodes.append(GeneNode(*gene_config))
return self.gene_nodes[-1]
def create_gene_link(self, gene_config):
#gene_config["historical_marker"] = self.get_new_hist_marker()
self.gene_links.append(GeneLink(*gene_config))
return self.gene_links[-1]
def get_or_create_gene_link(self, in_node_hist_marker, out_node_hist_marker):
""" Get gene link or create if not exists """
for link in self.gene_links:
if link.in_node.historical_marker == in_node_hist_marker and link.out_node.historical_marker == out_node_hist_marker:
return link
# No existing link so create one
self.gene_links.append(GeneLink(None,
self.get_node_from_hist_marker(in_node_hist_marker),
self.get_node_from_hist_marker(out_node_hist_marker),
self.get_new_hist_marker(),
enabled=True))
return self.gene_links[-1]
def get_new_hist_marker(self):
self._hist_marker_num += 1
return self._hist_marker_num
def get_node_from_hist_marker(self, hist_marker):
for node in self.gene_nodes:
if node.historical_marker == hist_marker:
return node
for node in self.gene_nodes_in:
if node.historical_marker == hist_marker:
return node
raise Exception("No node with historical marker found in func get_node_from_hist_marker genes.py")
def add_new_structures(self, new_genomes, new_structures):
""" add new structural mutations to the gene pool, get unique historical markers and update genomes """
nodes_added = []
hists = []
for i, structures in enumerate(new_structures):
if "new_link" in structures:
link = self.get_or_create_gene_link(structures["new_link"][0], structures["new_link"][1])
if "new_node" in structures:
if new_genomes[i].gene_links[-4].historical_marker is None:
new_genomes[i].gene_links[-4].historical_marker = link.historical_marker
else:
new_genomes[i].gene_links[-3].historical_marker = link.historical_marker
else:
new_genomes[i].gene_links[-1].historical_marker = link.historical_marker
if "new_node" in structures:
# If not new node
try:
ind = nodes_added.index(structures["new_node"])
for node in new_genomes[i].gene_nodes:
if node.historical_marker is None:
node.historical_marker = hists[ind][0]
break
new_genomes[i].gene_links[-2].historical_marker = hists[ind][1]
new_genomes[i].gene_links[-1].historical_marker = hists[ind][2]
except ValueError:
nodes_added.append(structures["new_node"])
hists.append((self.get_new_hist_marker(), self.get_new_hist_marker(), self.get_new_hist_marker()))
self.create_gene_node((structures["node_depth"], None, None, hists[-1][0]))
self.create_gene_link((None, self.get_node_from_hist_marker(structures["new_node"][0]), self.gene_nodes[-1], hists[-1][1]))
self.create_gene_link((None, self.gene_nodes[-1], self.get_node_from_hist_marker(structures["new_node"][1]), hists[-1][2]))
for node in new_genomes[i].gene_nodes:
if node.historical_marker is None:
node.historical_marker = hists[-1][0]
break
new_genomes[i].gene_links[-2].historical_marker = hists[-1][1]
new_genomes[i].gene_links[-1].historical_marker = hists[-1][2]
class Gene:
def __init__(self, historical_marker):
# Constants
self.historical_marker = historical_marker
class GeneLink(Gene):
def __init__(self, weight, in_node, out_node, historical_marker, enabled=True):
super().__init__(historical_marker)
# Constants
self.in_node = in_node
self.out_node = out_node
# Variables - these gene fields can change for different genomes
self.weight = weight
self.enabled = enabled
in_node.add_link(self, True)
out_node.add_link(self, False)
def __eq__(self, other):
return True if self.historical_marker == other.historical_marker else False
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.historical_marker)
class GeneNode(Gene):
def __init__(self, depth,
activation_func,
node_func,
historical_marker,
can_modify=True,
enabled=True,
bias=None,
freq=None,
amp=None,
vshift=None):
super().__init__(historical_marker)
# Constants
self.depth = depth # Ensures CPPN links don't go backwards i.e. DAG
# Variables - these gene fields can change for different genomes
self.bias = bias # Each node has a bias to shift the activation function - this is inherited from the parents and mutated
self.act_func = activation_func # The activation function this node contains. Incoming links are multiplied by their weights and summed before being passed to this func
self.node_func = node_func # function applied to data coming into node before going through activation func
self.ingoing_links = [] # links going into the node
self.outgoing_links = [] # links going out of the node
self.location = None # [x, y] 2d numpy array uniquely set for each CPPNGenome, location may be different for different genomes
self.node_ind = None # Set differently for each genome
self.can_modify = can_modify
self.enabled = enabled
self.freq = freq
self.amp = amp
self.vshift = vshift
if activation_func is not None:
if str(activation_func.__name__)[0] == "g":
if freq is None:
self.freq = random.uniform(-gauss_freq_range, gauss_freq_range)
self.amp = random.uniform(-func_amp_range, func_amp_range)
self.vshift = random.uniform(-gauss_vshift_range, gauss_vshift_range)
elif str(activation_func.__name__)[0] == "s":
if freq is None:
self.freq = random.uniform(-sin_freq_range, sin_freq_range)
self.amp = random.uniform(-func_amp_range, func_amp_range)
self.vshift = random.uniform(-sin_vshift_range, sin_vshift_range)
def __deepcopy__(self, memo):
""" deepcopy but exclude ingoing_links & outgoing_links as these will be created later """
return GeneNode(deepcopy(self.depth, memo),
deepcopy(self.act_func, memo),
deepcopy(self.node_func, memo),
deepcopy(self.historical_marker, memo),
deepcopy(self.can_modify, memo),
deepcopy(self.enabled, memo),
deepcopy(self.bias, memo),
deepcopy(self.freq, memo),
deepcopy(self.amp, memo),
deepcopy(self.vshift, memo))
def __eq__(self, other):
return True if self.historical_marker == other.historical_marker else False
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.historical_marker)
def add_link(self, link, is_ingoing):
if is_ingoing is True:
self.ingoing_links.append(link)
else:
self.outgoing_links.append(link)
def set_loc(self, loc):
self.location = loc