23namespace shortest_path {
52 typename F1 = std::function<
typename TGraph::edge_type::weight_type(
const TNode &)>,
53 typename F2 = std::function<void(
const TNode &,
const typename TGraph::edge_type::weight_type &)>>
60 F2 f_user = [](
const TNode &,
61 const typename TGraph::edge_type::weight_type &) {});
66 typename F1 = std::function<
typename TGraph::edge_type::weight_type(
const TNode &,
const TNode &)>
74 return findPath(
graph,
start, goal, [&](
const TNode &n) {
return f_heuristic(goal, n); });
103 typename F1 = std::function<
typename TGraph::edge_type::weight_type(
const TNode &)>,
104 typename F2 = std::function<
typename TGraph::edge_type::weight_type(
const TNode &)>,
105 typename F3 = std::function<void(
const TNode &,
const typename TGraph::edge_type::weight_type &)>>
111 F1 f_heuristic_forward,
112 F2 f_heuristic_backward,
113 F3 f_user = [](
const TNode &,
114 const typename TGraph::edge_type::weight_type &) {});
119 typename F1 = std::function<
typename TGraph::edge_type::weight_type(
const TNode &,
const TNode &)>
128 [&](
const TNode &n) {
return f_heuristic(goal, n); },
129 [&](
const TNode &n) {
return f_heuristic(
start, n); });
137 template<
typename TNode,
typename TWeight>
147 template<
typename TGraph,
typename TNode,
typename F1,
typename F2,
typename F3>
150 impl(
const TGraph &
graph,
160 template<
typename FSuccEdge,
typename TNode,
typename TWeight,
typename F1,
typename F2,
typename F3,
typename F4>
161 static bool relaxation(FSuccEdge successor_edges,
162 Data<TNode, TWeight> &data,
170 template<
typename TGraph,
typename TNode,
typename F1,
typename F2,
typename F3,
typename F4>
173 implBidirectional(
const TGraph &
graph,
176 F1 f_heuristic_forward,
177 F2 f_heuristic_backward,
183 template<
typename TNode,
typename TWeight,
typename F>
184 inline static void init(AStar::Data<TNode, TWeight> &data,
const TNode &
start, F f_heuristic);
191template<
typename TGraph,
typename TNode,
typename F1,
typename F2>
202 [&goal](
const TNode &n) ->
bool {
return goal == n; });
207template<
typename TGraph,
typename TNode,
typename F1,
typename F2,
typename F3>
212 F1 f_heuristic_forward,
213 F2 f_heuristic_backward,
216 return implBidirectional(
graph,
220 f_heuristic_backward,
222 [](
const TNode &) ->
bool {
return false; });
227template<
typename TGraph,
typename TNode,
typename F1,
typename F2,
typename F3>
229AStar::impl(
const TGraph &
graph,
235 using weight_type =
typename TGraph::edge_type::weight_type;
237 Data<TNode, weight_type> data;
240 init(data,
start, f_heuristic);
242 while (!data.queue.empty()) {
243 bool stop = relaxation([&](
const auto &
node) ->
auto {
return graph.successorEdges(
node); },
248 [](
const TNode &) ->
void {});
260template<
typename FSuccEdge,
typename TNode,
typename TWeight,
typename F1,
typename F2,
typename F3,
typename F4>
261bool AStar::relaxation(FSuccEdge successor_edges,
262 Data<TNode, TWeight> &data,
267 TNode n = data.queue.begin()->second;
268 data.queue.erase(data.queue.begin());
271 f_user(n, data.g[n]);
278 for (
const auto &s_edge: successor_edges(n)) {
282 if (s_edge.weight() < 0) {
283 throw std::out_of_range(
"AStar: Detect negative weight on edge in graph.");
287 TWeight gscore = data.g.at(n) + s_edge.weight();
290 auto search_g = data.g.find(s);
293 if (search_g == data.g.end() || data.g.at(s) > gscore) {
296 if (search_q != data.queue.end()) {
298 data.queue.erase(search_q);
302 data.f[s] = gscore + f_heuristic(s);
303 data.p.insert_or_assign(s, n);
315template<
typename TGraph,
typename TNode,
typename F1,
typename F2,
typename F3,
typename F4>
317AStar::implBidirectional(
const TGraph &
graph,
320 F1 f_heuristic_forward,
321 F2 f_heuristic_backward,
324 using TWeight =
typename TGraph::edge_type::weight_type;
328 Data<TNode, TWeight> forward_data;
329 Data<TNode, TWeight> backward_data;
332 init(forward_data,
start, f_heuristic_forward);
333 auto f_forward_update = [&](
const auto &s) ->
void {
334 if (backward_data.g.find(s) != backward_data.g.end()) {
335 if (forward_data.g.at(s) + backward_data.g.at(s) < p) {
336 p = forward_data.g.at(s) + backward_data.g.at(s);
337 intersection_nodes.push_back(s);
343 init(backward_data, goal, f_heuristic_backward);
344 auto f_backward_update = [&](
const auto &s) ->
void {
345 if (forward_data.g.find(s) != forward_data.g.end()) {
346 if (backward_data.g.at(s) + forward_data.g.at(s) < p) {
347 p = backward_data.g.at(s) + forward_data.g.at(s);
348 intersection_nodes.push_back(s);
353 while (!forward_data.queue.empty() && !backward_data.queue.empty()) {
354 if (
std::max(forward_data.queue.begin()->first, backward_data.queue.begin()->first) >= p) {
361 intersection_nodes.back());
365 if (forward_data.queue.begin()->first < backward_data.queue.begin()->first) {
367 relaxation([&](
const auto &
node) ->
auto {
return graph.successorEdges(
node); },
375 relaxation([&](
const auto &
node) ->
auto {
return graph.predecessorEdges(
node); },
377 f_heuristic_backward,
389template<
typename TNode,
typename TWeight,
typename F>
390void AStar::init(AStar::Data<TNode, TWeight> &data,
const TNode &
start, F f_heuristic) {
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
static ext::pair< ext::vector< TNode >, TWeight > reconstructWeightedPath(const ext::map< TNode, TNode > &p, const ext::map< TNode, TWeight > &g, const TNode &start, const TNode &goal)
Definition: ReconstructPath.hpp:132
static TNode const & other(const TEdge &e, const TNode &n)
static ext::pair< ext::vector< TNode >, typename TGraph::edge_type::weight_type > findPathBidirectional(const TGraph &graph, const TNode &start, const TNode &goal, F1 f_heuristic_forward, F2 f_heuristic_backward, F3 f_user=[](const TNode &, const typename TGraph::edge_type::weight_type &) {})
Definition: AStar.hpp:209
static ext::pair< ext::vector< TNode >, typename TGraph::edge_type::weight_type > findPath(const TGraph &graph, const TNode &start, const TNode &goal, F1 f_heuristic, F2 f_user=[](const TNode &, const typename TGraph::edge_type::weight_type &) {})
Definition: AStar.hpp:193
static ext::pair< ext::vector< TNode >, typename TGraph::edge_type::weight_type > findPathBidirectionalRegistration(const TGraph &graph, const TNode &start, const TNode &goal, F1 f_heuristic)
Definition: AStar.hpp:123
static ext::pair< ext::vector< TNode >, typename TGraph::edge_type::weight_type > findPathRegistration(const TGraph &graph, const TNode &start, const TNode &goal, F1 f_heuristic)
Definition: AStar.hpp:70
constexpr const T & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ReconstructPath.hpp:14
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14