PGROUTING  3.2
minCostMaxFlow_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: minCostMaxFlow_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2018 Maoguang Wang
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 <deque>
34 #include <vector>
35 #include <set>
36 
38 
39 #include "cpp_common/pgr_alloc.hpp"
40 #include "cpp_common/pgr_assert.h"
41 
42 void
44  pgr_costFlow_t *data_edges, size_t total_edges,
45  pgr_combination_t *combinations, size_t total_combinations,
46  int64_t *sourceVertices, size_t sizeSourceVerticesArr,
47  int64_t *sinkVertices, size_t sizeSinkVerticesArr,
48  bool only_cost,
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 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 }
150 
minCostMaxFlow_driver.h
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
pgrouting::graph::PgrCostFlowGraph::MinCostMaxFlow
double MinCostMaxFlow()
Definition: pgr_minCostMaxFlow.hpp:63
edge
Definition: trsp.h:41
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgr_alloc.hpp
pgr_minCostMaxFlow.hpp
pgr_combination_t
Definition: pgr_combination_t.h:43
edge::source
int64 source
Definition: trsp.h:43
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::graph::PgrCostFlowGraph::GetFlowEdges
std::vector< pgr_flow_t > GetFlowEdges() const
Definition: pgr_minCostMaxFlow.cpp:141
pgr_costFlow_t
Definition: pgr_costFlow_t.h:38
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
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: minCostMaxFlow_driver.cpp:43
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139