-
Notifications
You must be signed in to change notification settings - Fork 0
/
intepreter.py
365 lines (331 loc) · 11.3 KB
/
intepreter.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
import numpy as np
orientation = np.array([[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1]])
agpx = np.array([-1, -0.5 ,0, 0.5, 1, 0.5, 0, -0.5])
agpy = np.array([0, 0.5, 1, 0.5, 0, -0.5, -1, -0.5])
trace_ratio = 0.5
overrest_return = 0.75
minimum_rp_to_repr = 0.8
birth_tax = 0.05
movement_cpm = 0.003
rotate_cpm = 0.001
attack_cpm = 0.1
gather_rpm = 0.3
# stats
stats = {"repr_out":0,"repr_in":0, "rest_out":0, "rest_in":0,}
def sigmoid(x):
return 1/(1 + np.exp(-x))
class Intepreter():
InputNodes = [
"Rp", # resource
"Hp", # health
"Age", # age [0,1)
"BlFw", # blockage forward
"BlLF", # blockage left forward
"BlRF", # blockage right forward
"RsFw", # resource forward
"RsLF", # resource left forward
"RsRF", # resource right forward
"RsGd", # resource gradient forward
"Rnd", # random input
"Cnst", # Constant
# "Lx", # loc x [0,1]
# "Ly", # loc y [0,1]
"AgPx", # angle wrt +x [-1,1] with +x:1, -x:-1
"AgPy", # angle wrt +y [-1,1] with +y:1, -y:-1
"NNgh", # number of neighbors [0,8]/8
"Osc1s", # Oscillator 1 sin
"Osc1c", # Oscillator 1 cos
"Osc2s", # Oscillator 2 sin
"Osc2c", # Oscillator 2 cos
]
OutputNodes = [
"Repr", # reproduce
"MvFw", # move forward
"MvBw", # move backward
"MvRn", # move randomly
"RtLF", # rotate left forward
"RtRF", # rotate right forward
# "RtPx", # rotate towards +x
# "RtNx", # rotate towards -x
# "RtPy", # rotate towards +y
# "RtNy", # rotate towards -y
# "MvPx", # move towards +x
# "MvNx", # move towards -x
# "MvPy", # move towards +y
# "MvNy", # move towards -y
"Rest", # take a rest, recover hp
"Gath", # gather local resources
"AtkFw", # attack forward
"ESFw", # emit resource forward
"ESBw", # emit resource backward
"ESAr", # emit resource around
]
@staticmethod
def block(loc,world):
if not world.wrap and (loc[0]>=world.size or loc[0]<0 or loc[1]>=world.size or loc[1]<0):
return 1
if world.map[loc[0]%world.size,loc[1]%world.size]>0:
return 1
return 0
# inputs
@staticmethod
def Age(creature, world):
return creature.age/creature.life_expectency
@staticmethod
def Hp(creature, world):
return creature.hp/creature.max_health
@staticmethod
def Rp(creature, world):
return creature.rp/creature.max_resource
@staticmethod
def NNgh(creature, world):
locs = creature.loc + orientation
locs%=world.size
return sum(world.map[locs[:,0],locs[:,1]]>0)/8
@staticmethod
def Lx(creature, world):
return creature.loc[0]/world.size
@staticmethod
def Ly(creature, world):
return creature.loc[1]/world.size
@staticmethod
def AgPx(creature, world):
return agpx[creature.r]
@staticmethod
def AgPy(creature, world):
return agpy[creature.r]
@staticmethod
def Cnst(creature, world):
return 1
@staticmethod
def Rnd(creature, world):
return np.random.randn()
@staticmethod
def Osc1s(creature, world):
return np.sin(creature.age/creature.osc1_period*2*np.pi)
@staticmethod
def Osc1c(creature, world):
return np.cos(creature.age/creature.osc1_period*2*np.pi)
@staticmethod
def Osc2s(creature, world):
return np.sin(creature.age/creature.osc2_period*2*np.pi)
@staticmethod
def Osc2c(creature, world):
return np.cos(creature.age/creature.osc2_period*2*np.pi)
@staticmethod
def BlFw(creature, world):
loc = creature.loc + orientation[creature.r]
return Intepreter.block(loc,world)
@staticmethod
def BlLF(creature, world):
loc = creature.loc + orientation[(creature.r-1)%8]
return Intepreter.block(loc,world)
@staticmethod
def BlRF(creature, world):
loc = creature.loc + orientation[(creature.r+1)%8]
return Intepreter.block(loc,world)
@staticmethod
def RsFw(creature, world):
loc = creature.loc + orientation[creature.r]
loc%=world.size
return world.res[loc[0],loc[1]]/creature.max_resource
@staticmethod
def RsLF(creature, world):
loc = creature.loc + orientation[(creature.r-1)%8]
loc%=world.size
return world.res[loc[0],loc[1]]/creature.max_resource
@staticmethod
def RsRF(creature, world):
loc = creature.loc + orientation[(creature.r+1)%8]
loc%=world.size
return world.res[loc[0],loc[1]]/creature.max_resource
@staticmethod
def RsGd(creature, world):
loc1 = (creature.loc + orientation[creature.r])%world.size
loc2 = (loc1 + orientation[creature.r])%world.size
return (world.res[loc2[0],loc2[1]]-world.res[loc1[0],loc1[1]])/creature.max_resource
# outputs
@staticmethod
def Repr(creature, world, v):
if np.random.rand()>=v or not world.allow_repr:
return
if creature.rp<minimum_rp_to_repr*creature.max_resource:
return
loc = (creature.loc + np.random.randint(creature.spawn_range,size=2))%world.size
if world.get_creature_at(loc) is None:
r = creature.repl_resource * creature.rp
creature.rp -= r
c = creature.reproduce()
c.loc = loc
c.r = np.random.randint(8)
c.rp = r * (1-birth_tax)
c.generation = creature.generation + 1
world.add_creature(c)
@staticmethod
def Gath(creature, world, v):
if np.random.rand()>v:
return
x,y = creature.loc
max_r = gather_rpm*creature.mass
r = min(world.res[x,y], max_r, creature.max_resource-creature.rp)
creature.rp += r
world.res[x,y] -= r
@staticmethod
def Rest(creature, world, v):
recv = min(creature.rp, creature.max_resource * v)
creature.rp -= recv
creature.hp += recv
if creature.hp>creature.max_health:
creature.rp+=(creature.hp-creature.max_health)*overrest_return
creature.hp=creature.max_health
@staticmethod
def MvFw(creature, world, v):
if np.random.rand()>v:
return
loc = creature.loc + orientation[creature.r]
if not Intepreter.block(loc, world):
loc = loc%world.size
r = min(creature.rp, creature.mass * movement_cpm)
world.res[creature.loc[0],creature.loc[1]] += r * trace_ratio
creature.rp -= r
world.move_creature(creature, loc)
Intepreter.Gath(creature, world, 1)
@staticmethod
def MvBw(creature, world, v):
if np.random.rand()>v:
return
loc = creature.loc - orientation[creature.r]
if not Intepreter.block(loc, world):
loc = loc%world.size
r = min(creature.rp, creature.mass * movement_cpm)
world.res[creature.loc[0],creature.loc[1]] += r * trace_ratio
creature.rp -= r
world.move_creature(creature, loc)
Intepreter.Gath(creature, world, 1)
@staticmethod
def MvRn(creature, world, v):
if np.random.rand()>v:
return
loc = creature.loc + orientation[np.random.randint(len(orientation))]
if not Intepreter.block(loc, world):
loc = loc%world.size
r = min(creature.rp, creature.mass * movement_cpm * 1.5)
world.res[creature.loc[0],creature.loc[1]] += r * trace_ratio
creature.rp -= r
world.move_creature(creature, loc)
Intepreter.Gath(creature, world, 1)
@staticmethod
def RtLF(creature, world, v):
if np.random.rand()>v:
return
creature.r-=1
creature.r = creature.r%8
creature.rp -= min(creature.mass * rotate_cpm, creature.rp)
@staticmethod
def RtRF(creature, world, v):
if np.random.rand()>v:
return
creature.r+=1
creature.r = creature.r%8
creature.rp -= min(creature.mass * rotate_cpm, creature.rp)
@staticmethod
def MvNx(creature, world, v):
if np.random.rand()>v:
return
loc = creature.loc + np.array([-1,0])
if not Intepreter.block(loc, world):
loc = loc%world.size
world.move_creature(creature, loc)
@staticmethod
def MvPx(creature, world, v):
if np.random.rand()>v:
return
loc = creature.loc + np.array([1,0])
if not Intepreter.block(loc, world):
loc = loc%world.size
world.move_creature(creature, loc)
@staticmethod
def MvPy(creature, world, v):
if np.random.rand()>v:
return
loc = creature.loc + np.array([0,1])
if not Intepreter.block(loc, world):
loc = loc%world.size
world.move_creature(creature, loc)
@staticmethod
def MvNy(creature, world, v):
if np.random.rand()>v:
return
loc = creature.loc + np.array([0,-1])
if not Intepreter.block(loc, world):
loc = loc%world.size
world.move_creature(creature, loc)
@staticmethod
def RtPx(creature, world, v):
if np.random.rand()>v:
return
creature.r = 4
@staticmethod
def RtNx(creature, world, v):
if np.random.rand()>v:
return
creature.r = 0
@staticmethod
def RtPy(creature, world, v):
if np.random.rand()>v:
return
creature.r = 2
@staticmethod
def RtNy(creature, world, v):
if np.random.rand()>v:
return
creature.r = 6
@staticmethod
def ESFw(creature, world, v):
loc = creature.loc + orientation[creature.r]
loc%=world.size
r = max(creature.rp, v * creature.mass)
creature.rp -= r
world.res[loc[0],loc[1]]+= r
@staticmethod
def ESBw(creature, world, v):
loc = creature.loc + orientation[(creature.r+4)%8]
loc%=world.size
r = max(creature.rp, v * creature.mass)
creature.rp -= r
world.res[loc[0],loc[1]]+= r
@staticmethod
def ESAr(creature, world, v):
loc=creature.loc+orientation
loc%=world.size
r = max(creature.rp, v * creature.mass)
creature.rp -= r
world.res[loc[:,0],loc[:,1]]+= r/8
@staticmethod
def AtkFw(creature, world, v):
loc = creature.loc + orientation[creature.r]
loc%=world.size
atk = max(creature.rp, creature.mass * creature.attack * v)
creature.rp -= atk
victim = world.get_creature_at(loc)
if victim is not None:
victim.hp -= max(atk - victim.defense,0)
if victim.hp<=0:
world.remove_creature(victim)
Intepreter.MvFw(creature, world, 1)
# aggregation
@staticmethod
def aggregate(outputs):
outputs = sigmoid(outputs)
return outputs
if __name__=="__main__":
unimpl = []
for fn in Intepreter.InputNodes+Intepreter.OutputNodes:
try:
getattr(Intepreter, fn)
except AttributeError:
unimpl.append(fn)
if len(unimpl)>0:
print(f"unimplemented intepreters: {unimpl}")
else:
print("congrats! all intepreters are implemented")