-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepeatedAlgo.py
executable file
·62 lines (48 loc) · 2.39 KB
/
RepeatedAlgo.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
from SolveMaze import SolveMaze
from Metrics import Metrics
from Maze import Maze
class RepeatedAlgo:
def __init__(self, size, actual_maze, start_node_actual, goal_node_actual, algo_type):
self.size = size
self.actual_maze = actual_maze
self.start_node_actual = start_node_actual
self.goal_node_actual = goal_node_actual
self.algo_type = algo_type
self.agent_maze = Maze().generate_blank_maze(self.size)
self.start_node = self.agent_maze[self.start_node_actual.x][self.start_node_actual.y]
self.goal_node = self.agent_maze[self.goal_node_actual.x][self.goal_node_actual.y]
self.solvedMaze = []
self.w = Metrics().initializeVisuals(self.start_node, self.goal_node, self.size, self.actual_maze, self.agent_maze)
def repeated_algorithm(self):
start_node = self.start_node
Metrics().blockage_status_of_children(start_node, self.start_node_actual, self.w)
self.goal_node.update_g(float("inf"))
lastClosedList = set()
while start_node is not self.goal_node:
self.w.showMaze(self.agent_maze)
passed = 0
if self.algo_type == 1:
passed = SolveMaze().forward_A_star(start_node, self.goal_node, self.w)
elif self.algo_type == 2:
passed = SolveMaze().adaptive_A_star(start_node, self.goal_node, lastClosedList, self.w)
if passed == 0:
print("I can't reach the target")
self.w.noPath()
break
path = Metrics().traverse_path(self.goal_node, start_node)
self.w.pathLine(path)
for i in path:
if i.cost == self.actual_maze[i.x][i.y].cost:
if i in self.solvedMaze:
del self.solvedMaze[self.solvedMaze.index(i) + 1: len(self.solvedMaze)]
continue
self.solvedMaze.append(i)
else:
start_node = self.solvedMaze.pop()
start_node_actual = self.actual_maze[start_node.x][start_node.y]
Metrics().blockage_status_of_children(start_node, start_node_actual, self.w)
break
if self.solvedMaze[-1] == self.goal_node:
print("I reached the goal")
self.w.finalPath(self.actual_maze, self.solvedMaze)
break