Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
AStar.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
50#ifndef ASTAR_H
51#define ASTAR_H
52
53# include <ah-graph-concepts.H>
54
55# include <cmath>
56# include <iostream>
57# include <ahFunction.H>
58# include <ah-errors.H>
59# include <archeap.H>
60# include <tpl_find_path.H>
61# include <tpl_agraph.H>
62# include <shortest_path_common.H>
63
64namespace Aleph
65{
66
77template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
79{
81
82 Distance_Type operator()(typename GT::Node*, typename GT::Node*) const
83 {
84 return Distance_Type(0);
85 }
86};
87
168template <AlephGraph GT,
169 ArcDistance<GT> Distance = Dft_Dist<GT>,
170 class Heuristic = Zero_Heuristic<GT, Distance>,
171 template <typename, class> class Itor = Node_Arc_Iterator,
172 ArcFilter<GT> SA = Dft_Show_Arc<GT>,
173 template <class, class, class> class HeapT = ArcHeap>
175 : public Shortest_Path_Base<GT, Distance, Itor, SA, HeapT>
176{
178
179 // Import types from base class
180 using typename Base::Node_Info;
181 using typename Base::Tree_Node_Info;
182 using typename Base::Arc_Info;
183 using typename Base::Tree_Arc_Info;
184 using typename Base::Initialize_Node;
185 using typename Base::Initialize_Arc;
186 using typename Base::Initialize_Tree_Node;
187 using typename Base::Initialize_Tree_Arc;
188 using typename Base::Destroy_Node;
189 using typename Base::Destroy_Arc;
190 using typename Base::Destroy_Tree_Node;
191 using typename Base::Destroy_Tree_Arc;
192
193 // Local cookie access macros
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)
203
204 // Import member variables from base
205 using Base::sa;
206 using Base::heap;
207 using Base::painted;
208 using Base::ptr_g;
209 using Base::s;
210
211 // A* specific: heuristic functor
212 Heuristic heuristic;
213
214#ifndef NDEBUG
215 // Debug mode: track heuristic validation
217
219 typename GT::Node* goal,
220 const typename Distance::Distance_Type& actual_cost)
221 {
223 return;
224
225 auto h_estimate = heuristic(node, goal);
226 if (h_estimate > actual_cost)
227 {
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";
233 }
234 }
235#endif
236
237public:
245 Heuristic __heuristic = Heuristic(),
246 SA __sa = SA())
247 : Base(dist, __sa), heuristic(__heuristic)
248 {
249 // empty
250 }
251
252 // Import public methods from base
254 using Base::is_painted;
256 using Base::get_graph;
257 using Base::get_distance;
258 using Base::get_min_path;
260
261 // =========================================================================
262 // Methods WITH Heuristic (A* specific)
263 // =========================================================================
264
279 typename GT::Node*
281 typename GT::Node* start,
282 typename GT::Node* end,
283 GT& tree)
284 {
285 ah_domain_error_if(start == nullptr) << "start node cannot be null";
286 ah_domain_error_if(end == nullptr) << "end node cannot be null";
287 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
288
289 this->template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
290 auto guard =
291 this->template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
292 clear_graph(tree);
293
294 // Handle trivial case: start == end
295 if (start == end)
296 {
297 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
298 AACC(start) = 0;
299 auto tree_node = tree.insert_node(start->get_info());
300 ATREENODE(start) = tree_node;
301 NODE_COOKIE(tree_node) = start;
302 return tree_node;
303 }
304
305 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
306 AACC(start) = 0;
307 ATREENODE(start) = tree.insert_node(start->get_info());
308 NODE_COOKIE(ATREENODE(start)) = start;
309
310 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
311 {
312 auto arc = it.get_current_arc_ne();
313 auto tgt = it.get_tgt_node();
314 auto g_cost = AARC_DIST(arc);
315 auto h_cost = heuristic(tgt, end);
316 // a path through arc costs g + h or more: if that does not fit,
317 // neither does the path
318 if (this->add_if_fits(g_cost, h_cost, APOT(arc)))
319 heap.put_arc(arc, tgt);
320 }
321
322 typename GT::Node* result = nullptr;
323
324 while (not heap.is_empty())
325 {
326 auto garc = heap.get_min_arc();
328 continue;
329
330 auto gsrc = g.get_src_node(garc);
331 auto gtgt = g.get_tgt_node(garc);
332
335 continue;
336
337 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
338
340 std::swap(gsrc, gtgt);
341
342 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
343
344 auto ttgt = tree.insert_node(gtgt->get_info());
345 ATREENODE(gtgt) = ttgt;
346 auto tsrc = ATREENODE(gsrc);
347
348 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
349 ATREEARC(garc) = tarc;
350
351 AACC(gtgt) = this->checked_add(AACC(gsrc), AARC_DIST(garc));
352
353 if (gtgt == end)
354 {
355 result = ttgt;
356 break;
357 }
358
359 const auto& g_cost = AACC(gtgt);
360
361 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
362 {
363 auto arc = it.get_current_arc_ne();
365 continue;
366
367 auto tgt = it.get_tgt_node();
369 continue;
370
371 typename Distance::Distance_Type new_g{};
372 if (not this->add_if_fits(g_cost, AARC_DIST(arc), new_g))
373 continue; // no distance that fits goes through arc
374 auto h = heuristic(tgt, end);
375 if (this->add_if_fits(new_g, h, APOT(arc))) // as for start
376 heap.put_arc(arc, tgt);
377 }
378 }
379
380 if (this->beyond_max and result == nullptr)
381 this->check_beyond_max(g, start, end);
382
383 return result;
384 }
385
398 bool paint_partial_path(const GT& g,
399 typename GT::Node* start,
400 typename GT::Node* end)
401 {
402 ah_domain_error_if(start == nullptr) << "start node cannot be null";
403 ah_domain_error_if(end == nullptr) << "end node cannot be null";
404 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
405
406 this->template init<Initialize_Node, Initialize_Arc>(g, start);
407 auto guard = this->template uninit_on_exit<Destroy_Node, Destroy_Arc>();
408
409 // Handle trivial case: start == end
410 if (start == end)
411 {
412 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
413 AACC(start) = 0;
414 painted = true;
415 return true;
416 }
417
418 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
419 AACC(start) = 0;
420
421 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
422 {
423 auto arc = it.get_current_arc_ne();
424 auto tgt = it.get_tgt_node();
425 auto g_cost = AARC_DIST(arc);
426 auto h_cost = heuristic(tgt, end);
427 // a path through arc costs g + h or more: if that does not fit,
428 // neither does the path
429 if (this->add_if_fits(g_cost, h_cost, APOT(arc)))
430 heap.put_arc(arc, tgt);
431 }
432
433 bool found = false;
434
435 while (not heap.is_empty())
436 {
437 auto garc = heap.get_min_arc();
439 continue;
440
441 auto src = g.get_src_node(garc);
442 auto tgt = g.get_tgt_node(garc);
443
446 continue;
447
448 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
449
451 std::swap(src, tgt);
452
453 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
454 APARENT(tgt) = src;
455
456 if (tgt == end)
457 {
458 found = true;
459 break;
460 }
461
462 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
463 const auto& g_cost = AACC(tgt);
464
465 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
466 {
467 auto arc = it.get_current_arc_ne();
469 continue;
470
471 auto t = it.get_tgt_node();
473 continue;
474
475 typename Distance::Distance_Type new_g{};
476 if (not this->add_if_fits(g_cost, AARC_DIST(arc), new_g))
477 continue; // no distance that fits goes through arc
478 auto h = heuristic(t, end);
479 if (this->add_if_fits(new_g, h, APOT(arc))) // as for start
480 heap.put_arc(arc, t);
481 }
482 }
483
484 if (this->beyond_max and not found)
485 this->check_beyond_max(g, start, end);
486
487 painted = true;
488
489 return found;
490 }
491
505 find_path(const GT& g,
506 typename GT::Node* start,
507 typename GT::Node* end,
508 Path<GT>& path)
509 {
510 path.empty();
511 if (paint_partial_path(g, start, end))
512 return this->get_min_path(end, path);
513
514 return std::numeric_limits<typename Distance::Distance_Type>::max();
515 }
516
517 // =========================================================================
518 // Backward Compatibility Aliases
519 // =========================================================================
520
522 typename GT::Node*
523 compute_path(const GT& g, typename GT::Node* start,
524 typename GT::Node* end, GT& tree)
525 {
526 return compute_partial_path(g, start, end, tree);
527 }
528
530 bool paint_path(const GT& g, typename GT::Node* start, typename GT::Node* end)
531 {
532 return paint_partial_path(g, start, end);
533 }
534
535 // =========================================================================
536 // Methods WITHOUT Heuristic (Dijkstra-compatible)
537 // =========================================================================
538
555 typename GT::Node*
556 compute_min_paths_tree(const GT& g, typename GT::Node* start, GT& tree)
557 {
558 ah_domain_error_if(start == nullptr) << "start node cannot be null";
559 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
560
561 this->template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
562 auto guard =
563 this->template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
564
565 clear_graph(tree);
566
567 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
568 AACC(start) = 0;
569 auto ret = ATREENODE(start) = tree.insert_node(start->get_info());
570 NODE_COOKIE(ATREENODE(start)) = start;
571
572 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
573 {
574 auto arc = it.get_current_arc_ne();
575 APOT(arc) = AARC_DIST(arc); // No heuristic: pot = g only
576 heap.put_arc(arc, it.get_tgt_node());
577 }
578
579 const auto& n = g.get_num_nodes();
580
581 while (tree.get_num_nodes() < n and not heap.is_empty())
582 {
583 auto garc = heap.get_min_arc();
585 continue;
586
587 auto gsrc = g.get_src_node(garc);
588 auto gtgt = g.get_tgt_node(garc);
589
592 continue;
593
594 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
595
597 std::swap(gsrc, gtgt);
598
599 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
600
601 auto ttgt = tree.insert_node(gtgt->get_info());
602 ATREENODE(gtgt) = ttgt;
603 auto tsrc = ATREENODE(gsrc);
604
605 auto tarc = tree.insert_arc(tsrc, ttgt, garc->get_info());
606 ATREEARC(garc) = tarc;
607
608 AACC(gtgt) = this->checked_add(AACC(gsrc), AARC_DIST(garc));
609 const auto& acc = AACC(gtgt);
610
611 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
612 {
613 auto arc = it.get_current_arc_ne();
615 continue;
616
617 auto tgt = it.get_tgt_node();
619 continue;
620
621 if (this->add_if_fits(acc, AARC_DIST(arc), APOT(arc)))
622 heap.put_arc(arc, tgt);
623 }
624 }
625
626 if (this->beyond_max)
627 this->check_beyond_max(g, start);
628
629 return ret;
630 }
631
644 typename GT::Node* start,
645 typename GT::Node* end,
646 GT& tree)
647 {
648 ah_domain_error_if(start == nullptr) << "start node cannot be null";
649 ah_domain_error_if(end == nullptr) << "end node cannot be null";
650 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
651
652 this->template init<Initialize_Tree_Node, Initialize_Tree_Arc>(g, start);
653 auto guard =
654 this->template uninit_on_exit<Destroy_Tree_Node, Destroy_Tree_Arc>();
655 clear_graph(tree);
656
657 // Handle trivial case: start == end
658 if (start == end)
659 {
660 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
661 AACC(start) = 0;
662 auto tree_node = tree.insert_node(start->get_info());
663 ATREENODE(start) = tree_node;
664 NODE_COOKIE(tree_node) = start;
665 return;
666 }
667
668 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
669 AACC(start) = 0;
670 ATREENODE(start) = tree.insert_node(start->get_info());
671 NODE_COOKIE(ATREENODE(start)) = start;
672
673 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
674 {
675 auto arc = it.get_current_arc_ne();
676 APOT(arc) = AARC_DIST(arc);
677 heap.put_arc(arc, it.get_tgt_node());
678 }
679
680 const auto& n = g.get_num_nodes();
681
682 while (tree.get_num_nodes() < n and not heap.is_empty())
683 {
684 auto garc = heap.get_min_arc();
686 continue;
687
688 auto gsrc = g.get_src_node(garc);
689 auto gtgt = g.get_tgt_node(garc);
690
693 continue;
694
695 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
696
698 std::swap(gsrc, gtgt);
699
700 NODE_BITS(gtgt).set_bit(Aleph::Spanning_Tree, true);
701
702 auto ttgt = tree.insert_node(gtgt->get_info());
703 ATREENODE(gtgt) = ttgt;
704
705 auto tarc = tree.insert_arc(ATREENODE(gsrc), ATREENODE(gtgt), garc->get_info());
706 ATREEARC(garc) = tarc;
707
708 AACC(gtgt) = this->checked_add(AACC(gsrc), AARC_DIST(garc));
709
710 if (gtgt == end)
711 break;
712 const auto& acc = AACC(gtgt);
713
714 for (Itor<GT, SA> it(gtgt, sa); it.has_curr(); it.next_ne())
715 {
716 auto arc = it.get_current_arc_ne();
718 continue;
719
720 auto tgt = it.get_tgt_node();
722 continue;
723
724 if (this->add_if_fits(acc, AARC_DIST(arc), APOT(arc)))
725 heap.put_arc(arc, tgt);
726 }
727 }
728
730 this->check_beyond_max(g, start, end);
731 }
732
745 typename GT::Node* start,
746 typename GT::Node* end)
747 {
748 ah_domain_error_if(start == nullptr) << "start node cannot be null";
749 ah_domain_error_if(end == nullptr) << "end node cannot be null";
750 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
751
752 this->template init<Initialize_Node, Initialize_Arc>(g, start);
753 auto guard = this->template uninit_on_exit<Destroy_Node, Destroy_Arc>();
754
755 // Handle trivial case: start == end
756 if (start == end)
757 {
758 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
759 AACC(start) = 0;
760 painted = true;
761 return true;
762 }
763
764 bool ret_val = false;
765
766 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
767 AACC(start) = 0;
768
769 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
770 {
771 auto arc = it.get_current_arc_ne();
772 APOT(arc) = AARC_DIST(arc);
773 heap.put_arc(arc, it.get_tgt_node());
774 }
775
776 const auto& n = g.get_num_nodes();
777 size_t tn = 1;
778
779 while (tn < n and not heap.is_empty())
780 {
781 auto garc = heap.get_min_arc();
783 continue;
784
785 auto src = g.get_src_node(garc);
786 auto tgt = g.get_tgt_node(garc);
787
790 continue;
791
792 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
793
795 std::swap(src, tgt);
796
797 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
798 APARENT(tgt) = src;
799
800 ++tn;
801
802 if (tgt == end)
803 {
804 ret_val = true;
805 break;
806 }
807
808 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
809 const auto& acc = AACC(tgt);
810
811 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
812 {
813 auto a = it.get_current_arc_ne();
815 continue;
816
817 auto t = it.get_tgt_node();
819 continue;
820
821 if (this->add_if_fits(acc, AARC_DIST(a), APOT(a)))
822 heap.put_arc(a, t);
823 }
824 }
825
826 if (this->beyond_max and not ret_val)
827 this->check_beyond_max(g, start, end);
828
829 painted = true;
830
831 return ret_val;
832 }
833
844 void paint_min_paths_tree(const GT& g, typename GT::Node* start)
845 {
846 ah_domain_error_if(start == nullptr) << "start node cannot be null";
847 ah_domain_error_if(g.get_num_nodes() == 0) << "graph is empty";
848
849 this->template init<Initialize_Node, Initialize_Arc>(g, start);
850 auto guard = this->template uninit_on_exit<Destroy_Node, Destroy_Arc>();
851
852 NODE_BITS(start).set_bit(Aleph::Spanning_Tree, true);
853 AACC(start) = 0;
854
855 for (Itor<GT, SA> it(start, sa); it.has_curr(); it.next_ne())
856 {
857 auto arc = it.get_current_arc_ne();
858 APOT(arc) = AARC_DIST(arc);
859 heap.put_arc(arc, it.get_tgt_node());
860 }
861
862 const auto& n = g.get_num_nodes();
863 size_t tn = 1;
864
865 while (tn < n and not heap.is_empty())
866 {
867 auto garc = heap.get_min_arc();
869 continue;
870
871 auto src = g.get_src_node(garc);
872 auto tgt = g.get_tgt_node(garc);
873
876 continue;
877
878 ARC_BITS(garc).set_bit(Aleph::Spanning_Tree, true);
879
881 std::swap(src, tgt);
882
883 NODE_BITS(tgt).set_bit(Aleph::Spanning_Tree, true);
884 APARENT(tgt) = src;
885
886 ++tn;
887
888 AACC(tgt) = this->checked_add(AACC(src), AARC_DIST(garc));
889 const auto& acc = AACC(tgt);
890
891 for (Itor<GT, SA> it(tgt, sa); it.has_curr(); it.next_ne())
892 {
893 auto a = it.get_current_arc_ne();
895 continue;
896
897 auto t = it.get_tgt_node();
899 continue;
900
901 if (this->add_if_fits(acc, AARC_DIST(a), APOT(a)))
902 heap.put_arc(a, t);
903 }
904 }
905
906 if (this->beyond_max)
907 this->check_beyond_max(g, start);
908
909 painted = true;
910 }
911
925 typename GT::Node* start,
926 typename GT::Node* end,
927 Path<GT>& min_path)
928 {
929 min_path.empty();
930 if (paint_partial_min_paths_tree(g, start, end))
931 return this->get_min_path(end, min_path);
932
933 return std::numeric_limits<typename Distance::Distance_Type>::max();
934 }
935
936 // =========================================================================
937 // Operator Interfaces
938 // =========================================================================
939
948 void operator()(const GT& g, typename GT::Node* s, GT& tree)
949 {
950 compute_min_paths_tree(g, s, tree);
951 }
952
962 typename GT::Node* s,
963 typename GT::Node* e,
964 Path<GT>& path)
965 {
966 return find_path(g, s, e, path);
967 }
968
969#undef ANassert
970#undef APARENT
971#undef ATREENODE
972#undef AACC
973#undef AHEAPNODE
974#undef AAassert
975#undef AARC_DIST
976#undef ATREEARC
977#undef APOT
978};
979
990template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
992{
994
995 Distance_Type operator()(typename GT::Node* from, typename GT::Node* to) const
996 {
997 auto& f = from->get_info();
998 auto& t = to->get_info();
999 auto dx = f.x - t.x;
1000 auto dy = f.y - t.y;
1001 return static_cast<Distance_Type>(std::sqrt(dx * dx + dy * dy));
1002 }
1003};
1004
1015template <AlephGraph GT, ArcDistance<GT> Distance = Dft_Dist<GT>>
1017{
1019
1020 Distance_Type operator()(typename GT::Node* from, typename GT::Node* to) const
1021 {
1022 auto& f = from->get_info();
1023 auto& t = to->get_info();
1024 return static_cast<Distance_Type>(std::abs(f.x - t.x) + std::abs(f.y - t.y));
1025 }
1026};
1027
1028} // end namespace Aleph
1029
1030#endif // ASTAR_H
#define APARENT(p)
Definition AStar.H:198
#define APOT(p)
Definition AStar.H:202
#define AACC(p)
Definition AStar.H:196
#define ATREEARC(p)
Definition AStar.H:201
#define AARC_DIST(p)
Definition AStar.H:200
#define ATREENODE(p)
Definition AStar.H:195
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
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
long double h
Definition btreepic.C:154
A* algorithm for finding the shortest path between two nodes.
Definition AStar.H:176
Heuristic heuristic
Definition AStar.H:212
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).
Definition AStar.H:924
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.
Definition AStar.H:556
GT::Node * compute_path(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Alias for compute_partial_path (backward compatibility).
Definition AStar.H:523
void check_heuristic_admissibility(typename GT::Node *node, typename GT::Node *goal, const typename Distance::Distance_Type &actual_cost)
Definition AStar.H:218
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).
Definition AStar.H:643
AStar_Min_Path(Distance dist=Distance(), Heuristic __heuristic=Heuristic(), SA __sa=SA())
Constructor.
Definition AStar.H:244
Distance::Distance_Type operator()(const GT &g, typename GT::Node *s, typename GT::Node *e, Path< GT > &path)
Finds shortest path using A* heuristic.
Definition AStar.H:961
GT::Node * s
Start node.
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*.
Definition AStar.H:280
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).
Definition AStar.H:744
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*.
Definition AStar.H:398
bool paint_path(const GT &g, typename GT::Node *start, typename GT::Node *end)
Alias for paint_partial_path (backward compatibility).
Definition AStar.H:530
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*.
Definition AStar.H:505
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).
Definition AStar.H:844
void operator()(const GT &g, typename GT::Node *s, GT &tree)
Computes the spanning tree of all shortest paths.
Definition AStar.H:948
Default distance accessor for arc weights.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Path on a graph.
Definition tpl_graph.H:3832
void empty()
Clean the path: all the nodes and arc are removed.
Definition tpl_graph.H:3999
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.
Definition graph-dry.H:753
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
#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.
Definition tpl_graph.H:4776
#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.
@ Spanning_Tree
Definition aleph-graph.H:79
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.
Definition tpl_graph.H:1024
Euclidean distance heuristic for A* in 2D grids.
Definition AStar.H:992
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition AStar.H:995
typename Distance::Distance_Type Distance_Type
Definition AStar.H:993
Manhattan distance heuristic for A* in grid graphs.
Definition AStar.H:1017
Distance_Type operator()(typename GT::Node *from, typename GT::Node *to) const
Definition AStar.H:1020
typename Distance::Distance_Type Distance_Type
Definition AStar.H:1018
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
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).
Definition AStar.H:79
Distance_Type operator()(typename GT::Node *, typename GT::Node *) const
Definition AStar.H:82
typename Distance::Distance_Type Distance_Type
Definition AStar.H:80
Distance accessor.
Array-based graph implementation.
Path finding algorithms in graphs.