marci@1017
|
1 |
// -*- c++ -*-
|
marci@1017
|
2 |
#ifndef LEMON_MIN_COST_GEN_FLOW_H
|
marci@1017
|
3 |
#define LEMON_MIN_COST_GEN_FLOW_H
|
marci@1025
|
4 |
#include <iostream>
|
marci@1017
|
5 |
//#include <fstream>
|
marci@1017
|
6 |
|
marci@1025
|
7 |
#include <lemon/smart_graph.h>
|
marci@1025
|
8 |
#include <lemon/list_graph.h>
|
marci@1017
|
9 |
//#include <lemon/dimacs.h>
|
marci@1017
|
10 |
//#include <lemon/time_measure.h>
|
marci@1017
|
11 |
//#include <graph_wrapper.h>
|
marci@1025
|
12 |
#include <lemon/preflow.h>
|
marci@1017
|
13 |
//#include <augmenting_flow.h>
|
marci@1017
|
14 |
//#include <preflow_res.h>
|
marci@1025
|
15 |
#include <../merge_node_graph_wrapper.h>
|
marci@1017
|
16 |
#include <lemon/../work/marci/lp/lp_solver_wrapper.h>
|
marci@1017
|
17 |
|
marci@1017
|
18 |
namespace lemon {
|
marci@1017
|
19 |
|
marci@1017
|
20 |
template<typename Edge, typename EdgeIndexMap>
|
marci@1017
|
21 |
class PrimalMap {
|
marci@1017
|
22 |
protected:
|
marci@1017
|
23 |
LPSolverWrapper* lp;
|
marci@1017
|
24 |
EdgeIndexMap* edge_index_map;
|
marci@1017
|
25 |
public:
|
marci@1017
|
26 |
PrimalMap(LPSolverWrapper& _lp, EdgeIndexMap& _edge_index_map) :
|
marci@1017
|
27 |
lp(&_lp), edge_index_map(&_edge_index_map) { }
|
marci@1017
|
28 |
double operator[](Edge e) const {
|
marci@1017
|
29 |
return lp->getPrimal((*edge_index_map)[e]);
|
marci@1017
|
30 |
}
|
marci@1017
|
31 |
};
|
marci@1017
|
32 |
|
marci@1017
|
33 |
// excess: rho-delta
|
marci@1017
|
34 |
template <typename Graph, typename Num,
|
marci@1017
|
35 |
typename Excess=typename Graph::template NodeMap<Num>,
|
marci@1017
|
36 |
typename LCapMap=typename Graph::template EdgeMap<Num>,
|
marci@1017
|
37 |
typename CapMap=typename Graph::template EdgeMap<Num>,
|
marci@1017
|
38 |
typename FlowMap=typename Graph::template EdgeMap<Num>,
|
marci@1017
|
39 |
typename CostMap=typename Graph::template EdgeMap<Num> >
|
marci@1017
|
40 |
class MinCostGenFlow {
|
marci@1017
|
41 |
protected:
|
marci@1017
|
42 |
const Graph& g;
|
marci@1017
|
43 |
const Excess& excess;
|
marci@1017
|
44 |
const LCapMap& lcapacity;
|
marci@1017
|
45 |
const CapMap& capacity;
|
marci@1017
|
46 |
FlowMap& flow;
|
marci@1017
|
47 |
const CostMap& cost;
|
marci@1017
|
48 |
public:
|
marci@1017
|
49 |
MinCostGenFlow(const Graph& _g, const Excess& _excess,
|
marci@1017
|
50 |
const LCapMap& _lcapacity, const CapMap& _capacity,
|
marci@1017
|
51 |
FlowMap& _flow,
|
marci@1017
|
52 |
const CostMap& _cost) :
|
marci@1017
|
53 |
g(_g), excess(_excess), lcapacity(_lcapacity),
|
marci@1017
|
54 |
capacity(_capacity), flow(_flow), cost(_cost) { }
|
marci@1025
|
55 |
bool feasible() {
|
marci@1025
|
56 |
// std::cout << "making new vertices..." << std::endl;
|
marci@1025
|
57 |
typedef ListGraph Graph2;
|
marci@1025
|
58 |
Graph2 g2;
|
marci@1025
|
59 |
typedef MergeEdgeGraphWrapper<const Graph, Graph2> GW;
|
marci@1025
|
60 |
// std::cout << "merging..." << std::endl;
|
marci@1025
|
61 |
GW gw(g, g2);
|
marci@1025
|
62 |
typename GW::Node s(INVALID, g2.addNode(), true);
|
marci@1025
|
63 |
typename GW::Node t(INVALID, g2.addNode(), true);
|
marci@1025
|
64 |
typedef SmartGraph Graph3;
|
marci@1025
|
65 |
// std::cout << "making extender graph..." << std::endl;
|
marci@1025
|
66 |
typedef NewEdgeSetGraphWrapper2<GW, Graph3> GWW;
|
marci@1025
|
67 |
// {
|
marci@1025
|
68 |
// checkConcept<StaticGraph, GWW>();
|
marci@1025
|
69 |
// }
|
marci@1025
|
70 |
GWW gww(gw);
|
marci@1025
|
71 |
typedef AugmentingGraphWrapper<GW, GWW> GWWW;
|
marci@1025
|
72 |
GWWW gwww(gw, gww);
|
marci@1025
|
73 |
|
marci@1025
|
74 |
// std::cout << "making new edges..." << std::endl;
|
marci@1025
|
75 |
typename GWWW::template EdgeMap<Num> translated_cap(gwww);
|
marci@1025
|
76 |
|
marci@1025
|
77 |
for (typename GW::EdgeIt e(gw); e!=INVALID; ++e)
|
marci@1025
|
78 |
translated_cap.set(typename GWWW::Edge(e,INVALID,false),
|
marci@1025
|
79 |
capacity[e]-lcapacity[e]);
|
marci@1025
|
80 |
|
marci@1025
|
81 |
Num expected=0;
|
marci@1025
|
82 |
|
marci@1025
|
83 |
// std::cout << "making new edges 2..." << std::endl;
|
marci@1025
|
84 |
for (typename Graph::NodeIt n(g); n!=INVALID; ++n) {
|
marci@1025
|
85 |
Num a=0;
|
marci@1025
|
86 |
for (typename Graph::InEdgeIt e(g, n); e!=INVALID; ++e)
|
marci@1025
|
87 |
a+=lcapacity[e];
|
marci@1025
|
88 |
for (typename Graph::OutEdgeIt e(g, n); e!=INVALID; ++e)
|
marci@1025
|
89 |
a-=lcapacity[e];
|
marci@1025
|
90 |
if (excess[n]>a) {
|
marci@1025
|
91 |
typename GWW::Edge e=
|
marci@1025
|
92 |
gww.addEdge(typename GW::Node(n,INVALID,false), t);
|
marci@1025
|
93 |
translated_cap.set(typename GWWW::Edge(INVALID, e, true),
|
marci@1025
|
94 |
excess[n]-a);
|
marci@1025
|
95 |
}
|
marci@1025
|
96 |
if (excess[n]<a) {
|
marci@1025
|
97 |
typename GWW::Edge e=
|
marci@1025
|
98 |
gww.addEdge(s, typename GW::Node(n,INVALID,false));
|
marci@1025
|
99 |
translated_cap.set(typename GWWW::Edge(INVALID, e, true),
|
marci@1025
|
100 |
a-excess[n]);
|
marci@1025
|
101 |
expected+=a-excess[n];
|
marci@1025
|
102 |
}
|
marci@1025
|
103 |
}
|
marci@1025
|
104 |
|
marci@1025
|
105 |
// std::cout << "preflow..." << std::endl;
|
marci@1025
|
106 |
typename GWWW::template EdgeMap<Num> translated_flow(gwww, 0);
|
marci@1025
|
107 |
Preflow<GWWW, Num> preflow(gwww, s, t,
|
marci@1025
|
108 |
translated_cap, translated_flow);
|
marci@1025
|
109 |
preflow.run();
|
marci@1025
|
110 |
// std::cout << "fv: " << preflow.flowValue() << std::endl;
|
marci@1025
|
111 |
// std::cout << "expected: " << expected << std::endl;
|
marci@1025
|
112 |
|
marci@1025
|
113 |
for (typename Graph::EdgeIt e(g); e!=INVALID; ++e) {
|
marci@1025
|
114 |
typename GW::Edge ew(e, INVALID, false);
|
marci@1025
|
115 |
typename GWWW::Edge ewww(ew, INVALID, false);
|
marci@1025
|
116 |
flow.set(e, translated_flow[ewww]+lcapacity[e]);
|
marci@1025
|
117 |
}
|
marci@1025
|
118 |
return (expected>=preflow.flowValue());
|
marci@1025
|
119 |
}
|
marci@1017
|
120 |
void run() {
|
marci@1017
|
121 |
LPSolverWrapper lp;
|
marci@1017
|
122 |
lp.setMinimize();
|
marci@1017
|
123 |
typedef LPSolverWrapper::ColIt ColIt;
|
marci@1017
|
124 |
typedef LPSolverWrapper::RowIt RowIt;
|
marci@1017
|
125 |
typedef typename Graph::template EdgeMap<ColIt> EdgeIndexMap;
|
marci@1017
|
126 |
EdgeIndexMap edge_index_map(g);
|
marci@1017
|
127 |
PrimalMap<typename Graph::Edge, EdgeIndexMap> lp_flow(lp, edge_index_map);
|
marci@1017
|
128 |
for (typename Graph::EdgeIt e(g); e!=INVALID; ++e) {
|
marci@1017
|
129 |
ColIt col_it=lp.addCol();
|
marci@1017
|
130 |
edge_index_map.set(e, col_it);
|
marci@1017
|
131 |
if (lcapacity[e]==capacity[e])
|
marci@1017
|
132 |
lp.setColBounds(col_it, LPX_FX, lcapacity[e], capacity[e]);
|
marci@1017
|
133 |
else
|
marci@1017
|
134 |
lp.setColBounds(col_it, LPX_DB, lcapacity[e], capacity[e]);
|
marci@1017
|
135 |
lp.setObjCoef(col_it, cost[e]);
|
marci@1017
|
136 |
}
|
marci@1017
|
137 |
for (typename Graph::NodeIt n(g); n!=INVALID; ++n) {
|
marci@1017
|
138 |
typename Graph::template EdgeMap<Num> coeffs(g, 0);
|
marci@1017
|
139 |
for (typename Graph::InEdgeIt e(g, n); e!=INVALID; ++e)
|
marci@1017
|
140 |
coeffs.set(e, coeffs[e]+1);
|
marci@1017
|
141 |
for (typename Graph::OutEdgeIt e(g, n); e!=INVALID; ++e)
|
marci@1017
|
142 |
coeffs.set(e, coeffs[e]-1);
|
marci@1017
|
143 |
RowIt row_it=lp.addRow();
|
marci@1017
|
144 |
typename std::vector< std::pair<ColIt, double> > row;
|
marci@1017
|
145 |
//std::cout << "node:" <<g.id(n)<<std::endl;
|
marci@1017
|
146 |
for (typename Graph::EdgeIt e(g); e!=INVALID; ++e) {
|
marci@1017
|
147 |
if (coeffs[e]!=0) {
|
marci@1017
|
148 |
//std::cout << " edge:" <<g.id(e)<<" "<<coeffs[e];
|
marci@1017
|
149 |
row.push_back(std::make_pair(edge_index_map[e], coeffs[e]));
|
marci@1017
|
150 |
}
|
marci@1017
|
151 |
}
|
marci@1017
|
152 |
//std::cout << std::endl;
|
marci@1017
|
153 |
lp.setRowCoeffs(row_it, row.begin(), row.end());
|
marci@1017
|
154 |
lp.setRowBounds(row_it, LPX_FX, 0.0, 0.0);
|
marci@1017
|
155 |
}
|
marci@1017
|
156 |
lp.solveSimplex();
|
marci@1017
|
157 |
//std::cout << lp.colNum() << std::endl;
|
marci@1017
|
158 |
//std::cout << lp.rowNum() << std::endl;
|
marci@1017
|
159 |
//std::cout << "flow value: "<< lp.getObjVal() << std::endl;
|
marci@1017
|
160 |
for (typename Graph::EdgeIt e(g); e!=INVALID; ++e)
|
marci@1017
|
161 |
flow.set(e, lp_flow[e]);
|
marci@1017
|
162 |
}
|
marci@1017
|
163 |
};
|
marci@1017
|
164 |
|
marci@1017
|
165 |
} // namespace lemon
|
marci@1017
|
166 |
|
marci@1017
|
167 |
#endif //LEMON_MIN_COST_GEN_FLOW_H
|