PGROUTING  3.2
edge_disjoint_paths_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: edge_disjoint_paths_many_to_many_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
31 
32 #include <sstream>
33 #include <vector>
34 #include <set>
35 
36 #include "max_flow/pgr_maxflow.hpp"
37 
39 #include "cpp_common/pgr_alloc.hpp"
40 #include "cpp_common/pgr_assert.h"
41 
42 
43 static
44 std::vector<General_path_element_t>
46  std::vector<pgr_edge_t> edges,
47  int64_t source,
48  int64_t target,
49  bool directed) {
50  std::set<int64_t> set_source_vertices;
51  std::set<int64_t> set_sink_vertices;
52  set_source_vertices.insert(source);
53  set_sink_vertices.insert(target);
55  edges,
56  set_source_vertices,
57  set_sink_vertices, directed);
58 
59  /*
60  * boykov_kolmogorov is only for directed graphs
61  */
62  return G.edge_disjoint_paths();
63 }
64 
65 void
67  pgr_edge_t *data_edges,
68  size_t total_edges,
69  pgr_combination_t *combinations,
70  size_t total_combinations,
71  int64_t *sources,
72  size_t size_source_verticesArr,
73  int64_t *sinks,
74  size_t size_sink_verticesArr,
75  bool directed,
76  General_path_element_t **return_tuples,
77  size_t *return_count,
78  char** log_msg,
79  char** notice_msg,
80  char **err_msg) {
81  std::ostringstream log;
82  std::ostringstream notice;
83  std::ostringstream err;
84  try {
85  pgassert(!(*log_msg));
86  pgassert(!(*notice_msg));
87  pgassert(!(*err_msg));
88  pgassert(!(*return_tuples));
89  pgassert(*return_count == 0);
90  pgassert(data_edges);
91  pgassert(total_edges != 0);
92  pgassert((sources && sinks) || combinations);
93  pgassert((size_source_verticesArr && size_sink_verticesArr) || total_combinations);
94 
95  std::set<int64_t> set_source_vertices(
96  sources, sources + size_source_verticesArr);
97  std::set<int64_t> set_sink_vertices(
98  sinks, sinks + size_sink_verticesArr);
99  std::vector< pgr_combination_t > combinations_vector(
100  combinations, combinations + total_combinations);
101  std::vector<pgr_edge_t> edges(
102  data_edges, data_edges + total_edges);
103 
104  if (!combinations_vector.empty()) {
105  pgassert(set_source_vertices.empty());
106  pgassert(set_sink_vertices.empty());
107 
108  for (const pgr_combination_t &comb : combinations_vector) {
109  set_source_vertices.insert(comb.source);
110  set_sink_vertices.insert(comb.target);
111  }
112  }
113 
114 
115  std::vector<General_path_element_t> paths;
116  for (const auto &s : set_source_vertices) {
117  for (const auto &t : set_sink_vertices) {
118  auto path = single_execution(
119  edges,
120  s,
121  t,
122  directed);
123  paths.insert(paths.end(), path.begin(), path.end());
124  }
125  }
126 
127  if (paths.empty()) {
128  *return_tuples = nullptr;
129  *return_count = 0;
130  return;
131  }
132 
133  /*
134  * Initializing the cost
135  */
136  for (auto &r : paths) {
137  r.agg_cost = r.cost = 0;
138  }
139 
140  /*
141  * Calculating the cost
142  */
143  auto found = paths.size();
144  for (const auto &e : edges) {
145  for (auto &r : paths) {
146  if (r.edge == e.id) {
147  r.cost = (r.node == e.source) ?
148  e.cost : e.reverse_cost;
149  --found;
150  }
151  }
152  if (found == 0) break;
153  }
154 
155  /*
156  * Calculating the agg_cost
157  */
158  auto prev = paths[0];
159  for (auto &r : paths) {
160  if (r.seq == 1) {
161  r.agg_cost = 0;
162  } else {
163  r.agg_cost = prev.agg_cost + prev.cost;
164  }
165  prev = r;
166  }
167 
168  /*
169  * Numbering the paths
170  */
171  int path_id(0);
172  for (auto &r : paths) {
173  r.start_id = path_id;
174  if (r.edge == -1) ++path_id;
175  }
176 
177 
178  (*return_tuples) = pgr_alloc(paths.size(), (*return_tuples));
179  for (size_t i = 0; i < paths.size(); ++i) {
180  (*return_tuples)[i] = paths[i];
181  }
182  *return_count = paths.size();
183 
184 
185  *log_msg = log.str().empty()?
186  *log_msg :
187  pgr_msg(log.str().c_str());
188  *notice_msg = notice.str().empty()?
189  *notice_msg :
190  pgr_msg(notice.str().c_str());
191  } catch (AssertFailedException &except) {
192  (*return_tuples) = pgr_free(*return_tuples);
193  (*return_count) = 0;
194  err << except.what();
195  *err_msg = pgr_msg(err.str().c_str());
196  *log_msg = pgr_msg(log.str().c_str());
197  } catch (std::exception &except) {
198  (*return_tuples) = pgr_free(*return_tuples);
199  (*return_count) = 0;
200  err << except.what();
201  *err_msg = pgr_msg(err.str().c_str());
202  *log_msg = pgr_msg(log.str().c_str());
203  } catch(...) {
204  (*return_tuples) = pgr_free(*return_tuples);
205  (*return_count) = 0;
206  err << "Caught unknown exception!";
207  *err_msg = pgr_msg(err.str().c_str());
208  *log_msg = pgr_msg(log.str().c_str());
209  }
210 }
pgr_maxflow.hpp
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
edge_disjoint_paths_driver.h
single_execution
static std::vector< General_path_element_t > single_execution(std::vector< pgr_edge_t > edges, int64_t source, int64_t target, bool directed)
Definition: edge_disjoint_paths_driver.cpp:45
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::graph::PgrFlowGraph
Definition: pgr_maxflow.hpp:55
pgr_alloc.hpp
pgr_combination_t
Definition: pgr_combination_t.h:43
pgr_assert.h
An assert functionality that uses C++ throw().
General_path_element_t
Definition: general_path_element_t.h:37
do_pgr_edge_disjoint_paths
void do_pgr_edge_disjoint_paths(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *sources, size_t size_source_verticesArr, int64_t *sinks, size_t size_sink_verticesArr, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: edge_disjoint_paths_driver.cpp:66
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44
identifiers.hpp
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139