demo/lp_maxflow_demo.cc
author alpar
Tue, 14 Jun 2005 19:19:59 +0000
changeset 1488 92755f9a4e2a
parent 1394 f0c48d7fa73d
child 1518 f8efed98d6a3
permissions -rw-r--r--
Concept checking classes must also be installed
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
  
alpar@1381
    37
  typename G::template EdgeMap<LpDefault::Col> x(g);
alpar@1361
    38
  lp.addColSet(x);
alpar@1361
    39
  
alpar@1361
    40
  for(EdgeIt e(g);e!=INVALID;++e) {
alpar@1361
    41
    lp.colUpperBound(x[e],cap[e]);
alpar@1361
    42
    lp.colLowerBound(x[e],0);
alpar@1361
    43
  }
alpar@1361
    44
alpar@1361
    45
  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
alpar@1381
    46
    LpDefault::Expr ex;
alpar@1361
    47
    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
alpar@1361
    48
    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
alpar@1361
    49
    lp.addRow(ex==0);
alpar@1361
    50
  }
alpar@1361
    51
  {
alpar@1381
    52
    LpDefault::Expr ex;
alpar@1361
    53
    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
alpar@1361
    54
    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
alpar@1361
    55
    lp.setObj(ex);
alpar@1361
    56
  }
alpar@1361
    57
  lp.max();
alpar@1361
    58
alpar@1381
    59
#ifdef HAVE_GLPK
alpar@1361
    60
  lp.presolver(true);
alpar@1361
    61
  lp.messageLevel(3);
alpar@1381
    62
#endif
alpar@1361
    63
alpar@1361
    64
  lp.solve();
alpar@1361
    65
alpar@1361
    66
  return lp.primalValue();
alpar@1361
    67
}
alpar@1361
    68
alpar@1361
    69
int main() 
alpar@1361
    70
{
alpar@1361
    71
  ListGraph g;
alpar@1361
    72
  ListGraph::Node s;
alpar@1361
    73
  ListGraph::Node t;
alpar@1361
    74
  
alpar@1361
    75
  ListGraph::EdgeMap<double> cap(g);
alpar@1361
    76
  
alpar@1361
    77
  GraphReader<ListGraph> reader(std::cin,g);
deba@1394
    78
  reader.readNode("source",s).readNode("target",t)
deba@1394
    79
    .readEdgeMap("capacity",cap).run();
alpar@1361
    80
  
alpar@1361
    81
  // std::ifstream file("../test/preflow_");
alpar@1361
    82
//   readDimacs(file, g, cap, s, t);
alpar@1361
    83
alpar@1361
    84
  std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
alpar@1361
    85
alpar@1361
    86
}