-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_algorithms.py
More file actions
217 lines (143 loc) · 5.82 KB
/
search_algorithms.py
File metadata and controls
217 lines (143 loc) · 5.82 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
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
from node import *
from problem import *
import heapq
import matplotlib.pyplot as plt
class PriorityQueue:
def __init__(self, items = (), priority_function = (lambda x: x)):
self.priority_function = priority_function
self.pqueue = []
for item in items:
self.add(item)
def add(self, item):
pair = (self.priority_function(item), item)
heapq.heappush(self.pqueue, pair)
def pop(self):
return heapq.heappop(self.pqueue)[1]
def __len__(self):
return len(self. pqueue)
def expand(problem, node):
s = node.state
actions_node = problem.actions(s)
for action in actions_node:
s1 = problem.result(s, action)
cost = node.path_cost + problem.action_cost(s, action, s1)
yield Node(state=s1, parent_node=node, action_from_parent=action, path_cost=cost)
def get_path_actions(node):
actions = []
if node is None or node.parent_node is None:
return actions
while node.parent_node is not None:
actions.append(node.action_from_parent)
node = node.parent_node
actions.reverse()
return actions
def get_path_states(node):
states = []
if node is None:
return states
while node is not None:
states.append(node.state)
node = node.parent_node
states.reverse()
return states
def best_first_search(problem, f):
node = Node(problem.initial_state)
frontier = PriorityQueue([node], f)
reached = {problem.initial_state: node}
while frontier.__len__() > 0:
node = frontier.pop()
if problem.is_goal(node.state):
return node
for child in expand(problem, node):
s = child.state
if (s not in reached) or (child.path_cost < reached[s].path_cost):
reached[s] = child
frontier.add(child)
return None
def best_first_search_treelike(problem, f):
node = Node(problem.initial_state)
frontier = PriorityQueue([node], f)
while frontier.__len__() > 0:
node = frontier.pop()
if problem.is_goal(node.state):
return node
for child in expand(problem, node):
frontier.add(child)
return None
def breadth_first_search(problem, treelike = False):
if treelike:
return best_first_search_treelike(problem, lambda node: node.depth)
else:
return best_first_search(problem, lambda node: node.depth)
def depth_first_search(problem, treelike = False):
if treelike:
return best_first_search_treelike(problem, lambda node: -node.depth)
else:
return best_first_search(problem, lambda node: -node.depth)
def uniform_cost_search(problem, treelike = False):
if treelike:
return best_first_search_treelike(problem, lambda node: node.path_cost)
else:
return best_first_search(problem, lambda node: node.path_cost)
def greedy_search(problem, h, treelike = False):
if treelike:
return best_first_search_treelike(problem, lambda node: problem.h(node))
else:
return best_first_search(problem, lambda node: problem.h(node))
def astar_search(problem, h, treelike = False):
if treelike:
return best_first_search_treelike(problem, lambda node: (node.path_cost + problem.h(node)))
else:
return best_first_search(problem, lambda node: (node.path_cost + problem.h(node)))
#visualize route problem
def visualize_route_problem_solution(problem, goal_node, file_name):
keys = list(problem.map_coords.keys())
points = list(problem.map_coords.values())
i = 0
keys_len = len(keys)
while i < keys_len:
if keys[i] == problem.initial_state:
start = i
del keys[i]
keys_len -= 1
if keys[i] == problem.goal_state:
ends = i
del keys[i]
keys_len -= 1
i += 1
plt.scatter(points[start][0], points[start][1], c = 'red', marker = 's')
del points[start]
plt.scatter(points[ends][0], points[ends][1], c = 'green', marker = 's')
del points[ends]
for i in range(0, len(keys)):
plt.scatter(points[i][0], points[i][1], c = 'blue', marker = 's')
keys = list(problem.map_coords.keys())
points = list(problem.map_coords.values())
for i in range(0, len(keys)):
actions = problem.actions(keys[i])
for j in range(0, len(actions)):
for z in range(0, len(keys)):
if actions[j] == keys[z]:
plt.arrow(points[i][0], points[i][1], (points[z][0] - points[i][0]), (points[z][1] - points[i][1]), color = 'black')
paths = get_path_states(goal_node)
for i in range(len(paths)):
for j in range(0, len(keys)):
if paths[i] == keys[j]:
paths[i] = points[j]
for i in range(0, len(paths) - 1):
plt.arrow(paths[i][0], paths[i][1], (paths[i+1][0] - paths[i][0]), (paths[i+1][1] - paths[i][1]), color = 'magenta')
plt.savefig(file_name, format = 'png')
plt.close()
def visualize_grid_problem_solution(problem, goal_node, file_name):
plt.scatter(problem.initial_state[0][0], problem.initial_state[0][1], c = 'green', marker = 's')
for i in range(len(problem.wall_coords)):
plt.scatter(problem.wall_coords[i][0], problem.wall_coords[i][1], c = 'black', marker = 's')
for i in range(len(problem.food_coords)):
plt.scatter(problem.food_coords[i][0], problem.food_coords[i][1], c = 'red', marker = 'o')
states = get_path_states(goal_node)
for i in range(len(states)):
states[i] = states[i][0]
for i in range(0,len(states)-1):
plt.arrow(states[i][0], states[i][1], (states[i+1][0] - states[i][0] ) , (states[i+1][1] - states[i][1]), color ='magenta')
plt.savefig(file_name, format = 'png')
plt.close()