PGROUTING  3.2
max_flow_driver.cpp File Reference
#include "drivers/max_flow/max_flow_driver.h"
#include <sstream>
#include <vector>
#include <set>
#include "max_flow/pgr_maxflow.hpp"
#include "cpp_common/pgr_assert.h"
#include "cpp_common/pgr_alloc.hpp"
Include dependency graph for max_flow_driver.cpp:

Go to the source code of this file.

Functions

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)
 

Function Documentation

◆ 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 at line 43 of file max_flow_driver.cpp.

54  {
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 }

References pgrouting::graph::PgrFlowGraph::boykov_kolmogorov(), pgrouting::graph::PgrFlowGraph::edmonds_karp(), pgrouting::graph::PgrFlowGraph::get_flow_edges(), pgassert, pgr_alloc(), pgr_free(), pgr_msg(), pgrouting::graph::PgrFlowGraph::push_relabel(), edge::source, edge::target, and AssertFailedException::what().

Referenced by process().

pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
edge::target
int64 target
Definition: trsp.h:44
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_combination_t
Definition: pgr_combination_t.h:43
edge::source
int64 source
Definition: trsp.h:43
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139