[1560] | 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 | /// |
---|
[1583] | 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 LP |
---|
| 24 | /// constraints that arise in graph theory in our library LEMON . |
---|
[1560] | 25 | |
---|
[1387] | 26 | #ifdef HAVE_CONFIG_H |
---|
| 27 | #include <config.h> |
---|
| 28 | #endif |
---|
| 29 | |
---|
[1361] | 30 | #include<lemon/graph_reader.h> |
---|
| 31 | #include<lemon/list_graph.h> |
---|
| 32 | |
---|
[1560] | 33 | #include <fstream> |
---|
| 34 | #include <iostream> |
---|
| 35 | |
---|
[1381] | 36 | |
---|
| 37 | #ifdef HAVE_GLPK |
---|
| 38 | #include <lemon/lp_glpk.h> |
---|
| 39 | #elif HAVE_CPLEX |
---|
| 40 | #include <lemon/lp_cplex.h> |
---|
| 41 | #endif |
---|
| 42 | |
---|
[1361] | 43 | using namespace lemon; |
---|
| 44 | |
---|
[1381] | 45 | #ifdef HAVE_GLPK |
---|
| 46 | typedef LpGlpk LpDefault; |
---|
[1577] | 47 | const char default_solver_name[]="GLPK"; |
---|
[1381] | 48 | #elif HAVE_CPLEX |
---|
| 49 | typedef LpCplex LpDefault; |
---|
[1577] | 50 | const char default_solver_name[]="CPLEX"; |
---|
[1381] | 51 | #endif |
---|
| 52 | |
---|
| 53 | |
---|
[1361] | 54 | template<class G,class C> |
---|
| 55 | double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t) |
---|
| 56 | { |
---|
[1381] | 57 | LpDefault lp; |
---|
[1361] | 58 | |
---|
| 59 | typedef G Graph; |
---|
| 60 | typedef typename G::Node Node; |
---|
| 61 | typedef typename G::NodeIt NodeIt; |
---|
| 62 | typedef typename G::Edge Edge; |
---|
| 63 | typedef typename G::EdgeIt EdgeIt; |
---|
| 64 | typedef typename G::OutEdgeIt OutEdgeIt; |
---|
| 65 | typedef typename G::InEdgeIt InEdgeIt; |
---|
| 66 | |
---|
[1518] | 67 | //Define a map on the edges for the variables of the LP problem |
---|
[1381] | 68 | typename G::template EdgeMap<LpDefault::Col> x(g); |
---|
[1361] | 69 | lp.addColSet(x); |
---|
| 70 | |
---|
[1518] | 71 | //Nonnegativity and capacity constraints |
---|
[1361] | 72 | for(EdgeIt e(g);e!=INVALID;++e) { |
---|
| 73 | lp.colUpperBound(x[e],cap[e]); |
---|
| 74 | lp.colLowerBound(x[e],0); |
---|
| 75 | } |
---|
| 76 | |
---|
[1518] | 77 | |
---|
| 78 | //Flow conservation constraints for the nodes (except for 's' and 't') |
---|
[1361] | 79 | for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) { |
---|
[1381] | 80 | LpDefault::Expr ex; |
---|
[1361] | 81 | for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e]; |
---|
| 82 | for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e]; |
---|
| 83 | lp.addRow(ex==0); |
---|
| 84 | } |
---|
[1518] | 85 | |
---|
| 86 | //Objective function: the flow value entering 't' |
---|
[1571] | 87 | LpDefault::Expr obj; |
---|
| 88 | for(InEdgeIt e(g,t);e!=INVALID;++e) obj+=x[e]; |
---|
| 89 | for(OutEdgeIt e(g,t);e!=INVALID;++e) obj-=x[e]; |
---|
| 90 | lp.setObj(obj); |
---|
| 91 | |
---|
[1518] | 92 | |
---|
| 93 | //Maximization |
---|
[1361] | 94 | lp.max(); |
---|
| 95 | |
---|
[1381] | 96 | #ifdef HAVE_GLPK |
---|
[1361] | 97 | lp.presolver(true); |
---|
| 98 | lp.messageLevel(3); |
---|
[1381] | 99 | #endif |
---|
[1361] | 100 | |
---|
[1577] | 101 | std::cout<<"Solver used: "<<default_solver_name<<std::endl; |
---|
| 102 | |
---|
[1518] | 103 | //Solve with the underlying solver |
---|
[1361] | 104 | lp.solve(); |
---|
| 105 | |
---|
| 106 | return lp.primalValue(); |
---|
| 107 | } |
---|
| 108 | |
---|
[1560] | 109 | int main(int argc, char *argv[]) |
---|
[1361] | 110 | { |
---|
[1560] | 111 | if(argc<2) |
---|
| 112 | { |
---|
[1577] | 113 | std::cerr << " USAGE: lp_maxflow_demo input_file.lgf" << std::endl; |
---|
[1561] | 114 | std::cerr << " The file 'input_file.lgf' has to contain a max " |
---|
| 115 | << "flow instance in\n" |
---|
| 116 | << " LEMON format (e.g. sample.lgf is such a file)." |
---|
| 117 | << std::endl; |
---|
[1560] | 118 | return 0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | //input stream to read the graph from |
---|
| 123 | std::ifstream is(argv[1]); |
---|
| 124 | |
---|
| 125 | |
---|
[1361] | 126 | ListGraph g; |
---|
| 127 | ListGraph::Node s; |
---|
| 128 | ListGraph::Node t; |
---|
| 129 | |
---|
| 130 | ListGraph::EdgeMap<double> cap(g); |
---|
| 131 | |
---|
[1560] | 132 | GraphReader<ListGraph> reader(is,g); |
---|
[1394] | 133 | reader.readNode("source",s).readNode("target",t) |
---|
| 134 | .readEdgeMap("capacity",cap).run(); |
---|
[1361] | 135 | |
---|
| 136 | std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl; |
---|
| 137 | |
---|
| 138 | } |
---|