src/work/athos/lp/lp_test_cplex.cc
changeset 1299 e2545ef5d7d8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/athos/lp/lp_test_cplex.cc	Mon Apr 04 14:46:08 2005 +0000
     1.3 @@ -0,0 +1,185 @@
     1.4 +#include"lp_solver_skeleton.h"
     1.5 +#include"lp_cplex.h"
     1.6 +#include<lemon/list_graph.h>
     1.7 +
     1.8 +using namespace lemon;
     1.9 +
    1.10 +void lpTest(LpSolverBase & lp)
    1.11 +{
    1.12 +  typedef LpSolverBase LP;
    1.13 +
    1.14 +  std::vector<LP::Col> x;
    1.15 +  for(int i=0;i<10;i++) x.push_back(lp.addCol());
    1.16 +
    1.17 +  std::vector<LP::Col> y(10);
    1.18 +  lp.addColSet(y);
    1.19 +
    1.20 +  std::map<int,LP::Col> z;
    1.21 +  
    1.22 +  z.insert(std::make_pair(12,INVALID));
    1.23 +  z.insert(std::make_pair(2,INVALID));
    1.24 +  z.insert(std::make_pair(7,INVALID));
    1.25 +  z.insert(std::make_pair(5,INVALID));
    1.26 +  
    1.27 +  lp.addColSet(z);
    1.28 +
    1.29 +
    1.30 +  LP::Expr e,f,g;
    1.31 +  LP::Col p1,p2,p3,p4,p5;
    1.32 +  LP::Constr c;
    1.33 +  
    1.34 +  e[p1]=2;
    1.35 +  e.constComp()=12;
    1.36 +  e[p1]+=2;
    1.37 +  e.constComp()+=12;
    1.38 +  e[p1]-=2;
    1.39 +  e.constComp()-=12;
    1.40 +  
    1.41 +  e=2;
    1.42 +  e=2.2;
    1.43 +  e=p1;
    1.44 +  e=f;
    1.45 +
    1.46 +  e+=2;
    1.47 +  e+=2.2;
    1.48 +  e+=p1;
    1.49 +  e+=f;
    1.50 +
    1.51 +  e-=2;
    1.52 +  e-=2.2;
    1.53 +  e-=p1;
    1.54 +  e-=f;
    1.55 +
    1.56 +  e*=2;
    1.57 +  e*=2.2;
    1.58 +  e/=2;
    1.59 +  e/=2.2;
    1.60 +
    1.61 +  e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
    1.62 +      (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
    1.63 +      (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
    1.64 +      2.2*f+f*2.2+f/2.2+
    1.65 +      2*f+f*2+f/2+
    1.66 +      2.2*p1+p1*2.2+p1/2.2+
    1.67 +      2*p1+p1*2+p1/2
    1.68 +     );
    1.69 +  
    1.70 +
    1.71 +  c = (e  <= f  );
    1.72 +  c = (e  <= 2.2);
    1.73 +  c = (e  <= 2  );
    1.74 +  c = (e  <= p1 );
    1.75 +  c = (2.2<= f  );
    1.76 +  c = (2  <= f  );
    1.77 +  c = (p1 <= f  );
    1.78 +  c = (p1 <= p2 );
    1.79 +  c = (p1 <= 2.2);
    1.80 +  c = (p1 <= 2  );
    1.81 +  c = (2.2<= p2 );
    1.82 +  c = (2  <= p2 );
    1.83 +
    1.84 +  c = (e  >= f  );
    1.85 +  c = (e  >= 2.2);
    1.86 +  c = (e  >= 2  );
    1.87 +  c = (e  >= p1 );
    1.88 +  c = (2.2>= f  );
    1.89 +  c = (2  >= f  );
    1.90 +  c = (p1 >= f  );
    1.91 +  c = (p1 >= p2 );
    1.92 +  c = (p1 >= 2.2);
    1.93 +  c = (p1 >= 2  );
    1.94 +  c = (2.2>= p2 );
    1.95 +  c = (2  >= p2 );
    1.96 +
    1.97 +  c = (e  == f  );
    1.98 +  c = (e  == 2.2);
    1.99 +  c = (e  == 2  );
   1.100 +  c = (e  == p1 );
   1.101 +  c = (2.2== f  );
   1.102 +  c = (2  == f  );
   1.103 +  c = (p1 == f  );
   1.104 +  //c = (p1 == p2 );
   1.105 +  c = (p1 == 2.2);
   1.106 +  c = (p1 == 2  );
   1.107 +  c = (2.2== p2 );
   1.108 +  c = (2  == p2 );
   1.109 +
   1.110 +  c = (2 <= e <= 3);
   1.111 +  c = (2 <= p1<= 3);
   1.112 +
   1.113 +  c = (2 >= e >= 3);
   1.114 +  c = (2 >= p1>= 3);
   1.115 +
   1.116 +  e[x[3]]=2;
   1.117 +  e[x[3]]=4;
   1.118 +  e[x[3]]=1;
   1.119 +  e.constComp()=12;
   1.120 +  
   1.121 +  lp.addRow(LP::INF,e,23);
   1.122 +  lp.addRow(LP::INF,3.0*(p1+p2)-p3,23);
   1.123 +  lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
   1.124 +  lp.addRow(LP::INF,3.0*(p1+p2*2-5*p3+12-p4/3)+2*p4-4,23);
   1.125 +  lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
   1.126 +
   1.127 +  lp.addRow(x[1]+x[3]<=x[5]-3);
   1.128 +  lp.addRow(-7<=x[1]+x[3]-12<=3);
   1.129 +  //lp.addRow(x[1]<=x[5]);
   1.130 +
   1.131 +}
   1.132 +
   1.133 +
   1.134 +template<class G,class C>
   1.135 +double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
   1.136 +{
   1.137 +  LpGlpk lp;
   1.138 +  
   1.139 +  typedef G Graph;
   1.140 +  typedef typename G::Node Node;
   1.141 +  typedef typename G::NodeIt NodeIt;
   1.142 +  typedef typename G::Edge Edge;
   1.143 +  typedef typename G::EdgeIt EdgeIt;
   1.144 +  typedef typename G::OutEdgeIt OutEdgeIt;
   1.145 +  typedef typename G::InEdgeIt InEdgeIt;
   1.146 +  
   1.147 +  typename G::EdgeMap<LpGlpk::Col> x(g);
   1.148 +  lp.addColSet(x);
   1.149 +   //for(EdgeIt e(g);e!=INVALID;++e) x[e]=lp.addCol();
   1.150 +  
   1.151 +  for(EdgeIt e(g);e!=INVALID;++e) {
   1.152 +    lp.setColUpperBound(x[e],cap[e]);
   1.153 +    lp.setColLowerBound(x[e],0);
   1.154 +  }
   1.155 +
   1.156 +  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
   1.157 +    LpGlpk::Expr ex;
   1.158 +    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
   1.159 +    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
   1.160 +    lp.addRow(0,ex,0);
   1.161 +  }
   1.162 +  {
   1.163 +    LpGlpk::Expr ex;
   1.164 +    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
   1.165 +    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
   1.166 +    lp.setObj(ex);
   1.167 +  }
   1.168 +
   1.169 +  lp.solve();
   1.170 +
   1.171 +  return 0;
   1.172 +}
   1.173 +
   1.174 +int main() 
   1.175 +{
   1.176 +  LpSolverSkeleton lp_skel;
   1.177 +  LpGlpk lp_glpk;
   1.178 +  LpCplex lp_cplex;
   1.179 +
   1.180 +  lpTest(lp_skel);
   1.181 +  lpTest(lp_cplex);
   1.182 +
   1.183 +  ListGraph g;
   1.184 +  ListGraph::EdgeMap<double> cap(g);
   1.185 +  
   1.186 +  maxFlow(g,cap,ListGraph::NodeIt(g),ListGraph::NodeIt(g));
   1.187 +
   1.188 +}