From a8f0b05e9df53fe83a72b6549b4d232d742f0636 Mon Sep 17 00:00:00 2001 From: chzhang Date: Mon, 2 Jan 2023 16:36:03 +0800 Subject: astar - graph search --- search/atar_demo.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 search/atar_demo.py (limited to 'search/atar_demo.py') diff --git a/search/atar_demo.py b/search/atar_demo.py new file mode 100644 index 0000000..a32af92 --- /dev/null +++ b/search/atar_demo.py @@ -0,0 +1,32 @@ + +import astar + + +class GraphAStar(astar.AStar): + def __init__(self, nodes): + self.nodes = nodes + + def neighbors(self, node): + for n, d in self.nodes[node]: + yield n + + # g function + def distance_between(self, n1, n2): + for n, d in self.nodes[n1]: + if n == n2: + return d + + # h function + def heuristic_cost_estimate(self, current, goal): + return 1 + + +if __name__ == '__main__': + nodes = {'A': [('B', 100), ('C', 20)], + 'C': [('D', 20)], + 'D': [('B', 20)]} + # A -> B + + graph_solver = GraphAStar(nodes) + print(list(graph_solver.astar('A', 'B'))) + print(list(graph_solver.astar('A', 'C'))) -- cgit v1.2.3