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