PGROUTING  3.2
pgrouting::graph::PgrCostFlowGraph Class Reference

#include "pgr_minCostMaxFlow.hpp"

Collaboration diagram for pgrouting::graph::PgrCostFlowGraph:

Public Member Functions

 PgrCostFlowGraph ()=delete
 
 PgrCostFlowGraph (const std::vector< pgr_costFlow_t > &edges, const std::set< int64_t > &source_vertices, const std::set< int64_t > &sink_vertices)
 
std::vector< pgr_flow_tGetFlowEdges () const
 
int64_t GetMaxFlow () const
 
double MinCostMaxFlow ()
 

Private Types

typedef Traits::edge_descriptor E
 
typedef boost::graph_traits< CostFlowGraph >::edge_iterator E_it
 
typedef Traits::vertex_descriptor V
 
typedef boost::graph_traits< CostFlowGraph >::vertex_iterator V_it
 

Private Member Functions

E AddEdge (V v, V w, double wei, double cap)
 
template<typename T >
void AddVertices (const T &edges, const std::set< int64_t > &source_vertices, const std::set< int64_t > &sink_vertices)
 
V GetBoostVertex (int64_t id) const
 
int64_t GetEdgeId (E e) const
 
int64_t GetVertexId (V v) const
 
void InsertEdges (const std::vector< pgr_costFlow_t > &edges)
 
void SetSupersink (const std::set< int64_t > &sink_vertices)
 
void SetSupersource (const std::set< int64_t > &source_vertices)
 

Private Attributes

Capacity capacity
 
std::map< E, int64_t > eToId
 
CostFlowGraph graph
 
std::map< int64_t, VidToV
 
ResidualCapacity residual_capacity
 
Reversed rev
 
V supersink
 
V supersource
 
std::map< V, int64_t > vToId
 
Weight weight
 

Detailed Description

Definition at line 51 of file pgr_minCostMaxFlow.hpp.

Member Typedef Documentation

◆ E

typedef Traits::edge_descriptor pgrouting::graph::PgrCostFlowGraph::E
private

Definition at line 53 of file pgr_minCostMaxFlow.hpp.

◆ E_it

typedef boost::graph_traits<CostFlowGraph>::edge_iterator pgrouting::graph::PgrCostFlowGraph::E_it
private

Definition at line 55 of file pgr_minCostMaxFlow.hpp.

◆ V

typedef Traits::vertex_descriptor pgrouting::graph::PgrCostFlowGraph::V
private

Definition at line 52 of file pgr_minCostMaxFlow.hpp.

◆ V_it

typedef boost::graph_traits<CostFlowGraph>::vertex_iterator pgrouting::graph::PgrCostFlowGraph::V_it
private

Definition at line 54 of file pgr_minCostMaxFlow.hpp.

Constructor & Destructor Documentation

◆ PgrCostFlowGraph() [1/2]

pgrouting::graph::PgrCostFlowGraph::PgrCostFlowGraph ( )
delete

◆ PgrCostFlowGraph() [2/2]

pgrouting::graph::PgrCostFlowGraph::PgrCostFlowGraph ( const std::vector< pgr_costFlow_t > &  edges,
const std::set< int64_t > &  source_vertices,
const std::set< int64_t > &  sink_vertices 
)

Definition at line 37 of file pgr_minCostMaxFlow.cpp.

40  {
41  AddVertices(edges, sourceVertices, sinkVertices);
42 
43  capacity = get(boost::edge_capacity, graph);
44  weight = get(boost::edge_weight, graph);
45  rev = get(boost::edge_reverse, graph);
46  residual_capacity = get(boost::edge_residual_capacity, graph);
47 
48  InsertEdges(edges);
49 }

References AddVertices(), capacity, graph, InsertEdges(), residual_capacity, rev, and weight.

Member Function Documentation

◆ AddEdge()

PgrCostFlowGraph::E pgrouting::graph::PgrCostFlowGraph::AddEdge ( PgrCostFlowGraph::V  v,
PgrCostFlowGraph::V  w,
double  wei,
double  cap 
)
private

