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