PGROUTING  2.5
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 "./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 
51  std::set<int64_t> set_source_vertices;
52  std::set<int64_t> set_sink_vertices;
53  set_source_vertices.insert(source);
54  set_sink_vertices.insert(target);
56  edges,
57  set_source_vertices,
58  set_sink_vertices, directed);
59 
60  /*
61  * boykov_kolmogorov is only for directed graphs
62  */
63  return G.edge_disjoint_paths();
64 }
65 
66 void
68  pgr_edge_t *data_edges,
69  size_t total_edges,
70  int64_t *sources,
71  size_t size_source_verticesArr,
72  int64_t *sinks,
73  size_t size_sink_verticesArr,
74  bool directed,
75  General_path_element_t **return_tuples,
76  size_t *return_count,
77  char** log_msg,
78  char** notice_msg,
79  char **err_msg) {
80  std::ostringstream log;
81  std::ostringstream notice;
82  std::ostringstream err;
83  try {
84  std::set<int64_t> set_source_vertices(
85  sources, sources + size_source_verticesArr);
86  std::set<int64_t> set_sink_vertices(
87  sinks, sinks + size_sink_verticesArr);
88  std::vector<pgr_edge_t> edges(
89  data_edges, data_edges + total_edges);
90 
91 
92  std::vector<General_path_element_t> paths;
93  for (const auto &s : set_source_vertices) {
94  for (const auto &t : set_sink_vertices) {
95  auto path = single_execution(
96  edges,
97  s,
98  t,
99  directed);
100  paths.insert(paths.end(), path.begin(), path.end());
101  }
102  }
103 
104  if (paths.empty()) {
105  *return_tuples = nullptr;
106  *return_count = 0;
107  return;
108  }
109 
110  /*
111  * Initializing the cost
112  */
113  for (auto &r : paths) {
114  r.agg_cost = r.cost = 0;
115  }
116 
117  /*
118  * Calculating the cost
119  */
120  auto found = paths.size();
121  for (const auto &e : edges) {
122  for (auto &r : paths) {
123  if (r.edge == e.id) {
124  r.cost = (r.node == e.source) ?
125  e.cost : e.reverse_cost;
126  --found;
127  }
128  }
129  if (found == 0) break;
130  }
131 
132  /*
133  * Calculating the agg_cost
134  */
135  auto prev = paths[0];
136  for (auto &r : paths) {
137  if (r.seq == 1) {
138  r.agg_cost = 0;
139  } else {
140  r.agg_cost = prev.agg_cost + prev.cost;
141  }
142  prev = r;
143  }
144 
145  /*
146  * Numbering the paths
147  */
148  int path_id(0);
149  for (auto &r : paths) {
150  r.start_id = path_id;
151  if (r.edge == -1) ++path_id;
152  }
153 
154 
155  (*return_tuples) = pgr_alloc(paths.size(), (*return_tuples));
156  for (size_t i = 0; i < paths.size(); ++i) {
157  (*return_tuples)[i] = paths[i];
158  }
159  *return_count = paths.size();
160 
161 
162  *log_msg = log.str().empty()?
163  *log_msg :
164  pgr_msg(log.str().c_str());
165  *notice_msg = notice.str().empty()?
166  *notice_msg :
167  pgr_msg(notice.str().c_str());
168  } catch (AssertFailedException &except) {
169  (*return_tuples) = pgr_free(*return_tuples);
170  (*return_count) = 0;
171  err << except.what();
172  *err_msg = pgr_msg(err.str().c_str());
173  *log_msg = pgr_msg(log.str().c_str());
174  } catch (std::exception &except) {
175  (*return_tuples) = pgr_free(*return_tuples);
176  (*return_count) = 0;
177  err << except.what();
178  *err_msg = pgr_msg(err.str().c_str());
179  *log_msg = pgr_msg(log.str().c_str());
180  } catch(...) {
181  (*return_tuples) = pgr_free(*return_tuples);
182  (*return_count) = 0;
183  err << "Caught unknown exception!";
184  *err_msg = pgr_msg(err.str().c_str());
185  *log_msg = pgr_msg(log.str().c_str());
186  }
187 }
static edge_t edges[22573]
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:29
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