PGROUTING  3.2
pgr_minCostMaxFlow.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
5 
6 Copyright (c) 2018 Maoguang Wang
8 
9 ------
10 
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20 
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 
25  ********************************************************************PGR-GNU*/
26 
27 #ifndef INCLUDE_MAX_FLOW_PGR_MINCOSTMAXFLOW_HPP_
28 #define INCLUDE_MAX_FLOW_PGR_MINCOSTMAXFLOW_HPP_
29 #pragma once
30 
31 #include <map>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 #include <set>
36 #include <limits>
37 
39 
40 #include <boost/graph/successive_shortest_path_nonnegative_weights.hpp>
41 #include <boost/graph/find_flow_cost.hpp>
42 
43 
44 
45 #include "c_types/pgr_flow_t.h"
46 #include "c_types/pgr_costFlow_t.h"
47 
48 namespace pgrouting {
49 namespace graph {
50 
52  typedef Traits::vertex_descriptor V;
53  typedef Traits::edge_descriptor E;
54  typedef boost::graph_traits<CostFlowGraph>::vertex_iterator V_it;
55  typedef boost::graph_traits<CostFlowGraph>::edge_iterator E_it;
56 
61 
62  public:
63  double MinCostMaxFlow() {
64  boost::successive_shortest_path_nonnegative_weights(
65  graph,
67  supersink);
68  return boost::find_flow_cost(graph);
69  }
70 
71  PgrCostFlowGraph() = delete;
72 
74  const std::vector<pgr_costFlow_t> &edges,
75  const std::set<int64_t> &source_vertices,
76  const std::set<int64_t> &sink_vertices);
77 
78  int64_t GetMaxFlow() const;
79 
80  std::vector<pgr_flow_t> GetFlowEdges() const;
81 
82  private:
83  V GetBoostVertex(int64_t id) const {
84  return idToV.at(id);
85  }
86 
87  int64_t GetVertexId(V v) const {
88  return vToId.at(v);
89  }
90 
91  int64_t GetEdgeId(E e) const {
92  if (eToId.find(e) == eToId.end())
93  return -1;
94  return eToId.at(e);
95  }
96 
97  void SetSupersource(
98  const std::set<int64_t> &source_vertices);
99  void SetSupersink(
100  const std::set<int64_t> &sink_vertices);
101 
102  E AddEdge(V v, V w, double wei, double cap);
103 
104  void InsertEdges(
105  const std::vector<pgr_costFlow_t> &edges);
106 
107  /*
108  * vertices = {sources} U {sink} U {edges.source} U {edge.target}
109  */
110  template <typename T>
112  const T &edges,
113  const std::set<int64_t> &source_vertices,
114  const std::set<int64_t> &sink_vertices) {
115  std::set<int64_t> vertices(source_vertices);
116  vertices.insert(sink_vertices.begin(), sink_vertices.end());
117 
118  for (const auto e : edges) {
119  vertices.insert(e.source);
120  vertices.insert(e.target);
121  }
122 
123  for (const auto id : vertices) {
124  V v = add_vertex(graph);
125  idToV.insert(std::pair<int64_t, V>(id, v));
126  vToId.insert(std::pair<V, int64_t>(v, id));
127  }
128 
129  SetSupersource(source_vertices);
130  SetSupersink(sink_vertices);
131  }
132 
133  private:
135  std::map<int64_t, V> idToV;
136  std::map<V, int64_t> vToId;
137  std::map<E, int64_t> eToId;
138 
139 
140  /* In multi source flow graphs, a super source is created connected to all sources with "infinite" capacity
141  * The same applies for sinks.
142  * To avoid code repetition, a supersource/sink is used even in the one to one signature.
143  */
146 };
147 
148 } // namespace graph
149 } // namespace pgrouting
150 
151 #endif // INCLUDE_MAX_FLOW_PGR_MINCOSTMAXFLOW_HPP_
pgrouting::graph::PgrCostFlowGraph
Definition: pgr_minCostMaxFlow.hpp:51
pgrouting::graph::PgrCostFlowGraph::idToV
std::map< int64_t, V > idToV
Definition: pgr_minCostMaxFlow.hpp:135
pgrouting::graph::PgrCostFlowGraph::PgrCostFlowGraph
PgrCostFlowGraph()=delete
pgrouting::CostFlowGraph
boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::no_property, boost::property< boost::edge_capacity_t, double, boost::property< boost::edge_residual_capacity_t, double, boost::property< boost::edge_reverse_t, Traits::edge_descriptor, boost::property< boost::edge_weight_t, double > > > > > CostFlowGraph
Definition: pgr_costFlowGraph.hpp:49
pgrouting::graph::PgrCostFlowGraph::residual_capacity
ResidualCapacity residual_capacity
Definition: pgr_minCostMaxFlow.hpp:58
pgrouting::graph::PgrCostFlowGraph::rev
Reversed rev
Definition: pgr_minCostMaxFlow.hpp:59
pgrouting::graph::PgrCostFlowGraph::weight
Weight weight
Definition: pgr_minCostMaxFlow.hpp:60
pgrouting::graph::PgrCostFlowGraph::MinCostMaxFlow
double MinCostMaxFlow()
Definition: pgr_minCostMaxFlow.hpp:63
pgrouting::graph::PgrCostFlowGraph::vToId
std::map< V, int64_t > vToId
Definition: pgr_minCostMaxFlow.hpp:136
pgrouting::graph::PgrCostFlowGraph::SetSupersource
void SetSupersource(const std::set< int64_t > &source_vertices)
Definition: pgr_minCostMaxFlow.cpp:100
pgrouting::graph::PgrCostFlowGraph::E_it
boost::graph_traits< CostFlowGraph >::edge_iterator E_it
Definition: pgr_minCostMaxFlow.hpp:55
pgrouting::graph::PgrCostFlowGraph::GetBoostVertex
V GetBoostVertex(int64_t id) const
Definition: pgr_minCostMaxFlow.hpp:83
pgrouting::graph::PgrCostFlowGraph::GetVertexId
int64_t GetVertexId(V v) const
Definition: pgr_minCostMaxFlow.hpp:87
pgrouting::graph::PgrCostFlowGraph::V_it
boost::graph_traits< CostFlowGraph >::vertex_iterator V_it
Definition: pgr_minCostMaxFlow.hpp:54
pgr_costFlowGraph.hpp
pgrouting::graph::PgrCostFlowGraph::V
Traits::vertex_descriptor V
Definition: pgr_minCostMaxFlow.hpp:52
pgrouting::Capacity
boost::property_map< CostFlowGraph, boost::edge_capacity_t >::type Capacity
Definition: pgr_costFlowGraph.hpp:51
pgrouting::graph::PgrCostFlowGraph::GetMaxFlow
int64_t GetMaxFlow() const
Definition: pgr_minCostMaxFlow.cpp:128
pgrouting::ResidualCapacity
boost::property_map< CostFlowGraph, boost::edge_residual_capacity_t >::type ResidualCapacity
Definition: pgr_costFlowGraph.hpp:53
pgr_flow_t.h
pgrouting::graph::PgrCostFlowGraph::SetSupersink
void SetSupersink(const std::set< int64_t > &sink_vertices)
Definition: pgr_minCostMaxFlow.cpp:114
pgrouting::graph::PgrCostFlowGraph::capacity
Capacity capacity
Definition: pgr_minCostMaxFlow.hpp:57
pgrouting::graph::PgrCostFlowGraph::AddEdge
E AddEdge(V v, V w, double wei, double cap)
Definition: pgr_minCostMaxFlow.cpp:51
pgr_costFlow_t.h
pgrouting::graph::PgrCostFlowGraph::AddVertices
void AddVertices(const T &edges, const std::set< int64_t > &source_vertices, const std::set< int64_t > &sink_vertices)
Definition: pgr_minCostMaxFlow.hpp:111
pgrouting::graph::PgrCostFlowGraph::eToId
std::map< E, int64_t > eToId
Definition: pgr_minCostMaxFlow.hpp:137
pgrouting::Reversed
boost::property_map< CostFlowGraph, boost::edge_reverse_t >::type Reversed
Definition: pgr_costFlowGraph.hpp:57
pgrouting::graph::PgrCostFlowGraph::GetFlowEdges
std::vector< pgr_flow_t > GetFlowEdges() const
Definition: pgr_minCostMaxFlow.cpp:141
pgrouting::graph::PgrCostFlowGraph::E
Traits::edge_descriptor E
Definition: pgr_minCostMaxFlow.hpp:53
pgrouting::graph::PgrCostFlowGraph::GetEdgeId
int64_t GetEdgeId(E e) const
Definition: pgr_minCostMaxFlow.hpp:91
pgrouting::Weight
boost::property_map< CostFlowGraph, boost::edge_weight_t >::type Weight
Definition: pgr_costFlowGraph.hpp:55
pgrouting::graph::PgrCostFlowGraph::supersource
V supersource
Definition: pgr_minCostMaxFlow.hpp:144
pgrouting::graph::PgrCostFlowGraph::supersink
V supersink
Definition: pgr_minCostMaxFlow.hpp:145
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::graph::PgrCostFlowGraph::graph
CostFlowGraph graph
Definition: pgr_minCostMaxFlow.hpp:134
pgrouting::graph::PgrCostFlowGraph::InsertEdges
void InsertEdges(const std::vector< pgr_costFlow_t > &edges)
Definition: pgr_minCostMaxFlow.cpp:63