PGROUTING  2.5
 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 "./max_flow_driver.h"
31 
32 #include <sstream>
33 #include <vector>
34 #include <set>
35 
36 #include "./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  int64_t *source_vertices, size_t size_source_verticesArr,
46  int64_t *sink_vertices, size_t size_sink_verticesArr,
47  int algorithm,
48  bool only_flow,
49 
50  pgr_flow_t **return_tuples, size_t *return_count,
51  char** log_msg,
52  char** notice_msg,
53  char **err_msg) {
54  std::ostringstream log;
55  std::ostringstream notice;
56  std::ostringstream err;
57 
58  try {
59  pgassert(data_edges);
60  pgassert(source_vertices);
61  pgassert(sink_vertices);
62 
63  std::vector<pgr_edge_t> edges(data_edges, data_edges + total_edges);
64  std::set<int64_t> sources(
65  source_vertices, source_vertices + size_source_verticesArr);
66  std::set<int64_t> targets(
67  sink_vertices, sink_vertices + size_sink_verticesArr);
68 
69  std::set<int64_t> vertices(sources);
70  vertices.insert(targets.begin(), targets.end());
71 
72  if (vertices.size()
73  != (sources.size() + targets.size())) {
74  *err_msg = pgr_msg("A source found as sink");
75  // TODO(vicky) return as hint the sources that are also sinks
76  return;
77  }
78 
79 
80 
82  edges, sources, targets, algorithm);
83  // digraph.create_flow_graph(edges, sources, targets, algorithm);
84 
85  int64_t max_flow;
86  if (algorithm == 1) {
87  max_flow = digraph.push_relabel();
88  } else if (algorithm == 3) {
89  max_flow = digraph.edmonds_karp();
90  } else if (algorithm == 2) {
91  max_flow = digraph.boykov_kolmogorov();
92  } else {
93  log << "Unspecified algorithm!\n";
94  *err_msg = pgr_msg(log.str().c_str());
95  (*return_tuples) = NULL;
96  (*return_count) = 0;
97  return;
98  }
99 
100 
101  std::vector<pgr_flow_t> flow_edges;
102 
103  if (only_flow) {
105  edge.edge = -1;
106  edge.source = -1;
107  edge.target = -1;
108  edge.flow = max_flow;
109  edge.residual_capacity = -1;
110  flow_edges.push_back(edge);
111  } else {
112  flow_edges = digraph.get_flow_edges();
113  }
114  (*return_tuples) = pgr_alloc(flow_edges.size(), (*return_tuples));
115  for (size_t i = 0; i < flow_edges.size(); ++i) {
116  (*return_tuples)[i] = flow_edges[i];
117  }
118  *return_count = flow_edges.size();
119 
120 
121  *log_msg = log.str().empty()?
122  *log_msg :
123  pgr_msg(log.str().c_str());
124  *notice_msg = notice.str().empty()?
125  *notice_msg :
126  pgr_msg(notice.str().c_str());
127  } catch (AssertFailedException &except) {
128  (*return_tuples) = pgr_free(*return_tuples);
129  (*return_count) = 0;
130  err << except.what();
131  *err_msg = pgr_msg(err.str().c_str());
132  *log_msg = pgr_msg(log.str().c_str());
133  } catch (std::exception &except) {
134  (*return_tuples) = pgr_free(*return_tuples);
135  (*return_count) = 0;
136  err << except.what();
137  *err_msg = pgr_msg(err.str().c_str());
138  *log_msg = pgr_msg(log.str().c_str());
139  } catch(...) {
140  (*return_tuples) = pgr_free(*return_tuples);
141  (*return_count) = 0;
142  err << "Caught unknown exception!";
143  *err_msg = pgr_msg(err.str().c_str());
144  *log_msg = pgr_msg(log.str().c_str());
145  }
146 }
147 
static edge_t edges[22573]
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
Definition: trsp.h:31
int64_t source
Definition: pgr_flow_t.h:60
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:75
int64_t edge
Definition: pgr_flow_t.h:59
void do_pgr_max_flow(pgr_edge_t *data_edges, size_t total_edges, 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)
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
int64_t flow
Definition: pgr_flow_t.h:62
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:64
int64_t target
Definition: pgr_flow_t.h:61
std::vector< pgr_flow_t > get_flow_edges() const
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
int64_t residual_capacity
Definition: pgr_flow_t.h:63