-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpanda.py
455 lines (362 loc) · 12.2 KB
/
panda.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from math import pi, sin, cos
from direct.actor.Actor import Actor
from panda3d.core import *
from random import randrange
from pybrain.structure import FeedForwardNetwork, TanhLayer, FullConnection
import numpy
class Panda(object):
"""
This class is in charge of manipulating the pandas, the neural network, the collisions, etc
Attributes:
pandaIds (dict): Maps an int to a Panda object
pandaList (list): List of all the pandas
pandaActorIdle (Actor): Panda3d Actor class
pandaActorWalking (Actor): Panda3d Actor class
livingPandas (int): counter of living pandas
health (float): Amount of health
viewDistance (float): Maximum view distance
baseSpeed (float): Speed multiplier
baseTurnSpeed (float): Turn speed multiplier (in degrees)
isAlive (bool): True if alive
isDying (bool): True if health == 0
carrotsEaten (int): Number of carrots eaten by the panda (score)
brainWeights (list): List of weights for the net
network (FeedForwardNetwork): PyBrain Neural Network object
inputNumber (int): Number of view frustums
inputDistanceList (list): float between 1.0 and 0.0; 1.0 -> farther, 0.0 -> closer
inputTypeList (list): -1 spike, 0 nothing, 1 carrot
lensNodeList (list): list of the nodes of the frustums
handleLifeBar (NodePath): Node for the life bar
pandaHandle (NodePath): Node for the panda
pandaActorIdleHandle (NodePath): Node for the idle panda animation
pandaActorWalkingHandle (NodePath): Node for the walking panda animation
"""
pandaList = []
pandaIds = {}
pandaActorIdle = None
pandaActorWalking = None
livingPandas = 0
def __init__(self, game, x, y, genome):
"""
Initialize
Args:
game (game (Game): A reference to the Game object
x (int): x coordinate
y (int): y coordinate
genome (G1DList): PyEvolve's individual container
"""
if not Panda.pandaActorIdle:
Panda.pandaActorIdle = Actor("models/panda-model", {"walk": "models/panda-walk4"})
Panda.pandaActorIdle.setPos(0,0,0)
Panda.pandaActorIdle.setScale(0.005, 0.005, 0.005)
if not Panda.pandaActorWalking:
Panda.pandaActorWalking = Actor("models/panda-model", {"walk": "models/panda-walk4"})
Panda.pandaActorWalking.setPos(0,0,0)
Panda.pandaActorWalking.setScale(0.005, 0.005, 0.005)
Panda.pandaActorWalking.loop("walk")
self.health = 100.0
self.viewDistance = 50.0
self.baseSpeed = 1.0
self.baseTurnSpeed = 10.0
self.isAlive = True
self.isDying = False
self.carrotsEaten = 0
self.lensNodeList = []
self.inputNumber = 7 #number of view frustums
self.inputDistanceList = [0]*self.inputNumber # float between 1.0 and 0.0; 1.0 -> farther, 0.0 -> closer
self.inputTypeList = [0]*self.inputNumber #-1 spike, 0 nothing, 1 carrot
h = randrange(0, 360)
self.pandaHandle = game.render.attachNewNode("pandaHandle")
self.pandaHandle.setPos(x, y, 0)
self.pandaHandle.setH(h)
#self.pandaHandle.showTightBounds()
self.pandaActorWalkingHandle = self.pandaHandle.attachNewNode("pandaActorWalkingHandle")
self.pandaActorWalkingHandle.setPos(0,0,0)
self.pandaActorIdleHandle = self.pandaHandle.attachNewNode("pandaActorIdleHandle")
self.pandaActorIdleHandle.setPos(0,0,0)
Panda.pandaActorWalking.instanceTo(self.pandaActorWalkingHandle)
Panda.pandaList.append(self)
Panda.livingPandas += 1
Panda.pandaIds[genome.getParam("pandaId")] = self
self.brainWeights = genome.genomeList
self.__setUpLifeBar()
self.__setUpLens()
self.__setUpBrain(genome)
def __delete(self, task = None):
"""
Free memory allocated by this panda
Args:
task (task): Panda3D requires this param
"""
self.isAlive = False
for node in self.lensNodeList:
node.removeNode()
del self.lensNodeList[:]
self.bgHandle.removeNode()
self.fgHandle.removeNode()
self.handleLifeBar.removeNode()
self.pandaActorIdleHandle.removeNode()
self.pandaActorWalkingHandle.removeNode()
self.pandaHandle.removeNode()
Panda.livingPandas -= 1
def __die(self, game):
"""
Prepare to die
Args:
game (Game): A reference to the Game object
"""
if not self.isDying:
self.health = 0.0 #maybe it is < 0
self.isDying = True
Panda.pandaActorIdle.instanceTo(self.pandaActorIdleHandle)
self.pandaActorWalkingHandle.hide()
game.taskMgr.doMethodLater(3, self.__delete, 'delete panda')
def __setUpLens(self):
"""
Set up the view frustums
"""
hfov = 180.0 / self.inputNumber
vfov = 10.0
for i in range(1, self.inputNumber*2, 2):
delta = 180.0 - (90 + vfov/2)
delta *= (pi / 180.0)
hfovRad = hfov*(pi / 180.0)
pointTo = Vec3(cos(hfovRad*i/2), -sin(hfovRad*i/2), cos(delta))
lens = PerspectiveLens()
lens.setFov(hfov, vfov)
lens.setNear(0.01)
lens.setFar(self.viewDistance)
lens.setViewVector(pointTo, pointTo.up())
lNode = LensNode("lensNode", lens)
#lNode.showFrustum()
handleLens = self.pandaHandle.attachNewNode(lNode)
handleLens.setPos(0, 0, 0)
self.lensNodeList.append(handleLens)
def __setUpLifeBar(self):
"""
Set up the life bar
"""
self.handleLifeBar = self.pandaHandle.attachNewNode("lifeBarNode")
self.handleLifeBar.setBillboardPointEye()
self.handleLifeBar.setPos(0, 0, 3)
self.bgCard = CardMaker("bg")
self.bgCard.setColor(1, 0, 0, 1)
self.bgCard.setFrame(-1, 1, -0.2, 0.2)
self.bgHandle = self.handleLifeBar.attachNewNode(self.bgCard.generate())
self.bgHandle.setPos(0,0,0)
self.fgCard = CardMaker("fg")
self.fgCard.setColor(0, 1, 0, 1)
self.fgCard.setFrame(-1, 1, -0.2, 0.2)
self.fgHandle = self.handleLifeBar.attachNewNode(self.fgCard.generate())
self.fgHandle.setPos(0,0,0)
self.__updateHealthBar()
def update(self, game, carrots, spikes):
"""
Args:
game (Game): A reference to the Game object
carrots (list): list of carrots
spikes (list): list of spikes
Returns:
None: if is dying
"""
if self.isDying:
return
self.__updateInputs(carrots, spikes)
brainInput = self.inputDistanceList + self.inputTypeList
brainOutput = self.network.activate(brainInput).tolist()
self.pandaHandle.setH(self.pandaHandle, (brainOutput[0])*self.baseTurnSpeed)
self.pandaHandle.setPos(self.pandaHandle, 0, -((brainOutput[1]+1.0)*self.baseSpeed), 0)
self.__handleCollisions(game, carrots, spikes)
self.__decrementHealth()
self.__updateHealthBar()
if self.health <= 0.0:
self.__die(game)
def __decrementHealth(self):
"""
Decrement the health of the panda each frame
"""
self.health -= 0.2
def __updateInputs(self, carrots, spikes):
"""
Update the inputs of the neural network
Args:
carrots (list): list of carrots
spikes (list): list of spikes
"""
self.inputDistanceList = [self.viewDistance]*self.inputNumber # float between 1.0 and 0.0; 1.0 -> farther, 0.0 -> closer
self.inputTypeList = [0]*self.inputNumber #-1 spike, 0 nothing, 1 carrot
for i in range(self.inputNumber):
carrotDistances = []
spikeDistances = []
for carrot in carrots:
if not carrot.isActive:
continue
point = carrot.carrotHandle.getPos(self.lensNodeList[i])
if self.lensNodeList[i].node().isInView(Point3(point.getX(), point.getY(), 0.01)):
dist = (point.getXy() - self.lensNodeList[i].getPos().getXy()).length()
carrotDistances.append(dist)
for spike in spikes:
point = spike.spikeHandle.getPos(self.lensNodeList[i])
if self.lensNodeList[i].node().isInView(Point3(point.getX(), point.getY(), 0.01)):
dist = (point.getXy() - self.lensNodeList[i].getPos().getXy()).length()
spikeDistances.append(dist)
self.inputTypeList[i] = 0
minDist = None
if carrotDistances:
minDist = min(carrotDistances)
self.inputTypeList[i] = 1
if spikeDistances:
minS = min(spikeDistances)
if minDist != None: #minDist can be 0, which is evaluted false
if minS < minDist:
minDist = minS
self.inputTypeList[i] = -1
else:
minDist = minS
self.inputTypeList[i] = -1
if minDist != None:
self.inputDistanceList[i] = minDist
if self.inputDistanceList[i] > self.viewDistance:
self.inputDistanceList[i] = self.viewDistance
self.inputDistanceList = [float(i)/self.viewDistance for i in self.inputDistanceList] #normalize the distances
#print([round(i, 2) for i in self.inputDistanceList])
def __updateHealthBar(self):
"""
Update health bar
"""
self.bgHandle.setScale(1.0 - self.health/100.0, 1, 1)
self.bgHandle.setPos((self.health/100.0),0,0)
self.fgHandle.setScale(self.health/100.0, 1, 1)
self.fgHandle.setPos(-(1.0 - (self.health/100.0)),0,0)
def __handleCollisions(self, game, carrots, spikes):
"""
If there is a collision with a carrot, increment health
If there is a collision with a spike, kill the panda (NOOOO!!!)
Args:
game (Game): A reference to the Game object
carrots (list): list of carrots
spikes (list): list of spikes
"""
for carrot in carrots:
if not carrot.isActive:
continue
point = carrot.carrotHandle.getPos()
dist = (point.getXy() - self.pandaHandle.getPos().getXy()).lengthSquared()
if dist < 10:
self.__eatCarrot()
carrot.goToHeaven(game, Panda.pandaList, spikes)
break
for spike in spikes:
point = spike.spikeHandle.getPos()
dist = (point.getXy() - self.pandaHandle.getPos().getXy()).lengthSquared()
if dist < 20:
self.__die(game)
break
def __eatCarrot(self):
"""
Increment score and life points
"""
self.carrotsEaten += 1
self.health += 40.0
if self.health > 100.0:
self.health = 100.0
def __setUpBrain(self, genome):
"""
Set up PyBrain's neural network
Args:
genome (G1DList): PyEvolve's individual container
"""
self.network = FeedForwardNetwork()
inLayer = TanhLayer(14)
hiddenLayer = TanhLayer(12)
hiddenLayer2 = TanhLayer(6)
outLayer = TanhLayer(2)
self.network.addInputModule(inLayer)
self.network.addModule(hiddenLayer)
self.network.addModule(hiddenLayer2)
self.network.addOutputModule(outLayer)
in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_hidden2 = FullConnection(hiddenLayer, hiddenLayer2)
hidden2_to_out = FullConnection(hiddenLayer2, outLayer)
self.network.addConnection(in_to_hidden)
self.network.addConnection(hidden_to_hidden2)
self.network.addConnection(hidden2_to_out)
self.network.sortModules()
new_params = numpy.array(genome.genomeList)
self.network._setParameters(new_params)
@staticmethod
def getBestPanda():
"""
Returns the panda with the highest score
Returns:
Panda: panda with the highest score
"""
best = Panda.pandaList[0]
for panda in Panda.pandaList:
if best.carrotsEaten < panda.carrotsEaten:
best = panda
return best
@staticmethod
def getAvgScore():
"""
Returns the average population score
Returns:
float: Average score
"""
avg = 0.0
for panda in Panda.pandaList:
avg += panda.carrotsEaten
avg = avg / len(Panda.pandaList)
return round(avg,3)
@staticmethod
def clearPandas(game):
"""
Remove all the pandas
Args:
game (Game): A reference to the Game object
"""
game.taskMgr.remove('delete panda')
for panda in Panda.pandaList[:]:
if panda.isAlive:
panda.__delete()
del Panda.pandaList[:]
Panda.pandaIds.clear()
@staticmethod
def getScoreById(pandaId):
"""
Given the Id of a panda return its score
Args:
pandaId (int): Id of panda
Returns:
int: panda score
"""
score = Panda.pandaIds[pandaId].carrotsEaten
if not Panda.pandaIds[pandaId].isDying:
score += 3
return score
def drawDebugLines(self, game, carrots, spikes):
"""
Debug info
Args:
game (Game): A reference to the Game object
carrots (list): list of carrots
spikes (list): list of spikes
"""
for carrot in carrots:
if not carrot.isActive:
continue
point = carrot.carrotHandle.getPos()
linesegs = LineSegs("lines")
linesegs.setColor(1,1,1,1)
linesegs.drawTo(point)
linesegs.drawTo(self.pandaHandle.getPos())
node = linesegs.create(False)
game.render.attachNewNode(node)
for spike in spikes:
point = spike.spikeHandle.getPos(self.pandaHandle)
linesegs = LineSegs("lines")
linesegs.setColor(1,1,1,1)
linesegs.drawTo(point)
linesegs.drawTo(self.pandaHandle.getPos())
node = linesegs.create(False)
game.render.attachNewNode(node)