This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Maze.py
276 lines (209 loc) · 8.68 KB
/
Maze.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
from Coord import *
from Wriggler import *
from State import *
from Action import *
import static
import util
import math
from pqdict import PQDict
# hackish way to include libs locally
import os, sys, inspect
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "treelib")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
# treelib modified for project
from treelib import Node, Tree
class Maze:
def __init__(self, puzzleFile):
self.wrigglers = [] # Sorted list by ID
self.parsePuzzleFile(puzzleFile)
self.tree = Tree()
def parsePuzzleFile(self, filename):
f = open(filename, 'r')
columns, rows, numWrigglers = f.readline().split()
static.columns = int(columns)
static.rows = int(rows)
static.board = [['-1' for y in range(static.rows)] for x in range(static.columns)]
for y in range(static.rows):
line = f.readline().split()
for x in range(len(line)):
static.board[x][y] = line[x]
assert len(static.board) == static.columns
assert len(static.board[0]) == static.rows
static.goal = Coord(static.columns - 1, static.rows - 1)
for _ in range(int(numWrigglers)):
self.wrigglers.append(self.makeWriggler())
self.wrigglers.sort()
print("Empty board")
util.printBoard()
def clearMaze(self):
for y in range(static.rows):
for x in range(static.columns):
if(static.board[x][y] != static.WALL):
static.board[x][y] = static.EMPTY
def makeWriggler(self):
wriggler = Wriggler()
bodyCoords = []
tailCoord = None
breakOut = False
# Find head
for y in range(static.rows):
for x in range(static.columns):
if(wriggler.isHead(x, y)):
headCoord = Coord(x, y)
breakOut = True
break
if(breakOut):
break
nextCoord = Coord(newCoord=headCoord)
bFirst = True
while(True):
oldCoord = Coord(newCoord=nextCoord)
if(bFirst):
nextCoord, _ = wriggler.followBody(nextCoord, True)
static.board[oldCoord.x][oldCoord.y] = static.EMPTY
bFirst = False
else:
nextCoord, isTail = wriggler.followBody(nextCoord)
if(isTail):
tailCoord = Coord(newCoord=nextCoord)
wrigglerID = int(static.board[tailCoord.x][tailCoord.y])
static.board[tailCoord.x][tailCoord.y] = static.EMPTY
break
static.board[oldCoord.x][oldCoord.y] = static.EMPTY
bodyCoords.append(Coord(newCoord=oldCoord))
wrigglerCoords = [headCoord, bodyCoords, tailCoord]
wriggler.location = wrigglerCoords
wriggler.id = wrigglerID
return wriggler
def testGoal(self, state):
inGoal = False
wriggler = state.wrigglers[0] # assuming sorted
inGoal = self.checkGoal(wriggler.location[0]) or self.checkGoal(wriggler.location[2])
if(inGoal):
return True
return inGoal
# Returns true if coords are goal coords
def checkGoal(self, coord):
return coord.x == static.goal.x and coord.y == static.goal.y
def getWrigglerLocations(self):
wrigglerLocs = []
for wriggler in self.wrigglers:
wrigglerLocs.append(wriggler.location)
return wrigglerLocs
# Run normal heuristic
def a_star_normal(self):
return self.a_star(self.heuristic)
def a_star(self, heuristic):
node = self.tree.create_node(state=State(self.wrigglers), pathCost=0)
node.heuristic = heuristic(node)
frontier = PQDict()
stateFrontier = {}
explored = {}
# Sacrifice memory to have a huge speed up being able to instantly check for state in frontier
stateFrontier[str(node.state)] = node.heuristic
frontier.additem(node._identifier, node.heuristic)
while(True):
if(len(frontier) == 0):
return None
nodeID = frontier.popitem()[0]
node = self.tree.get_node(nodeID)
nodeStateStr = str(node.state)
del stateFrontier[nodeStateStr]
if self.testGoal(node.state):
return node
explored[nodeStateStr] = -1 # we don't care what the hash matches
actions = self.getActions(node.state)
for action in actions:
child = self.childNode(node, action)
child.heuristic = heuristic(child)
childStr = str(child.state)
inExplored = False
inFrontier = False
if childStr in explored:
inExplored = True
bGreater = False
if childStr in stateFrontier:
if(stateFrontier[childStr] < child.heuristic + child.pathCost):
bGreater = True
inFrontier = True
if(not inExplored and not inFrontier):
stateFrontier[childStr] = child.heuristic
frontier.additem(child._identifier, child.heuristic + child.pathCost)
elif(bGreater):
bHappened = False
for key in frontier:
if(str(self.tree.get_node(key).state) == childStr):
bHappened = True
frontier.pop(key)
frontier.additem(child._identifier, child.heuristic + child.pathCost)
break
assert bHappened
# Optimistic manhatten distance, but still terrible
def heuristic(self, node):
wriggler = node.state.wrigglers[0]
headDiffX = static.goal.x - wriggler.location[0].x
headDiffY = static.goal.y - wriggler.location[0].y
headDiff = headDiffX + headDiffY
tailDiffX = static.goal.x - wriggler.location[2].x
tailDiffY = static.goal.y - wriggler.location[2].y
tailDiff = tailDiffX + tailDiffY
return min(headDiff, tailDiff)
def euclidean_distance(self, coord0, coord1):
return math.sqrt((coord0.x - coord1.x) ** 2 + (coord0.y - coord1.y) ** 2)
def generateSolution(self, goalNode):
parentID = goalNode._bpointer
actions = []
text = ''
actions.append(goalNode.action)
while(parentID is not None):
parentNode = self.tree.get_node(parentID)
actions.append(parentNode.action)
parentID = parentNode._bpointer
actLength = 0
for action in reversed(actions):
if(action is not None):
if(action.headMoved):
headVal = '0'
else:
headVal = '1'
actLength += 1
text += str(action.wrigglerID) + " " + headVal + " " + str(action.movedCoord.x) + " " + str(action.movedCoord.y) + "\n"
return text, actLength
def getActions(self, state):
actions = []
wrigglers = state.wrigglers
self.clearMaze()
self.updateAllMaze(state.wrigglers)
for i in range(len(wrigglers)):
moves = wrigglers[i].getPossibleMoves()
for move in moves:
actions.append(Action(wrigglerID=i, movedCoord=move[0], headMoved=move[1]))
return actions
def updateAllMaze(self, wrigglers, bNot0=False):
if(bNot0):
for i in range(1, len(wrigglers)):
wrigglers[i].updateMaze()
else:
for wriggler in wrigglers:
wriggler.updateMaze()
def childNode(self, parent, action):
newState = State(parent.state.wrigglers, True)
wriggler = newState.wrigglers[action.wrigglerID]
if(action.headMoved):
symbol = wriggler.getSymbol(wriggler.location[0], action.movedCoord)
else:
symbol = wriggler.getSymbol(wriggler.location[2], action.movedCoord)
symbol = wriggler.convertToHeadSymbol(symbol)
wriggler.move(symbol, action.headMoved)
return self.tree.create_node(pathCost=parent.pathCost + 1, parent=parent._identifier, state=newState, action=action)
def getDepth(self, node):
parentID = node._bpointer
depth = 0
if(parentID is None):
return depth
while(parentID is not None):
depth += 1
parentNode = self.tree.get_node(parentID)
parentID = parentNode._bpointer
return depth