src/work/athos/lp/lp_test.cc
author alpar
Fri, 25 Mar 2005 18:56:07 +0000
changeset 1264 92ba3e62825d
parent 1263 a490938ad0aa
child 1272 17be4c5bc6c6
permissions -rw-r--r--
Constraints (expressions containing <= or >=) can be passed to addRow()
and setRow()
     1 #include"lp_solver_skeleton.h"
     2 #include"lp_glpk.h"
     3 #include<lemon/list_graph.h>
     4 
     5 using namespace lemon;
     6 
     7 void lpTest(LpSolverBase & lp)
     8 {
     9   typedef LpSolverBase LP;
    10 
    11   std::vector<LP::Col> x;
    12   for(int i=0;i<10;i++) x.push_back(lp.addCol());
    13 
    14   std::vector<LP::Col> y(10);
    15   lp.addColSet(y);
    16 
    17   std::map<int,LP::Col> z;
    18   
    19   z.insert(std::make_pair(12,INVALID));
    20   z.insert(std::make_pair(2,INVALID));
    21   z.insert(std::make_pair(7,INVALID));
    22   z.insert(std::make_pair(5,INVALID));
    23   
    24   lp.addColSet(z);
    25 
    26 
    27   LP::Expr e;
    28   e[x[3]]=2;
    29   e[x[3]]=4;
    30   e[x[3]]=1;
    31   e.constComp()=12;
    32 
    33   LP::Col p1,p2,p3,p4,p5;
    34   
    35   lp.addRow(LP::INF,e,23);
    36   lp.addRow(LP::INF,3.0*(p1+p2)-p3,23);
    37   lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
    38   lp.addRow(LP::INF,3.0*(p1+p2*2-5*p3+12-p4/3)+2*p4-4,23);
    39   lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
    40 
    41   lp.addRow(x[1]+x[3]<=x[5]-3);
    42   lp.addRow(-7<=x[1]+x[3]-12<=3);
    43 }
    44 
    45 
    46 template<class G,class C>
    47 double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
    48 {
    49   LpGlpk lp;
    50   
    51   typedef G Graph;
    52   typedef typename G::Node Node;
    53   typedef typename G::NodeIt NodeIt;
    54   typedef typename G::Edge Edge;
    55   typedef typename G::EdgeIt EdgeIt;
    56   typedef typename G::OutEdgeIt OutEdgeIt;
    57   typedef typename G::InEdgeIt InEdgeIt;
    58   
    59   typename G::EdgeMap<LpGlpk::Col> x(g);
    60   // lp.addColSet(x);
    61   for(EdgeIt e(g);e!=INVALID;++e) x[e]=lp.addCol();
    62   
    63   for(EdgeIt e(g);e!=INVALID;++e) {
    64     lp.setColUpperBound(x[e],cap[e]);
    65     lp.setColLowerBound(x[e],0);
    66   }
    67 
    68   for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
    69     LpGlpk::Expr ex;
    70     for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
    71     for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
    72     lp.addRow(0,ex,0);
    73   }
    74   {
    75     LpGlpk::Expr ex;
    76     for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
    77     for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
    78     lp.setObj(ex);
    79   }
    80 
    81   lp.solve();
    82 
    83   return 0;
    84 }
    85 
    86 int main() 
    87 {
    88   LpSolverSkeleton lp_skel;
    89   LpGlpk lp_glpk;
    90 
    91   lpTest(lp_skel);
    92   lpTest(lp_glpk);
    93 
    94   ListGraph g;
    95   ListGraph::EdgeMap<double> cap(g);
    96   
    97   maxFlow(g,cap,ListGraph::NodeIt(g),ListGraph::NodeIt(g));
    98 
    99 }