/
molecule.py
427 lines (387 loc) · 14.4 KB
/
molecule.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""
Everything related to a molecule is in here
"""
from atom import *
import random
import networkx as nx
import pygraphviz as pgv
import matplotlib.pyplot as plt
from networkx.readwrite import json_graph
graph_colours = {
"motor":"red",
"sensory":"blue",
"transform":"green"
}
######################
# Base Molecules
######################
class Molecule(object):
"""
Base class for a Molecule
"""
def __init__(self,memory,atoms):
self.atoms = atoms
self.molecular_graph = None
self.active = True
self.active_hist = True
self.memory = memory
self.type = "base"
self.times_tested = 0
id = ""
self.fitness = -999999
self.json = {}
def constructor(self):
pass
def conditional_activate(self):
"""
previous: conditionalActivate
"""
pass
def set_connections(self):
"""
takes graph connections between nodes and
converts them to atom.messages
"""
for n in self.molecular_graph:
atom = self.atoms[n]
connections = self.molecular_graph.predecessors(n)
atom.messages = connections
def get_atoms_as_list(self):
atoms = []
for n in self.molecular_graph:
atom = self.atoms[n]
atoms.append(atom)
return atoms
def get_atom(self,id):
return self.memory.get_atom(id)
def deactivate(self):
self.active = False
self.active_hist = False
def mutate(self):
pass
def add_atom(self):
pass
def delete_atom(self):
pass
def find_atoms_of_types(self,graph,types):
nodes = []
for node in graph.nodes():
atom = self.get_atom(node)
if atom.type in types:
nodes.append(node)
return nodes
def print_graph(self,filename):
graph = nx.to_agraph(self.molecular_graph)
for n in graph.nodes():
try:
colour = graph_colours[self.get_atom(n)].type
except:
colour = 'black'
graph.get_node(n).attr['color'] = colour
graph.layout()
graph.draw('{0}.png'.format(filename))
def to_json(self):
self.json["molecular_graph"] = json_graph.dumps(self.molecular_graph)
self.json["type"] = self.type
self.json["class"]=self.__class__.__name__
self.json["atoms"] = [a.get_json() for a in self.get_atoms_as_list()]
def get_json(self):
self.to_json()
return self.json
def __str__(self):
# long oneliner!
return " - ".join(
["[id:{0} active:{1} type:{2}]".format(self.atoms[a].id,self.atoms[a].active,self.atoms[a].type)
for a in self.molecular_graph.nodes()
])
class GameMolecule(Molecule):
"""
The data structure for a game molecule
"""
def __init__(self,*args):
super(GameMolecule, self).__init__(*args)
self.type = "game"
self.game_atoms = []
def activate(self):
"""
activate molecule
"""
self.times_tested += 1
for atom in self.get_atoms_as_list():
if atom.type == "sensory":
atom.activate()
# check to see if sensory conditions have been met
if atom.active:
self.active = True
self.active_hist = True
def act(self):
self.times_tested += 1
for atom in [atom for atom in self.get_atoms_as_list() if atom.active is True]:
if atom.active is True:
atom.act()
def conditional_activate(self):
for atom in [atom for atom in self.get_atoms_as_list() if atom.active is False]:
atom.conditional_activate()
def get_fitness(self):
fitness = -999999
for game in self.game_atoms:
fitness = self.get_atom(game).get_fitness()
return fitness
def get_state_history(self):
for game in self.game_atoms:
state = self.get_atom(game).state
return state
def deactivate(self):
Molecule.deactivate(self)
for game in self.game_atoms:
game = self.get_atom(game)
if game is not None:
game.state = []
class ActorMolecule(Molecule):
"""
The data structure for an actor molecule
"""
def __init__(self,*args):
super(ActorMolecule, self).__init__(*args)
self.type = "actor_molecule"
def activate(self):
"""
activate molecule
"""
self.times_tested += 1
for atom in self.get_atoms_as_list():
if atom.type == "sensory":
atom.activate()
# check to see if sensory conditions have been met
if atom.active:
self.active = True
self.active_hist = True
def conditional_activate(self):
for atom in [atom for atom in self.get_atoms_as_list() if atom.active is False]:
atom.conditional_activate()
def act(self):
self.times_tested += 1
for atom in [atom for atom in self.get_atoms_as_list() if atom.active is True]:
if atom.active is True:
atom.act()
######################
# Nao Molecules
######################
class NaoMaxSensorGameMolecule(GameMolecule):
"""
The data structure for a basic game molecule
"""
def __init__(self, memory,atoms,nao_memory):
super(NaoMaxSensorGameMolecule, self).__init__(memory,atoms)
self.nao_memory = nao_memory
self.constructor()
self.set_connections()
id = "g-{0}".format(random.randint(1,500000))
while id in self.memory.molecules:
id = "g-{0}".format(random.randint(1,500000))
self.id = id
def constructor(self):
atom_1 = NaoSensorAtom(memory=self.memory,nao_memory=self.nao_memory,
sensors=[143],
sensory_conditions=[-10.0],
messages=[],
message_delays=[0])
atom_2 = TransformAtom(memory=self.memory,messages=[],message_delays=[0],
parameters = {
"time_active":"always",
})
atom_3 = NaoMaxSensorGame(
memory=self.memory,messages=[],message_delays=[0]
)
# add atom to shared list of atoms
for a in [atom_1,atom_2,atom_3]:
self.atoms[a.get_id()] = a
self.molecular_graph = nx.DiGraph()
self.molecular_graph.add_node(atom_1.get_id())
self.molecular_graph.add_node(atom_2.get_id())
self.molecular_graph.add_node(atom_3.get_id())
self.molecular_graph.add_edges_from([
(atom_1.get_id(),atom_2.get_id()),
(atom_2.get_id(),atom_3.get_id())
])
self.game_atoms.append(atom_3.get_id())
class NAOActorMolecule(ActorMolecule):
"""
The data structure for a Nao Actor molecule
"""
def __init__(self, memory,atoms,nao_memory,nao_motion,duplication=False):
super(NAOActorMolecule, self).__init__(memory,atoms)
self.nao_memory = nao_memory
self.nao_motion = nao_motion
if duplication == False:
self.constructor()
self.set_connections()
id = "m-{0}".format(random.randint(1,5000))
while id in self.memory.molecules:
id = "m-{0}".format(random.randint(1,5000))
self.id = id
def constructor(self):
atom_1 = NaoSensorAtom(memory=self.memory,nao_memory=self.nao_memory,
sensors=[143],
sensory_conditions=[-10.0],
messages=[],
message_delays=[0])
atom_2 = TransformAtom(memory=self.memory,messages=[],message_delays=[2],
parameters = {
"time_active":5,
})
atom_3 = NaoMotorAtom(
memory=self.memory,nao_memory=self.nao_memory,nao_motion=self.nao_motion,
messages=[],
message_delays=[random.randint(0,300)],
motors = self.get_random_motors(self.nao_memory,3),
parameters = {
"time_active":random.randint(0,3),
"motor_parameters":[
2*(random.random()-0.5),
2*(random.random()-0.5),
2*(random.random()-0.5)
],
"times":[1, 1, 1]
})
atom_4 = NaoMotorAtom(
memory=self.memory,nao_memory=self.nao_memory,nao_motion=self.nao_motion,
messages=[],
message_delays=[random.randint(0,300)],
motors = self.get_random_motors(self.nao_memory,3),
parameters = {
"time_active":random.randint(0,3),
"motor_parameters":[
2*(random.random()-0.5),
2*(random.random()-0.5),
2*(random.random()-0.5)
],
"times":[1, 1, 1]
})
atom_5 = NaoMotorAtom(
memory=self.memory,nao_memory=self.nao_memory,nao_motion=self.nao_motion,
messages=[],
message_delays=[random.randint(0,300)],
motors = self.get_random_motors(self.nao_memory,3),
parameters = {
"time_active":random.randint(0,3),
"motor_parameters":[
2*(random.random()-0.5),
2*(random.random()-0.5),
2*(random.random()-0.5)
],
"times":[1, 1, 1]
})
for a in [atom_1,atom_2,atom_3,atom_4,atom_5]:
self.atoms[a.get_id()]=a
self.molecular_graph = nx.DiGraph()
self.molecular_graph.add_node(atom_1.get_id(),color=graph_colours[atom_1.type])
self.molecular_graph.add_node(atom_2.get_id(),color=graph_colours[atom_2.type])
self.molecular_graph.add_node(atom_3.get_id(),color=graph_colours[atom_3.type])
self.molecular_graph.add_node(atom_4.get_id(),color=graph_colours[atom_4.type])
self.molecular_graph.add_node(atom_5.get_id(),color=graph_colours[atom_5.type])
self.molecular_graph.add_edges_from([
(atom_1.get_id(),atom_2.get_id()),
(atom_2.get_id(),atom_3.get_id()),
(atom_2.get_id(),atom_4.get_id()),
(atom_2.get_id(),atom_5.get_id())
])
def get_random_motors(self,nao_memory,n_motors):
motors = []
for i in range(0,n_motors):
motor = nao_memory.getRandomMotor()
while motor in motors:
motor = nao_memory.getRandomMotor()
motors.append(motor)
return motors
def mutate(self):
# intra atomic mutations
for atom in self.get_atoms_as_list():
atom.mutate()
if random.random() < 0.05:
self.create_and_add_atom()
if random.random() < 0.05:
self.delete_atom_mutation()
def create_random_motor_atom(self):
no_motors = random.choice([1,2,3,4])
# message_delays_mean = random.choice([0,50,100,150,250])
# message_delays = [int(random.gauss(message_delays_mean,0.1)*100)]
# for i in message_delays:
# if i < 0:
# i = 0
# elif i > 300:
# i = 300
atom = NaoMotorAtom(
memory=self.memory,nao_memory=self.nao_memory,nao_motion=self.nao_motion,
messages=[],
message_delays=[random.randint(0,300)],
motors = self.get_random_motors(self.nao_memory,no_motors),
parameters = {
"time_active":random.randint(5,50),
"motor_parameters":[2*(random.random()-0.5) for i in range(0,no_motors)],
"times":[1, 1, 1]
})
self.memory.add_atom(atom)
return atom
def create_and_add_atom(self):
atom = self.create_random_motor_atom()
allowed_connectors = self.find_atoms_of_types(self.molecular_graph,atom.can_connect_to())
parent = random.choice(allowed_connectors)
self.add_atom(atom.get_id(),parent=parent)
def add_atom(self,atom_id,parent=None,):
if parent == None:
parent = []
atom = self.get_atom(atom_id)
self.molecular_graph.add_node(atom_id)
if len(parent) > 0:
self.molecular_graph.add_edge(parent,atom_id)
def delete_atom(self,atom_id):
delete_list = [atom_id]
atom = self.get_atom(atom_id)
successors = self.molecular_graph.successors(atom_id)
open_list = successors
while len(open_list) > 0:
node = open_list.pop(0)
open_list += [s for s in self.molecular_graph.successors(node) if s not in open_list]
to_delete = True
for n in self.molecular_graph.predecessors(node):
if n not in delete_list:
to_delete = False
if to_delete:
delete_list.append(node)
for node in delete_list:
self.molecular_graph.remove_node(node)
def delete_atom_mutation(self):
atoms = self.find_atoms_of_types(self.molecular_graph,'motor')
deleting = random.choice(atoms)
self.delete_atom(deleting)
def deactivate(self):
for atom in self.get_atoms_as_list():
atom.deactivate()
def duplicate(self):
new_molecule = NAOActorMolecule(memory=self.memory,
atoms=self.atoms,
nao_memory=self.nao_memory,
nao_motion=self.nao_motion,
duplication=True)
graph_dict = {}
new_graph_dict = {}
new_graph = nx.DiGraph()
# duplicate graph with new atoms
for node in self.molecular_graph.nodes():
graph_dict[node]={}
graph_dict[node]["predecessors"]=self.molecular_graph.predecessors(node)
new_atom = self.atoms[node].duplicate()
self.atoms[new_atom.get_id()]=new_atom
graph_dict[node]["child"] = new_atom.get_id()
new_graph_dict[new_atom.get_id()]={}
new_graph_dict[new_atom.get_id()]["parent"]=node
new_graph.add_node(new_atom.get_id())
# add edges
for node in new_graph.nodes():
parent = new_graph_dict[node]["parent"]
for p in self.molecular_graph.predecessors(parent):
new_graph.add_edge(graph_dict[p]["child"],node)
new_molecule.molecular_graph=new_graph
new_molecule.set_connections()
return new_molecule