summaryrefslogtreecommitdiff
path: root/search/atar_demo.py
diff options
context:
space:
mode:
authorchzhang <zch921005@126.com>2023-01-02 16:36:03 +0800
committerchzhang <zch921005@126.com>2023-01-02 16:36:03 +0800
commita8f0b05e9df53fe83a72b6549b4d232d742f0636 (patch)
treece0bd7e10c1b192c3116b354e4f1cfd0fa0beb95 /search/atar_demo.py
parentb7bee14413762c694d01a6b1a3980e4f40e69fe1 (diff)
astar - graph search
Diffstat (limited to 'search/atar_demo.py')
-rw-r--r--search/atar_demo.py32
1 files changed, 32 insertions, 0 deletions
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')))