Definition at line 51 of file pgr_minCostMaxFlow.cpp.

53  {
54  bool b;
56  boost::tie(e, b) = boost::add_edge(vertex(v, graph),
57  vertex(w, graph), graph);
58  capacity[e] = cap;
59  weight[e] = wei;
60  return e;
61 }

References capacity, graph, and weight.

Referenced by InsertEdges(), SetSupersink(), and SetSupersource().

◆ AddVertices()

template<typename T >
void pgrouting::graph::PgrCostFlowGraph::AddVertices ( const T &  edges,
const std::set< int64_t > &  source_vertices,
const std::set< int64_t > &  sink_vertices 
)
inlineprivate

Definition at line 111 of file pgr_minCostMaxFlow.hpp.

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

References graph, idToV, SetSupersink(), SetSupersource(), and vToId.

Referenced by PgrCostFlowGraph().

◆ GetBoostVertex()

V pgrouting::graph::PgrCostFlowGraph::GetBoostVertex ( int64_t  id) const
inlineprivate

Definition at line 83 of file pgr_minCostMaxFlow.hpp.

83  {
84  return idToV.at(id);
85  }

References idToV.

Referenced by InsertEdges(), SetSupersink(), and SetSupersource().

◆ GetEdgeId()

int64_t pgrouting::graph::PgrCostFlowGraph::GetEdgeId ( E  e) const
inlineprivate

Definition at line 91 of file pgr_minCostMaxFlow.hpp.

91  {
92  if (eToId.find(e) == eToId.end())
93  return -1;
94  return eToId.at(e);
95  }

References eToId.

Referenced by GetFlowEdges().

◆ GetFlowEdges()

std::vector< pgr_flow_t > pgrouting::graph::PgrCostFlowGraph::GetFlowEdges ( ) const

Definition at line 141 of file pgr_minCostMaxFlow.cpp.

141  {
142  std::vector<pgr_flow_t> flowEdges;
143  E_it e, eEnd;
144  for (boost::tie(e, eEnd) = boost::edges(graph); e != eEnd; ++e) {
145  if (((capacity[*e] - residual_capacity[*e]) > 0) &&
146  ((*e).m_source != supersource) &&
147  ((*e).m_target != supersink)) {
149  edge.edge = GetEdgeId(*e);
150  edge.source = GetVertexId((*e).m_source);
151  edge.target = GetVertexId((*e).m_target);
152  edge.flow =
153  static_cast<int64_t>(capacity[*e] - residual_capacity[*e]);
154  edge.residual_capacity =
155  static_cast<int64_t>(residual_capacity[*e]);
156  edge.cost = weight[*e] * static_cast<double>(edge.flow);
157  if (flowEdges.size() == 0)
158  edge.agg_cost = edge.cost;
159  else
160  edge.agg_cost = (flowEdges.back()).agg_cost + edge.cost;
161  flowEdges.push_back(edge);
162  }
163  }
164  return flowEdges;
165 }

References capacity, edge::cost, GetEdgeId(), GetVertexId(), graph, residual_capacity, edge::source, supersink, supersource, edge::target, and weight.

Referenced by do_pgr_minCostMaxFlow(), and pgrouting::graph::PgrDirectedChPPGraph::setPathEdges().

◆ GetMaxFlow()

int64_t pgrouting::graph::PgrCostFlowGraph::GetMaxFlow ( ) const

Definition at line 128 of file pgr_minCostMaxFlow.cpp.

128  {
129  int64_t maxFlow = 0;
130  E_it e, eEnd;
131  for (boost::tie(e, eEnd) = boost::edges(graph); e != eEnd; ++e) {
132  if (((capacity[*e] - residual_capacity[*e]) > 0) &&
133  ((*e).m_source == supersource))
134  maxFlow +=
135  static_cast<int64_t>(capacity[*e] - residual_capacity[*e]);
136  }
137  return maxFlow;
138 }

References capacity, graph, residual_capacity, and supersource.

