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