demo/lp_maxflow_demo.cc
author deba
Wed, 13 Jul 2005 14:05:49 +0000
changeset 1553 434f9add42cd
parent 1435 8e85e6bbefdf
child 1560 01707a8a4ca6
permissions -rw-r--r--
Demo about descriptor map.
ladanyi@1387
     1
#ifdef HAVE_CONFIG_H
ladanyi@1387
     2
#include <config.h>
ladanyi@1387
     3
#endif
ladanyi@1387
     4
alpar@1361
     5
#include<lemon/graph_reader.h>
alpar@1361
     6
#include<lemon/list_graph.h>
alpar@1361
     7
alpar@1381
     8
alpar@1381
     9
#ifdef HAVE_GLPK
alpar@1381
    10
#include <lemon/lp_glpk.h>
alpar@1381
    11
#elif HAVE_CPLEX
alpar@1381
    12
#include <lemon/lp_cplex.h>
alpar@1381
    13
#endif
alpar@1381
    14
alpar@1361
    15
using namespace lemon;
alpar@1361
    16
alpar@1381
    17
#ifdef HAVE_GLPK
alpar@1381
    18
typedef LpGlpk LpDefault;
alpar@1381
    19
#elif HAVE_CPLEX
alpar@1381
    20
typedef LpCplex LpDefault;
alpar@1381
    21
#endif
alpar@1381
    22
alpar@1381
    23
alpar@1361
    24
template<class G,class C>
alpar@1361
    25
double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
alpar@1361
    26
{
alpar@1381
    27
  LpDefault lp;
alpar@1361
    28
  
alpar@1361
    29
  typedef G Graph;
alpar@1361
    30
  typedef typename G::Node Node;
alpar@1361
    31
  typedef typename G::NodeIt NodeIt;
alpar@1361
    32
  typedef typename G::Edge Edge;
alpar@1361
    33
  typedef typename G::EdgeIt EdgeIt;
alpar@1361
    34
  typedef typename G::OutEdgeIt OutEdgeIt;
alpar@1361
    35
  typedef typename G::InEdgeIt InEdgeIt;
alpar@1361
    36
  
athos@1518
    37
  //Define a map on the edges for the variables of the LP problem
alpar@1381
    38
  typename G::template EdgeMap<LpDefault::Col> x(g);
alpar@1361
    39
  lp.addColSet(x);
alpar@1361
    40
  
athos@1518
    41
  //Nonnegativity and capacity constraints
alpar@1361
    42
  for(EdgeIt e(g);e!=INVALID;++e) {
alpar@1361
    43
    lp.colUpperBound(x[e],cap[e]);
alpar@1361
    44
    lp.colLowerBound(x[e],0);
alpar@1361
    45
  }
alpar@1361
    46
athos@1518
    47
athos@1518
    48
  //Flow conservation constraints for the nodes (except for 's' and 't')
alpar@1361
    49
  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
alpar@1381
    50
    LpDefault::Expr ex;
alpar@1361
    51
    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
alpar@1361
    52
    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
alpar@1361
    53
    lp.addRow(ex==0);
alpar@1361
    54
  }
athos@1518
    55
  
athos@1518
    56
  //Objective function: the flow value entering 't'
alpar@1361
    57
  {
alpar@1381
    58
    LpDefault::Expr ex;
alpar@1361
    59
    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
alpar@1361
    60
    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
alpar@1361
    61
    lp.setObj(ex);
alpar@1361
    62
  }
athos@1518
    63
athos@1518
    64
  //Maximization
alpar@1361
    65
  lp.max();
alpar@1361
    66
alpar@1381
    67
#ifdef HAVE_GLPK
alpar@1361
    68
  lp.presolver(true);
alpar@1361
    69
  lp.messageLevel(3);
alpar@1381
    70
#endif
alpar@1361
    71
athos@1518
    72
  //Solve with the underlying solver
alpar@1361
    73
  lp.solve();
alpar@1361
    74
alpar@1361
    75
  return lp.primalValue();
alpar@1361
    76
}
alpar@1361
    77
alpar@1361
    78
int main() 
alpar@1361
    79
{
alpar@1361
    80
  ListGraph g;
alpar@1361
    81
  ListGraph::Node s;
alpar@1361
    82
  ListGraph::Node t;
alpar@1361
    83
  
alpar@1361
    84
  ListGraph::EdgeMap<double> cap(g);
alpar@1361
    85
  
alpar@1361
    86
  GraphReader<ListGraph> reader(std::cin,g);
deba@1394
    87
  reader.readNode("source",s).readNode("target",t)
deba@1394
    88
    .readEdgeMap("capacity",cap).run();
alpar@1361
    89
  
alpar@1361
    90
  // std::ifstream file("../test/preflow_");
alpar@1361
    91
//   readDimacs(file, g, cap, s, t);
alpar@1361
    92
alpar@1361
    93
  std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
alpar@1361
    94
alpar@1361
    95
}