-
Notifications
You must be signed in to change notification settings - Fork 0
/
bustersAgents.py
860 lines (704 loc) · 31.7 KB
/
bustersAgents.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
from __future__ import print_function
# bustersAgents.py
# ----------------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# ([email protected]) and Dan Klein ([email protected]).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel ([email protected]).
from builtins import range
from builtins import object
import time
import util
from game import Agent
from game import Directions
from keyboardAgents import KeyboardAgent
import inference
import busters
class NullGraphics(object):
"Placeholder for graphics"
def initialize(self, state, isBlue = False):
pass
def update(self, state):
pass
def pause(self):
pass
def draw(self, state):
pass
def updateDistributions(self, dist):
pass
def finish(self):
pass
class KeyboardInference(inference.InferenceModule):
"""
Basic inference module for use with the keyboard.
"""
def initializeUniformly(self, gameState):
"Begin with a uniform distribution over ghost positions."
self.beliefs = util.Counter()
for p in self.legalPositions: self.beliefs[p] = 1.0
self.beliefs.normalize()
def observe(self, observation, gameState):
noisyDistance = observation
emissionModel = busters.getObservationDistribution(noisyDistance)
pacmanPosition = gameState.getPacmanPosition()
allPossible = util.Counter()
for p in self.legalPositions:
trueDistance = util.manhattanDistance(p, pacmanPosition)
if emissionModel[trueDistance] > 0:
allPossible[p] = 1.0
allPossible.normalize()
self.beliefs = allPossible
def elapseTime(self, gameState):
pass
def getBeliefDistribution(self):
return self.beliefs
class BustersAgent(object):
"An agent that tracks and displays its beliefs about ghost positions."
def __init__( self, index = 0, inference = "ExactInference", ghostAgents = None, observeEnable = True, elapseTimeEnable = True):
inferenceType = util.lookup(inference, globals())
self.inferenceModules = [inferenceType(a) for a in ghostAgents]
self.observeEnable = observeEnable
self.elapseTimeEnable = elapseTimeEnable
self.last_score = None
self.countTicks = 0
self.last_turn=""
def registerInitialState(self, gameState):
"Initializes beliefs and inference modules"
import __main__
self.display = __main__._display
for inference in self.inferenceModules:
inference.initialize(gameState)
self.ghostBeliefs = [inf.getBeliefDistribution() for inf in self.inferenceModules]
self.firstMove = True
self.distancer = Distancer(gameState.data.layout, False)
def observationFunction(self, gameState):
"Removes the ghost states from the gameState"
agents = gameState.data.agentStates
gameState.data.agentStates = [agents[0]] + [None for i in range(1, len(agents))]
return gameState
def getAction(self, gameState):
"Updates beliefs, then chooses an action based on updated beliefs."
return self.chooseAction(gameState)
def chooseAction(self, gameState):
"By default, a BustersAgent just stops. This should be overridden."
return Directions.STOP
class BustersKeyboardAgent(BustersAgent, KeyboardAgent):
"An agent controlled by the keyboard that displays beliefs about ghost positions."
def __init__(self, index = 0, inference = "KeyboardInference", ghostAgents = None):
KeyboardAgent.__init__(self, index)
BustersAgent.__init__(self, index, inference, ghostAgents)
def getAction(self, gameState):
return BustersAgent.getAction(self, gameState)
def chooseAction(self, gameState):
return KeyboardAgent.getAction(self, gameState)
from distanceCalculator import Distancer
from game import Actions
from game import Directions
import random, sys
'''Random PacMan Agent'''
class RandomPAgent(BustersAgent):
def registerInitialState(self, gameState):
BustersAgent.registerInitialState(self, gameState)
self.distancer = Distancer(gameState.data.layout, False)
''' Example of counting something'''
def countFood(self, gameState):
food = 0
for width in gameState.data.food:
for height in width:
if(height == True):
food = food + 1
return food
''' Print the layout'''
def printGrid(self, gameState):
table = ""
##print(gameState.data.layout) ## Print by terminal
for x in range(gameState.data.layout.width):
for y in range(gameState.data.layout.height):
food, walls = gameState.data.food, gameState.data.layout.walls
table = table + gameState.data._foodWallStr(food[x][y], walls[x][y]) + ","
table = table[:-1]
return table
def chooseAction(self, gameState):
move = Directions.STOP
legal = gameState.getLegalActions(0) ##Legal position from the pacman
move_random = random.randint(0, 3)
if ( move_random == 0 ) and Directions.WEST in legal: move = Directions.WEST
if ( move_random == 1 ) and Directions.EAST in legal: move = Directions.EAST
if ( move_random == 2 ) and Directions.NORTH in legal: move = Directions.NORTH
if ( move_random == 3 ) and Directions.SOUTH in legal: move = Directions.SOUTH
return move
class GreedyBustersAgent(BustersAgent):
"An agent that charges the closest ghost."
def registerInitialState(self, gameState):
"Pre-computes the distance between every two points."
BustersAgent.registerInitialState(self, gameState)
self.distancer = Distancer(gameState.data.layout, False)
def chooseAction(self, gameState):
"""
First computes the most likely position of each ghost that has
not yet been captured, then chooses an action that brings
Pacman closer to the closest ghost (according to mazeDistance!).
To find the mazeDistance between any two positions, use:
self.distancer.getDistance(pos1, pos2)
To find the successor position of a position after an action:
successorPosition = Actions.getSuccessor(position, action)
livingGhostPositionDistributions, defined below, is a list of
util.Counter objects equal to the position belief
distributions for each of the ghosts that are still alive. It
is defined based on (these are implementation details about
which you need not be concerned):
1) gameState.getLivingGhosts(), a list of booleans, one for each
agent, indicating whether or not the agent is alive. Note
that pacman is always agent 0, so the ghosts are agents 1,
onwards (just as before).
2) self.ghostBeliefs, the list of belief distributions for each
of the ghosts (including ghosts that are not alive). The
indices into this list should be 1 less than indices into the
gameState.getLivingGhosts() list.
"""
pacmanPosition = gameState.getPacmanPosition()
legal = [a for a in gameState.getLegalPacmanActions()]
livingGhosts = gameState.getLivingGhosts()
livingGhostPositionDistributions = \
[beliefs for i, beliefs in enumerate(self.ghostBeliefs)
if livingGhosts[i+1]]
return Directions.EAST
class BasicAgentAA(BustersAgent):
# Este agente no realiza ningun cambio en la q-table,
# solo se encarga de ejecutar el pacman de acuerdo a las instrucciones de esta
def registerInitialState(self, gameState):
BustersAgent.registerInitialState(self, gameState)
self.distancer = Distancer(gameState.data.layout, False)
self.countActions = 0
self.actions = {Directions.NORTH:0, Directions.SOUTH:1, Directions.EAST:2, Directions.WEST:3}
self.q_table = self.readQtable()
self.epsilon = 0.05
def readQtable(self):
"Read qtable from disc"
self.table_file = open("qtable.txt", "r+")
table = self.table_file.readlines()
q_table = []
for i, line in enumerate(table):
row = line.split()
row = [float(x) for x in row]
q_table.append(row)
self.table_file.close()
return q_table
# state is a array with
# direction_ghost (norte, sur, este, oeste)
# distance_ghost (inmediata, cerca, lejos)
# direction_pacdot (norte, sur, este, oeste, none)
# distance_pacdot (inmediata, cerca, lejos)
def computePosition(self, state):
"""
Compute the row of the qtable for a given state.
"""
return state[0]+state[1]*4+state[2]*12+state[3]*60
def getQValue(self, state, action):
"""
Returns Q(state,action)
Should return 0.0 if we have never seen a state
or the Q node value otherwise
"""
position = self.computePosition(state)
action_column = self.actions[action]
return self.q_table[position][action_column]
def computeValueFromQValues(self, state, gameState):
"""
Returns max_action Q(state,action)
where the max is over legal actions. Note that if
there are no legal actions, which is the case at the
terminal state, you should return a value of 0.0.
"""
legalActions = gameState.getLegalActions(0)
if len(legalActions)==0:
return 0
return max(self.q_table[self.computePosition(state)])
def computeActionFromQValues(self, state, gameState):
"""
Compute the best action to take in a state. Note that if there
are no legal actions, which is the case at the terminal state,
you should return None.
"""
legalActions = gameState.getLegalActions(0)
if len(legalActions)==0:
return None
legalActions.remove(Directions.STOP)
best_actions = [legalActions[0]]
best_value = self.getQValue(state, legalActions[0])
for action in legalActions:
value = self.getQValue(state, action)
if value == best_value:
best_actions.append(action)
if value > best_value:
best_actions = [action]
best_value = value
return random.choice(best_actions)
def getActionQtable(self, state, gameState):
"""
Compute the action to take in the current state. With
probability self.epsilon, we should take a random action and
take the best policy action otherwise. Note that if there are
no legal actions, which is the case at the terminal state, you
should choose None as the action.
"""
# Pick Action
legalActions = gameState.getLegalActions(0)
legalActions.remove(Directions.STOP)
action = None
if len(legalActions) == 0:
return action
flip = util.flipCoin(self.epsilon)
if flip:
return random.choice(legalActions)
return self.getPolicy(state, gameState)
def getPolicy(self, state, gameState):
"Return the best action in the qtable for a given state"
return self.computeActionFromQValues(state, gameState)
def getValue(self, state, gameState):
"Return the highest q value for a given state"
return self.computeValueFromQValues(state, gameState)
def chooseAction(self, gameState):
# Calcular el state
# Default action
self.countActions = self.countActions + 1
move = Directions.STOP
legal = gameState.getLegalActions(0) ##Legal position from the pacman
pacman_position = gameState.getPacmanPosition()
ghosts_position = gameState.getGhostPositions()
num_ghost = 0
for ghost in gameState.getLivingGhosts():
if ghost == True:
num_ghost += 1
# the target object is a pacdot
pacdot_position = None
minDistance = 900000
pacmanPosition = gameState.getPacmanPosition()
num_pacdot = 0
# search the nearest pacdot
for i in range(gameState.data.layout.width):
for j in range(gameState.data.layout.height):
if gameState.hasFood(i, j):
num_pacdot +=1
foodPosition = i, j
distance = self.distancer.getDistance(pacmanPosition, foodPosition)
if distance < minDistance:
minDistance = distance
pacdot_position = foodPosition
object_distances = []
for ghost_position in ghosts_position:
object_distances.append(self.distancer.getDistance(pacman_position,ghost_position))
# Get the min distance in the list
num_object = None
index = 0
final_value = 100000
for value in object_distances:
if value and value <= final_value:
final_value = value
num_object = index
index +=1
near_object_position = ghosts_position[num_object]
next_distances = {}
for action in legal:
next_position = [pacman_position[0],pacman_position[1]]
if action == Directions.NORTH:
next_position[1]+=1
next_distances[Directions.NORTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.SOUTH:
next_position[1]-=1
next_distances[Directions.SOUTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.EAST:
next_position[0]+=1
next_distances[Directions.EAST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.WEST:
next_position[0]-=1
next_distances[Directions.WEST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
final_direction = None
min_distance = 100000
for _direction, _distance in next_distances.items():
if _distance < min_distance:
min_distance = _distance
final_direction = _direction
direction_ghost = None
if final_direction == Directions.NORTH:
direction_ghost = NORTE
if final_direction == Directions.SOUTH:
direction_ghost = SUR
if final_direction == Directions.EAST:
direction_ghost = ESTE
if final_direction == Directions.WEST:
direction_ghost = OESTE
# For the ghost distance
distance_ghost = None
dis_ghost = object_distances[num_object]
if object_distances[num_object] == 1:
distance_ghost = INMEDIATA
elif object_distances[num_object] == 2:
distance_ghost = CERCANA
else:
distance_ghost = LEJOS
# For the pacdot direction
direction_pacdot = None
if pacdot_position:
near_object_position = pacdot_position
next_distances = {}
for action in legal:
next_position = [pacman_position[0],pacman_position[1]]
if action == Directions.NORTH:
next_position[1]+=1
next_distances[Directions.NORTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.SOUTH:
next_position[1]-=1
next_distances[Directions.SOUTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.EAST:
next_position[0]+=1
next_distances[Directions.EAST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.WEST:
next_position[0]-=1
next_distances[Directions.WEST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
final_direction = None
min_distance = 100000
for _direction, _distance in next_distances.items():
if _distance < min_distance:
min_distance = _distance
final_direction = _direction
if final_direction == Directions.NORTH:
direction_pacdot = NORTE
if final_direction == Directions.SOUTH:
direction_pacdot = SUR
if final_direction == Directions.EAST:
direction_pacdot = ESTE
if final_direction == Directions.WEST:
direction_pacdot = OESTE
else:
direction_pacdot = NONE
# Para pacdot distance
if pacdot_position:
pacdot_num_distance = self.distancer.getDistance(pacman_position,pacdot_position)
dis_pacdot = pacdot_num_distance
distance_pacdot = None
if pacdot_num_distance == 1:
distance_pacdot = INMEDIATA
elif pacdot_num_distance == 2:
distance_pacdot = CERCANA
else:
distance_pacdot = LEJOS
else:
distance_pacdot = LEJOS
state = [direction_ghost,distance_ghost,direction_pacdot,distance_pacdot]
move = self.getActionQtable(state, gameState)
return move
NORTE = 0
SUR = 1
ESTE = 2
OESTE = 3
NONE = 4
INMEDIATA = 0
CERCANA = 1
LEJOS = 2
class QLearningAgent(BustersAgent):
def __init__(self, index = 0, inference = "ExactInference", ghostAgents = None, observeEnable = True, elapseTimeEnable = True,
epsilon = 0.05,alpha = 0,discount=0.9,tickLimit=0,entrenamiento=0):
super().__init__(index, inference, ghostAgents, observeEnable, elapseTimeEnable)
self.epsilon = float(epsilon) # Cuanto mayor, mayor la probabilidad de coger un valor aleatorio
self.alpha = float(alpha) # Ratio de aprendizaje
self.discount = float(discount) # factor de discount
self.tickLimit=float(tickLimit)
self.entrenamiento = float(entrenamiento)
def registerInitialState(self, gameState):
BustersAgent.registerInitialState(self, gameState)
self.distancer = Distancer(gameState.data.layout, False)
self.countActions = 0
self.actions = {Directions.NORTH:0, Directions.SOUTH:1, Directions.EAST:2, Directions.WEST:3}
self.q_table = self.readQtable()
self.last_state = None
self.last_move = None
self.last_num_ghost = None
self.last_num_pacdot = None
# state is a array with
# direction_ghost (norte, sur, este, oeste)
# distance_ghost (inmediata, cerca, lejos)
# direction_pacdot (norte, sur, este, oeste, none)
# distance_pacdot (inmediata, cerca, lejos)
def readQtable(self):
"Read qtable from disc"
self.table_file = open("qtable.txt", "r+")
table = self.table_file.readlines()
q_table = []
for i, line in enumerate(table):
row = line.split()
row = [float(x) for x in row]
q_table.append(row)
return q_table
def writeQtable(self):
"Write qtable to disc"
self.table_file.seek(0)
self.table_file.truncate()
for line in self.q_table:
for item in line:
self.table_file.write(str(item)+" ")
self.table_file.write("\n")
self.table_file.close()
def __del__(self):
"Destructor. Invokation at the end of each episode"
self.writeQtable()
# state is a array with
# direction_ghost (norte, sur, este, oeste)
# distance_ghost (inmediata, cerca, lejos)
# direction_pacdot (norte, sur, este, oeste, none)
# distance_pacdot (inmediata, cerca, lejos)
def computePosition(self, state):
"""
Compute the row of the qtable for a given state.
"""
return state[0]+state[1]*4+state[2]*12+state[3]*60
def getQValue(self, state, action):
"""
Returns Q(state,action)
Should return 0.0 if we have never seen a state
or the Q node value otherwise
"""
position = self.computePosition(state)
action_column = self.actions[action]
return self.q_table[position][action_column]
def computeValueFromQValues(self, state, gameState):
"""
Returns max_action Q(state,action)
where the max is over legal actions. Note that if
there are no legal actions, which is the case at the
terminal state, you should return a value of 0.0.
"""
legalActions = gameState.getLegalActions(0)
if len(legalActions)==0:
return 0
return max(self.q_table[self.computePosition(state)])
def computeActionFromQValues(self, state, gameState):
"""
Compute the best action to take in a state. Note that if there
are no legal actions, which is the case at the terminal state,
you should return None.
"""
legalActions = gameState.getLegalActions(0)
if len(legalActions)==0:
return None
legalActions.remove(Directions.STOP)
best_actions = [legalActions[0]]
best_value = self.getQValue(state, legalActions[0])
for action in legalActions:
value = self.getQValue(state, action)
if value == best_value:
best_actions.append(action)
if value > best_value:
best_actions = [action]
best_value = value
return random.choice(best_actions)
def getActionQtable(self, state, gameState):
"""
Compute the action to take in the current state. With
probability self.epsilon, we should take a random action and
take the best policy action otherwise. Note that if there are
no legal actions, which is the case at the terminal state, you
should choose None as the action.
"""
# Pick Action
legalActions = gameState.getLegalActions(0)
legalActions.remove(Directions.STOP)
action = None
if len(legalActions) == 0:
return action
flip = util.flipCoin(self.epsilon)
if flip:
return random.choice(legalActions)
return self.getPolicy(state, gameState)
def update(self, state, action, nextState, reward,gameState):
"""
The parent class calls this to observe a
state = action => nextState and reward transition.
You should do your Q-Value update here
Good Terminal state -> reward 1
Bad Terminal state -> reward -1
Otherwise -> reward 0
Q-Learning update:
if terminal_state:
Q(state,action) <- (1-self.alpha) Q(state,action) + self.alpha * (r + 0)
else:
Q(state,action) <- (1-self.alpha) Q(state,action) + self.alpha * (r + self.discount * max a' Q(nextState, a'))
"""
# TRACE for transition and position to update. Comment the following lines if you do not want to see that trace
if self.entrenamiento == 0:
print("Update Q-table with transition: ", state, action, nextState, reward)
position = self.computePosition(state)
action_column = self.actions[action]
if self.entrenamiento == 0:
print("Corresponding Q-table cell to update:", position, action_column)
position = self.computePosition(state)
"*** YOUR CODE HERE ***"
self.q_table[position][action_column] = (1-self.alpha) * self.getQValue(state,action) + self.alpha * (reward + self.discount * self.computeValueFromQValues(nextState,gameState))
# TRACE for updated q-table. Comment the following lines if you do not want to see that trace
# print("Q-table:")
# self.printQtable()
def getPolicy(self, state, gameState):
"Return the best action in the qtable for a given state"
return self.computeActionFromQValues(state, gameState)
def getValue(self, state, gameState):
"Return the highest q value for a given state"
return self.computeValueFromQValues(state, gameState)
def chooseAction(self, gameState):
# Si tarda mucho termina
self.countActions = self.countActions + 1
if self.tickLimit != 0 and self.countActions > self.tickLimit:
quit()
# Calcular el state
# Default action
move = Directions.STOP
legal = gameState.getLegalActions(0) ##Legal position from the pacman
pacman_position = gameState.getPacmanPosition()
ghosts_position = gameState.getGhostPositions()
num_ghost = 0
for ghost in gameState.getLivingGhosts():
if ghost == True:
num_ghost += 1
# the target object is a pacdot
pacdot_position = None
minDistance = 900000
pacmanPosition = gameState.getPacmanPosition()
num_pacdot = 0
# search the nearest pacdot
for i in range(gameState.data.layout.width):
for j in range(gameState.data.layout.height):
if gameState.hasFood(i, j):
num_pacdot +=1
foodPosition = i, j
distance = self.distancer.getDistance(pacmanPosition, foodPosition)
if distance < minDistance:
minDistance = distance
pacdot_position = foodPosition
object_distances = []
for ghost_position in ghosts_position:
object_distances.append(self.distancer.getDistance(pacman_position,ghost_position))
# Get the min distance in the list
num_object = None
index = 0
final_value = 100000
for value in object_distances:
if value and value <= final_value:
final_value = value
num_object = index
index +=1
near_object_position = ghosts_position[num_object]
next_distances = {}
for action in legal:
next_position = [pacman_position[0],pacman_position[1]]
if action == Directions.NORTH:
next_position[1]+=1
next_distances[Directions.NORTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.SOUTH:
next_position[1]-=1
next_distances[Directions.SOUTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.EAST:
next_position[0]+=1
next_distances[Directions.EAST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.WEST:
next_position[0]-=1
next_distances[Directions.WEST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
final_direction = None
min_distance = 100000
for _direction, _distance in next_distances.items():
if _distance < min_distance:
min_distance = _distance
final_direction = _direction
direction_ghost = None
if final_direction == Directions.NORTH:
direction_ghost = NORTE
if final_direction == Directions.SOUTH:
direction_ghost = SUR
if final_direction == Directions.EAST:
direction_ghost = ESTE
if final_direction == Directions.WEST:
direction_ghost = OESTE
# For the ghost distance
distance_ghost = None
dis_ghost = object_distances[num_object]
if object_distances[num_object] == 1:
distance_ghost = INMEDIATA
elif object_distances[num_object] == 2:
distance_ghost = CERCANA
else:
distance_ghost = LEJOS
# For the pacdot direction
direction_pacdot = None
if pacdot_position:
near_object_position = pacdot_position
next_distances = {}
for action in legal:
next_position = [pacman_position[0],pacman_position[1]]
if action == Directions.NORTH:
next_position[1]+=1
next_distances[Directions.NORTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.SOUTH:
next_position[1]-=1
next_distances[Directions.SOUTH]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.EAST:
next_position[0]+=1
next_distances[Directions.EAST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
elif action == Directions.WEST:
next_position[0]-=1
next_distances[Directions.WEST]=self.distancer.getDistance(tuple(next_position),tuple(near_object_position))
final_direction = None
min_distance = 100000
for _direction, _distance in next_distances.items():
if _distance < min_distance:
min_distance = _distance
final_direction = _direction
if final_direction == Directions.NORTH:
direction_pacdot = NORTE
if final_direction == Directions.SOUTH:
direction_pacdot = SUR
if final_direction == Directions.EAST:
direction_pacdot = ESTE
if final_direction == Directions.WEST:
direction_pacdot = OESTE
else:
direction_pacdot = NONE
# Para pacdot distance
if pacdot_position:
pacdot_num_distance = self.distancer.getDistance(pacman_position,pacdot_position)
dis_pacdot = pacdot_num_distance
distance_pacdot = None
if pacdot_num_distance == 1:
distance_pacdot = INMEDIATA
elif pacdot_num_distance == 2:
distance_pacdot = CERCANA
else:
distance_pacdot = LEJOS
else:
distance_pacdot = LEJOS
state = [direction_ghost,distance_ghost,direction_pacdot,distance_pacdot]
move = self.getActionQtable(state, gameState)
if self.last_state != None and self.last_move != None:
# Calcular la recompensa
reward = 0
if self.last_num_ghost > num_ghost:
# Cuando se come un fantasma
reward += 50
if self.last_num_pacdot > num_pacdot:
# Cuando se come un pacdot
reward += 100
# state is a array with
# direction_ghost (norte, sur, este, oeste)
# distance_ghost (inmediata, cerca, lejos)
# direction_pacdot (norte, sur, este, oeste, none)
# distance_pacdot (inmediata, cerca, lejos)
self.update(self.last_state,self.last_move,state,reward,gameState)
# self.writeQtable()
self.last_state = state
self.last_move = move
self.last_num_ghost = num_ghost
self.last_num_pacdot = num_pacdot
return move