-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdpAgentswithPrintTest.py
1176 lines (1176 loc) · 47.9 KB
/
mdpAgentswithPrintTest.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# # -*-coding:utf-8-*-
# # coding=utf-8
# # mdpAgents.py
# # parsons/20-nov-2017
# #
# # Version 1
# #
# # The starting point for CW2.
# #
# # Intended to work with the PacMan AI projects from:
# #
# # http://ai.berkeley.edu/
# #
# # These use a simple API that allow us to control Pacman's interaction with
# # the environment adding a layer on top of the AI Berkeley code.
# #
# # As required by the licensing agreement for the PacMan AI we have:
# #
# # 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, including a link to http://ai.berkeley.edu.
# #
# # Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# # The core projects and autograders were primarily created by John DeNero
# # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# # Student side autograding was added by Brad Miller, Nick Hay, and
# # Pieter Abbeel (pabbeel@cs.berkeley.edu).
#
# # The agent here is was written by Simon Parsons, based on the code in
# # pacmanAgents.py
#
# from pacman import Directions
# from game import Agent
# import api
# import random
# import game
# import util
#
# # # Default
# # class MDPAgent(Agent):
# #
# # # Constructor: this gets run when we first invoke pacman.py
# # def __init__(self):
# # print "Starting up MDPAgent!"
# # name = "Pacman"
# #
# # # Gets run after an MDPAgent object is created and once there is
# # # game state to access.
# # def registerInitialState(self, state):
# # print "Running registerInitialState for MDPAgent!"
# # print "I'm at:"
# # print api.whereAmI(state)
# #
# # # This is what gets run in between multiple games
# # def final(self, state):
# # print "Looks like the game just ended!"
# #
# # # For now I just move randomly
# # def getAction(self, state):
# # # Get the actions we can try, and remove "STOP" if that is one of them.
# # legal = api.legalActions(state)
# # if Directions.STOP in legal:
# # legal.remove(Directions.STOP)
# # # Random choice between the legal options.
# # return api.makeMove(random.choice(legal), legal)
#
# # class SatNav:
# #
# # def __init__(self, state):
# # # Number of Rows
# # wall = api.walls(state)
# # w = wall[-1][0] + 1
# # self.row = w
# # # Number of Columns
# # h = wall[-1][1] + 1
# # self.column = h
# #
# # # Build up the map
# # self.map = []
# # while len(self.map) != self.row:
# # self.map.append([0] * self.column)
# # # Pacman location
# # pac = api.whereAmI(state)
# # self.map[pac[0]][pac[1]] = 'P'
# # # Walls
# # for x, y in wall:
# # self.map[x][y] = '%'
# # # Foods
# # food = api.food(state)
# # for x, y in food:
# # self.map[x][y] = '*'
# # # Ghosts
# # ghost = api.ghosts(state)
# # if ghost:
# # for x, y in ghost:
# # self.map[x][y] = 'G'
# # # Capsules
# # capsule = api.capsules(state)
# # if capsule:
# # for x, y in capsule:
# # self.map[x][y] = 'C'
# # # self.map = m
# #
# # # Indexing systems
# # self.freespace = self.Freespace()
# # self.moves = self.legalMoves()
# # self.utility = self.Utility()
# # self.tableUtility = self.table_utility()
# # self.policy = self.policyInit()
# #
# # #
# # # Parameters initialisation methods
# # #
# #
# # # 'Walkable' paths
# # def Freespace(self):
# # f = []
# # for x in range(self.row):
# # for y in range(self.column):
# # if self.map[x][y] != '%':
# # f.append((x, y))
# # return f
# #
# # # Utility values matrix initialisation
# # def Utility(self):
# # u = self.map
# # for x in range(self.row):
# # for y in range(self.column):
# # # Pacman & Walls
# # if u[x][y] == "P" or u[x][y] == "%":
# # u[x][y] = 0
# # # Foods
# # if u[x][y] == '*':
# # u[x][y] = 1
# # # Ghosts
# # if u[x][y] == 'G':
# # u[x][y] = -1
# # # Capsules
# # if u[x][y] == 'C':
# # u[x][y] = 10
# # return u
# #
# # # Action-next-state Utility values table
# # def table_utility(self):
# # # TABLE of UTILITY:
# # tu = {}
# # # Create a table that retrieve the utility value of next move with the format
# # # tu[location][action] -> utility value
# # #
# # # Adding elements to the Nested Dictionary
# # # From https://www.programiz.com/python-programming/nested-dictionary
# # #
# # for x, y in self.freespace:
# # tu[(x, y)] = {}
# # # NORTH
# # if self.map[x][y + 1] != '%':
# # tu[(x, y)]['North'] = self.utility[x][y + 1]
# # else:
# # tu[(x, y)]['North'] = self.utility[x][y]
# # # EAST
# # if self.map[x + 1][y] != '%':
# # tu[(x, y)]['East'] = self.utility[x + 1][y]
# # else:
# # tu[(x, y)]['East'] = self.utility[x][y]
# # # SOUTH
# # if self.map[x][y - 1] != '%':
# # tu[(x, y)]['South'] = self.utility[x][y - 1]
# # else:
# # tu[(x, y)]['South'] = self.utility[x][y]
# # # WEST
# # if self.map[x - 1][y] != '%':
# # tu[(x, y)]['West'] = self.utility[x - 1][y]
# # else:
# # tu[(x, y)]['West'] = self.utility[x][y]
# # return tu
# #
# # # Initialise next-move guide
# # def policyInit(self):
# # p = {}
# # for x, y in self.freespace:
# # p[(x, y)] = 'East'
# # return p
# #
# # # Initialise the legal actions table
# # def legalMoves(self):
# # lm = {}
# # for x, y in self.freespace:
# # lm[(x, y)] = []
# # # NORTH
# # if self.map[x][y + 1] != '%':
# # lm[(x, y)].append('North')
# # # EAST
# # if self.map[x + 1][y] != '%':
# # lm[(x, y)].append('East')
# # # SOUTH
# # if self.map[x][y - 1] != '%':
# # lm[(x, y)].append('South')
# # # WEST
# # if self.map[x - 1][y] != '%':
# # lm[(x, y)].append('West')
# # return lm
# #
# # #
# # # Value assignment methods
# # #
# #
# # # Print out the map
# # def printmap(self):
# # print 'Current map: '
# # for i in range(self.row):
# # for j in range(self.column):
# # print self.map[i][j],
# # print '\n'
# #
# # # Print out the utility matrix
# # def printutility(self):
# # print 'Utility values: '
# # for x in range(self.row):
# # for y in range(self.column):
# # print repr(round(self.utility[x][y], 3)).rjust(5),
# # print '\n'
# #
# # # Print out free grids
# # def printfreespace(self):
# # print 'Freespace: ',
# # print self.freespace
# #
# # # Update Utility values at new runtime
# # def updateUtility(self, u_new):
# # self.utility = u_new
# #
# # # Update Table of Utility values at new runtime
# # def updateUtilityTable(self, u_new):
# # for x, y in self.freespace:
# # if 'North' in self.moves[(x, y)]:
# # self.tableUtility[(x, y)]['North'] = u_new[x][y + 1]
# # else:
# # self.tableUtility[(x, y)]['North'] = u_new[x][y]
# # if 'East' in self.moves[(x, y)]:
# # self.tableUtility[(x, y)]['East'] = u_new[x + 1][y]
# # else:
# # self.tableUtility[(x, y)]['East'] = u_new[x][y]
# # if 'South' in self.moves[(x, y)]:
# # self.tableUtility[(x, y)]['South'] = u_new[x][y - 1]
# # else:
# # self.tableUtility[(x, y)]['South'] = u_new[x][y]
# # if 'West' in self.moves[(x, y)]:
# # self.tableUtility[(x, y)]['West'] = u_new[x - 1][y]
# # else:
# # self.tableUtility[(x, y)]['West'] = u_new[x][y]
#
#
# # MDP Solver
# class MDPAgent(Agent):
# # Constructor: this gets run when we first invoke pacman.py
# def __init__(self):
# print "Starting up MDPAgent!"
# self.delta = 0.001 # Bellman factor: decides if the model converges
# self.gamma = 0.5 # Discount factor: reduces the utility values with each time step
# self.reward = -0.5 # Incentive for Pac-Man to keep moving
#
# # Gets run after an MDPAgent object is created and once there is
# # game state to access.
# def registerInitialState(self, state):
# # print "Running registerInitialState for MDPAgent!"
# # print "I'm at: ", api.whereAmI(state)
# print "\n======= Establishing new environment ========\n"
# self.buildmap(state)
# self.Freespace()
# print "======= Map in registerInitialState: ========"
# self.printmap()
# self.legalMoves()
# print "\n==== Ghost States ====\n"
# self.ghoststate = api.ghostStates(state)
# print self.ghoststate
# print "\n==== Ghost Time Remain ====\n"
# self.ghosttime = api.ghostStatesWithTimes(state)
# print self.ghosttime, '\n'
# print "==== Ghost Status Table ====\n"
# self.ghoststatus = self.ghostStatus()
# print self.ghoststatus, '\n'
# # Something's wrong with the Utility table, once passed it, the Map turns into
# # 导致出错的主要数据根源 utility
# self.Utility(state)
#
# self.table_utility()
# self.policyInit()
#
#
# # This is what gets run in between multiple games
# def final(self, state):
# print "Looks like the game just ended!"
# # self.buildmap(state)
# # self.Freespace()
# # self.legalMoves()
# # self.Utility(state)
# # self.table_utility()
# # self.policyInit()
#
# #
# # Data structures methods
# #
#
# # Map
# def buildmap(self, state):
# # Number of Rows
# wall = api.walls(state)
# w = wall[-1][0] + 1
# self.row = w
# # Number of Columns
# h = wall[-1][1] + 1
# self.column = h
# self.map = []
# while len(self.map) != self.row:
# self.map.append([0] * self.column)
# # Pacman location
# pac = api.whereAmI(state)
# self.map[pac[0]][pac[1]] = 'P'
# # Walls
# for x, y in wall:
# self.map[x][y] = '%'
# # Foods
# food = api.food(state)
# for x, y in food:
# self.map[x][y] = '*'
# # Capsules
# capsule = api.capsules(state)
# if capsule:
# for x, y in capsule:
# self.map[x][y] = 'C'
# # Ghosts
# ghost = api.ghosts(state)
# if ghost:
# for x, y in ghost:
# self.map[int(x)][int(y)] = 'G'
#
# # # Utility array v1
# # def Utility(self):
# # print "====== Map BEFORE in Utility: ========="
# # self.printmap()
# # m = self.map
# # self.utility = m
# # for x in range(self.row):
# # for y in range(self.column):
# # # Pacman & Walls
# # if m[x][y] == "P" or m[x][y] == "%":
# # self.utility[x][y] = 0
# # # Foods
# # if m[x][y] == '*':
# # self.utility[x][y] = 0.1
# # # Ghosts
# # if m[x][y] == 'G':
# # self.utility[x][y] = -30
# # # Capsules
# # if m[x][y] == 'C':
# # self.utility[x][y] = 0.1
# # print "====== Map AFTER in Utility: ========="
# # self.printmap()
#
# # Utility array v2
# def Utility(self, state):
# # print "====== Map BEFORE in Utility: ========="
# # self.printmap()
# self.utility = []
# foods = api.food(state)
# cap = api.capsules(state)
# ghosts = api.ghosts(state)
# f = self.freespace
# print "===== Free Space ====="
# print f
# while len(self.utility) != self.row:
# self.utility.append([0] * self.column)
# for x, y in foods:
# self.utility[x][y] = 0.1
# for x, y in cap:
# self.utility[x][y] = 10
# # Hunting Mode change ghosts utility by checking EDIBLE status
# # mediumClassic
# if self.row > 8:
# status = self.ghoststatus
# # Keep yourself alive!
# if not status[1]:
# for x, y in ghosts:
# self.utility[int(x)][int(y)] = -30
# # Ghosts dangerous perimeters
# # North
# if (x, y + 1) in f:
# self.utility[int(x)][int(y + 1)] = -30
# if (x, y + 2) in f:
# self.utility[int(x)][int(y + 2)] = -30
# # NorthEast
# if (x + 1, y + 1) in f:
# self.utility[int(x + 1)][int(y + 1)] = -30
# if (x + 1, y + 2) in f:
# self.utility[int(x + 1)][int(y + 2)] = -30
# if (x + 2, y + 1) in f:
# self.utility[int(x + 2)][int(y + 1)] = -30
# # East
# if (x + 1, y) in f:
# self.utility[int(x + 1)][int(y)] = -30
# if (x + 2, y) in f:
# self.utility[int(x + 2)][int(y)] = -30
# # SouthEast
# if (x + 1, y - 1) in f:
# self.utility[int(x + 1)][int(y - 1)] = -30
# if (x + 1, y - 2) in f:
# self.utility[int(x + 1)][int(y - 2)] = -30
# if (x + 2, y - 1) in f:
# self.utility[int(x + 2)][int(y - 1)] = -30
# # South
# if (x, y - 1) in f:
# self.utility[int(x)][int(y - 1)] = -30
# if (x, y - 2) in f:
# self.utility[int(x)][int(y - 2)] = -30
# # SouthWest
# if (x - 1, y - 1) in f:
# self.utility[int(x - 1)][int(y - 1)] = -30
# if (x - 1, y - 2) in f:
# self.utility[int(x - 1)][int(y - 2)] = -30
# if (x - 2, y - 1) in f:
# self.utility[int(x - 2)][int(y - 1)] = -30
# # West
# if (x - 1, y) in f:
# self.utility[int(x - 1)][int(y)] = -30
# if (x - 2, y) in f:
# self.utility[int(x - 2)][int(y)] = -30
# # NorthWest
# if (x - 1, y + 1) in f:
# self.utility[int(x - 1)][int(y + 1)] = -30
# if (x - 1, y + 2) in f:
# self.utility[int(x - 1)][int(y + 2)] = -30
# if (x - 2, y + 1) in f:
# self.utility[int(x - 2)][int(y + 1)] = -30
# # Ghosts are scared!
# else:
# if self.ghosttime[0][1] > 3:
# # Edible ghosts
# for x, y in foods:
# self.utility[x][y] = 0
# for x, y in cap:
# self.utility[x][y] = 0
# for x, y in status[1]:
# self.utility[int(x)][int(y)] = 30
# # Dangerous ghosts
# for x, y in status[0]:
# self.utility[int(x)][int(y)] = -30
# # Ghosts dangerous perimeters
# # North
# if (x, y + 1) in f:
# self.utility[int(x)][int(y + 1)] = -30
# if (x, y + 2) in f:
# self.utility[int(x)][int(y + 2)] = -30
# # NorthEast
# if (x + 1, y + 1) in f:
# self.utility[int(x + 1)][int(y + 1)] = -30
# if (x + 1, y + 2) in f:
# self.utility[int(x + 1)][int(y + 2)] = -30
# if (x + 2, y + 1) in f:
# self.utility[int(x + 2)][int(y + 1)] = -30
# # East
# if (x + 1, y) in f:
# self.utility[int(x + 1)][int(y)] = -30
# if (x + 2, y) in f:
# self.utility[int(x + 2)][int(y)] = -30
# # SouthEast
# if (x + 1, y - 1) in f:
# self.utility[int(x + 1)][int(y - 1)] = -30
# if (x + 1, y - 2) in f:
# self.utility[int(x + 1)][int(y - 2)] = -30
# if (x + 2, y - 1) in f:
# self.utility[int(x + 2)][int(y - 1)] = -30
# # South
# if (x, y - 1) in f:
# self.utility[int(x)][int(y - 1)] = -30
# if (x, y - 2) in f:
# self.utility[int(x)][int(y - 2)] = -30
# # SouthWest
# if (x - 1, y - 1) in f:
# self.utility[int(x - 1)][int(y - 1)] = -30
# if (x - 1, y - 2) in f:
# self.utility[int(x - 1)][int(y - 2)] = -30
# if (x - 2, y - 1) in f:
# self.utility[int(x - 2)][int(y - 1)] = -30
# # West
# if (x - 1, y) in f:
# self.utility[int(x - 1)][int(y)] = -30
# if (x - 2, y) in f:
# self.utility[int(x - 2)][int(y)] = -30
# # NorthWest
# if (x - 1, y + 1) in f:
# self.utility[int(x - 1)][int(y + 1)] = -30
# if (x - 1, y + 2) in f:
# self.utility[int(x - 1)][int(y + 2)] = -30
# if (x - 2, y + 1) in f:
# self.utility[int(x - 2)][int(y + 1)] = -30
# # smallGrid
# else:
# for x, y in ghosts:
# self.utility[int(x)][int(y)] = -30
# # Ghosts dangerous perimeters
# # North
# # if (x, y + 1) in f:
# # self.utility[int(x)][int(y + 1)] = -30
# # if (x, y + 2) in f:
# # self.utility[int(x)][int(y + 2)] = -30
# # if (x, y + 3) in f:
# # self.utility[int(x)][int(y + 3)] = -30
# # NorthEast
# # if (x + 1, y + 1) in f:
# # self.utility[int(x + 1)][int(y + 1)] = -30
# # if (x + 1, y + 2) in f:
# # self.utility[int(x + 1)][int(y + 2)] = -30
# # if (x + 2, y + 1) in f:
# # self.utility[int(x + 2)][int(y + 1)] = -30
# # East
# # if (x + 1, y) in f:
# # self.utility[int(x + 1)][int(y)] = -30
# # if (x + 2, y) in f:
# # self.utility[int(x + 2)][int(y)] = -30
# # if (x + 3, y) in f:
# # self.utility[int(x + 3)][int(y)] = -30
# # SouthEast
# # if (x + 1, y - 1) in f:
# # self.utility[int(x + 1)][int(y - 1)] = -30
# # if (x + 1, y - 2) in f:
# # self.utility[int(x + 1)][int(y - 2)] = -30
# # if (x + 2, y - 1) in f:
# # self.utility[int(x + 2)][int(y - 1)] = -30
# # South
# # if (x, y - 1) in f:
# # self.utility[int(x)][int(y - 1)] = -30
# # if (x, y - 2) in f:
# # self.utility[int(x)][int(y - 2)] = -30
# # if (x, y - 3) in f:
# # self.utility[int(x)][int(y - 3)] = -30
# # SouthWest
# if (x - 1, y - 1) in f:
# self.utility[int(x - 1)][int(y - 1)] = -30
# if (x - 1, y - 2) in f:
# self.utility[int(x - 1)][int(y - 2)] = -30
# # if (x - 2, y - 1) in f:
# # self.utility[int(x - 2)][int(y - 1)] = -30
# # West
# # if (x - 1, y) in f:
# # self.utility[int(x - 1)][int(y)] = -30
# # if (x - 2, y) in f:
# # self.utility[int(x - 2)][int(y)] = -30
# # if (x - 3, y) in f:
# # self.utility[int(x - 3)][int(y)] = -30
# # NorthWest
# # if (x - 1, y + 1) in f:
# # self.utility[int(x - 1)][int(y + 1)] = -30
# # if (x - 1, y + 2) in f:
# # self.utility[int(x - 1)][int(y + 2)] = -30
# # if (x - 2, y + 1) in f:
# # self.utility[int(x - 2)][int(y + 1)] = -30
# # print "====== Map AFTER in Utility: ========="
# # self.printmap()
# print "====== NEW UTILITY ======="
# self.printutility()
#
# # Free space
# def Freespace(self):
# self.freespace = []
# for x in range(self.row):
# for y in range(self.column):
# if self.map[x][y] != '%':
# self.freespace.append((x, y))
# # print "========= Map in Freespace: =========="
# # self.printmap()
# # print "Freespace: ", self.freespace
#
# # Legal moves at each state
# def legalMoves(self):
# self.availMoves = {}
# # print "========= Map in legalMoves: ==========="
# # self.printmap()
# m = self.map
# # print "===== Map in legalMoves ====="
# # self.printmap()
# # print "=== Freespace in legalMoves: ==="
# # print self.freespace
# for x, y in self.freespace:
# self.availMoves[(x, y)] = []
# # NORTH
# if m[x][y + 1] != '%': # and m[x][y + 1] != 'G'
# self.availMoves[(x, y)].append('North')
# # EAST
# if m[x + 1][y] != '%': # and m[x + 1][y] != 'G'
# self.availMoves[(x, y)].append('East')
# # SOUTH
# if m[x][y - 1] != '%': # and m[x][y - 1] != 'G'
# self.availMoves[(x, y)].append('South')
# # WEST
# if m[x - 1][y] != '%': # and m[x - 1][y] != 'G':
# self.availMoves[(x, y)].append('West')
# # Overseeing workspace
# # print repr(self.availMoves)
#
# # Table of Utility: legal moves utility & illegal moves 'still'
# def table_utility(self):
# # TABLE of UTILITY:
# self.tableUtility = {}
# # print "========= Map in table_utlity: ==========="
# # self.printmap()
# # Create a table that retrieve the utility value of next move with the format
# # tu[location][action] -> utility value
# #
# # Adding elements to the Nested Dictionary
# # From https://www.programiz.com/python-programming/nested-dictionary
# #
# for x, y in self.freespace:
# self.tableUtility[(x, y)] = {}
# # NORTH
# if self.map[x][y + 1] != '%':
# self.tableUtility[(x, y)]['North'] = self.utility[x][y + 1]
# else:
# self.tableUtility[(x, y)]['North'] = self.utility[x][y]
# # EAST
# if self.map[x + 1][y] != '%':
# self.tableUtility[(x, y)]['East'] = self.utility[x + 1][y]
# else:
# self.tableUtility[(x, y)]['East'] = self.utility[x][y]
# # SOUTH
# if self.map[x][y - 1] != '%':
# self.tableUtility[(x, y)]['South'] = self.utility[x][y - 1]
# else:
# self.tableUtility[(x, y)]['South'] = self.utility[x][y]
# # WEST
# if self.map[x - 1][y] != '%':
# self.tableUtility[(x, y)]['West'] = self.utility[x - 1][y]
# else:
# self.tableUtility[(x, y)]['West'] = self.utility[x][y]
# # self.tableUtility = tu
# # Overseeing workspace
# # print repr(self.tableUtility)
#
# # Global policy
# def policyInit(self):
# p = {}
# for x, y in self.freespace:
# p[(x, y)] = 'East'
# self.policy = p
#
# # Ghost Status
# def ghostStatus(self):
# status = {0: [], 1: []}
# for gh, st in self.ghoststate:
# status[st].append(gh)
# return status
#
# #
# # Data Update methods
# #
# def updateUtilityTable(self, nu):
# for x, y in self.freespace:
# if 'North' in self.availMoves[(x, y)]:
# self.tableUtility[(x, y)]['North'] = nu[x][y + 1]
# else:
# self.tableUtility[(x, y)]['North'] = nu[x][y]
# if 'East' in self.availMoves[(x, y)]:
# self.tableUtility[(x, y)]['East'] = nu[x + 1][y]
# else:
# self.tableUtility[(x, y)]['East'] = nu[x][y]
# if 'South' in self.availMoves[(x, y)]:
# self.tableUtility[(x, y)]['South'] = nu[x][y - 1]
# else:
# self.tableUtility[(x, y)]['South'] = nu[x][y]
# if 'West' in self.availMoves[(x, y)]:
# self.tableUtility[(x, y)]['West'] = nu[x - 1][y]
# else:
# self.tableUtility[(x, y)]['West'] = nu[x][y]
#
# #
# # Print methods
# #
# # Print map
# def printmap(self):
# print 'Current Environment: '
# for i in range(self.row):
# for j in range(self.column):
# print self.map[i][j],
# print '\n'
#
# # Print Utility
# def printutility(self):
# print "Utility: "
# for i in range(self.row):
# for j in range(self.column):
# print repr(round(self.utility[i][j], 3)).rjust(7),
# print '\n'
#
# # Print out free grids
# def printfreespace(self):
# print 'Freespace: ',
# print self.freespace
#
# #
# # Value Iteration methods
# #
# # Bellman function
# def bellman(self, ns): # ns -> N States: tuple(x, y)
# tu = self.tableUtility
# u = {}
# # Loop over legal actions
# for action in self.availMoves[ns]:
# u[action] = 0.8 * tu[ns][action]
# if action == 'North' or action == 'South':
# u[action] += 0.1 * (tu[ns]['East'] + tu[ns]['West'])
# if action == 'East' or action == 'West':
# u[action] += 0.1 * (tu[ns]['North'] + tu[ns]['South'])
# u[action] = self.reward + self.gamma * u[action]
# v = list(u.values())
# k = list(u.keys())
# # If all actions have the same utility? Shouldn't have same utility at the end of algorithm
# return max(v), k[v.index(max(v))]
#
# # Value iteration algorithm: return current optimal policy
# def valueIteration(self, state):
# # maxdiff = -1e9
# print "==== Value Iteration Algorithm ===="
# # u_old = self.utility
# u = self.utility
# p = self.policy
# print "==== TEST 1 ===="
# foods = api.food(state)
# ghosts = api.ghosts(state)
# g = api.ghosts(state)
# cap = api.capsules(state)
# print "==== TEST 2 ===="
# f = self.freespace
# print "==== TEST 3 ===="
# # Ghosts dangerous perimeters
# # V1: Manipulate food list
# # pass
# # for x, y in ghosts:
# # # North
# # if (x, y + 1) in foods:
# # foods.remove((x, y + 1))
# # if (x, y + 1) in cap:
# # cap.remove((x, y + 1))
# # # NorthEast
# # if (x + 1, y + 1) in foods:
# # foods.remove((x + 1, y + 1))
# # if (x + 1, y + 1) in cap:
# # cap.remove((x + 1, y + 1))
# # if (x + 1, y + 2) in foods:
# # foods.remove((x + 1, y + 2))
# # if (x + 1, y + 2) in cap:
# # cap.remove((x + 1, y + 2))
# # # East
# # if (x + 1, y) in foods:
# # foods.remove((x + 1, y))
# # if (x + 1, y) in cap:
# # cap.remove((x + 1, y))
# # # SouthEast
# # if (x + 1, y - 1) in foods:
# # foods.remove((x + 1, y - 1))
# # if (x + 1, y - 1) in cap:
# # cap.remove((x + 1, y - 1))
# # if (x + 1, y - 2) in foods:
# # foods.remove((x + 1, y - 2))
# # if (x + 1, y - 2) in cap:
# # cap.remove((x + 1, y - 2))
# # # South
# # if (x, y - 1) in foods:
# # foods.remove((x, y - 1))
# # if (x, y - 1) in cap:
# # cap.remove((x, y - 1))
# # # SouthWest
# # if (x - 1, y - 1) in foods:
# # foods.remove((x - 1, y - 1))
# # if (x - 1, y - 1) in cap:
# # cap.remove((x - 1, y - 1))
# # if (x - 1, y - 2) in foods:
# # foods.remove((x - 1, y - 2))
# # if (x - 1, y - 2) in cap:
# # cap.remove((x - 1, y - 2))
# # # West
# # if (x - 1, y) in foods:
# # foods.remove((x - 1, y))
# # if (x - 1, y) in cap:
# # cap.remove((x - 1, y))
# # # NorthWest
# # if (x - 1, y + 1) in foods:
# # foods.remove((x - 1, y + 1))
# # if (x - 1, y + 1) in cap:
# # cap.remove((x - 1, y + 1))
# # if (x - 1, y + 2) in foods:
# # foods.remove((x - 1, y + 2))
# # if (x - 1, y + 2) in cap:
# # cap.remove((x - 1, y + 2))
#
# # pass
# # Ghosts dangerous perimeters
# # V2: Manipulate Ghost list
# print "==== TEST 4 ===="
# # mediumClassic
# if self.row > 8:
# # Keep yourself alive!
# for x, y in g:
# x = int(x)
# y = int(y)
# # North
# print "==== TEST 5 ===="
# if (x, y + 1) in f:
# ghosts.append((x, y + 1))
# # if (x, y + 2) in f:
# # ghosts.append((x, y + 2))
# # NorthEast
# # if (x + 1, y + 1) in f:
# # ghosts.append((x + 1, y + 1))
# # if (x + 1, y + 2) in f:
# # ghosts.append((x + 1, y + 2))
# # if (x + 2, y + 1) in f:
# # ghosts.append((x + 1, y + 2))
# # East
# if (x + 1, y) in f:
# ghosts.append((x + 1, y))
# # if (x + 2, y) in f:
# # ghosts.append((x + 2, y))
# # SouthEast
# # if (x + 1, y - 1) in f:
# # ghosts.append((x + 1, y - 1))
# # if (x + 1, y - 2) in f:
# # ghosts.append((x + 1, y - 2))
# # if (x + 2, y - 1) in f:
# # ghosts.append((x + 1, y - 1))
# # South
# if (x, y - 1) in f:
# ghosts.append((x, y - 1))
# # if (x, y - 2) in f:
# # ghosts.append((x, y - 2))
# # SouthWest
# # if (x - 1, y - 1) in f:
# # ghosts.append((x - 1, y - 1))
# # if (x - 1, y - 2) in f:
# # ghosts.append((x - 1, y - 2))
# # if (x - 2, y - 1) in f:
# # ghosts.append((x - 2, y - 1))
# # West
# if (x - 1, y) in f:
# ghosts.append((x - 1, y))
# # if (x - 2, y) in f:
# # ghosts.append((x - 2, y))
# # NorthWest
# # if (x - 1, y + 1) in f:
# # ghosts.append((x - 1, y - 1))
# # if (x - 1, y + 2) in f:
# # ghosts.append((x - 1, y + 2))
# # if (x - 2, y + 1) in f:
# # ghosts.append((x - 2, y + 1))
# # smallGrid
# else:
# status = [0, 0]
# for x, y in g:
# # North
# print "==== TEST 5 ===="
# # if (x, y + 1) in f:
# # ghosts.append((x, y + 1))
# # if (x, y + 2) in f:
# # ghosts.append((x, y + 2))
# # NorthEast
# # if (x + 1, y + 1) in f:
# # ghosts.append((x + 1, y + 1))
# # if (x + 1, y + 2) in f:
# # ghosts.append((x + 1, y + 2))
# # if (x + 2, y + 1) in f:
# # ghosts.append((x + 1, y + 2))
# # East
# # if (x + 1, y) in f:
# # ghosts.append((x + 1, y))
# # if (x + 2, y) in f:
# # ghosts.append((x + 2, y))
# # SouthEast
# # if (x + 1, y - 1) in f:
# # ghosts.append((x + 1, y - 1))
# # if (x + 1, y - 2) in f:
# # ghosts.append((x + 1, y - 2))
# # if (x + 2, y - 1) in f:
# # ghosts.append((x + 1, y - 1))
# # SouthWest
# if (x - 1, y - 1) in f:
# ghosts.append((x - 1, y - 1))
# if (x - 1, y - 2) in f:
# ghosts.append((x, y - 2))
# # SouthWest
# # if (x - 1, y - 1) in f:
# # ghosts.append((x - 1, y - 1))
# # if (x - 1, y - 2) in f:
# # ghosts.append((x - 1, y - 2))
# # if (x - 2, y - 1) in f:
# # ghosts.append((x - 2, y - 1))
# # West
# # if (x - 1, y) in f:
# # ghosts.append((x - 1, y))
# # if (x - 2, y) in f:
# # ghosts.append((x - 2, y))
# # NorthWest
# # if (x - 1, y + 1) in f:
# # ghosts.append((x - 1, y - 1))
# # if (x - 1, y + 2) in f:
# # ghosts.append((x - 1, y + 2))
# # if (x - 2, y + 1) in f:
# # ghosts.append((x - 2, y + 1))
# # print "== Save Zone Built =="
# # print ghosts
# c = 0
# # Algorithm body
# # Iterating until the maximum difference between iteration < delta(threshold)
# print "\n== Ghost List in VI ==\n"
# print ghosts
# print "== Start Iterations ==\n"
# while True:
# # Iteration starts
# c += 1
# # print "Entered Iteration", c
# # Go through all states except foods, capsules & ghosts positions
# for s in f:
# if (s in foods) or (s in list(set(ghosts))) or (s in cap):
# continue
# x = s[0]
# y = s[1]
# # if (s in foods) or (s in cap):
# # if u[x][y] != -30:
# # continue
# u[x][y], p[s] = self.bellman(s)
# # Check if values converge. If so, determine the optimal policy
# # Maximum Expected Utility (MEU)
# # for x in range(self.row):
# # for y in range(self.column):
# # maxdiff = max(maxdiff, abs(u[x][y] - u_old[x][y]))
# # print "Maximum Difference: ", maxdiff
# # u_old = u # U_i+1 <- U_i
# print "=== Iteration ", c, " ==="
# self.updateUtilityTable(u) # U_i+1 <- U_i
# self.utility = u
# self.policy = p
# self.printutility()
# # 循环出口:只循环了一次??
# # Shouldn't have same utility at the end of algorithm
# # if maxdiff < self.delta:
# # if maxdiff <= self.delta:
# # break
# if c > 20:
# break
#
# pacman = api.whereAmI(state)
# Legal = self.availMoves[pacman]
# x = pacman[0]
# y = pacman[1]
# #
# # Maximum Expected Utility
# #
# radar = {}
# for direct in Legal:
# if direct == 'North':
# radar['North'] = []
# if (x, y + 1) in f:
# radar['North'].append(u[x][y + 1])
# if (x, y + 2) in f:
# radar['North'].append(u[x][y + 2])
# if (x, y + 3) in f:
# radar['North'].append(u[x][y + 3])
# # Corner Issue
# if len(radar['North']) <= 1:
# p = x
# q = y + 1
# safe = self.availMoves[(p, q)]
# for sf in safe:
# if sf == 'East':
# if (p + 1, q) in f:
# radar['North'].append(u[p + 1][q])
# if (p + 2, q) in f:
# radar['North'].append(u[p + 2][q])
# # if (p + 3, q) in f:
# # radar['North'].append(u[p + 3][q])
# if sf == 'West':
# if (p - 1, q) in f:
# radar['North'].append(u[p - 1][q])
# if (p - 2, q) in f:
# radar['North'].append(u[p - 2][q])
# # if (p - 3, q) in f:
# # radar['North'].append(u[p - 3][q])
# radar['North'] = float(sum(radar['North'])) / len(radar['North'])
# else:
# radar['North'] = float(sum(radar['North'])) / len(radar['North'])
# if direct == 'East':
# radar['East'] = []
# if (x + 1, y) in f:
# radar['East'].append(u[x + 1][y])
# if (x + 2, y) in f:
# radar['East'].append(u[x + 2][y])
# if (x + 3, y) in f:
# radar['East'].append(u[x + 3][y])
# # Corner Issue
# if len(radar['East']) <= 1:
# p = x + 1
# q = y