src/work/athos/lp/lp_test_cplex.cc
author alpar
Fri, 15 Apr 2005 19:56:25 +0000
changeset 1359 1581f961cfaa
permissions -rw-r--r--
Correct the english name of EGRES.
athos@1299
     1
#include"lp_solver_skeleton.h"
athos@1299
     2
#include"lp_cplex.h"
athos@1299
     3
#include<lemon/list_graph.h>
athos@1299
     4
athos@1299
     5
using namespace lemon;
athos@1299
     6
athos@1299
     7
void lpTest(LpSolverBase & lp)
athos@1299
     8
{
athos@1299
     9
  typedef LpSolverBase LP;
athos@1299
    10
athos@1299
    11
  std::vector<LP::Col> x;
athos@1299
    12
  for(int i=0;i<10;i++) x.push_back(lp.addCol());
athos@1299
    13
athos@1299
    14
  std::vector<LP::Col> y(10);
athos@1299
    15
  lp.addColSet(y);
athos@1299
    16
athos@1299
    17
  std::map<int,LP::Col> z;
athos@1299
    18
  
athos@1299
    19
  z.insert(std::make_pair(12,INVALID));
athos@1299
    20
  z.insert(std::make_pair(2,INVALID));
athos@1299
    21
  z.insert(std::make_pair(7,INVALID));
athos@1299
    22
  z.insert(std::make_pair(5,INVALID));
athos@1299
    23
  
athos@1299
    24
  lp.addColSet(z);
athos@1299
    25
athos@1299
    26
athos@1299
    27
  LP::Expr e,f,g;
athos@1299
    28
  LP::Col p1,p2,p3,p4,p5;
athos@1299
    29
  LP::Constr c;
athos@1299
    30
  
athos@1299
    31
  e[p1]=2;
athos@1299
    32
  e.constComp()=12;
athos@1299
    33
  e[p1]+=2;
athos@1299
    34
  e.constComp()+=12;
athos@1299
    35
  e[p1]-=2;
athos@1299
    36
  e.constComp()-=12;
athos@1299
    37
  
athos@1299
    38
  e=2;
athos@1299
    39
  e=2.2;
athos@1299
    40
  e=p1;
athos@1299
    41
  e=f;
athos@1299
    42
athos@1299
    43
  e+=2;
athos@1299
    44
  e+=2.2;
athos@1299
    45
  e+=p1;
athos@1299
    46
  e+=f;
athos@1299
    47
athos@1299
    48
  e-=2;
athos@1299
    49
  e-=2.2;
athos@1299
    50
  e-=p1;
athos@1299
    51
  e-=f;
athos@1299
    52
athos@1299
    53
  e*=2;
athos@1299
    54
  e*=2.2;
athos@1299
    55
  e/=2;
athos@1299
    56
  e/=2.2;
athos@1299
    57
athos@1299
    58
  e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
athos@1299
    59
      (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
athos@1299
    60
      (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
athos@1299
    61
      2.2*f+f*2.2+f/2.2+
athos@1299
    62
      2*f+f*2+f/2+
athos@1299
    63
      2.2*p1+p1*2.2+p1/2.2+
athos@1299
    64
      2*p1+p1*2+p1/2
athos@1299
    65
     );
athos@1299
    66
  
athos@1299
    67
athos@1299
    68
  c = (e  <= f  );
athos@1299
    69
  c = (e  <= 2.2);
athos@1299
    70
  c = (e  <= 2  );
athos@1299
    71
  c = (e  <= p1 );
athos@1299
    72
  c = (2.2<= f  );
athos@1299
    73
  c = (2  <= f  );
athos@1299
    74
  c = (p1 <= f  );
athos@1299
    75
  c = (p1 <= p2 );
athos@1299
    76
  c = (p1 <= 2.2);
athos@1299
    77
  c = (p1 <= 2  );
athos@1299
    78
  c = (2.2<= p2 );
athos@1299
    79
  c = (2  <= p2 );
athos@1299
    80
athos@1299
    81
  c = (e  >= f  );
athos@1299
    82
  c = (e  >= 2.2);
athos@1299
    83
  c = (e  >= 2  );
athos@1299
    84
  c = (e  >= p1 );
athos@1299
    85
  c = (2.2>= f  );
athos@1299
    86
  c = (2  >= f  );
athos@1299
    87
  c = (p1 >= f  );
athos@1299
    88
  c = (p1 >= p2 );
athos@1299
    89
  c = (p1 >= 2.2);
athos@1299
    90
  c = (p1 >= 2  );
athos@1299
    91
  c = (2.2>= p2 );
athos@1299
    92
  c = (2  >= p2 );
athos@1299
    93
athos@1299
    94
  c = (e  == f  );
athos@1299
    95
  c = (e  == 2.2);
athos@1299
    96
  c = (e  == 2  );
athos@1299
    97
  c = (e  == p1 );
athos@1299
    98
  c = (2.2== f  );
athos@1299
    99
  c = (2  == f  );
athos@1299
   100
  c = (p1 == f  );
athos@1299
   101
  //c = (p1 == p2 );
athos@1299
   102
  c = (p1 == 2.2);
athos@1299
   103
  c = (p1 == 2  );
athos@1299
   104
  c = (2.2== p2 );
athos@1299
   105
  c = (2  == p2 );
athos@1299
   106
athos@1299
   107
  c = (2 <= e <= 3);
athos@1299
   108
  c = (2 <= p1<= 3);
athos@1299
   109
athos@1299
   110
  c = (2 >= e >= 3);
athos@1299
   111
  c = (2 >= p1>= 3);
athos@1299
   112
athos@1299
   113
  e[x[3]]=2;
athos@1299
   114
  e[x[3]]=4;
athos@1299
   115
  e[x[3]]=1;
athos@1299
   116
  e.constComp()=12;
athos@1299
   117
  
athos@1299
   118
  lp.addRow(LP::INF,e,23);
athos@1299
   119
  lp.addRow(LP::INF,3.0*(p1+p2)-p3,23);
athos@1299
   120
  lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
athos@1299
   121
  lp.addRow(LP::INF,3.0*(p1+p2*2-5*p3+12-p4/3)+2*p4-4,23);
athos@1299
   122
  lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
athos@1299
   123
athos@1299
   124
  lp.addRow(x[1]+x[3]<=x[5]-3);
athos@1299
   125
  lp.addRow(-7<=x[1]+x[3]-12<=3);
athos@1299
   126
  //lp.addRow(x[1]<=x[5]);
athos@1299
   127
athos@1299
   128
}
athos@1299
   129
athos@1299
   130
athos@1299
   131
template<class G,class C>
athos@1299
   132
double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
athos@1299
   133
{
athos@1299
   134
  LpGlpk lp;
athos@1299
   135
  
athos@1299
   136
  typedef G Graph;
athos@1299
   137
  typedef typename G::Node Node;
athos@1299
   138
  typedef typename G::NodeIt NodeIt;
athos@1299
   139
  typedef typename G::Edge Edge;
athos@1299
   140
  typedef typename G::EdgeIt EdgeIt;
athos@1299
   141
  typedef typename G::OutEdgeIt OutEdgeIt;
athos@1299
   142
  typedef typename G::InEdgeIt InEdgeIt;
athos@1299
   143
  
athos@1299
   144
  typename G::EdgeMap<LpGlpk::Col> x(g);
athos@1299
   145
  lp.addColSet(x);
athos@1299
   146
   //for(EdgeIt e(g);e!=INVALID;++e) x[e]=lp.addCol();
athos@1299
   147
  
athos@1299
   148
  for(EdgeIt e(g);e!=INVALID;++e) {
athos@1299
   149
    lp.setColUpperBound(x[e],cap[e]);
athos@1299
   150
    lp.setColLowerBound(x[e],0);
athos@1299
   151
  }
athos@1299
   152
athos@1299
   153
  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
athos@1299
   154
    LpGlpk::Expr ex;
athos@1299
   155
    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
athos@1299
   156
    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
athos@1299
   157
    lp.addRow(0,ex,0);
athos@1299
   158
  }
athos@1299
   159
  {
athos@1299
   160
    LpGlpk::Expr ex;
athos@1299
   161
    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
athos@1299
   162
    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
athos@1299
   163
    lp.setObj(ex);
athos@1299
   164
  }
athos@1299
   165
athos@1299
   166
  lp.solve();
athos@1299
   167
athos@1299
   168
  return 0;
athos@1299
   169
}
athos@1299
   170
athos@1299
   171
int main() 
athos@1299
   172
{
athos@1299
   173
  LpSolverSkeleton lp_skel;
athos@1299
   174
  LpGlpk lp_glpk;
athos@1299
   175
  LpCplex lp_cplex;
athos@1299
   176
athos@1299
   177
  lpTest(lp_skel);
athos@1299
   178
  lpTest(lp_cplex);
athos@1299
   179
athos@1299
   180
  ListGraph g;
athos@1299
   181
  ListGraph::EdgeMap<double> cap(g);
athos@1299
   182
  
athos@1299
   183
  maxFlow(g,cap,ListGraph::NodeIt(g),ListGraph::NodeIt(g));
athos@1299
   184
athos@1299
   185
}