src/work/athos/lp/lp_test.cc
author hegyi
Fri, 01 Apr 2005 09:44:29 +0000
changeset 1289 142633fc5014
parent 1272 17be4c5bc6c6
child 1293 8ede2a6b2594
permissions -rw-r--r--
Graph displayer is now displaying nodes. Edges remain still undisplayed yet.
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@1272
    27
  LP::Expr e,f,g;
alpar@1272
    28
  LP::Col p1,p2,p3,p4,p5;
alpar@1272
    29
  LP::Constr c;
alpar@1272
    30
  
alpar@1272
    31
  e[p1]=2;
alpar@1272
    32
  e.constComp()=12;
alpar@1272
    33
  e[p1]+=2;
alpar@1272
    34
  e.constComp()+=12;
alpar@1272
    35
  e[p1]-=2;
alpar@1272
    36
  e.constComp()-=12;
alpar@1272
    37
  
alpar@1272
    38
  e=2;
alpar@1272
    39
  e=2.2;
alpar@1272
    40
  e=p1;
alpar@1272
    41
  e=f;
alpar@1272
    42
alpar@1272
    43
  e+=2;
alpar@1272
    44
  e+=2.2;
alpar@1272
    45
  e+=p1;
alpar@1272
    46
  e+=f;
alpar@1272
    47
alpar@1272
    48
  e-=2;
alpar@1272
    49
  e-=2.2;
alpar@1272
    50
  e-=p1;
alpar@1272
    51
  e-=f;
alpar@1272
    52
alpar@1272
    53
  e*=2;
alpar@1272
    54
  e*=2.2;
alpar@1272
    55
  e/=2;
alpar@1272
    56
  e/=2.2;
alpar@1272
    57
alpar@1272
    58
  e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
alpar@1272
    59
      (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
alpar@1272
    60
      (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
alpar@1272
    61
      2.2*f+f*2.2+f/2.2+
alpar@1272
    62
      2*f+f*2+f/2+
alpar@1272
    63
      2.2*p1+p1*2.2+p1/2.2+
alpar@1272
    64
      2*p1+p1*2+p1/2
alpar@1272
    65
     );
alpar@1272
    66
  
alpar@1272
    67
alpar@1272
    68
  c = (e  <= f  );
alpar@1272
    69
  c = (e  <= 2.2);
alpar@1272
    70
  c = (e  <= 2  );
alpar@1272
    71
  c = (e  <= p1 );
alpar@1272
    72
  c = (2.2<= f  );
alpar@1272
    73
  c = (2  <= f  );
alpar@1272
    74
  c = (p1 <= f  );
alpar@1272
    75
  c = (p1 <= p2 );
alpar@1272
    76
  c = (p1 <= 2.2);
alpar@1272
    77
  c = (p1 <= 2  );
alpar@1272
    78
  c = (2.2<= p2 );
alpar@1272
    79
  c = (2  <= p2 );
alpar@1272
    80
alpar@1272
    81
  c = (e  >= f  );
alpar@1272
    82
  c = (e  >= 2.2);
alpar@1272
    83
  c = (e  >= 2  );
alpar@1272
    84
  c = (e  >= p1 );
alpar@1272
    85
  c = (2.2>= f  );
alpar@1272
    86
  c = (2  >= f  );
alpar@1272
    87
  c = (p1 >= f  );
alpar@1272
    88
  c = (p1 >= p2 );
alpar@1272
    89
  c = (p1 >= 2.2);
alpar@1272
    90
  c = (p1 >= 2  );
alpar@1272
    91
  c = (2.2>= p2 );
alpar@1272
    92
  c = (2  >= p2 );
alpar@1272
    93
alpar@1272
    94
  c = (e  == f  );
alpar@1272
    95
  c = (e  == 2.2);
alpar@1272
    96
  c = (e  == 2  );
alpar@1272
    97
  c = (e  == p1 );
alpar@1272
    98
  c = (2.2== f  );
alpar@1272
    99
  c = (2  == f  );
alpar@1272
   100
  c = (p1 == f  );
alpar@1272
   101
  //c = (p1 == p2 );
alpar@1272
   102
  c = (p1 == 2.2);
alpar@1272
   103
  c = (p1 == 2  );
alpar@1272
   104
  c = (2.2== p2 );
alpar@1272
   105
  c = (2  == p2 );
alpar@1272
   106
alpar@1272
   107
  c = (2 <= e <= 3);
alpar@1272
   108
  c = (2 <= p1<= 3);
alpar@1272
   109
alpar@1272
   110
  c = (2 >= e >= 3);
alpar@1272
   111
  c = (2 >= p1>= 3);
alpar@1272
   112
alpar@1256
   113
  e[x[3]]=2;
alpar@1256
   114
  e[x[3]]=4;
alpar@1256
   115
  e[x[3]]=1;
alpar@1256
   116
  e.constComp()=12;
alpar@1259
   117
  
alpar@1256
   118
  lp.addRow(LP::INF,e,23);
alpar@1259
   119
  lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
alpar@1259
   120
  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
   121
alpar@1264
   122
  lp.addRow(x[1]+x[3]<=x[5]-3);
alpar@1264
   123
  lp.addRow(-7<=x[1]+x[3]-12<=3);
alpar@1273
   124
  lp.addRow(x[1]<=x[5]);
alpar@1272
   125
alpar@1254
   126
}
alpar@1263
   127
