-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
195 lines (175 loc) · 6.21 KB
/
search.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
"""
Generic search algorithms implemented in python.
"""
from data_structures import Stack, Queue, PriorityQueue
class Node(object):
"""
Record of state during search.
"""
def __init__(self, state, parent_node, cost=0.0, heuristic=0.0):
"""
Node object for keeping track of state during a generic search.
Parameters
----------
state : Generic
Current state of search. For example, during maze-solving search,
the state would be the current location in the maze.
parent_node : Node
Previous node from which current Node is derived.
Can be None.
cost : float
Evaluated result of cost function (optional)
heuristic : float
Evaluated heuristic (optional)
"""
self.state = state
self.parent = parent_node
self.cost = cost
self.heuristic = heuristic
def __lt__(self, other):
return (self.cost + self.heuristic) < (other.cost + other.heuristic)
def dfs(initial, goal_test, successors):
"""
Depth-first search.
Parameters
----------
initial : Generic
Starting point of the search.
goal_test : Callable
Callable returing boolean value indicating search success.
successors : Callable
Callable returning list of next possible locations in search space.
Returns
-------
found : Generic
Node corresponding to successful goal_test.
Returns None if search fails.
"""
# References to candidate and previously-explored nodes in search space
frontier = Stack()
explored = Stack()
# Initialize candidate search locations with initial condition
frontier.push(Node(initial, None))
# Continue search as long as their are candidates in the search space
while not frontier.empty:
current_node = frontier.pop()
current_state = current_node.state
# If current node meets goal, then search completes successfully
if goal_test(current_state):
return current_node
# Populate next step in search
for child in successors(current_state):
# Skip previously-explored states
if child in explored:
continue
explored.push(child)
frontier.push(Node(child, current_node))
# Search terminates without finding goal
return None
def a_star(initial, goal_test, successors, cost, heuristic):
"""
A* search using priority queue.
Parameters
----------
initial : Generic
Starting point of the search.
goal_test : Callable
Callable returing boolean value indicating search success.
successors : Callable
Callable returning list of next possible locations in search space.
cost : Callable
Cost function returning the cost of a proposed move between nodes in
search space.
heuristic : Callable
Heuristic to evaluate proposed nodes
Returns
-------
found : Generic
Node corresponding to successful goal_test.
Returns None if search fails.
"""
# Initialize frontier
frontier = PriorityQueue()
frontier.push(Node(initial, None, cost=0.0,
heuristic=heuristic(initial)))
# Structure for holding explored nodes (with their costs)
explored = {initial : 0.0}
# Continue search as long as their are candidates in the search space
while not frontier.empty:
current_node = frontier.pop()
current_state = current_node.state
# If current node meets goal, then search completes successfully
if goal_test(current_state):
return current_node
# Populate next step in search
for child in successors(current_state):
new_cost = cost(current_node)
if child not in explored or explored[child] > new_cost:
explored[child] = new_cost
frontier.push(Node(child,
parent_node=current_node,
cost=new_cost,
heuristic=heuristic(child)))
# Search terminates without finding goal
return None
def bfs(initial, goal_test, successors):
"""
Breadth-first search.
Parameters
----------
initial : Generic
Starting point of the search.
goal_test : Callable
Callable returing boolean value indicating search success.
successors : Callable
Callable returning list of next possible locations in search space.
Returns
-------
found : Generic
Node corresponding to successful goal_test.
Returns None if search fails.
"""
# References to candidate and previously-explored nodes in search space
frontier = Queue()
explored = Stack()
# Initialize candidate search locations with initial condition
frontier.push(Node(initial, None))
# Continue search as long as their are candidates in the search space
while not frontier.empty:
current_node = frontier.pop()
current_state = current_node.state
# If current node meets goal, then search completes successfully
if goal_test(current_state):
return current_node
# Populate next step in search
for child in successors(current_state):
# Skip previously-explored states
if child in explored:
continue
explored.push(child)
frontier.push(Node(child, current_node))
# Search terminates without finding goal
return None
def node_to_path(goal_node):
"""
Back-track through nodes to determine path through search space for a
successful search using the node.parent.
Parameters
----------
goal_node : Node
Node returned by successful search
Returns
-------
path : List
List of nodes comprising the successful search path, from start to end
"""
# Initialize path with end state
path = [goal_node.state]
# Work backwards through nodes
node = goal_node
while node.parent is not None:
node = node.parent
path.append(node.state)
# Flip order of path so that it is from start->end
path.reverse()
return path