COIN-OR::LEMON - Graph Library

source: lemon-0.x/demo/lp_maxflow_demo.cc @ 1610:893dacc1866c

Last change on this file since 1610:893dacc1866c was 1610:893dacc1866c, checked in by Alpar Juttner, 19 years ago

A default LP solver is defined in lp.h

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