Referenced by pgrouting::graph::PgrDirectedChPPGraph::PgrDirectedChPPGraph(), and pgrouting::graph::PgrDirectedChPPGraph::setPathEdges().

◆ GetVertexId()

int64_t pgrouting::graph::PgrCostFlowGraph::GetVertexId ( V  v) const
inlineprivate

Definition at line 87 of file pgr_minCostMaxFlow.hpp.

87  {
88  return vToId.at(v);
89  }

References vToId.

Referenced by GetFlowEdges().

◆ InsertEdges()

void pgrouting::graph::PgrCostFlowGraph::InsertEdges ( const std::vector< pgr_costFlow_t > &  edges)
private

Definition at line 63 of file pgr_minCostMaxFlow.cpp.

64  {
65  for (const auto edge : edges) {
66  PgrCostFlowGraph::E e1, e1Rev, e2, e2Rev;
69 
70  if (edge.capacity > 0) {
71  e1 = AddEdge(v1, v2, edge.cost,
72  static_cast<double>(edge.capacity));
73  e1Rev = AddEdge(v2, v1, -edge.cost, 0);
74 
75  eToId.insert(std::pair<PgrCostFlowGraph::E,
76  int64_t>(e1, edge.edge_id));
77  eToId.insert(std::pair<PgrCostFlowGraph::E,
78  int64_t>(e1Rev, edge.edge_id));
79 
80  rev[e1] = e1Rev;
81  rev[e1Rev] = e1;
82  }
83 
84  if (edge.reverse_capacity > 0) {
85  e2 = AddEdge(v2, v1, edge.reverse_cost,
86  static_cast<double>(edge.reverse_capacity));
87  e2Rev = AddEdge(v1, v2, -edge.reverse_cost, 0);
88 
89  eToId.insert(std::pair<PgrCostFlowGraph::E,
90  int64_t>(e2, edge.edge_id));
91  eToId.insert(std::pair<PgrCostFlowGraph::E,
92  int64_t>(e2Rev, edge.edge_id));
93 
94  rev[e2] = e2Rev;
95  rev[e2Rev] = e2;
96  }
97  }
98 }

References AddEdge(), edge::cost, eToId, GetBoostVertex(), rev, edge::reverse_cost, edge::source, and edge::target.

Referenced by PgrCostFlowGraph().

◆ MinCostMaxFlow()

double pgrouting::graph::PgrCostFlowGraph::MinCostMaxFlow ( )
inline

Definition at line 63 of file pgr_minCostMaxFlow.hpp.

63  {
64  boost::successive_shortest_path_nonnegative_weights(
65  graph,
67  supersink);
68  return boost::find_flow_cost(graph);
69  }

References graph, supersink, and supersource.

Referenced by do_pgr_minCostMaxFlow(), pgrouting::graph::PgrDirectedChPPGraph::PgrDirectedChPPGraph(), and pgrouting::graph::PgrDirectedChPPGraph::setPathEdges().

◆ SetSupersink()

void pgrouting::graph::PgrCostFlowGraph::SetSupersink ( const std::set< int64_t > &  sink_vertices)
private

Definition at line 114 of file pgr_minCostMaxFlow.cpp.

115  {
116  supersink = add_vertex(graph);
117  for (int64_t sink_id : sinkVertices) {
118  PgrCostFlowGraph::V sink = GetBoostVertex(sink_id);
119  PgrCostFlowGraph::E e, eRev;
120  e = AddEdge(sink, supersink, 0, (std::numeric_limits<int32_t>::max)());
121  eRev = AddEdge(supersink, sink, 0, 0);
122  rev[e] = eRev;
123  rev[eRev] = e;
124  }
125 }

References AddEdge(), GetBoostVertex(), graph, rev, and supersink.

Referenced by AddVertices().

◆ SetSupersource()

void pgrouting::graph::PgrCostFlowGraph::SetSupersource ( const std::set< int64_t > &  source_vertices)
private

Definition at line 100 of file pgr_minCostMaxFlow.cpp.

