-
Notifications
You must be signed in to change notification settings - Fork 10
/
SearchAgent.py
263 lines (207 loc) · 7.57 KB
/
SearchAgent.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
from PriorityQueue import PriorityQueue
from Node import Node
class SearchAgent(object):
"""docstring for SearchAgent"""
def __init__(self, graph={}):
super(SearchAgent, self).__init__()
self.__agent_status = "idle"
self.graph = graph
################################################
######## Search Algorithms ########
################################################
def breadth_first_search(self):
source = self.source
if not self.reserve_agent():
return
self.reset_graph()
fringe = []
node = source
fringe.append(node)
while fringe:
node = fringe.pop(0)
if self.is_goal_state(node):
self.finished("success", node)
return
if self.node_state(node) != "visited":
self.set_node_state(node, "visited")
for n in self.expand(node):
if self.node_state(n) != "visited":
fringe.append(n)
yield
self.finished("failed", source)
def depth_first_search(self):
source = self.source
if not self.reserve_agent():
return
self.reset_graph()
fringe = []
node = source
fringe.append(node)
while fringe:
node = fringe.pop()
if self.is_goal_state(node):
self.finished("success", node)
return
if self.node_state(node) != "visited":
self.set_node_state(node, "visited")
for n in self.expand(node):
if self.node_state(n) != "visited":
fringe.append(n)
yield
self.finished("failed", source)
def depth_limit_search(self, limit):
source = self.source
if not self.reserve_agent():
return
self.reset_graph()
fringe = []
node = source
fringe.append(node)
while fringe:
node = fringe.pop()
if self.is_goal_state(node):
self.finished("success", node)
return
if self.node_state(node) != "visited":
self.set_node_state(node, "visited")
if len(node.path) < limit:
for n in self.expand(node):
if self.node_state(n) != "visited":
fringe.append(n)
yield
self.finished("failed", source)
def iterative_deepening_search(self, max_depth_limit):
for limit in range(1, max_depth_limit):
source = self.source
if not self.reserve_agent():
return
self.reset_graph()
fringe = []
node = source
fringe.append(node)
while fringe:
node = fringe.pop()
if self.is_goal_state(node):
self.finished("success", node)
return
if self.node_state(node) != "visited":
self.set_node_state(node, "visited")
if len(node.path) < limit:
for i in self.expand(node):
if self.node_state(i) != "visited":
fringe.append(i)
yield
self.finished("failed", source)
def uniform_cost_search(self):
source = self.source
if not self.reserve_agent():
return
self.reset_graph()
fringe = PriorityQueue()
node = source
fringe.add(node, node.cost)
while fringe.isNotEmpty():
node = fringe.pop()
if self.is_goal_state(node):
self.finished("success", node)
return
if self.node_state(node) != "visited":
self.set_node_state(node, "visited")
for n in self.expand(node):
if self.node_state(n) != "visited":
fringe.add(n, n.cost)
yield
self.finished("failed", source)
def greedy_search(self):
source = self.source
if not self.reserve_agent():
return
self.reset_graph()
fringe = PriorityQueue()
node = source
fringe.add(node, node.heuristic)
while fringe.isNotEmpty():
node = fringe.pop()
if self.is_goal_state(node):
self.finished("success", node)
return
if self.node_state(node) != "visited":
self.set_node_state(node, "visited")
for n in self.expand(node):
if self.node_state(n) != "visited":
fringe.add(n, n.heuristic)
yield
self.finished("failed", source)
def a_star_search(self):
source = self.source
if not self.reserve_agent():
return
self.reset_graph()
fringe = PriorityQueue()
node = source
fringe.add(node, node.cost + node.heuristic)
while fringe.isNotEmpty():
node = fringe.pop()
if self.is_goal_state(node):
self.finished("success", node)
return
if self.node_state(node) != "visited":
self.set_node_state(node, "visited")
for n in self.expand(node):
if self.node_state(n) != "visited":
fringe.add(n, n.cost + n.heuristic)
yield
self.finished("failed", source)
################################################
######## Utility Functions ########
################################################
@property
def dimensions(self):
return self.__dimensions
@property
def agent_status(self):
return self.__agent_status
@property
def is_agent_searching(self):
return self.__agent_status == "searching"
# Reserve the agent and prevent starting new alogorithms while searching
def reserve_agent(self):
if self.__agent_status == "searching":
return False
self.__agent_status = "searching"
return True
# To reset the grid to its initial state
def reset_graph(self):
for node_name, node in self.graph.items():
self.graph[node_name].state = self.graph[node_name].state if self.graph[node_name].state in [
"source", "goal"] else "empty"
# The state of a certain node
def node_state(self, node):
return self.graph[node.name].state
def set_node_state(self, node, state):
self.graph[node.name].state = state
# Checks whether the state is the goal state (goal)
def is_goal_state(self, node):
return self.node_state(node) == "goal"
# Expand a node to its valid new states
def expand(self, node):
return [Node.copy_from(self.graph[name], cost=node.cost + node.children[name], path=node.path + [node.name]) for name in node.children.keys()]
# Return actual cost
def cost(self, node):
return node.cost
# Retuen Heuristic
def heuristic(self, node):
return node.heuristic
# Get the source node (start state)
@property
def source(self):
return self.graph[0]
# Finished with "success" or "failed"
def finished(self, result, goal):
self.__agent_status = result
if result == "failed":
self.graph[goal.name].state = "source"
return
for node_name in goal.path[0:]:
self.graph[node_name].state = "path"
self.graph[goal.path[0]].state = "source"