forked from mackorone/mms-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mouse.py
379 lines (310 loc) · 13.5 KB
/
Mouse.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
from Direction import Direction
from Maze import Maze, MazeCell
import API
class NotANeighborException(Exception):
"""
Exception raised when a cell is not a neighbor to another cell in the maze.
Attributes:
cell (MazeCell): The cell that is not a neighbor.
neighbor_cell (MazeCell): The cell that is expected to be a neighbor.
message (str): Additional message explaining the reason for the exception.
"""
def __init__(self, cell: MazeCell, neighbor_cell: MazeCell, message: str = ""):
API.setColor(*neighbor_cell.get_position(), "r")
super().__init__("\n{}\nis not a neighbor to\n{}\n\nReason: {}".format(
neighbor_cell, cell, message))
class PathBlockedException(Exception):
"""
Exception raised when a path is blocked by a neighboring cell.
Attributes:
cell (MazeCell): The current cell.
neighbor_cell (MazeCell): The neighboring cell that is blocking the path.
message (str): Additional message explaining the reason for the blockage.
"""
def __init__(self, cell: MazeCell, neighbor_cell: MazeCell, message: str = ""):
API.setColor(*neighbor_cell.get_position(), "r")
super().__init__("\n{}\nis blocked by\n{}\n\nReason: {}".format(
neighbor_cell, cell, message))
class Mouse():
"""
Represents a mouse in the micromouse simulator.
Attributes:
- position: The current position of the mouse in the maze.
- direction: The current direction the mouse is facing.
- maze: The maze object representing the maze the mouse is in.
- cell: The current cell the mouse is in.
- visited: A set of cells that the mouse has visited.
- updating_distances: A flag indicating whether the mouse is updating distances in the maze.
"""
def __init__(self, position: tuple[int, int], direction: Direction, maze: Maze):
self.position = position
self.direction = direction
self.maze = maze
self.cell = maze.get_cell(position)
self.sense_walls()
self.visited = set()
self.updating_distances = True
def get_position(self) -> tuple[int, int]:
"""
Returns the current position of the mouse.
Returns:
tuple: A tuple containing the x and y coordinates of the mouse's position.
"""
return self.position
def get_direction(self) -> Direction:
"""
Returns the current direction of the mouse.
Returns:
int: The current direction of the mouse.
"""
return self.direction
def turn_left(self) -> None:
"""
Turns the mouse to the left by 90 degrees.
"""
API.turnLeft()
self.direction = self.direction.minus_90()
def turn_right(self) -> None:
"""
Turns the mouse 90 degrees to the right.
"""
API.turnRight()
self.direction = self.direction.plus_90()
def turn_around(self, left: bool = True) -> None:
"""
Turns the mouse around by making it perform two consecutive turns in the specified direction.
Parameters:
- left (bool): If True, the mouse turns left twice. If False, the mouse turns right twice.
"""
if left:
self.turn_left()
self.turn_left()
else:
self.turn_right()
self.turn_right()
def move_forward(self, distance: int = 1) -> None:
"""
Move the mouse forward by the specified distance.
Args:
distance (int): The distance to move forward (default is 1).
Raises:
PathBlockedException: If there is a wall in the way.
"""
if API.wallFront():
raise PathBlockedException(self.cell, self.maze.get_cell(
self.get_front()), "Wall in the way")
API.moveForward(distance)
for _ in range(distance):
self.position = self.direction.add_to_position(self.position)
self.cell = self.maze.get_cell(self.position)
self.visited.add(self.cell)
if self.updating_distances:
self.cell.set_distance_is_confirmed(True)
def turn_towards_neighbor(self, neighbor: MazeCell) -> None:
"""
Turns the mouse towards the given neighbor cell.
Args:
neighbor (MazeCell): The neighbor cell to turn towards.
Raises:
NotANeighborException: If the given cell is not a valid neighbor.
Returns:
None
"""
if neighbor.get_position() == self.get_front():
pass
elif neighbor.get_position() == self.get_left():
self.turn_left()
elif neighbor.get_position() == self.get_right():
self.turn_right()
elif neighbor.get_position() == self.get_back():
self.turn_around()
else:
raise NotANeighborException(self.cell, neighbor, "Too far away")
def get_reachable_neighbors(self) -> list[MazeCell]:
"""
Returns a list of reachable neighboring cells.
This method checks the surrounding cells of the mouse's current position and returns a list of neighboring
cells that are reachable (i.e., not blocked by walls).
Returns:
list[MazeCell]: A list of reachable neighboring cells.
"""
neighbors = []
if not API.wallFront() and self.maze.contains(self.get_front()):
neighbors.append(self.maze.get_cell(self.get_front()))
if not API.wallLeft() and self.maze.contains(self.get_left()):
neighbors.append(self.maze.get_cell(self.get_left()))
if not API.wallRight() and self.maze.contains(self.get_right()):
neighbors.append(self.maze.get_cell(self.get_right()))
# if no neighbors are reachable, turn around and check again
if len(neighbors) == 0:
self.turn_around()
if not API.wallFront() and self.maze.contains(self.get_front()):
neighbors.append(self.maze.get_cell(
self.get_front()))
return neighbors
def get_front(self) -> tuple[int, int]:
"""
Returns the position of the cell in front of the mouse based on its current direction.
"""
return self.direction.add_to_position(self.position)
def get_left(self) -> tuple[int, int]:
"""
Returns the position of the cell to the left of the current position, based on the current direction.
"""
return self.direction.minus_90().add_to_position(self.position)
def get_right(self) -> tuple[int, int]:
"""
Returns the position of the cell to the right of the current cell,
based on the current direction and position of the mouse.
"""
return self.direction.plus_90().add_to_position(self.position)
def get_back(self) -> tuple[int, int]:
"""
Returns the position that is opposite to the current direction.
Returns:
Position: The position that is opposite to the current direction.
"""
return self.direction.minus_180().add_to_position(self.position)
def sense_walls(self) -> None:
"""
Sense the walls in the current direction and update the maze accordingly.
"""
direction_offset = self.direction.value - Direction.NORTH.value
walls = [False, False, False, False]
if API.wallFront():
walls[self.direction.value] = True
API.setWall(*self.position,
self.direction.get_API_representation())
self.maze.set_wall(self.cell, self.direction)
if API.wallLeft():
walls[(self.direction.minus_90().value + direction_offset) % 4] = True
API.setWall(*self.position,
self.direction.minus_90().get_API_representation())
self.maze.set_wall(self.cell, self.direction.minus_90())
if API.wallRight():
walls[(self.direction.plus_90().value + direction_offset) % 4] = True
API.setWall(*self.position,
self.direction.plus_90().get_API_representation())
self.maze.set_wall(self.cell, self.direction.plus_90())
def get_best_candidate(self, reachable_neighbors: list[MazeCell]) -> MazeCell:
"""
Choose the best candidate from a list of reachable neighbors based on the following criteria:
1. Smallest distance
2. If there is a tie in distance, choose the unvisited neighbor
3. If there is a tie in distance and visitation status,
choose the neighbor with the smallest amount of turns needed to reach it.
Args:
reachable_neighbors (list): A list of reachable neighbors.
Returns:
The best candidate neighbor based on the defined criteria.
"""
def heuristic(neighbor: MazeCell): return (
neighbor.get_distance(),
1 if neighbor in self.visited else 0,
self.get_turns(neighbor)
)
return min(reachable_neighbors,
key=heuristic)
def get_turns(self, neighbor: MazeCell) -> int:
"""
Calculates the number of turns required to turn towards a neighboring cell.
Args:
neighbor (MazeCell): The neighboring cell to calculate turns for.
Returns:
int: The number of turns required to turn towards the neighboring cell.
"""
desired_direction = None
if neighbor.get_position() == self.get_front():
desired_direction = self.direction
elif neighbor.get_position() == self.get_left():
desired_direction = self.direction.minus_90()
elif neighbor.get_position() == self.get_right():
desired_direction = self.direction.plus_90()
elif neighbor.get_position() == self.get_back():
desired_direction = self.direction.minus_180()
diff = (desired_direction.value - self.direction.value) % 4
return min(diff, 4 - diff)
def find_goal_explore(self, goal: tuple[int, int]) -> None:
"""
Finds the goal and explores the maze until the mouse reaches the goal.
Parameters:
- goal: The goal position in the maze.
Returns:
None
"""
while self.position != goal:
self.sense_walls()
self.maze.update_flood_fill_distances(goal)
self.cell.set_distance_is_confirmed(True)
reachable_neighbors = self.get_reachable_neighbors()
# select the neighbor with the lowest distance
min_neighbor = self.get_best_candidate(reachable_neighbors)
self.turn_towards_neighbor(min_neighbor)
self.move_forward(1)
def find_goal_fast(self, goal: tuple[int, int]) -> None:
"""
Finds the goal position by following the shortest path according to flood fill distances.
Args:
goal: The goal position to reach.
Returns:
None
"""
while self.position != goal:
reachable_neighbors = self.get_reachable_neighbors()
# remove unconfirmed neighbors
reachable_neighbors = list(
filter(lambda neighbor: neighbor.get_distance_is_confirmed(), reachable_neighbors))
# select the neighbor with the lowest distance
min_neighbor = self.get_best_candidate(reachable_neighbors)
self.turn_towards_neighbor(min_neighbor)
self.move_forward(1)
def follow_path(self, path: list[MazeCell]) -> None:
"""
Follows the given path of MazeCells.
Args:
path (list[MazeCell]): The path to follow.
Returns:
None
"""
self.updating_distances = False
times_forward = 0
cell = self.cell
self.turn_towards_neighbor(path[1])
forward_direction = self.direction
for i in range(len(path) - 1):
cell = path[i]
next_cell = path[i + 1] if i + 1 < len(path) else None
next_cell_is_infrontof_cell = next_cell.get_position(
) == Direction.get_position_from_direction(cell.get_position(), forward_direction)
if next_cell_is_infrontof_cell:
times_forward += 1
else:
if times_forward > 0:
self.move_forward(times_forward)
API.log("Moved forward {} times".format(times_forward))
times_forward = 0
self.turn_towards_neighbor(next_cell)
times_forward += 1
i -= 1
forward_direction = self.direction
self.updating_distances = True
def return_to_start(self, start: tuple[int, int], goal: tuple[int, int]) -> None:
"""
Moves the mouse back to the start position using flood fill algorithm.
Args:
start (tuple): The start position coordinates.
goal (tuple): The goal position coordinates.
Returns:
None
"""
while self.position != start:
self.sense_walls()
self.maze.update_flood_fill_distances(goal, start=False, draw=True)
self.maze.update_flood_fill_distances(
start, start=True, draw=False)
reachable_neighbors = self.get_reachable_neighbors()
# select the neighbor with the lowest distance
min_neighbor = min(reachable_neighbors,
key=lambda neighbor: neighbor.get_distance(start=True))
self.turn_towards_neighbor(min_neighbor)
self.move_forward(1)