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

Go to the source code of this file.

Functions

void do_pgr_minCostMaxFlow (pgr_costFlow_t *data_edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *sourceVertices, size_t sizeSourceVerticesArr, int64_t *sinkVertices, size_t sizeSinkVerticesArr, bool only_cost, pgr_flow_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
 

Function Documentation

◆ do_pgr_minCostMaxFlow()

void do_pgr_minCostMaxFlow ( pgr_costFlow_t data_edges,
size_t  total_edges,
pgr_combination_t combinations,
size_t  total_combinations,
int64_t *  sourceVertices,
size_t  sizeSourceVerticesArr,
int64_t *  sinkVertices,
size_t  sizeSinkVerticesArr,
bool  only_cost,
pgr_flow_t **  return_tuples,
size_t *  return_count,
char **  log_msg,
char **  notice_msg,
char **  err_msg 
)

Definition at line 43 of file minCostMaxFlow_driver.cpp.

53  {
54  std::ostringstream log;
55  std::ostringstream err;
56  std::ostringstream notice;
57  try {
58  pgassert(!(*log_msg));
59  pgassert(!(*notice_msg));
60  pgassert(!(*err_msg));
61  pgassert(!(*return_tuples));
62  pgassert(*return_count == 0);
63  pgassert(data_edges);
64  pgassert(total_edges != 0);
65  pgassert((sourceVertices && sinkVertices) || combinations);
66  pgassert((sizeSourceVerticesArr && sizeSinkVerticesArr) || total_combinations);
67 
68  std::vector<pgr_costFlow_t> edges(data_edges, data_edges + total_edges);
69  std::set<int64_t> sources(
70  sourceVertices, sourceVertices + sizeSourceVerticesArr);
71  std::set<int64_t> targets(
72  sinkVertices, sinkVertices + sizeSinkVerticesArr);
73  std::vector< pgr_combination_t > combinations_vector(
74  combinations, combinations + total_combinations);
75 
76  if (!combinations_vector.empty()) {
77  pgassert(sources.empty());
78  pgassert(targets.empty());
79 
80  for (const pgr_combination_t &comb : combinations_vector) {
81  sources.insert(comb.source);
82  targets.insert(comb.target);
83  }
84  }
85 
86  std::set<int64_t> vertices(sources);
87  vertices.insert(targets.begin(), targets.end());
88 
89  if (vertices.size()
90  != (sources.size() + targets.size())) {
91  *err_msg = pgr_msg("A source found as sink");
92  return;
93  }
94 
96  edges, sources, targets);
97 
98  double min_cost;
99  min_cost = digraph.MinCostMaxFlow();
100 
101  std::vector<pgr_flow_t> flow_edges;
102 
103  if (only_cost) {
105  edge.edge = -1;
106  edge.source = -1;
107  edge.target = -1;
108  edge.flow = -1;
109  edge.residual_capacity = -1;
110  edge.cost = min_cost;
111  edge.agg_cost = min_cost;
112  flow_edges.push_back(edge);
113  } else {
114  flow_edges = digraph.GetFlowEdges();
115  }
116 
117  (*return_tuples) = pgr_alloc(flow_edges.size(), (*return_tuples));
118  for (size_t i = 0; i < flow_edges.size(); i++) {
119  (*return_tuples)[i] = flow_edges[i];
120  }
121  *return_count = flow_edges.size();
122 
123  pgassert(*err_msg == NULL);
124  *log_msg = log.str().empty()?
125  *log_msg :
126  pgr_msg(log.str().c_str());
127  *notice_msg = notice.str().empty()?
128  *notice_msg :
129  pgr_msg(notice.str().c_str());
130  } catch (AssertFailedException &except) {
131  (*return_tuples) = pgr_free(*return_tuples);
132  (*return_count) = 0;
133  err << except.what();
134  *err_msg = pgr_msg(err.str().c_str());
135  *log_msg = pgr_msg(log.str().c_str());
136  } catch (std::exception &except) {
137  (*return_tuples) = pgr_free(*return_tuples);
138  (*return_count) = 0;
139  err << except.what();
140  *err_msg = pgr_msg(err.str().c_str());
141  *log_msg = pgr_msg(log.str().c_str());
142  } catch(...) {
143  (*return_tuples) = pgr_free(*return_tuples);
144  (*return_count) = 0;
145  err << "Caught unknown exception!";
146  *err_msg = pgr_msg(err.str().c_str());
147  *log_msg = pgr_msg(log.str().c_str());
148  }
149 }

References edge::cost, pgrouting::graph::PgrCostFlowGraph::GetFlowEdges(), pgrouting::graph::PgrCostFlowGraph::MinCostMaxFlow(), pgassert, pgr_alloc(), pgr_free(), pgr_msg(), edge::source, edge::target, and AssertFailedException::what().

Referenced by process().

pgrouting::graph::PgrCostFlowGraph
Definition: pgr_minCostMaxFlow.hpp:51
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
edge::cost
float8 cost
Definition: trsp.h:45
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
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