PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
max_flow_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: max_flow_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 
30 #include "./pgr_maxflow.hpp"
31 
32 #include <sstream>
33 #include <vector>
34 #include <set>
35 
36 #include "./max_flow_driver.h"
37 
38 #include "../../common/src/pgr_assert.h"
39 #include "../../common/src/pgr_alloc.hpp"
40 #include "./../../common/src/pgr_types.h"
41 
42 
43 void
45  pgr_edge_t *data_edges,
46  size_t total_tuples,
47  int64_t *source_vertices,
48  size_t size_source_verticesArr,
49  int64_t *sink_vertices,
50  size_t size_sink_verticesArr,
51  char *algorithm,
52  bool only_flow,
53  pgr_flow_t **return_tuples,
54  size_t *return_count,
55  char** log_msg,
56  char** notice_msg,
57  char **err_msg) {
58  std::ostringstream log;
59  std::ostringstream notice;
60  std::ostringstream err;
61 
62  try {
63  pgassert(data_edges);
64  pgassert(source_vertices);
65  pgassert(sink_vertices);
66 
68  std::set<int64_t> set_source_vertices;
69  std::set<int64_t> set_sink_vertices;
70  for (size_t i = 0; i < size_source_verticesArr; ++i) {
71  set_source_vertices.insert(source_vertices[i]);
72  }
73  for (size_t i = 0; i < size_sink_verticesArr; ++i) {
74  set_sink_vertices.insert(sink_vertices[i]);
75  }
76  std::set<int64_t> vertices(set_source_vertices);
77  vertices.insert(set_sink_vertices.begin(), set_sink_vertices.end());
78  if (vertices.size()
79  != (set_source_vertices.size() + set_sink_vertices.size())) {
80  *err_msg = pgr_msg("A source found as sink");
81  // TODO(vicky) return as hint the sources that are also sinks
82  return;
83  }
84 
85 
86 
87  G.create_flow_graph(data_edges, total_tuples, set_source_vertices,
88  set_sink_vertices, algorithm);
89 
90  int64_t max_flow;
91  if (strcmp(algorithm, "push_relabel") == 0) {
92  max_flow = G.push_relabel();
93  } else if (strcmp(algorithm, "edmonds_karp") == 0) {
94  max_flow = G.edmonds_karp();
95  } else if (strcmp(algorithm, "boykov_kolmogorov") == 0) {
96  max_flow = G.boykov_kolmogorov();
97  } else {
98  log << "Unspecified algorithm!\n";
99  *err_msg = pgr_msg(log.str().c_str());
100  (*return_tuples) = NULL;
101  (*return_count) = 0;
102  return;
103  }
104 
105 
106  std::vector<pgr_flow_t> flow_edges;
107 
108  if (only_flow) {
110  edge.edge = -1;
111  edge.source = -1;
112  edge.target = -1;
113  edge.flow = max_flow;
114  edge.residual_capacity = -1;
115  flow_edges.push_back(edge);
116  } else {
117  G.get_flow_edges(flow_edges);
118  }
119  (*return_tuples) = pgr_alloc(flow_edges.size(), (*return_tuples));
120  for (size_t i = 0; i < flow_edges.size(); ++i) {
121  (*return_tuples)[i] = flow_edges[i];
122  }
123  *return_count = flow_edges.size();
124 
125 
126  *log_msg = log.str().empty()?
127  *log_msg :
128  pgr_msg(log.str().c_str());
129  *notice_msg = notice.str().empty()?
130  *notice_msg :
131  pgr_msg(notice.str().c_str());
132  } catch (AssertFailedException &except) {
133  (*return_tuples) = pgr_free(*return_tuples);
134  (*return_count) = 0;
135  err << except.what();
136  *err_msg = pgr_msg(err.str().c_str());
137  *log_msg = pgr_msg(log.str().c_str());
138  } catch (std::exception &except) {
139  (*return_tuples) = pgr_free(*return_tuples);
140  (*return_count) = 0;
141  err << except.what();
142  *err_msg = pgr_msg(err.str().c_str());
143  *log_msg = pgr_msg(log.str().c_str());
144  } catch(...) {
145  (*return_tuples) = pgr_free(*return_tuples);
146  (*return_count) = 0;
147  err << "Caught unknown exception!";
148  *err_msg = pgr_msg(err.str().c_str());
149  *log_msg = pgr_msg(log.str().c_str());
150  }
151 }
152 
void create_flow_graph(pgr_edge_t *data_edges, size_t total_tuples, const std::set< int64_t > &source_vertices, const std::set< int64_t > &sink_vertices, char *algorithm)
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
int64_t source
Definition: pgr_types.h:151
void do_pgr_max_flow(pgr_edge_t *data_edges, size_t total_tuples, int64_t *source_vertices, size_t size_source_verticesArr, int64_t *sink_vertices, size_t size_sink_verticesArr, char *algorithm, bool only_flow, pgr_flow_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
void get_flow_edges(std::vector< pgr_flow_t > &flow_edges)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:73
int64_t edge
Definition: pgr_types.h:150
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
int64_t flow
Definition: pgr_types.h:153
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:62
int64_t target
Definition: pgr_types.h:152
virtual const char * what() const
Definition: pgr_assert.cpp:53
int64_t residual_capacity
Definition: pgr_types.h:154
char * err_msg
Definition: BDATester.cpp:50