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