-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent3.py
255 lines (194 loc) · 8.44 KB
/
Agent3.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
from Agent2 import Agent2
from MazeGeneration import MazeGeneration
import random
from collections import deque
from UtilityFunctions import Utility
from collections import defaultdict
from copy import deepcopy
from heapq import heapify, heappop
import math
class Agent3:
# Given the number of times a cell is visited, this function returns the penalty
# that we need to add for the step. This function initially grows slowly until
# the value reaches around 50. After which, this function grows
# exponentially and penalty dominates the success rate and distance
# in making the decision thereby breaking the wiggle.
# Since wiggling is nice until 30-35 moves, this heuristic was chosen.
def getPenalty(self, x):
return x**2 / 5000
# This is an iterative version of Agent 3. We moved from the recursive version
# because our recursive version was throwing a maximum recursion depth.
def agent3Iterative(
self, currRow, currCol, grid, path, ghostMap, visited=defaultdict(int)
):
while True:
visited[(currRow, currCol)] += 1
# print(currRow, currCol)
# If there is a ghost in the current cell we are in, then
# we return False indicating the agents death
if (currRow, currCol) in ghostMap:
print("1")
return False, grid, (currRow, currCol), ghostMap
# If the above condition fails and we reach the destination,
# we return True
if currRow == len(grid) - 1 and currCol == len(grid[0]) - 1:
return True, grid, (currRow, currCol), ghostMap
rows = [0, 0, -1, 1]
cols = [-1, 1, 0, 0]
# This is our heap datastructure in which we store the tuples.
# The first value in the tuple contains our heuristic value
# and the second value contains the step that we are taking.
d = []
# Utility.printMaze(grid)
# Traversing the four directions
for i in range(4):
newRow = currRow + rows[i]
newCol = currCol + cols[i]
# If we have reached the destination, we return True
if newRow == len(grid) - 1 and newCol == len(grid[0]) - 1:
return True, grid, (newRow, newCol), ghostMap
# If the new cell is valid and the ghost doesnt
# exist in the new cell, then we consider the cell
if (
0 <= newRow < len(grid)
and 0 <= newCol < len(grid[0])
and grid[newRow][newCol] % 2 == 0
and (newRow, newCol) not in ghostMap
):
# We run agent 2 from the new cell for 10 iterations and
# calculate the success rate.
successRate = 0
for j in range(10):
a2 = Agent2()
successRate += a2.agent2(
newRow, newCol, deepcopy(grid), path, deepcopy(ghostMap)
)[0]
# Since we get the success rate, we can determine the
# failure rate.
failureRate = 10 - successRate
penality = self.getPenalty(visited[(newRow, newCol)])
distance = path[newRow][newCol][2]
# If the penality doesnt overdominate our heuristic,
# we append this into the heap as one of the potential choices.
if penality < 100:
# We append the Failure Rate + penality + distance as our decision heuristic
# and the new coordinates.
# Our aim is to minimize this value
d.append(
(
failureRate + penality + distance,
(newRow, newCol),
)
)
# print(d)
# Utility.printMaze(grid)
# We heapify the heap and if there are no elements in the heap,
# then we choose the shortest path thereby making some progress.
heapify(d)
if len(d) > 0:
direction = heappop(d)
newAgentPosition = direction[1]
else:
newAgentPosition = (
path[currRow][currCol][0],
path[currRow][currCol][1],
)
# We iterate through every ghost present in the ghostMap
# and move the ghost by one step.
newGhostMap = defaultdict(int)
for key, value in ghostMap.items():
g = value
while g > 0:
row = key[0]
col = key[1]
newPosition = Utility.moveGhost(row, col, grid)
# If we move ghost into the cell containing the agent,
# then we return false indicating the agents death
if newAgentPosition == newPosition:
print("2")
return False, grid, newPosition, ghostMap
newGhostMap[newPosition] += 1
g -= 1
ghostMap = deepcopy(newGhostMap)
# Utility.printMaze(grid)
currRow = newAgentPosition[0]
currCol = newAgentPosition[1]
print(currRow, currCol)
def agent3Recursive(self, currRow, currCol, grid, path, ghostMap, visited):
visited[(currRow, currCol)] += 1
print(currRow, currCol)
if (currRow, currCol) in ghostMap:
print("1")
return False, grid, (currRow, currCol), ghostMap
if currRow == len(grid) - 1 and currCol == len(grid[0]) - 1:
return True, grid, (currRow, currCol), ghostMap
rows = [0, 0, -1, 1]
cols = [-1, 1, 0, 0]
d = [
(-self.getPenalty(visited[(currRow, currCol)]), (currRow, currCol)),
]
for i in range(4):
newRow = currRow + rows[i]
newCol = currCol + cols[i]
if newRow == len(grid) - 1 and newCol == len(grid[0]) - 1:
return True, grid, (newRow, newCol), ghostMap
if (
0 <= newRow < len(grid)
and 0 <= newCol < len(grid[0])
and grid[newRow][newCol] == 0
):
successRate = 0
for j in range(10):
successRate += self.agent2.agent2(
newRow, newCol, grid, path, ghostMap
)[0]
d.append(
(
-(successRate - self.getPenalty(visited[(newRow, newCol)])),
(newRow, newCol),
)
)
# print(d)
heapify(d)
# print(d, currRow, currCol)
direction = heappop(d)
newAgentPosition = direction[1]
self.newGhostMap = defaultdict(int)
for key, value in ghostMap.items():
g = value
while g > 0:
row = key[0]
col = key[1]
newPosition = Utility.moveGhost(row, col, grid)
if newAgentPosition == newPosition:
print("2")
return False, grid, newPosition, ghostMap
self.newGhostMap[newPosition] += 1
g -= 1
ghostMap = deepcopy(self.newGhostMap)
return self.agent3(
newAgentPosition[0], newAgentPosition[1], grid, path, ghostMap, visited
)
def findPath(self, gridSize, numberOfGhosts):
self.mg = MazeGeneration()
self.agent2 = Agent2()
ghostMap = defaultdict(int)
grid, path = self.mg.generateMaze(gridSize)
Utility.spawnGhosts(grid, numberOfGhosts, ghostMap)
# Utility.printMaze(grid)
visited = defaultdict(int)
(
result,
finalGrid,
finalAgentPosition,
finalGhostPosition,
) = self.agent3Iterative(0, 0, grid, path, ghostMap, visited)
print(result)
# print("___________________________-")
# Utility.printMaze(finalGrid)
# print(finalAgentPosition)
# print(finalGhostPosition)
return result
if __name__ == "__main__":
agent3 = Agent3()
agent3.findPath(51, 50)