5 #include <lemon/smart_graph.h>
6 #include <lemon/list_graph.h>
7 #include <lemon/dimacs.h>
8 #include <lemon/time_measure.h>
9 //#include <graph_wrapper.h>
10 #include <lemon/preflow.h>
11 #include <augmenting_flow.h>
12 //#include <preflow_res.h>
13 #include <lp_solver_wrapper.h>
15 using namespace lemon;
17 // Use a DIMACS max flow file as stdin.
18 // max_flow_demo < dimacs_max_flow_file
20 template<typename Edge, typename EdgeIndexMap>
24 EdgeIndexMap* edge_index_map;
26 PrimalMap(LPSolverWrapper& _lp, EdgeIndexMap& _edge_index_map) :
27 lp(&_lp), edge_index_map(&_edge_index_map) { }
28 double operator[](Edge e) const {
29 return lp->getPrimal((*edge_index_map)[e]);
33 int main(int, char **) {
35 typedef ListGraph MutableGraph;
36 typedef SmartGraph Graph;
37 typedef Graph::Node Node;
38 typedef Graph::EdgeIt EdgeIt;
43 Graph::EdgeMap<int> cap(g);
44 //readDimacsMaxFlow(std::cin, g, s, t, cap);
45 readDimacs(std::cin, g, cap, s, t);
47 Graph::EdgeMap<int> flow(g); //0 flow
48 Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >
49 max_flow_test(g, s, t, cap, flow);
50 AugmentingFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >
51 augmenting_flow_test(g, s, t, cap, flow);
53 Graph::NodeMap<bool> cut(g);
56 std::cout << "preflow ..." << std::endl;
59 std::cout << "elapsed time: " << ts << std::endl;
60 std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
61 max_flow_test.minCut(cut);
63 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
64 if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e])
65 std::cout << "Slackness does not hold!" << std::endl;
66 if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0)
67 std::cout << "Slackness does not hold!" << std::endl;
72 // std::cout << "preflow ..." << std::endl;
73 // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
75 // max_flow_test.preflow(Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
76 // std::cout << "elapsed time: " << ts << std::endl;
77 // std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
79 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
80 // if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e])
81 // std::cout << "Slackness does not hold!" << std::endl;
82 // if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0)
83 // std::cout << "Slackness does not hold!" << std::endl;
88 // std::cout << "wrapped preflow ..." << std::endl;
89 // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
91 // pre_flow_res.run();
92 // std::cout << "elapsed time: " << ts << std::endl;
93 // std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
97 std::cout << "physical blocking flow augmentation ..." << std::endl;
98 for (Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
101 while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
102 std::cout << "elapsed time: " << ts << std::endl;
103 std::cout << "number of augmentation phases: " << i << std::endl;
104 std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
106 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
107 if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e])
108 std::cout << "Slackness does not hold!" << std::endl;
109 if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0)
110 std::cout << "Slackness does not hold!" << std::endl;
115 // std::cout << "faster physical blocking flow augmentation ..." << std::endl;
116 // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
119 // while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { ++i; }
120 // std::cout << "elapsed time: " << ts << std::endl;
121 // std::cout << "number of augmentation phases: " << i << std::endl;
122 // std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
126 std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
127 for (Graph::EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
130 while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; }
131 std::cout << "elapsed time: " << ts << std::endl;
132 std::cout << "number of augmentation phases: " << i << std::endl;
133 std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
135 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
136 if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e])
137 std::cout << "Slackness does not hold!" << std::endl;
138 if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0)
139 std::cout << "Slackness does not hold!" << std::endl;
144 // std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
145 // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
148 // while (augmenting_flow_test.augmentOnShortestPath()) { ++i; }
149 // std::cout << "elapsed time: " << ts << std::endl;
150 // std::cout << "number of augmentation phases: " << i << std::endl;
151 // std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
153 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
154 // if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e])
155 // std::cout << "Slackness does not hold!" << std::endl;
156 // if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0)
157 // std::cout << "Slackness does not hold!" << std::endl;
162 // std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
163 // FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
166 // while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; }
167 // std::cout << "elapsed time: " << ts << std::endl;
168 // std::cout << "number of augmentation phases: " << i << std::endl;
169 // std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
171 // FOR_EACH_LOC(Graph::EdgeIt, e, g) {
172 // if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e])
173 // std::cout << "Slackness does not hold!" << std::endl;
174 // if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0)
175 // std::cout << "Slackness does not hold!" << std::endl;
182 typedef LPSolverWrapper::ColIt ColIt;
183 typedef LPSolverWrapper::RowIt RowIt;
184 typedef Graph::EdgeMap<ColIt> EdgeIndexMap;
185 EdgeIndexMap edge_index_map(g);
186 PrimalMap<Graph::Edge, EdgeIndexMap> lp_flow(lp, edge_index_map);
187 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
188 ColIt col_it=lp.addCol();
189 edge_index_map.set(e, col_it);
190 lp.setColBounds(col_it, LPX_DB, 0.0, cap[e]);
192 for (Graph::NodeIt n(g); n!=INVALID; ++n) {
195 Graph::EdgeMap<int> coeffs(g, 0);
196 for (Graph::InEdgeIt e(g, n); e!=INVALID; ++e)
197 coeffs.set(e, coeffs[e]+1);
198 for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e)
199 coeffs.set(e, coeffs[e]-1);
201 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
203 lp.setObjCoef(edge_index_map[e], coeffs[e]);
206 RowIt row_it=lp.addRow();
207 std::vector< std::pair<ColIt, double> > row;
208 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
210 row.push_back(std::make_pair(edge_index_map[e], coeffs[e]));
212 lp.setRowCoeffs(row_it, row.begin(), row.end());
213 lp.setRowBounds(row_it, LPX_FX, 0.0, 0.0);
218 std::cout << "flow value: "<< lp.getObjVal() << std::endl;
219 std::cout << "elapsed time: " << ts << std::endl;