src/demo/lp_demo.cc
author alpar
Tue, 12 Apr 2005 14:24:57 +0000
changeset 1341 bda966891ea0
parent 1322 cfc26d103bcf
permissions -rw-r--r--
GraphReader is made silent
     1 #include<lemon/lp_glpk.h>
     2 #include<lemon/graph_reader.h>
     3 #include<lemon/list_graph.h>
     4 
     5 using namespace lemon;
     6 
     7 template<class G,class C>
     8 double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
     9 {
    10   LpGlpk lp;
    11   
    12   typedef G Graph;
    13   typedef typename G::Node Node;
    14   typedef typename G::NodeIt NodeIt;
    15   typedef typename G::Edge Edge;
    16   typedef typename G::EdgeIt EdgeIt;
    17   typedef typename G::OutEdgeIt OutEdgeIt;
    18   typedef typename G::InEdgeIt InEdgeIt;
    19   
    20   typename G::template EdgeMap<LpGlpk::Col> x(g);
    21   lp.addColSet(x);
    22   
    23   for(EdgeIt e(g);e!=INVALID;++e) {
    24     lp.colUpperBound(x[e],cap[e]);
    25     lp.colLowerBound(x[e],0);
    26   }
    27 
    28   for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
    29     LpGlpk::Expr ex;
    30     for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
    31     for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
    32     lp.addRow(ex==0);
    33   }
    34   {
    35     LpGlpk::Expr ex;
    36     for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
    37     for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
    38     lp.setObj(ex);
    39   }
    40   lp.max();
    41 
    42   lp.presolver(true);
    43   
    44   lp.messageLevel(3);
    45 
    46   lp.solve();
    47 
    48   return lp.primalValue();
    49 }
    50 
    51 int main() 
    52 {
    53   ListGraph g;
    54   ListGraph::Node s;
    55   ListGraph::Node t;
    56   
    57   ListGraph::EdgeMap<double> cap(g);
    58   
    59   GraphReader<ListGraph> reader(std::cin,g);
    60   reader.addNode("source",s).addNode("target",t)
    61     .addEdgeMap("capacity",cap).run();
    62   
    63   // std::ifstream file("../test/preflow_");
    64 //   readDimacs(file, g, cap, s, t);
    65 
    66   std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
    67 
    68 }