PGROUTING  2.6
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
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2016 Andrea Nardelli
10 Mail: nrd.nardelli@gmail.com
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  int64_t *sources,
70  size_t size_source_verticesArr,
71  int64_t *sinks,
72  size_t size_sink_verticesArr,
73  bool directed,
74  General_path_element_t **return_tuples,
75  size_t *return_count,
76  char** log_msg,
77  char** notice_msg,
78  char **err_msg) {
79  std::ostringstream log;
80  std::ostringstream notice;
81  std::ostringstream err;
82  try {
83  std::set<int64_t> set_source_vertices(
84  sources, sources + size_source_verticesArr);
85  std::set<int64_t> set_sink_vertices(
86  sinks, sinks + size_sink_verticesArr);
87  std::vector<pgr_edge_t> edges(
88  data_edges, data_edges + total_edges);
89 
90 
91  std::vector<General_path_element_t> paths;
92  for (const auto &s : set_source_vertices) {
93  for (const auto &t : set_sink_vertices) {
94  auto path = single_execution(
95  edges,
96  s,
97  t,
98  directed);
99  paths.insert(paths.end(), path.begin(), path.end());
100  }
101  }
102 
103  if (paths.empty()) {
104  *return_tuples = nullptr;
105  *return_count = 0;
106  return;
107  }
108 
109  /*
110  * Initializing the cost
111  */
112  for (auto &r : paths) {
113  r.agg_cost = r.cost = 0;
114  }
115 
116  /*
117  * Calculating the cost
118  */
119  auto found = paths.size();
120  for (const auto &e : edges) {
121  for (auto &r : paths) {
122  if (r.edge == e.id) {
123  r.cost = (r.node == e.source) ?
124  e.cost : e.reverse_cost;
125  --found;
126  }
127  }
128  if (found == 0) break;
129  }
130 
131  /*
132  * Calculating the agg_cost
133  */
134  auto prev = paths[0];
135  for (auto &r : paths) {
136  if (r.seq == 1) {
137  r.agg_cost = 0;
138  } else {
139  r.agg_cost = prev.agg_cost + prev.cost;
140  }
141  prev = r;
142  }
143 
144  /*
145  * Numbering the paths
146  */
147  int path_id(0);
148  for (auto &r : paths) {
149  r.start_id = path_id;
150  if (r.edge == -1) ++path_id;
151  }
152 
153 
154  (*return_tuples) = pgr_alloc(paths.size(), (*return_tuples));
155  for (size_t i = 0; i < paths.size(); ++i) {
156  (*return_tuples)[i] = paths[i];
157  }
158  *return_count = paths.size();
159 
160 
161  *log_msg = log.str().empty()?
162  *log_msg :
163  pgr_msg(log.str().c_str());
164  *notice_msg = notice.str().empty()?
165  *notice_msg :
166  pgr_msg(notice.str().c_str());
167  } catch (AssertFailedException &except) {
168  (*return_tuples) = pgr_free(*return_tuples);
169  (*return_count) = 0;
170  err << except.what();
171  *err_msg = pgr_msg(err.str().c_str());
172  *log_msg = pgr_msg(log.str().c_str());
173  } catch (std::exception &except) {
174  (*return_tuples) = pgr_free(*return_tuples);
175  (*return_count) = 0;
176  err << except.what();
177  *err_msg = pgr_msg(err.str().c_str());
178  *log_msg = pgr_msg(log.str().c_str());
179  } catch(...) {
180  (*return_tuples) = pgr_free(*return_tuples);
181  (*return_count) = 0;
182  err << "Caught unknown exception!";
183  *err_msg = pgr_msg(err.str().c_str());
184  *log_msg = pgr_msg(log.str().c_str());
185  }
186 }
std::vector< General_path_element_t > edge_disjoint_paths()
Definition: pgr_maxflow.hpp:95
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
static std::vector< General_path_element_t > single_execution(std::vector< pgr_edge_t > edges, int64_t source, int64_t target, bool directed)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:79
void do_pgr_edge_disjoint_paths(pgr_edge_t *data_edges, size_t total_edges, 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)
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:68
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53