1 | /* -*- C++ -*- |
---|
2 | * demo/lp_maxflow_demo.cc - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | ///\ingroup demos |
---|
18 | ///\file |
---|
19 | ///\brief Max flow problem solved with an LP solver (demo). |
---|
20 | /// |
---|
21 | ///This demo program shows how to solve a maximum (or maximal) flow |
---|
22 | ///problem using the LEMON LP solver interface. We would like to lay |
---|
23 | ///the emphasis on the simplicity of the way one can formulate the LP |
---|
24 | ///constraints with LEMON that arise in graph theory. |
---|
25 | |
---|
26 | #ifdef HAVE_CONFIG_H |
---|
27 | #include <config.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | #include<lemon/graph_reader.h> |
---|
31 | #include<lemon/list_graph.h> |
---|
32 | |
---|
33 | #include <fstream> |
---|
34 | #include <iostream> |
---|
35 | |
---|
36 | |
---|
37 | #ifdef HAVE_GLPK |
---|
38 | #include <lemon/lp_glpk.h> |
---|
39 | #elif HAVE_CPLEX |
---|
40 | #include <lemon/lp_cplex.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | using namespace lemon; |
---|
44 | |
---|
45 | #ifdef HAVE_GLPK |
---|
46 | typedef LpGlpk LpDefault; |
---|
47 | #elif HAVE_CPLEX |
---|
48 | typedef LpCplex LpDefault; |
---|
49 | #endif |
---|
50 | |
---|
51 | |
---|
52 | template<class G,class C> |
---|
53 | double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t) |
---|
54 | { |
---|
55 | LpDefault lp; |
---|
56 | |
---|
57 | typedef G Graph; |
---|
58 | typedef typename G::Node Node; |
---|
59 | typedef typename G::NodeIt NodeIt; |
---|
60 | typedef typename G::Edge Edge; |
---|
61 | typedef typename G::EdgeIt EdgeIt; |
---|
62 | typedef typename G::OutEdgeIt OutEdgeIt; |
---|
63 | typedef typename G::InEdgeIt InEdgeIt; |
---|
64 | |
---|
65 | //Define a map on the edges for the variables of the LP problem |
---|
66 | typename G::template EdgeMap<LpDefault::Col> x(g); |
---|
67 | lp.addColSet(x); |
---|
68 | |
---|
69 | //Nonnegativity and capacity constraints |
---|
70 | for(EdgeIt e(g);e!=INVALID;++e) { |
---|
71 | lp.colUpperBound(x[e],cap[e]); |
---|
72 | lp.colLowerBound(x[e],0); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | //Flow conservation constraints for the nodes (except for 's' and 't') |
---|
77 | for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) { |
---|
78 | LpDefault::Expr ex; |
---|
79 | for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e]; |
---|
80 | for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e]; |
---|
81 | lp.addRow(ex==0); |
---|
82 | } |
---|
83 | |
---|
84 | //Objective function: the flow value entering 't' |
---|
85 | { |
---|
86 | LpDefault::Expr ex; |
---|
87 | for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e]; |
---|
88 | for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e]; |
---|
89 | lp.setObj(ex); |
---|
90 | } |
---|
91 | |
---|
92 | //Maximization |
---|
93 | lp.max(); |
---|
94 | |
---|
95 | #ifdef HAVE_GLPK |
---|
96 | lp.presolver(true); |
---|
97 | lp.messageLevel(3); |
---|
98 | #endif |
---|
99 | |
---|
100 | //Solve with the underlying solver |
---|
101 | lp.solve(); |
---|
102 | |
---|
103 | return lp.primalValue(); |
---|
104 | } |
---|
105 | |
---|
106 | int main(int argc, char *argv[]) |
---|
107 | { |
---|
108 | if(argc<2) |
---|
109 | { |
---|
110 | std::cerr << " USAGE: lp_maxflow_demo <input_file.lgf>" << std::endl; |
---|
111 | std::cerr << " The file 'input_file.lgf' has to contain a max " |
---|
112 | << "flow instance in\n" |
---|
113 | << " LEMON format (e.g. sample.lgf is such a file)." |
---|
114 | << std::endl; |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | //input stream to read the graph from |
---|
120 | std::ifstream is(argv[1]); |
---|
121 | |
---|
122 | |
---|
123 | ListGraph g; |
---|
124 | ListGraph::Node s; |
---|
125 | ListGraph::Node t; |
---|
126 | |
---|
127 | ListGraph::EdgeMap<double> cap(g); |
---|
128 | |
---|
129 | GraphReader<ListGraph> reader(is,g); |
---|
130 | reader.readNode("source",s).readNode("target",t) |
---|
131 | .readEdgeMap("capacity",cap).run(); |
---|
132 | |
---|
133 | std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl; |
---|
134 | |
---|
135 | } |
---|