1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/*PGR-GNU*****************************************************************
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Copyright (c) 2018 Sourabh Garg
sourabh.garg.mat@gmail.com
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#ifndef INCLUDE_DAGSHORTESTPATH_PGR_DAGSHORTESTPATH_HPP_
#define INCLUDE_DAGSHORTESTPATH_PGR_DAGSHORTESTPATH_HPP_
#pragma once
#include <deque>
#include <set>
#include <vector>
#include <algorithm>
#include <sstream>
#include <functional>
#include <limits>
#include <map>
#include <boost/config.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dag_shortest_paths.hpp>
#include "cpp_common/basePath_SSEC.hpp"
#include "cpp_common/pgr_base_graph.hpp"
#include "cpp_common/interruption.h"
#include "c_types/ii_t_rt.h"
template < class G >
class Pgr_dag {
public:
typedef typename G::V V;
typedef typename G::E E;
//! Dijkstra 1 to 1
Path dag(
G &graph,
int64_t start_vertex,
int64_t end_vertex,
bool only_cost = false) {
clear();
// adjust predecessors and distances vectors
predecessors.resize(graph.num_vertices());
distances.resize(
graph.num_vertices(),
std::numeric_limits<double>::infinity());
if (!graph.has_vertex(start_vertex)
|| !graph.has_vertex(end_vertex)) {
return Path(start_vertex, end_vertex);
}
// get the graphs source and target
auto v_source(graph.get_V(start_vertex));
auto v_target(graph.get_V(end_vertex));
// perform the algorithm
dag_1_to_1(graph, v_source, v_target);
// get the results
return Path(
graph,
v_source, v_target,
predecessors, distances,
only_cost, true);
}
//! Dijkstra 1 to many
std::deque<Path> dag(
G &graph,
int64_t start_vertex,
const std::vector< int64_t > &end_vertex,
bool only_cost) {
// adjust predecessors and distances vectors
clear();
size_t n_goals = (std::numeric_limits<size_t>::max)();
predecessors.resize(graph.num_vertices());
distances.resize(
graph.num_vertices(),
std::numeric_limits<double>::infinity());
// get the graphs source and target
if (!graph.has_vertex(start_vertex))
return std::deque<Path>();
auto v_source(graph.get_V(start_vertex));
std::set< V > s_v_targets;
for (const auto &vertex : end_vertex) {
if (graph.has_vertex(vertex)) {
s_v_targets.insert(graph.get_V(vertex));
}
}
std::vector< V > v_targets(s_v_targets.begin(), s_v_targets.end());
// perform the algorithm
dag_1_to_many(graph, v_source, v_targets, n_goals);
std::deque< Path > paths;
// get the results // route id are the targets
paths = get_paths(graph, v_source, v_targets, only_cost);
std::stable_sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.end_id() < e2.end_id();
});
return paths;
}
// preparation for many to 1
std::deque<Path> dag(
G &graph,
const std::vector < int64_t > &start_vertex,
int64_t end_vertex,
bool only_cost) {
std::deque<Path> paths;
for (const auto &start : start_vertex) {
paths.push_back(
dag(graph, start, end_vertex, only_cost));
}
std::stable_sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.start_id() < e2.start_id();
});
return paths;
}
// preparation for many to many
std::deque<Path> dag(
G &graph,
const std::vector< int64_t > &start_vertex,
const std::vector< int64_t > &end_vertex,
bool only_cost) {
// a call to 1 to many is faster for each of the sources
std::deque<Path> paths;
for (const auto &start : start_vertex) {
auto r_paths = dag(
graph,
start, end_vertex,
only_cost);
paths.insert(paths.begin(), r_paths.begin(), r_paths.end());
}
std::sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.end_id() < e2.end_id();
});
std::stable_sort(paths.begin(), paths.end(),
[](const Path &e1, const Path &e2)->bool {
return e1.start_id() < e2.start_id();
});
return paths;
}
// preparation for parallel arrays
std::deque<Path> dag(
G &graph,
const std::vector<II_t_rt> &combinations,
bool only_cost) {
std::deque<Path> paths;
// group targets per distinct source
std::map< int64_t, std::vector<int64_t> > vertex_map;
for (const II_t_rt &comb : combinations) {
std::map< int64_t, std::vector<int64_t> >::iterator it = vertex_map.find(comb.d1.source);
if (it != vertex_map.end()) {
it->second.push_back(comb.d2.target);
} else {
std::vector<int64_t > targets{comb.d2.target};
vertex_map[comb.d1.source] = targets;
}
}
for (const auto &start_ends : vertex_map) {
auto result_paths = dag(
graph,
start_ends.first,
start_ends.second,
only_cost);
paths.insert(
paths.end(),
std::make_move_iterator(result_paths.begin()),
std::make_move_iterator(result_paths.end()));
}
return paths;
}
//@}
private:
//! Call to Dijkstra 1 source to 1 target
bool dag_1_to_1(
G &graph,
V source,
V target) {
/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();
try {
boost::dag_shortest_paths(graph.graph, source,
boost::predecessor_map(&predecessors[0])
.weight_map(get(&G::G_T_E::cost, graph.graph))
.distance_map(&distances[0])
.visitor(dijkstra_one_goal_visitor(target)));
} catch(found_goals &) {
return true;
} catch (boost::exception const& ex) {
(void)ex;
throw;
} catch (std::exception &e) {
(void)e;
throw;
} catch (...) {
throw;
}
return true;
}
//! Dijkstra 1 source to many targets
bool dag_1_to_many(
G &graph,
V source,
const std::vector< V > &targets,
size_t n_goals = (std::numeric_limits<size_t>::max)()) {
/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();
try {
boost::dag_shortest_paths(graph.graph, source,
boost::predecessor_map(&predecessors[0])
.weight_map(get(&G::G_T_E::cost, graph.graph))
.distance_map(&distances[0])
.distance_inf(std::numeric_limits<double>::infinity())
.visitor(dijkstra_many_goal_visitor(targets, n_goals)));
} catch(found_goals &) {
return true;
} catch (boost::exception const& ex) {
(void)ex;
throw;
} catch (std::exception &e) {
(void)e;
throw;
} catch (...) {
throw;
}
return true;
}
void clear() {
predecessors.clear();
distances.clear();
nodesInDistance.clear();
}
// used when multiple goals
std::deque<Path> get_paths(
const G &graph,
V source,
std::vector< V > &targets,
bool only_cost) const {
std::deque<Path> paths;
for (const auto target : targets) {
paths.push_back(Path(
graph,
source, target,
predecessors, distances,
only_cost, true));
}
return paths;
}
//! @name members
//@{
struct found_goals{}; //!< exception for termination
std::vector< V > predecessors;
std::vector< double > distances;
std::deque< V > nodesInDistance;
std::ostringstream log;
//@}
//! @name Stopping classes
//@{
//! class for stopping when 1 target is found
class dijkstra_one_goal_visitor : public boost::default_dijkstra_visitor {
public:
explicit dijkstra_one_goal_visitor(V goal) : m_goal(goal) {}
template <class B_G>
void examine_vertex(V &u, B_G &) {
if (u == m_goal) throw found_goals();
}
private:
V m_goal;
};
//! class for stopping when all targets are found
class dijkstra_many_goal_visitor : public boost::default_dijkstra_visitor {
public:
explicit dijkstra_many_goal_visitor(
const std::vector< V > &goals,
size_t n_goals) :
m_goals(goals.begin(), goals.end()),
m_n_goals(n_goals) {}
template <class B_G>
void examine_vertex(V u, B_G &) {
auto s_it = m_goals.find(u);
if (s_it == m_goals.end()) return;
// found one more goal
m_goals.erase(s_it);
// all goals found
if (m_goals.size() == 0) throw found_goals();
// number of requested goals found
--m_n_goals;
if (m_n_goals == 0) throw found_goals();
}
private:
std::set< V > m_goals;
size_t m_n_goals;
};
};
#endif // INCLUDE_DAGSHORTESTPATH_PGR_DAGSHORTESTPATH_HPP_
|