src/demo/lp_maxflow_demo.cc
author ladanyi
Sun, 24 Apr 2005 22:26:37 +0000
changeset 1385 49d1ca28ebd8
parent 1361 04733359bac2
child 1387 37d1b20cd9ef
permissions -rw-r--r--
Don't set {GLPK,CPLEX}_{CFLAGS,LIBS} if the check fails.
alpar@1361
     1
#include<lemon/graph_reader.h>
alpar@1361
     2
#include<lemon/list_graph.h>
alpar@1361
     3
alpar@1381
     4
alpar@1381
     5
#ifdef HAVE_GLPK
alpar@1381
     6
#include <lemon/lp_glpk.h>
alpar@1381
     7
#elif HAVE_CPLEX
alpar@1381
     8
#include <lemon/lp_cplex.h>
alpar@1381
     9
#endif
alpar@1381
    10
alpar@1361
    11
using namespace lemon;
alpar@1361
    12
alpar@1381
    13
#ifdef HAVE_GLPK
alpar@1381
    14
typedef LpGlpk LpDefault;
alpar@1381
    15
#elif HAVE_CPLEX
alpar@1381
    16
typedef LpCplex LpDefault;
alpar@1381
    17
#endif
alpar@1381
    18
alpar@1381
    19
alpar@1361
    20
template<class G,class C>
alpar@1361
    21
double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
alpar@1361
    22
{
alpar@1381
    23
  LpDefault lp;
alpar@1361
    24
  
alpar@1361
    25
  typedef G Graph;
alpar@1361
    26
  typedef typename G::Node Node;
alpar@1361
    27
  typedef typename G::NodeIt NodeIt;
alpar@1361
    28
  typedef typename G::Edge Edge;
alpar@1361
    29
  typedef typename G::EdgeIt EdgeIt;
alpar@1361
    30
  typedef typename G::OutEdgeIt OutEdgeIt;
alpar@1361
    31
  typedef typename G::InEdgeIt InEdgeIt;
alpar@1361
    32
  
alpar@1381
    33
  typename G::template EdgeMap<LpDefault::Col> x(g);
alpar@1361
    34
  lp.addColSet(x);
alpar@1361
    35
  
alpar@1361
    36
  for(EdgeIt e(g);e!=INVALID;++e) {
alpar@1361
    37
    lp.colUpperBound(x[e],cap[e]);
alpar@1361
    38
    lp.colLowerBound(x[e],0);
alpar@1361
    39
  }
alpar@1361
    40
alpar@1361
    41
  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
alpar@1381
    42
    LpDefault::Expr ex;
alpar@1361
    43
    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
alpar@1361
    44
    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
alpar@1361
    45
    lp.addRow(ex==0);
alpar@1361
    46
  }
alpar@1361
    47
  {
alpar@1381
    48
    LpDefault::Expr ex;
alpar@1361
    49
    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
alpar@1361
    50
    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
alpar@1361
    51
    lp.setObj(ex);
alpar@1361
    52
  }
alpar@1361
    53
  lp.max();
alpar@1361
    54
alpar@1381
    55
#ifdef HAVE_GLPK
alpar@1361
    56
  lp.presolver(true);
alpar@1361
    57
  lp.messageLevel(3);
alpar@1381
    58
#endif
alpar@1361
    59
alpar@1361
    60
  lp.solve();
alpar@1361
    61
alpar@1361
    62
  return lp.primalValue();
alpar@1361
    63
}
alpar@1361
    64
alpar@1361
    65
int main() 
alpar@1361
    66
{
alpar@1361
    67
  ListGraph g;
alpar@1361
    68
  ListGraph::Node s;
alpar@1361
    69
  ListGraph::Node t;
alpar@1361
    70
  
alpar@1361
    71
  ListGraph::EdgeMap<double> cap(g);
alpar@1361
    72
  
alpar@1361
    73
  GraphReader<ListGraph> reader(std::cin,g);
alpar@1361
    74
  reader.addNode("source",s).addNode("target",t)
alpar@1361
    75
    .addEdgeMap("capacity",cap).run();
alpar@1361
    76
  
alpar@1361
    77
  // std::ifstream file("../test/preflow_");
alpar@1361
    78
//   readDimacs(file, g, cap, s, t);
alpar@1361
    79
alpar@1361
    80
  std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
alpar@1361
    81
alpar@1361
    82
}