|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
A* shortest path in Aleph-w (grid graph, heuristics, and comparison with Dijkstra). More...
#include <iostream>#include <iomanip>#include <vector>#include <cmath>#include <chrono>#include <limits>#include <tpl_graph.H>#include <AStar.H>#include <Dijkstra.H>Go to the source code of this file.
Classes | |
| struct | GridCell |
| Node info containing 2D coordinates. More... | |
| struct | GridDistance |
| Distance functor that reads arc weights. More... | |
| struct | EuclideanHeuristic |
| Euclidean distance heuristic. More... | |
| struct | ManhattanHeuristic |
| Manhattan distance heuristic. More... | |
Typedefs | |
| using | GridNode = Graph_Node< GridCell > |
| using | GridArc = Graph_Arc< double > |
| using | GridGraph = List_Graph< GridNode, GridArc > |
Functions | |
| GridGraph | create_grid_graph (int width, int height, bool diagonal, vector< GridGraph::Node * > &nodes) |
| Creates a 2D grid graph. | |
| void | print_grid_with_path (int width, int height, const vector< GridGraph::Node * > &nodes, GridGraph::Node *start, GridGraph::Node *end, const Path< GridGraph > &path) |
| Prints the grid with the path highlighted. | |
| template<typename Func > | |
| double | measure_time_ms (Func &&f) |
| int | main () |
A* shortest path in Aleph-w (grid graph, heuristics, and comparison with Dijkstra).
This example demonstrates Aleph-w's A* implementation (AStar.H) on a 2D grid. A* is a best-first shortest-path algorithm that uses a heuristic h(n) to guide the search, typically expanding far fewer nodes than Dijkstra when the heuristic is informative.
It also compares A* with Dijkstra on the same grid setup.
GridGraph = List_Graph<Graph_Node<GridCell>, Graph_Arc<double>>GridCell { x, y, blocked }double)The helper create_grid_graph(width, height, diagonal, nodes) builds a regular grid and optionally adds diagonal connections (8-connected vs 4-connected).
This example has no command-line options; the demo scenarios are hard-coded.
A* uses:
g(n): cost from start to nh(n): heuristic estimate from n to goalf(n) = g(n) + h(n) to prioritize expansionsTwo heuristics are shown:
sqrt(dx^2 + dy^2)): admissible when diagonal moves are allowed.|dx| + |dy|): admissible for 4-connected grids.A* is guaranteed to find an optimal path when h is admissible (never overestimates) and typically behaves best when it is also consistent.
Let V be the number of nodes and E the number of edges.
O((V + E) log V) (priority queue).O(V).In practice, a good heuristic can reduce the number of expanded nodes.
h overestimates, A* may return suboptimal paths.AStar.H (implementation)Dijkstra.H / dijkstra_example.cc (uninformed shortest paths)Definition in file astar_example.cc.
Definition at line 140 of file astar_example.cc.
| using GridGraph = List_Graph<GridNode, GridArc> |
Definition at line 141 of file astar_example.cc.
| using GridNode = Graph_Node<GridCell> |
Definition at line 139 of file astar_example.cc.
| GridGraph create_grid_graph | ( | int | width, |
| int | height, | ||
| bool | diagonal, | ||
| vector< GridGraph::Node * > & | nodes | ||
| ) |
Creates a 2D grid graph.
| width | Grid width (number of columns). |
| height | Grid height (number of rows). |
| diagonal | If true, add diagonal connections (8-connected). |
| nodes | Output vector of node pointers indexed by y*width+x. |
Definition at line 217 of file astar_example.cc.
References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::maps(), nodes, sqrt(), and y.
Referenced by main().
| int main | ( | ) |
Definition at line 347 of file astar_example.cc.
References create_grid_graph(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), HEIGHT, Aleph::maps(), measure_time_ms(), print_grid_with_path(), Aleph::Path< GT >::size(), sqrt(), and WIDTH.
| double measure_time_ms | ( | Func && | f | ) |
Definition at line 335 of file astar_example.cc.
Referenced by main().
| void print_grid_with_path | ( | int | width, |
| int | height, | ||
| const vector< GridGraph::Node * > & | nodes, | ||
| GridGraph::Node * | start, | ||
| GridGraph::Node * | end, | ||
| const Path< GridGraph > & | path | ||
| ) |
Prints the grid with the path highlighted.
Legend: S = Start E = End
Definition at line 292 of file astar_example.cc.
References GTNodeCommon< NodeInfo >::get_info(), Aleph::Dlink::Iterator::has_curr(), Aleph::maps(), nodes, and y.
Referenced by main().