31 #ifndef INCLUDE_DIJKSTRA_PGR_DIJKSTRA_HPP_
32 #define INCLUDE_DIJKSTRA_PGR_DIJKSTRA_HPP_
35 #include <boost/config.hpp>
36 #include <boost/graph/adjacency_list.hpp>
39 #include <boost/graph/dijkstra_shortest_paths.hpp>
57 #include "./../../common/src/signalhandler.h"
70 std::vector< int64_t > start_vids,
73 std::ostringstream &log) {
91 bool only_cost =
false) {
93 return fn_dijkstra.
dijkstra(graph, source, target, only_cost);
112 int64_t start_vertex,
118 auto path =
Path(graph,
124 std::sort(path.begin(), path.end(),
126 {return l.node < r.node;});
127 std::stable_sort(path.begin(), path.end(),
129 {return l.agg_cost < r.agg_cost;});
134 Path p(start_vertex, start_vertex);
142 const std::vector< int64_t > &start_vertex,
145 std::ostringstream &the_log) {
151 the_log <<
log.str();
174 int64_t start_vertex,
176 bool only_cost =
false) {
182 graph.num_vertices(),
183 std::numeric_limits<double>::infinity());
186 if (!graph.has_vertex(start_vertex)
187 || !graph.has_vertex(end_vertex)) {
188 return Path(start_vertex, end_vertex);
192 auto v_source(graph.get_V(start_vertex));
193 auto v_target(graph.get_V(end_vertex));
214 int64_t start_vertex,
215 const std::vector< int64_t > &end_vertex,
223 graph.num_vertices(),
224 std::numeric_limits<double>::infinity());
228 if (!graph.has_vertex(start_vertex))
return std::deque<Path>();
230 auto v_source(graph.get_V(start_vertex));
232 std::set< V > s_v_targets;
233 for (
const auto &vertex : end_vertex) {
234 if (graph.has_vertex(vertex)) s_v_targets.insert(graph.get_V(vertex));
237 std::vector< V > v_targets(s_v_targets.begin(), s_v_targets.end());
241 std::deque< Path > paths;
243 paths =
get_paths(graph, v_source, v_targets, only_cost);
251 const std::vector < int64_t > &start_vertex,
254 std::deque<Path> paths;
256 for (
const auto &start : start_vertex) {
258 dijkstra(graph, start, end_vertex, only_cost));
261 std::stable_sort(paths.begin(), paths.end(),
262 [](
const Path &e1,
const Path &e2)->bool {
263 return e1.start_id() < e2.start_id();
272 const std::vector< int64_t > &start_vertex,
273 const std::vector< int64_t > &end_vertex,
275 size_t n_goals = (std::numeric_limits<size_t>::max)()) {
277 std::deque<Path> paths;
279 for (
const auto &start : start_vertex) {
284 paths.insert(paths.begin(), r_paths.begin(), r_paths.end());
293 const std::vector< pgr_combination_t > &combinations,
297 std::deque<Path> paths;
300 std::map<int64_t , std::vector<int64_t> > vertex_map;
302 std::map< int64_t , std::vector<int64_t> >::iterator it = vertex_map.find(comb.source);
303 if (it != vertex_map.end()) {
304 it->second.push_back(comb.target);
306 std::vector<int64_t > targets{comb.target};
307 vertex_map[comb.source] = targets;
311 for (
const auto &start_ends : vertex_map) {
314 start_ends.first, start_ends.second,
316 paths.insert(paths.begin(), r_paths.begin(), r_paths.end());
331 CHECK_FOR_INTERRUPTS();
333 boost::dijkstra_shortest_paths(graph.graph, source,
335 .weight_map(get(&G::G_T_E::cost, graph.graph))
340 }
catch (boost::exception
const& ex) {
343 }
catch (std::exception &e) {
364 CHECK_FOR_INTERRUPTS();
366 boost::dijkstra_shortest_paths(graph.graph, source,
368 .weight_map(get(&G::G_T_E::cost, graph.graph))
376 }
catch (boost::exception
const&) {
378 }
catch (std::exception&) {
398 std::vector<boost::default_color_type> color_map(graph.num_vertices());
400 CHECK_FOR_INTERRUPTS();
402 boost::dijkstra_shortest_paths_no_init(graph.graph, source,
403 make_iterator_property_map(
406 make_iterator_property_map(
409 get(&G::G_T_E::cost, graph.graph),
412 boost::closed_plus<double>(),
413 static_cast<double>(0),
421 boost::make_iterator_property_map(
427 }
catch (boost::exception
const& ex) {
430 }
catch (std::exception &e) {
455 int64_t start_vertex,
461 graph.num_vertices(),
462 std::numeric_limits<double>::infinity());
465 if (!graph.has_vertex(start_vertex)) {
471 graph.get_V(start_vertex),
520 const std::vector< int64_t > &start_vertex,
523 log <<
"Number of edges:" << boost::num_edges(graph.graph) <<
"\n";
527 graph.num_vertices(),
528 std::numeric_limits<double>::infinity());
537 std::deque< std::vector< V > >
pred(start_vertex.size());
541 for (
const auto &vertex : start_vertex) {
547 if (graph.has_vertex(vertex)) {
562 for (
const auto &vertex : start_vertex) {
563 for (
auto &p :
pred) {
564 if (!p.empty() && graph.has_vertex(vertex))
565 p[graph.get_V(vertex)] = graph.get_V(vertex);
586 const std::vector< int64_t > &start_vertex,
587 std::deque< std::vector< V > > &
pred,
598 std::deque<Path> paths;
599 for (
const auto vertex : start_vertex) {
600 paths.push_back(
Path(vertex, vertex));
601 paths.back().push_back({vertex, -1, 0, 0});
612 if (!(
distances[d] <= distance))
continue;
614 for (
auto i = start_vertex.size(); i > 0; --i) {
618 if (
pred[i - 1].empty())
break;
625 if (
pred[i - 1][d] == d)
continue;
628 auto edge_id = graph.get_edge_id(
pred[i - 1][d], d, cost);
630 paths[i - 1].push_back(
638 for (
auto &path : paths) {
639 path.sort_by_node_agg_cost();
648 const std::vector< int64_t > &start_vertex,
651 std::deque<Path> paths;
652 for (
const auto &vertex : start_vertex) {
660 path.sort_by_node_agg_cost();
661 paths.push_back(path);
663 Path p(vertex, vertex);
676 const std::vector< V > &targets,
679 CHECK_FOR_INTERRUPTS();
680 std::set<V> goals_found;
681 std::set<V> goals(targets.begin(), targets.end());
683 boost::dijkstra_shortest_paths(graph.graph, source,
685 .weight_map(get(&G::G_T_E::cost, graph.graph))
687 .distance_inf(std::numeric_limits<double>::infinity())
690 for (
const auto &g : goals) {
691 if (goals_found.find(g) == goals_found.end()) {
697 }
catch (boost::exception
const& ex) {
700 }
catch (std::exception &e) {
723 std::vector< V > &targets,
724 bool only_cost)
const {
725 std::deque<Path> paths;
726 for (
const auto target : targets) {
727 paths.push_back(
Path(
753 const std::set<V> &goals,
755 std::set<V> &f_goals) :
765 if (s_it ==
m_goals.end())
return;
790 double distance_goal,
815 :
public boost::default_dijkstra_visitor {
818 std::ostringstream &p_log,
820 double distance_goal,
823 std::vector<boost::default_color_type> &color_map) :
842 m_color[u] = boost::black_color;
848 if (source(e, g) !=
first
850 m_color[target(e, g)] = boost::black_color;
863 if (source(e, g) !=
first
865 m_color[target(e, g)] = boost::black_color;
878 m_color[u] = boost::black_color;
889 std::vector<boost::default_color_type> &
m_color;
896 #endif // INCLUDE_DIJKSTRA_PGR_DIJKSTRA_HPP_