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