demo/lp_maxflow_demo.cc
author athos
Fri, 22 Jul 2005 15:03:23 +0000
changeset 1582 5fd89def7cbc
parent 1571 68ce4302fb0b
child 1583 2b329fd595ef
permissions -rw-r--r--
File demoprograms.dox thrown away: demos module used instead.
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;
athos@1577
    47
const char default_solver_name[]="GLPK";
alpar@1381
    48
#elif HAVE_CPLEX
alpar@1381
    49
typedef LpCplex LpDefault;
athos@1577
    50
const char default_solver_name[]="CPLEX";
alpar@1381
    51
#endif
alpar@1381
    52
alpar@1381
    53
alpar@1361
    54
template<class G,class C>
alpar@1361
    55
double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
alpar@1361
    56
{
alpar@1381
    57
  LpDefault lp;
alpar@1361
    58
  
alpar@1361
    59
  typedef G Graph;
alpar@1361
    60
  typedef typename G::Node Node;
alpar@1361
    61
  typedef typename G::NodeIt NodeIt;
alpar@1361
    62
  typedef typename G::Edge Edge;
alpar@1361
    63
  typedef typename G::EdgeIt EdgeIt;
alpar@1361
    64
  typedef typename G::OutEdgeIt OutEdgeIt;
alpar@1361
    65
  typedef typename G::InEdgeIt InEdgeIt;
alpar@1361
    66
  
athos@1518
    67
  //Define a map on the edges for the variables of the LP problem
alpar@1381
    68
  typename G::template EdgeMap<LpDefault::Col> x(g);
alpar@1361
    69
  lp.addColSet(x);
alpar@1361
    70
  
athos@1518
    71
  //Nonnegativity and capacity constraints
alpar@1361
    72
  for(EdgeIt e(g);e!=INVALID;++e) {
alpar@1361
    73
    lp.colUpperBound(x[e],cap[e]);
alpar@1361
    74
    lp.colLowerBound(x[e],0);
alpar@1361
    75
  }
alpar@1361
    76
athos@1518
    77
athos@1518
    78
  //Flow conservation constraints for the nodes (except for 's' and 't')
alpar@1361
    79
  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
alpar@1381
    80
    LpDefault::Expr ex;
alpar@1361
    81
    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
alpar@1361
    82
    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
alpar@1361
    83
    lp.addRow(ex==0);
alpar@1361
    84
  }
athos@1518
    85
  
athos@1518
    86
  //Objective function: the flow value entering 't'
alpar@1571
    87
  LpDefault::Expr obj;
alpar@1571
    88
  for(InEdgeIt  e(g,t);e!=INVALID;++e) obj+=x[e];
alpar@1571
    89
  for(OutEdgeIt e(g,t);e!=INVALID;++e) obj-=x[e];
alpar@1571
    90
  lp.setObj(obj);
alpar@1571
    91
athos@1518
    92
athos@1518
    93
  //Maximization
alpar@1361
    94
  lp.max();
alpar@1361
    95
alpar@1381
    96
#ifdef HAVE_GLPK
alpar@1361
    97
  lp.presolver(true);
alpar@1361
    98
  lp.messageLevel(3);
alpar@1381
    99
#endif
alpar@1361
   100
athos@1577
   101
  std::cout<<"Solver used: "<<default_solver_name<<std::endl;
athos@1577
   102
athos@1518
   103
  //Solve with the underlying solver
alpar@1361
   104
  lp.solve();
alpar@1361
   105
alpar@1361
   106
  return lp.primalValue();
alpar@1361
   107
}
alpar@1361
   108
athos@1560
   109
int main(int argc, char *argv[]) 
alpar@1361
   110
{
athos@1560
   111
  if(argc<2)
athos@1560
   112
  {
athos@1577
   113
      std::cerr << "  USAGE: lp_maxflow_demo input_file.lgf" << std::endl;
alpar@1561
   114
      std::cerr << "  The file 'input_file.lgf' has to contain a max "
alpar@1561
   115
		<< "flow instance in\n"
alpar@1561
   116
		<< "  LEMON format (e.g. sample.lgf is such a file)."
alpar@1561
   117
		<< std::endl;
athos@1560
   118
      return 0;
athos@1560
   119
  }
athos@1560
   120
athos@1560
   121
athos@1560
   122
  //input stream to read the graph from
athos@1560
   123
  std::ifstream is(argv[1]);
athos@1560
   124
athos@1560
   125
alpar@1361
   126
  ListGraph g;
alpar@1361
   127
  ListGraph::Node s;
alpar@1361
   128
  ListGraph::Node t;
alpar@1361
   129
  
alpar@1361
   130
  ListGraph::EdgeMap<double> cap(g);
alpar@1361
   131
  
athos@1560
   132
  GraphReader<ListGraph> reader(is,g);
deba@1394
   133
  reader.readNode("source",s).readNode("target",t)
deba@1394
   134
    .readEdgeMap("capacity",cap).run();
alpar@1361
   135
  
alpar@1361
   136
  std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
alpar@1361
   137
alpar@1361
   138
}