-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreature.py
223 lines (184 loc) · 8.61 KB
/
creature.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
import numpy as np
from intepreter import Intepreter
mutation_table = np.array([0x1<<n for n in range(32)])
base_cpm = 0.008
genome_mass = 0.1
life_mass = 0.2
mutation_rate=0.005
shift_rate=0.01
class Reflex():
def __init__(self, genome, ninternal, nmem):
self.ninternal = ninternal
self.nmem = nmem
self.mem = np.zeros(nmem)
self.enabled_inputs = []
self.enabled_outputs = []
self.connections = self.get_connections(genome)
def get_connections(self, genome):
connections = dict([(k,[]) for k in ["io","is","ss","so"]]) # 4 types connections
inputs_enabled = dict()
outputs_enabled = dict()
for gene in genome:
# 32 bits: [1 in_type] [7 in_id] [1 out_type] [7 out_id] [16 weight]
in_type = (gene>>24 &0x80) >>7
in_id = gene>>24 & 0x7f
out_type = (gene>>16 &0x80) >>7
out_id = gene>>16 &0x7f
weight = gene &0xffff
weight = (weight - 2**15)/2**15*4
if in_type and out_type:
# from input to output
in_node = Intepreter.InputNodes[in_id % len(Intepreter.InputNodes)]
if in_node not in inputs_enabled:
in_pos = len(self.enabled_inputs)
self.enabled_inputs.append(in_node)
inputs_enabled[in_node] = in_pos
else:
in_pos = inputs_enabled[in_node]
out_node = Intepreter.OutputNodes[out_id % len(Intepreter.OutputNodes)]
if out_node not in outputs_enabled:
out_pos = len(self.enabled_outputs)
self.enabled_outputs.append(out_node)
outputs_enabled[out_node] = out_pos
else:
out_pos = outputs_enabled[out_node]
connections["io"].append((in_pos, out_pos, weight))
elif in_type and not out_type:
# from input to internal
in_node = Intepreter.InputNodes[in_id % len(Intepreter.InputNodes)]
if in_node not in inputs_enabled:
in_pos = len(self.enabled_inputs)
self.enabled_inputs.append(in_node)
inputs_enabled[in_node] = in_pos
else:
in_pos = inputs_enabled[in_node]
out_pos = out_id % (self.ninternal+self.nmem)
connections["is"].append((in_pos, out_pos, weight))
elif not in_type and out_type:
# from internal to output
in_pos = in_id % (self.ninternal+self.nmem)
out_node = Intepreter.OutputNodes[out_id % len(Intepreter.OutputNodes)]
if out_node not in outputs_enabled:
out_pos = len(self.enabled_outputs)
self.enabled_outputs.append(out_node)
outputs_enabled[out_node] = out_pos
else:
out_pos = outputs_enabled[out_node]
connections["so"].append((in_pos, out_pos, weight))
else:
# from internal to internal
in_pos = in_id % (self.ninternal+self.nmem)
out_pos = out_id % self.ninternal
connections["ss"].append((in_pos, out_pos, weight))
# clean useless connections
i_reachable = set()
o_reachable = set()
for conn in connections["is"]:
i_reachable.add(conn[1])
for conn in connections["so"]:
o_reachable.add(conn[0])
for conn in connections["ss"]:
if conn[0] in i_reachable:
i_reachable.add(conn[1])
if conn[1] in o_reachable:
o_reachable.add(conn[0])
valid_int = i_reachable.intersection(o_reachable)
connections["is"] = [conn for conn in connections["is"] if conn[1] in valid_int]
connections["ss"] = [conn for conn in connections["ss"] if conn[0] in valid_int and conn[1] in valid_int]
connections["so"] = [conn for conn in connections["so"] if conn[0] in valid_int]
# clean useless nodes
valid_i = set()
valid_o = set()
for conn in connections["is"]+connections["io"]:
valid_i.add(conn[0])
for conn in connections["so"]+connections["io"]:
valid_o.add(conn[1])
valid_i = list(valid_i)
valid_o = list(valid_o)
self.enabled_inputs = [self.enabled_inputs[i] for i in valid_i]
self.enabled_outputs = [self.enabled_outputs[i] for i in valid_o]
in_posdict = dict([(i,n) for n,i in enumerate(valid_i)])
out_posdict = dict([(i,n) for n,i in enumerate(valid_o)])
connections["io"] = [(in_posdict[conn[0]],out_posdict[conn[1]],conn[2]) for conn in connections["io"]]
connections["is"] = [(in_posdict[conn[0]],conn[1],conn[2]) for conn in connections["is"]]
connections["so"] = [(conn[0],out_posdict[conn[1]],conn[2]) for conn in connections["so"]]
return connections
def forward(self, inputs):
outputs = np.zeros(len(self.enabled_outputs))
cells = np.concatenate([self.mem, np.zeros(self.ninternal)])
for in_node, out_node, weight in self.connections["is"]:
cells[out_node] += inputs[in_node] * weight
for in_node, out_node, weight in self.connections["ss"]:
cells[out_node] += cells[in_node] * weight
cells = np.tanh(cells)
self.mem = cells[:len(self.mem)]
for in_node, out_node, weight in self.connections["so"]:
outputs[out_node] += cells[in_node] * weight
for in_node, out_node, weight in self.connections["io"]:
outputs[out_node] += inputs[in_node] * weight
return outputs
__call__=forward
class Creature():
def __init__(self, genome):
self.alive = True
self.genome = genome
# traits (constant during lifetime)
self.solve_traits(genome[0])
self.reflex = Reflex(genome[1:], self.ninternal, self.nmem)
self.mutation_rate=mutation_rate
self.shift_rate=shift_rate
# states (variable during lifetime)
self.age = 0
self.rp = self.max_resource*0.8
self.hp = self.max_health*0.8
self.loc = 0 # coordinate x,y
self.r = 0 # orientation 0-7, 0 towards north, increment clockwise
# statistics for performance tracking
self.generation = 0
self.recent_input_values = []
self.recent_output_values = []
self.n_children = 0
def solve_traits(self, gene):
# 32 bits:
# 8: [1?] [3 life_expectency(log)] [2 ninternal(log)] [2 nmem(log)]
# 16: [3 attack] [3 defense] [3 max_health] [3 max_resource] [4 repl_resource] (all log)
# 8: [3 Osc1_period (log)] [3 Osc2_period (log)] [2 spawn_range(log)]
self.life_expectency = 2**(((gene>>24 &0x70) >>4)+2)
self.ninternal = 2**((gene>>24 &0xc) >> 2)
self.nmem = 2**(gene>>24 &0x3)
self.attack = 2**((gene>>8 &0xe000) >> 13)
self.defense = 2**((gene>>8 &0x1c00) >> 10)
self.max_health = 2**((gene>>8 &0x380) >> 7)
self.max_resource = 2**((gene>>8 &0x70) >> 4)
self.repl_resource = 0.5**(gene>>8 &0xf)
self.osc1_period = 2**((gene &0xe0)>>5)
self.osc2_period = 2**((gene &0x1c)>>2)
self.spawn_range = 2**(gene &0x3)
self.mass = np.log2(self.attack * self.defense * self.max_health * self.max_resource * self.life_expectency) + len(self.genome) * genome_mass
def reproduce(self):
self.n_children+=1
repl = []
for g in self.genome:
# point mutation
repl.append(g^sum(mutation_table[np.random.rand(32)<self.mutation_rate]))
repl = np.array(repl)
if np.random.rand()<self.shift_rate:
if np.random.rand()<0.5:
# insertion
repl=np.insert(repl,
np.random.randint(len(repl)+1),
np.random.randint(2**32,dtype=np.uint32))
else:
if len(repl)>1:
# delete
repl=np.delete(repl,np.random.randint(len(repl)))
return Creature(repl)
def step(self, inputs):
self.rp -= base_cpm * self.mass
self.age+=1
self.recent_input_values = inputs
outputs = self.recent_output_values = self.reflex(inputs)
return outputs
def __str__(self):
return f"Creature @ {self.loc} twds {self.r} w/ genome: {[hex(g) for g in self.genome]}"
__repr__=__str__