alpar@1263
   128
alpar@1264
   129
template<class G,class C>
alpar@1264
   130
double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
alpar@1264
   131
{
alpar@1264
   132
  LpGlpk lp;
alpar@1264
   133
  
alpar@1264
   134
  typedef G Graph;
alpar@1264
   135
  typedef typename G::Node Node;
alpar@1264
   136
  typedef typename G::NodeIt NodeIt;
alpar@1264
   137
  typedef typename G::Edge Edge;
alpar@1264
   138
  typedef typename G::EdgeIt EdgeIt;
alpar@1264
   139
  typedef typename G::OutEdgeIt OutEdgeIt;
alpar@1264
   140
  typedef typename G::InEdgeIt InEdgeIt;
alpar@1264
   141
  
alpar@1264
   142
  typename G::EdgeMap<LpGlpk::Col> x(g);
alpar@1272
   143
  lp.addColSet(x);
alpar@1264
   144
  
alpar@1264
   145
  for(EdgeIt e(g);e!=INVALID;++e) {
alpar@1264
   146
    lp.setColUpperBound(x[e],cap[e]);
alpar@1264
   147
    lp.setColLowerBound(x[e],0);
alpar@1264
   148
  }
alpar@1264
   149
alpar@1264
   150
  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
alpar@1264
   151
    LpGlpk::Expr ex;
alpar@1264
   152
    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
alpar@1264
   153
    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
alpar@1273
   154
    lp.addRow(ex==0);
alpar@1264
   155
  }
alpar@1264
   156
  {
alpar@1264
   157
    LpGlpk::Expr ex;
alpar@1264
   158
    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
alpar@1264
   159
    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
alpar@1264
   160
    lp.setObj(ex);
alpar@1264
   161
  }
alpar@1264
   162
alpar@1264
   163
  lp.solve();
alpar@1264
   164
alpar@1264
   165
  return 0;
alpar@1264
   166
}
alpar@1264
   167
alpar@1263
   168
int main() 
alpar@1263
   169
{
alpar@1263
   170
  LpSolverSkeleton lp_skel;
alpar@1263
   171
  LpGlpk lp_glpk;
alpar@1263
   172
alpar@1263
   173
  lpTest(lp_skel);
alpar@1263
   174
  lpTest(lp_glpk);
alpar@1264
   175
alpar@1264
   176
  ListGraph g;
alpar@1273
   177
  ListGraph::Node s=g.addNode();
alpar@1273
   178
  ListGraph::Node t=g.addNode();
alpar@1273
   179
alpar@1264
   180
  ListGraph::EdgeMap<double> cap(g);
alpar@1264
   181
  
alpar@1273
   182
  maxFlow(g,cap,s,t);
alpar@1264
   183
alpar@1263
   184
}