-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.py
More file actions
executable file
·187 lines (162 loc) · 6.56 KB
/
Algorithm.py
File metadata and controls
executable file
·187 lines (162 loc) · 6.56 KB
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
import random
class Algorithm:
'''
Super class for algorithms
Just put the solve methods in for each, put an Algorithm object in Maze, have Maze.solve() call Algorithm.solve()
Then, Maze is an interface. Takes in > filename and algorithm object, outputs image that is saved.
'''
def __init__(self, img_load, MARK):
self.MARK = MARK
self.img_load = img_load
def Solve(self, G, v, MARK):
raise NotImplementedError #Only call on subclasses
def EnsureGraphLabelling(self, G, v):
'''
Resets all labels and weights of Nodes, resets root distance to 0
Input: A Grapg G, the root Node v
'''
for vert in G.vertices.values():
vert.label = "UNEXPLORED"
vert.weight = 2**63 + 1
for e in vert.edges:
e.label = "UNEXPLORED"
v.distance = 0
def MarkNodes(self, v, w, edge):
'''
Marks the image with nodes that were considered in Purple. Used by all algorithms
Input: a Node v, a Node w, the Edge between them edge
'''
v_x, v_y = map(int, v.value.split())
self.img_load[v_y, v_x] = (255, 0, 255)
w_x, w_y = map(int, w.value.split())
self.img_load[w_y, w_x] = (255, 0, 255)
for middleVertex in edge.middle:
m_x, m_y = map(int, middleVertex.value.split())
self.img_load[m_y, m_x] = (255, 0, 255)
class Dijkstra(Algorithm):
def __init__(self, img_load, mark):
Algorithm.__init__(self, img_load, mark)
def Solve(self, G, v):
'''
Dijkstra Shortest Path through the maze. Slower, but guarentees a shortest path
Input: a Graph G, start node v
Output: number of nodes the algorithm explored
'''
self.EnsureGraphLabelling(G, v)
count = 0
while(v != None and v != G.end):
count+=1
v.label = "VISITED"
for edge in v.edges:
w = edge.opposite(v)
if(w.label != "VISITED" and w.distance > v.distance + edge.weight):
w.distance = v.distance + edge.weight
G.Heap.changeLocator(w.distance, w.value)
#G.Heap.insert((w.distance, w.value))
w.prev = v
if self.MARK:
self.MarkNodes(v, w, edge)
v = G.smallestHeap()
return count
class BFS(Algorithm):
def __init__(self, img_load, mark):
Algorithm.__init__(self, img_load, mark)
def Solve(self, G, v):
'''
Breadth First Search through the maze. Finds shortest route, but slowly
Input: a Graph G, start node v
Output: number of nodes the algorithm explored
'''
self.EnsureGraphLabelling(G, v)
S = [v]
count = 0
distance = 0
while(len(S) > 0):
v = S.pop()
count+=1
#G.Heap.insert((count, v.value))
v.label = "VISITED"
distance = v.distance
for edge in v.edges:
w = edge.opposite(v)
if w.label == "UNEXPLORED":
distance_temp = distance + edge.weight
w.label = "CONSIDERING"
S.insert(0, w)
w.distance = distance_temp
w.prev = v
if self.MARK:
self.MarkNodes(v, w, edge)
if w == G.end:
w.prev = v
return count
#v = G.smallestHeap()
return count
class DFS(Algorithm):
def __init__(self, img_load, mark):
Algorithm.__init__(self, img_load, mark)
def Solve(self, G, v):
'''
Depth First Search through the maze. Fast algorithm, but not necessarily shortest path.
Input: a Graph G, start node v
Output: number of nodes the algorithm explored
'''
self.EnsureGraphLabelling(G, v)
S = [v]
count = 0
distance = 0
while(len(S) > 0):
v = S.pop()
count+=1
v.label = "VISITED"
random.shuffle(v.edges)
distance = v.distance
for edge in v.edges:
w = edge.opposite(v)
distance_temp = distance + edge.weight
if w.label == "UNEXPLORED":
w.label = "CONSIDERING"
S.append(w)
w.distance = distance_temp
w.prev = v
if self.MARK:
self.MarkNodes(v, w, edge)
if w == G.end:
w.prev = v
return count
return count
class AStar(Algorithm):
def __init__(self, img_load, mark):
Algorithm.__init__(self, img_load, mark)
def Solve(self, G, v):
'''
A* is essentially a Dijkstra algorithm, but will priritize downward movements instead of lateral ones
A* will give the same result as Dijkstra, but hopefully faster
Input: a Graph G, start node v
Output: G is labelled, number of nodes the solution explored
'''
HEURISTIC_MULT = 1 #Should give optimal path when 1
self.EnsureGraphLabelling(G, v)
v_i, v_j = map(int, v.value.split())
end_i, end_j = map(int, G.end.value.split())
v.heuristic = HEURISTIC_MULT * ((abs(v_i - end_i)) + (abs(v_j - end_j)))
count = 0
while(v != None and v != G.end):
count += 1
v.label = "VISITED"
for edge in v.edges:
w = edge.opposite(v)
node_i, node_j = map(int, w.value.split())
if w.heuristic == -1:
#distFromEnd = math.ceil(((node_i - end_i)**2 + (node_j - end_j)**2) ** 0.5)
distFromEnd = HEURISTIC_MULT * ((abs(node_i - end_i)) + (abs(node_j - end_j)))
w.heuristic = distFromEnd
if(w.label != "VISITED" and w.distance> v.distance + edge.weight ):
w.distance = v.distance + edge.weight
#G.Heap.insert((w.distance + w.heuristic, w.value))
G.Heap.changeLocator(w.distance + w.heuristic, w.value)
w.prev = v
if self.MARK:
self.MarkNodes(v, w, edge)
v = G.smallestHeap()
return count