45#include <gtest/gtest.h>
62 template <
class G,
class D,
class A>
68 template <
class G,
class D,
class A>
133 double dx = from->
x - to->
x;
134 double dy = from->
y - to->
y;
135 return std::sqrt(dx*dx + dy*dy);
146 return std::abs(from->
x - to->
x) + std::abs(from->
y - to->
y);
156 std::vector<SimpleGraph::Node*>
nodes;
168 for (
int i = 0; i < 4; ++i)
182 std::vector<GridGraph::Node*>
nodes;
244 EXPECT_DOUBLE_EQ(astar_cost, dijkstra_cost);
245 EXPECT_EQ(astar_path.
size(), dijkstra_path.
size());
257 EXPECT_DOUBLE_EQ(cost, 3.0);
258 EXPECT_EQ(path.
size(), 3);
272 EXPECT_DOUBLE_EQ(cost, 3.0);
283 EXPECT_NE(end_tree_node,
nullptr);
291 auto isolated = g.insert_node(99);
298 EXPECT_EQ(cost, std::numeric_limits<double>::max());
319 auto start = get_node(0, 0);
320 auto end = get_node(GRID_SIZE-1, GRID_SIZE-1);
322 auto cost = astar.
find_path(g, start, end, path);
325 double expected = (GRID_SIZE-1) + (GRID_SIZE-1);
335 auto start = get_node(0, 0);
336 auto end = get_node(GRID_SIZE-1, GRID_SIZE-1);
338 auto cost = astar.
find_path(g, start, end, path);
340 double expected = (GRID_SIZE-1) + (GRID_SIZE-1);
351 auto start = get_node(0, 0);
352 auto end = get_node(4, 4);
354 auto cost = astar.
find_path(g, start, end, path);
357 EXPECT_EQ(cost, 8.0);
366 auto cost = astar(g,
nodes[0],
nodes[2], path);
368 EXPECT_DOUBLE_EQ(cost, 3.0);
372TEST(AStarEdgeCases, SingleNodeGraph)
385TEST(AStarEdgeCases, NullParametersThrow)
393 EXPECT_THROW(astar.
find_path(g,
nullptr, node, path), std::domain_error);
394 EXPECT_THROW(astar.
find_path(g, node,
nullptr, path), std::domain_error);
398TEST(AStarEdgeCases, EmptyGraphThrows)
407 EXPECT_THROW(astar.
find_path(empty_g, node, node, path), std::domain_error);
411TEST(AStarComparison, RandomGraphMatchesDijkstra)
414 std::vector<SimpleGraph::Node*>
nodes;
415 std::mt19937
rng(42);
416 std::uniform_real_distribution<double> weight_dist(0.1, 10.0);
419 for (
int i = 0; i < 20; ++i)
423 for (
size_t i = 0; i <
nodes.size(); ++i)
424 for (
size_t j = i + 1; j <
nodes.size(); ++j)
427 double w = weight_dist(
rng);
436 for (
int i = 0; i < 5; ++i)
447 auto astar_cost = astar.
find_path(g, start, end, astar_path);
448 auto dijkstra_cost = dijkstra.
find_min_path(g, start, end, dijkstra_path);
451 EXPECT_NEAR(astar_cost, dijkstra_cost, 1e-9)
452 <<
"Mismatch for start=" << start->get_info()
453 <<
" end=" << end->get_info();
463 std::vector<SimpleGraph::Node*>
nodes;
474 for (
int i = 0; i < 4; ++i)
493 ASSERT_NE(
root,
nullptr);
512 for (
auto node :
nodes)
516 EXPECT_LT(dist, std::numeric_limits<double>::max());
532 EXPECT_DOUBLE_EQ(astar_cost, dijkstra_cost);
533 EXPECT_EQ(astar_path.
size(), dijkstra_path.
size());
563 EXPECT_EQ(cost, 3.0);
609 astar(g,
nodes[0], tree);
615TEST(AStarErrorCases, GetDistanceThrowsIfNotPainted)
622 EXPECT_THROW(astar.
get_distance(node), std::domain_error);
626TEST(AStarErrorCases, CopyPaintedThrowsIfNotPainted)
654TEST(AStarDijkstraMode, DisconnectedGraph)
694template <
typename HeapTag>
699 std::vector<SimpleGraph::Node*>
nodes;
703 for (
int i = 0; i < 5; ++i)
716using HeapTypes = ::testing::Types<BinHeapTag, FibHeapTag>;
725 TypeParam::template Heap>;
729 auto cost = astar.find_path(this->g, this->
nodes[0], this->
nodes[4], path);
731 EXPECT_EQ(cost, 10.0);
741 TypeParam::template Heap>;
745 auto root = astar.compute_min_paths_tree(this->g, this->
nodes[0], tree);
747 EXPECT_NE(
root,
nullptr);
758 TypeParam::template Heap>;
761 astar.paint_min_paths_tree(this->g, this->
nodes[0]);
763 EXPECT_EQ(astar.get_distance(this->nodes[0]), 0.0);
764 EXPECT_EQ(astar.get_distance(this->nodes[1]), 1.0);
765 EXPECT_EQ(astar.get_distance(this->nodes[2]), 3.0);
766 EXPECT_EQ(astar.get_distance(this->nodes[3]), 6.0);
767 EXPECT_EQ(astar.get_distance(this->nodes[4]), 10.0);
776 TypeParam::template Heap>;
779 TypeParam::template Heap>;
787 auto astar_cost = astar.find_min_path(this->g, this->
nodes[0], this->
nodes[4], astar_path);
790 EXPECT_DOUBLE_EQ(astar_cost, dijkstra_cost);
791 EXPECT_EQ(astar_path.
size(), dijkstra_path.
size());
797TEST(AStarEdgeCaseTest, SelfLoop)
810 auto cost = astar.find_min_path(g, n1, n2, path);
812 EXPECT_DOUBLE_EQ(cost, 5.0);
813 EXPECT_EQ(path.
size(), 2u);
817TEST(AStarEdgeCaseTest, MultipleArcsBetweenNodes)
831 auto cost = astar.find_min_path(g, n1, n2, path);
833 EXPECT_DOUBLE_EQ(cost, 5.0);
837TEST(AStarEdgeCaseTest, UniformWeights)
840 std::vector<SimpleGraph::Node*>
nodes;
842 for (
int i = 0; i < 4; ++i)
855 auto cost = astar.find_min_path(g,
nodes[0],
nodes[3], path);
857 EXPECT_DOUBLE_EQ(cost, 1.0);
858 EXPECT_EQ(path.
size(), 2u);
862TEST(AStarEdgeCaseTest, LargeWeights)
869 double large = 1e100;
877 auto cost = astar.find_min_path(g, n1, n3, path);
879 EXPECT_GT(cost, large);
884TEST(AStarEdgeCaseTest, Reusability)
887 std::vector<SimpleGraph::Node*>
nodes;
889 for (
int i = 0; i < 5; ++i)
903 auto cost1 = astar.find_min_path(g,
nodes[0],
nodes[2], path1);
904 EXPECT_DOUBLE_EQ(cost1, 3.0);
910 auto cost2 = astar.find_min_path(g,
nodes[1],
nodes[4], path2);
911 EXPECT_DOUBLE_EQ(cost2, 9.0);
917 auto cost3 = astar.find_min_path(g,
nodes[0],
nodes[4], path3);
918 EXPECT_DOUBLE_EQ(cost3, 10.0);
923TEST(AStarEdgeCaseTest, ZeroWeightArcs)
937 auto cost = astar.find_min_path(g, n1, n3, path);
939 EXPECT_DOUBLE_EQ(cost, 5.0);
940 EXPECT_EQ(path.
size(), 3u);
944TEST(AStarEdgeCaseTest, InadmissibleHeuristic)
948 n1->
x = 0; n1->y = 0;
951 n2->
x = 1; n2->y = 0;
954 n3->
x = 2; n3->y = 0;
963 using Distance_Type = double;
966 double dx = from->
x - to->
x;
967 double dy = from->
y - to->
y;
968 return 100.0 * std::sqrt(dx*dx + dy*dy);
976 auto cost = astar.find_path(g, n1, n3, path);
980 EXPECT_GT(cost, 0.0);
1000TEST(AStarPolicyTest, UsesTheDistanceItWasGiven)
1014 EXPECT_EQ(astar.find_path(g, a, c, path), 21);
1015 EXPECT_EQ(astar.find_min_path(g, a, c, path), 21);
1017 ASSERT_TRUE(astar.paint_partial_path(g, a, c));
1018 EXPECT_EQ(astar.get_min_path(c, path), 21);
1020 ASSERT_TRUE(astar.paint_partial_min_paths_tree(g, a, c));
1021 EXPECT_EQ(astar.get_min_path(c, path), 21);
1023 astar.paint_min_paths_tree(g, a);
1024 EXPECT_EQ(astar.get_distance(c), 21);
1026 EXPECT_EQ(astar.copy_painted_min_paths_tree(g, painted), 21);
1028 ASSERT_NE(astar.compute_partial_path(g, a, c, tree),
nullptr);
1029 EXPECT_EQ(astar.get_min_path(tree, c, path), 21);
1031 astar.compute_min_paths_tree(g, a, tree);
1032 EXPECT_EQ(astar.get_min_path(tree, c, path), 21);
1034 astar.compute_partial_min_paths_tree(g, a, c, tree);
1035 EXPECT_EQ(astar.get_min_path(tree, c, path), 21);
1045 const int *
h =
nullptr;
1049 return h[from->get_info()];
1057TEST(AStarPolicyTest, FindsAPathOfMaxThroughAPriorityThatDoesNotFit)
1059 constexpr int M = std::numeric_limits<int>::max();
1061 auto s = g.insert_node(0);
1062 auto a = g.insert_node(1);
1063 auto b = g.insert_node(2);
1064 auto t = g.insert_node(3);
1065 g.insert_arc(s, b, 2);
1066 g.insert_arc(s, a, 1);
1067 g.insert_arc(b, a, 0);
1068 g.insert_arc(a, t, M - 1);
1069 const int h[] = {M, M - 1, M - 1, 0};
1074 EXPECT_EQ(astar.find_path(g, s, t, path), M);
1075 EXPECT_EQ(path.size(), 3u);
1077 ASSERT_TRUE(astar.paint_partial_path(g, s, t));
1078 EXPECT_EQ(astar.get_min_path(t, path), M);
1081 ASSERT_NE(astar.compute_partial_path(g, s, t, tree),
nullptr);
1082 EXPECT_EQ(astar.get_min_path(tree, t, path), M);
1099TEST(AStarPolicyTest, TakesADistanceWithoutADefaultConstructor)
1113 EXPECT_EQ(astar.
find_path(g, a, c, path), 21);
1137 throw std::runtime_error(
"policy failed");
1173TEST(AStarPolicyTest, DistanceOrHeuristicThatThrowsLeavesTheGraphReusable)
1177 std::vector<IntGraph::Node *> n;
1178 for (
int i = 0; i < 12; ++i)
1180 for (
int r = 0;
r < 3; ++
r)
1181 for (
int c = 0; c < 4; ++c)
1183 const int i = 4 *
r + c;
1185 g.
insert_arc(n[i], n[i + 1], 1 + (7 * i) % 9);
1187 g.
insert_arc(n[i], n[i + 4], 1 + (5 * i + 3) % 9);
1190 const auto start = n[0];
1191 const auto end = n[11];
1204 const std::vector<std::function<void()>> methods = {
1207 EXPECT_EQ(astar.find_path(g, start, end, path),
expected);
1210 ASSERT_NE(astar.compute_partial_path(g, start, end, tree),
nullptr);
1212 EXPECT_EQ(astar.get_min_path(tree, end, path),
expected);
1216 EXPECT_EQ(astar.find_min_path(g, start, end, path),
expected);
1219 astar.paint_min_paths_tree(g, start);
1220 EXPECT_EQ(astar.get_distance(end),
expected);
1223 astar.compute_min_paths_tree(g, start, tree);
1225 EXPECT_EQ(astar.get_min_path(tree, end, path),
expected);
1228 astar.compute_partial_min_paths_tree(g, start, end, tree);
1230 EXPECT_EQ(astar.get_min_path(tree, end, path),
expected);
1234 for (
auto * failure : {&heuristic_failure, &distance_failure})
1235 for (
size_t m = 0;
m < methods.
size(); ++
m)
1239 const long calls = failure->calls;
1240 if (failure == &distance_failure)
1241 ASSERT_GT(calls, 0) <<
"method " <<
m;
1243 for (
long k = 1;
k <= calls; ++
k)
1246 failure->fail_at =
k;
1247 EXPECT_THROW(methods[
m](), std::runtime_error)
1248 <<
"method " <<
m <<
", call " <<
k;
1250 failure->fail_at = 0;
1258 ::testing::InitGoogleTest(&argc, argv);
1259 return RUN_ALL_TESTS();
A* shortest path algorithm.
Dijkstra's shortest path algorithm.
TYPED_TEST(AStarHeapTest, FindPathWithHeuristic)
List_Graph< SimpleNode, SimpleArc > SimpleGraph
TEST_F(AStarBasicTest, ZeroHeuristicMatchesDijkstra)
::testing::Types< BinHeapTag, FibHeapTag > HeapTypes
TYPED_TEST_SUITE(AStarHeapTest, HeapTypes)
std::vector< SimpleGraph::Node * > nodes
std::vector< SimpleGraph::Node * > nodes
std::vector< GridGraph::Node * > nodes
GridGraph::Node * get_node(int x, int y)
static constexpr int GRID_SIZE
std::vector< SimpleGraph::Node * > nodes
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).
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.
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).
bool has_computation() const noexcept
Check if a computation has been performed.
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*.
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.
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).
GT::Node * get_start_node() const noexcept
Get the start node of the last computation.
bool paint_path(const GT &g, typename GT::Node *start, typename GT::Node *end)
Alias for paint_partial_path (backward compatibility).
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).
Default distance accessor for arc weights.
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Node Node
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes the shortest path between start and end by painting the graph.
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.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
size_t size() const noexcept
Return the path length in nodes.
bool is_empty() const noexcept
Return true if the path is empty.
Minimal std::expected-style result type for C++20.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
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.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
Default filter for filtered iterators on arcs.
Arc of graph implemented with double-linked adjacency lists.
Node belonging to a graph implemented with a double linked adjacency list.
Filtered iterator of adjacent arcs of a node.
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
int operator()(IntDigraph::Node *from, IntDigraph::Node *) const
Distance_Type operator()(GridGraph::Arc *arc) const
Distance_Type operator()(SimpleGraph::Arc *arc) const
static void set_zero(SimpleGraph::Arc *arc)
int operator()(IntGraph::Arc *a) const
Factor_Only_Distance(int f) noexcept
int operator()(IntGraph::Arc *a) const
int operator()(IntGraph::Node *, IntGraph::Node *) const
Distance_Type operator()(GridGraph::Node *from, GridGraph::Node *to) const
Distance_Type operator()(GridGraph::Node *from, GridGraph::Node *to) const
GridNode(int id, int _x, int _y)
int operator()(IntGraph::Arc *a) const
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.