77template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
168template <AlephGraph
GT,
173 template <
class,
class,
class>
class HeapT =
ArcHeap>
194#define ANassert(p) (static_cast<Node_Info*>(NODE_COOKIE(p)))
195#define ATREENODE(p) (static_cast<Tree_Node_Info*>(NODE_COOKIE(p))->tree_node)
196#define AACC(p) (ANassert(p)->dist)
197#define AHEAPNODE(p) (ANassert(p)->heap_node)
198#define APARENT(p) (ANassert(p)->ret_node)
199#define AAassert(p) (static_cast<Arc_Info*>(ARC_COOKIE(p)))
200#define AARC_DIST(p) (this->distance(p))
201#define ATREEARC(p) (static_cast<Tree_Arc_Info*>(ARC_COOKIE(p))->tree_arc)
202#define APOT(p) (AAassert(p)->pot)
226 if (h_estimate > actual_cost)
228 std::cerr <<
"WARNING: Inadmissible heuristic detected!\n";
229 std::cerr <<
" Heuristic estimate h(" << node <<
") = " << h_estimate <<
"\n";
230 std::cerr <<
" Actual remaining cost = " << actual_cost <<
"\n";
231 std::cerr <<
" Heuristic overestimates by " << (h_estimate - actual_cost) <<
"\n";
232 std::cerr <<
" A* may return suboptimal paths.\n";
245 Heuristic __heuristic = Heuristic(),
289 this->
template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
291 this->
template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
310 for (Itor<GT, SA> it(start,
sa); it.has_curr(); it.next_ne())
312 auto arc = it.get_current_arc_ne();
313 auto tgt = it.get_tgt_node();
319 heap.put_arc(arc, tgt);
322 typename GT::Node* result =
nullptr;
324 while (not
heap.is_empty())
326 auto garc =
heap.get_min_arc();
340 std::swap(gsrc, gtgt);
348 auto tarc = tree.
insert_arc(tsrc, ttgt, garc->get_info());
359 const auto& g_cost =
AACC(gtgt);
361 for (Itor<GT, SA> it(gtgt,
sa); it.has_curr(); it.next_ne())
363 auto arc = it.get_current_arc_ne();
367 auto tgt = it.get_tgt_node();
376 heap.put_arc(arc, tgt);
406 this->
template init<Initialize_Node, Initialize_Arc>(g, start);
407 auto guard = this->
template uninit_on_exit<Destroy_Node, Destroy_Arc>();
421 for (Itor<GT, SA> it(start,
sa); it.has_curr(); it.next_ne())
423 auto arc = it.get_current_arc_ne();
424 auto tgt = it.get_tgt_node();
430 heap.put_arc(arc, tgt);
435 while (not
heap.is_empty())
437 auto garc =
heap.get_min_arc();
463 const auto& g_cost =
AACC(tgt);
465 for (Itor<GT, SA> it(tgt,
sa); it.has_curr(); it.next_ne())
467 auto arc = it.get_current_arc_ne();
471 auto t = it.get_tgt_node();
480 heap.put_arc(arc, t);
514 return std::numeric_limits<typename Distance::Distance_Type>::max();
561 this->
template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
563 this->
template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
572 for (Itor<GT, SA> it(start,
sa); it.has_curr(); it.next_ne())
574 auto arc = it.get_current_arc_ne();
576 heap.put_arc(arc, it.get_tgt_node());
583 auto garc =
heap.get_min_arc();
597 std::swap(gsrc, gtgt);
605 auto tarc = tree.
insert_arc(tsrc, ttgt, garc->get_info());
609 const auto& acc =
AACC(gtgt);
611 for (Itor<GT, SA> it(gtgt,
sa); it.has_curr(); it.next_ne())
613 auto arc = it.get_current_arc_ne();
617 auto tgt = it.get_tgt_node();
622 heap.put_arc(arc, tgt);
652 this->
template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
654 this->
template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
673 for (Itor<GT, SA> it(start,
sa); it.has_curr(); it.next_ne())
675 auto arc = it.get_current_arc_ne();
677 heap.put_arc(arc, it.get_tgt_node());
684 auto garc =
heap.get_min_arc();
698 std::swap(gsrc, gtgt);
712 const auto& acc =
AACC(gtgt);
714 for (Itor<GT, SA> it(gtgt,
sa); it.has_curr(); it.next_ne())
716 auto arc = it.get_current_arc_ne();
720 auto tgt = it.get_tgt_node();
725 heap.put_arc(arc, tgt);
752 this->
template init<Initialize_Node, Initialize_Arc>(g, start);
753 auto guard = this->
template uninit_on_exit<Destroy_Node, Destroy_Arc>();
764 bool ret_val =
false;
769 for (Itor<GT, SA> it(start,
sa); it.has_curr(); it.next_ne())
771 auto arc = it.get_current_arc_ne();
773 heap.put_arc(arc, it.get_tgt_node());
779 while (tn < n
and not
heap.is_empty())
781 auto garc =
heap.get_min_arc();
809 const auto& acc =
AACC(tgt);
811 for (Itor<GT, SA> it(tgt,
sa); it.has_curr(); it.next_ne())
813 auto a = it.get_current_arc_ne();
817 auto t = it.get_tgt_node();
849 this->
template init<Initialize_Node, Initialize_Arc>(g, start);
850 auto guard = this->
template uninit_on_exit<Destroy_Node, Destroy_Arc>();
855 for (Itor<GT, SA> it(start,
sa); it.has_curr(); it.next_ne())
857 auto arc = it.get_current_arc_ne();
859 heap.put_arc(arc, it.get_tgt_node());
865 while (tn < n
and not
heap.is_empty())
867 auto garc =
heap.get_min_arc();
889 const auto& acc =
AACC(tgt);
891 for (Itor<GT, SA> it(tgt,
sa); it.has_curr(); it.next_ne())
893 auto a = it.get_current_arc_ne();
897 auto t = it.get_tgt_node();
933 return std::numeric_limits<typename Distance::Distance_Type>::max();
990template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
1000 auto dy = f.y - t.y;
1001 return static_cast<Distance_Type>(std::sqrt(dx * dx + dy * dy));
1015template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
1024 return static_cast<Distance_Type>(std::abs(f.x - t.x) + std::abs(f.y - t.y));
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
Standard functor implementations and comparison objects.
Arc heap for graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
A* algorithm for finding the shortest path between two nodes.
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes shortest path by painting the graph (without heuristic).
GT::Node * compute_min_paths_tree(const GT &g, typename GT::Node *start, GT &tree)
Computes the spanning tree of ALL shortest paths from the start node.
GT::Node * compute_path(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Alias for compute_partial_path (backward compatibility).
void check_heuristic_admissibility(typename GT::Node *node, typename GT::Node *goal, const typename Distance::Distance_Type &actual_cost)
void compute_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Computes the partial spanning tree from start to end (without a heuristic).
AStar_Min_Path(Distance dist=Distance(), Heuristic __heuristic=Heuristic(), SA __sa=SA())
Constructor.
Distance::Distance_Type operator()(const GT &g, typename GT::Node *s, typename GT::Node *e, Path< GT > &path)
Finds shortest path using A* heuristic.
GT::Node * compute_partial_path(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Computes the shortest path from start to end using A*.
bool paint_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end)
Paints on graph g the partial shortest paths tree (without heuristic).
bool paint_partial_path(const GT &g, typename GT::Node *start, typename GT::Node *end)
Paints the shortest path from start to end on the graph using A*.
bool paint_path(const GT &g, typename GT::Node *start, typename GT::Node *end)
Alias for paint_partial_path (backward compatibility).
bool painted
Whether graph has been painted.
Distance::Distance_Type find_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &path)
Finds the shortest path from start to end using A*.
void paint_min_paths_tree(const GT &g, typename GT::Node *start)
Paints on graph g the spanning tree of ALL shortest paths starting from start (without heuristic).
void operator()(const GT &g, typename GT::Node *s, GT &tree)
Computes the spanning tree of all shortest paths.
Default distance accessor for arc weights.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
void empty()
Clean the path: all the nodes and arc are removed.
Base class providing common infrastructure for shortest path algorithms.
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
void check_beyond_max(const GT &g, typename GT::Node *start, typename GT::Node *end=nullptr) const
After a painting in which a sum went beyond max(), throws if a node that start reaches was left out,...
Distance::Distance_Type copy_painted_min_paths_tree(GT &g, GT &tree)
Extracts the painted shortest paths tree and puts it in tree.
bool is_painted() const noexcept
Check if the graph has been painted.
bool has_computation() const noexcept
Check if a computation has been performed.
Distance::Distance_Type checked_add(const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b) const
Checked addition to prevent integer overflow.
GT * get_graph() const noexcept
Get the graph of the last computation.
Distance::Distance_Type get_distance(typename GT::Node *node)
Gets the accumulated distance to a node after painting.
GT::Node * get_start_node() const noexcept
Get the start node of the last computation.
bool add_if_fits(const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b, typename Distance::Distance_Type &out)
Adds a tentative distance that may go beyond max().
bool beyond_max
A sum of the painting went beyond max()
GT * ptr_g
Pointer to the graph.
bool painted
Whether graph has been painted.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define ARC_BITS(p)
Return the control bits of arc p.
#define NODE_COOKIE(p)
Return the node cookie
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
#define NODE_BITS(p)
Get the control bits of a node.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Common utilities and base class for shortest path algorithms.
Default filter for filtered iterators on arcs.
Euclidean distance heuristic for A* in 2D grids.
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
typename Distance::Distance_Type Distance_Type
Manhattan distance heuristic for A* in grid graphs.
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
typename Distance::Distance_Type Distance_Type
Filtered iterator of adjacent arcs of a node.
Information stored in arc cookies for painting version.
Arc cleanup for painting version.
Node cleanup for painting version.
Arc cleanup and mapping for tree-building version.
Node cleanup and mapping for tree-building version.
Arc initialization for painting version.
Node initialization for painting version.
Arc initialization for tree-building version.
Node initialization for tree-building version.
Information stored in node cookies for painting version.
Extended arc info with tree arc mapping.
Extended node info with tree node mapping.
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
Distance_Type operator()(typename GT::Node *, typename GT::Node *) const
typename Distance::Distance_Type Distance_Type
Array-based graph implementation.
Path finding algorithms in graphs.