45#include <gtest/gtest.h>
59 template <
class G,
class D,
class A>
65 template <
class G,
class D,
class A>
143 return std::abs(
from->x -
to->x) + std::abs(
from->y -
to->y);
153 std::vector<SimpleGraph::Node*>
nodes;
165 for (
int i = 0; i < 4; ++i)
179 std::vector<GridGraph::Node*>
nodes;
268 auto cost =
astar.get_min_path(
nodes[2], path);
295 EXPECT_EQ(cost, std::numeric_limits<double>::max());
316 auto start = get_node(0, 0);
317 auto end = get_node(GRID_SIZE-1, GRID_SIZE-1);
319 auto cost =
astar.find_path(g, start, end, path);
322 double expected = (GRID_SIZE-1) + (GRID_SIZE-1);
332 auto start = get_node(0, 0);
333 auto end = get_node(GRID_SIZE-1, GRID_SIZE-1);
335 auto cost =
astar.find_path(g, start, end, path);
337 double expected = (GRID_SIZE-1) + (GRID_SIZE-1);
348 auto start = get_node(0, 0);
349 auto end = get_node(4, 4);
351 auto cost =
astar.find_path(g, start, end, path);
411 std::vector<SimpleGraph::Node*>
nodes;
412 std::mt19937
rng(42);
413 std::uniform_real_distribution<double>
weight_dist(0.1, 10.0);
416 for (
int i = 0; i < 20; ++i)
420 for (
size_t i = 0; i <
nodes.size(); ++i)
421 for (
size_t j = i + 1; j <
nodes.size(); ++j)
433 for (
int i = 0; i < 5; ++i)
449 <<
"Mismatch for start=" << start->get_info()
450 <<
" end=" << end->get_info();
460 std::vector<SimpleGraph::Node*>
nodes;
471 for (
int i = 0; i < 4; ++i)
509 for (
auto node :
nodes)
512 auto dist =
astar.get_min_path(node, path);
513 EXPECT_LT(dist, std::numeric_limits<double>::max());
559 auto cost =
astar.get_min_path(
nodes[2], path);
663 astar.compute_min_paths_tree(g, n0, tree);
691template <
typename HeapTag>
696 std::vector<SimpleGraph::Node*>
nodes;
700 for (
int i = 0; i < 5; ++i)
713using HeapTypes = ::testing::Types<BinHeapTag, FibHeapTag>;
722 TypeParam::template Heap>;
726 auto cost =
astar.find_path(this->g, this->
nodes[0], this->
nodes[4], path);
738 TypeParam::template Heap>;
742 auto root =
astar.compute_min_paths_tree(this->g, this->
nodes[0], tree);
755 TypeParam::template Heap>;
758 astar.paint_min_paths_tree(this->g, this->
nodes[0]);
773 TypeParam::template Heap>;
776 TypeParam::template Heap>;
807 auto cost =
astar.find_min_path(g, n1, n2, path);
828 auto cost =
astar.find_min_path(g, n1, n2, path);
837 std::vector<SimpleGraph::Node*>
nodes;
839 for (
int i = 0; i < 4; ++i)
874 auto cost =
astar.find_min_path(g, n1, n3, path);
884 std::vector<SimpleGraph::Node*>
nodes;
886 for (
int i = 0; i < 5; ++i)
934 auto cost =
astar.find_min_path(g, n1, n3, path);
945 n1->
x = 0; n1->y = 0;
948 n2->
x = 1; n2->y = 0;
951 n3->
x = 2; n3->y = 0;
960 using Distance_Type =
double;
965 return 100.0 * std::sqrt(
dx*
dx +
dy*
dy);
973 auto cost =
astar.find_path(g, n1, n3, path);
983 ::testing::InitGoogleTest(&
argc,
argv);
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.
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
size_t size() const noexcept
Count the number of elements of the list.
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.
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
__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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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).
Distance_Type operator()(GridGraph::Arc *arc) const
Distance_Type operator()(SimpleGraph::Arc *arc) const
static void set_zero(SimpleGraph::Arc *arc)
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)
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.