MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / search

Method search

graphs/bidirectional_a_star.py:189–230  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

187 self.reached = False
188
189 def search(self) -> list[TPosition]:
190 while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes:
191 self.fwd_astar.open_nodes.sort()
192 self.bwd_astar.open_nodes.sort()
193 current_fwd_node = self.fwd_astar.open_nodes.pop(0)
194 current_bwd_node = self.bwd_astar.open_nodes.pop(0)
195
196 if current_bwd_node.pos == current_fwd_node.pos:
197 return self.retrace_bidirectional_path(
198 current_fwd_node, current_bwd_node
199 )
200
201 self.fwd_astar.closed_nodes.append(current_fwd_node)
202 self.bwd_astar.closed_nodes.append(current_bwd_node)
203
204 self.fwd_astar.target = current_bwd_node
205 self.bwd_astar.target = current_fwd_node
206
207 successors = {
208 self.fwd_astar: self.fwd_astar.get_successors(current_fwd_node),
209 self.bwd_astar: self.bwd_astar.get_successors(current_bwd_node),
210 }
211
212 for astar in [self.fwd_astar, self.bwd_astar]:
213 for child_node in successors[astar]:
214 if child_node in astar.closed_nodes:
215 continue
216
217 if child_node not in astar.open_nodes:
218 astar.open_nodes.append(child_node)
219 else:
220 # retrieve the best current path
221 better_node = astar.open_nodes.pop(
222 astar.open_nodes.index(child_node)
223 )
224
225 if child_node.g_cost < better_node.g_cost:
226 astar.open_nodes.append(child_node)
227 else:
228 astar.open_nodes.append(better_node)
229
230 return [self.fwd_astar.start.pos]
231
232 def retrace_bidirectional_path(
233 self, fwd_node: Node, bwd_node: Node

Callers

nothing calls this directly

Calls 5

sortMethod · 0.80
popMethod · 0.45
appendMethod · 0.45
get_successorsMethod · 0.45

Tested by

no test coverage detected