PGROUTING  3.2
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
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 
38 #include "cpp_common/pgr_assert.h"
39 #include "cpp_common/pgr_alloc.hpp"
40 
41 
42 void
44  pgr_edge_t *data_edges, size_t total_edges,
45  pgr_combination_t *combinations, size_t total_combinations,
46  int64_t *source_vertices, size_t size_source_verticesArr,
47  int64_t *sink_vertices, size_t size_sink_verticesArr,
48  int algorithm,
49  bool only_flow,
50 
51  pgr_flow_t **return_tuples, size_t *return_count,
52  char** log_msg,
53  char** notice_msg,
54  char **err_msg) {
55  std::ostringstream log;
56  std::ostringstream notice;
57  std::ostringstream err;
58 
59  try {
60  pgassert(!(*log_msg));
61  pgassert(!(*notice_msg));
62  pgassert(!(*err_msg));
63  pgassert(!(*return_tuples));
64  pgassert(*return_count == 0);
65  pgassert(data_edges);
66  pgassert(total_edges != 0);
67  pgassert((source_vertices && sink_vertices) || combinations);
68  pgassert((size_source_verticesArr && size_sink_verticesArr) || total_combinations);
69 
70  std::vector<pgr_edge_t> edges(data_edges, data_edges + total_edges);
71  std::set<int64_t> sources(
72  source_vertices, source_vertices + size_source_verticesArr);
73  std::set<int64_t> targets(
74  sink_vertices, sink_vertices + size_sink_verticesArr);
75  std::vector< pgr_combination_t > combinations_vector(
76  combinations, combinations + total_combinations);
77 
78  if (!combinations_vector.empty()) {
79  pgassert(sources.empty());
80  pgassert(targets.empty());
81 
82  for (const pgr_combination_t &comb : combinations_vector) {
83  sources.insert(comb.source);
84  targets.insert(comb.target);
85  }
86  }
87 
88  std::set<int64_t> vertices(sources);
89  vertices.insert(targets.begin(), targets.end());
90 
91  if (vertices.size()
92  != (sources.size() + targets.size())) {
93  *err_msg = pgr_msg("A source found as sink");
94  // TODO(vicky) return as hint the sources that are also sinks
95  return;
96  }
97 
98 
99 
101  edges, sources, targets, algorithm);
102  // digraph.create_flow_graph(edges, sources, targets, algorithm);
103 
104  int64_t max_flow;
105  if (algorithm == 1) {
106  max_flow = digraph.push_relabel();
107  } else if (algorithm == 3) {
108  max_flow = digraph.edmonds_karp();
109  } else if (algorithm == 2) {
110  max_flow = digraph.boykov_kolmogorov();
111  } else {
112  log << "Unspecified algorithm!\n";
113  *err_msg = pgr_msg(log.str().c_str());
114  (*return_tuples) = NULL;
115  (*return_count) = 0;
116  return;
117  }
118 
119 
120  std::vector<pgr_flow_t> flow_edges;
121 
122  if (only_flow) {
124  edge.edge = -1;
125  edge.source = -1;
126  edge.target = -1;
127  edge.flow = max_flow;
128  edge.residual_capacity = -1;
129  flow_edges.push_back(edge);
130  } else {
131  flow_edges = digraph.get_flow_edges();
132  }
133  (*return_tuples) = pgr_alloc(flow_edges.size(), (*return_tuples));
134  for (size_t i = 0; i < flow_edges.size(); ++i) {
135  (*return_tuples)[i] = flow_edges[i];
136  }
137  *return_count = flow_edges.size();
138 
139 
140  *log_msg = log.str().empty()?
141  *log_msg :
142  pgr_msg(log.str().c_str());
143  *notice_msg = notice.str().empty()?
144  *notice_msg :
145  pgr_msg(notice.str().c_str());
146  } catch (AssertFailedException &except) {
147  (*return_tuples) = pgr_free(*return_tuples);
148  (*return_count) = 0;
149  err << except.what();
150  *err_msg = pgr_msg(err.str().c_str());
151  *log_msg = pgr_msg(log.str().c_str());
152  } catch (std::exception &except) {
153  (*return_tuples) = pgr_free(*return_tuples);
154  (*return_count) = 0;
155  err << except.what();
156  *err_msg = pgr_msg(err.str().c_str());
157  *log_msg = pgr_msg(log.str().c_str());
158  } catch(...) {
159  (*return_tuples) = pgr_free(*return_tuples);
160  (*return_count) = 0;
161  err << "Caught unknown exception!";
162  *err_msg = pgr_msg(err.str().c_str());
163  *log_msg = pgr_msg(log.str().c_str());
164  }
165 }
166 
pgr_maxflow.hpp
max_flow_driver.h
pgrouting::graph::PgrFlowGraph::boykov_kolmogorov
int64_t boykov_kolmogorov()
Definition: pgr_maxflow.hpp:90
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgrouting::graph::PgrFlowGraph::get_flow_edges
std::vector< pgr_flow_t > get_flow_edges() const
Definition: pgr_maxflow.cpp:204
edge::target
int64 target
Definition: trsp.h:44
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
pgr_flow_t
Definition: pgr_flow_t.h:37
edge
Definition: trsp.h:41
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
edge::source
int64 source
Definition: trsp.h:43
pgrouting::graph::PgrFlowGraph::push_relabel
int64_t push_relabel()
Definition: pgr_maxflow.hpp:72
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::graph::PgrFlowGraph::edmonds_karp
int64_t edmonds_karp()
Definition: pgr_maxflow.hpp:81
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
do_pgr_max_flow
void do_pgr_max_flow(pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *source_vertices, size_t size_source_verticesArr, int64_t *sink_vertices, size_t size_sink_verticesArr, int algorithm, bool only_flow, pgr_flow_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: max_flow_driver.cpp:43
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139