101  {
102  supersource = add_vertex(graph);
103  for (int64_t source_id : sourceVertices) {
104  PgrCostFlowGraph::V source = GetBoostVertex(source_id);
105  PgrCostFlowGraph::E e, eRev;
106  e = AddEdge(supersource, source,
107  0, (std::numeric_limits<int32_t>::max)());
108  eRev = AddEdge(source, supersource, 0, 0);
109  rev[e] = eRev;
110  rev[eRev] = e;
111  }
112 }

References AddEdge(), GetBoostVertex(), graph, rev, and supersource.

Referenced by AddVertices().

Member Data Documentation

◆ capacity

Capacity pgrouting::graph::PgrCostFlowGraph::capacity
private

Definition at line 57 of file pgr_minCostMaxFlow.hpp.

Referenced by AddEdge(), GetFlowEdges(), GetMaxFlow(), and PgrCostFlowGraph().

◆ eToId

std::map<E, int64_t> pgrouting::graph::PgrCostFlowGraph::eToId
private

Definition at line 137 of file pgr_minCostMaxFlow.hpp.

Referenced by GetEdgeId(), and InsertEdges().

◆ graph

CostFlowGraph pgrouting::graph::PgrCostFlowGraph::graph
private

◆ idToV

std::map<int64_t, V> pgrouting::graph::PgrCostFlowGraph::idToV
private

Definition at line 135 of file pgr_minCostMaxFlow.hpp.

Referenced by AddVertices(), and GetBoostVertex().

◆ residual_capacity

ResidualCapacity pgrouting::graph::PgrCostFlowGraph::residual_capacity
private

Definition at line 58 of file pgr_minCostMaxFlow.hpp.

Referenced by GetFlowEdges(), GetMaxFlow(), and PgrCostFlowGraph().

◆ rev

Reversed pgrouting::graph::PgrCostFlowGraph::rev
private

Definition at line 59 of file pgr_minCostMaxFlow.hpp.

Referenced by InsertEdges(), PgrCostFlowGraph(), SetSupersink(), and SetSupersource().

◆ supersink

V pgrouting::graph::PgrCostFlowGraph::supersink
private

Definition at line 145 of file pgr_minCostMaxFlow.hpp.

Referenced by GetFlowEdges(), MinCostMaxFlow(), and SetSupersink().

◆ supersource

V pgrouting::graph::PgrCostFlowGraph::supersource
private

Definition at line 144 of file pgr_minCostMaxFlow.hpp.

Referenced by GetFlowEdges(), GetMaxFlow(), MinCostMaxFlow(), and SetSupersource().

◆ vToId

std::map<V, int64_t> pgrouting::graph::PgrCostFlowGraph::vToId
private

Definition at line 136 of file pgr_minCostMaxFlow.hpp.

Referenced by AddVertices(), and GetVertexId().

◆ weight

Weight pgrouting::graph::PgrCostFlowGraph::weight
private

Definition at line 60 of file pgr_minCostMaxFlow.hpp.

Referenced by AddEdge(), GetFlowEdges(), and PgrCostFlowGraph().


The documentation for this class was generated from the following files:
edge::reverse_cost
float8 reverse_cost
Definition: trsp.h:46
edge::cost
float8 cost
Definition: trsp.h:45
edge::target
int64 target
Definition: trsp.h:44
pgrouting::graph::PgrCostFlowGraph::idToV
std::map< int64_t, V > idToV
Definition: pgr_minCostMaxFlow.hpp:135
pgr_flow_t
Definition: pgr_flow_t.h:37
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::vToId
std::map< V, int64_t > vToId
Definition: pgr_minCostMaxFlow.hpp:136
edge
Definition: trsp.h:41
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
Traits::vertex_descriptor V
Definition: pgr_minCostMaxFlow.hpp:52
edge::source
int64 source
Definition: trsp.h:43
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
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::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::graph::PgrCostFlowGraph::supersource
V supersource
Definition: pgr_minCostMaxFlow.hpp:144
pgrouting::graph::PgrCostFlowGraph::supersink
V supersink
Definition: pgr_minCostMaxFlow.hpp:145
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