Started cplex low level interface.
authorathos
Mon, 04 Apr 2005 14:46:08 +0000
changeset 1299e2545ef5d7d8
parent 1298 d1ca90316b59
child 1300 d1fc1bf7decc
Started cplex low level interface.
src/work/athos/lp/lp_cplex.cc
src/work/athos/lp/lp_cplex.h
src/work/athos/lp/lp_test_cplex.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/athos/lp/lp_cplex.cc	Mon Apr 04 14:46:08 2005 +0000
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/lp_cplex.cc
     1.6 + * - Part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.9 + * (Egervary Combinatorial Optimization Research Group, EGRES).
    1.10 + *
    1.11 + * Permission to use, modify and distribute this software is granted
    1.12 + * provided that this copyright notice appears in all copies. For
    1.13 + * precise terms see the accompanying LICENSE file.
    1.14 + *
    1.15 + * This software is provided "AS IS" with no warranty of any kind,
    1.16 + * express or implied, and with no claim as to its suitability for any
    1.17 + * purpose.
    1.18 + *
    1.19 + */
    1.20 +
    1.21 +#include"lp_cplex.h"
    1.22 +
    1.23 +///\file
    1.24 +///\brief Implementation of the LEMON-CPLEX lp solver interface.
    1.25 +namespace lemon {
    1.26 +  
    1.27 +  int LpCplex::_addCol()
    1.28 +  {
    1.29 +    int i = CPXgetnumcols (env, lp);
    1.30 +    int lb[1],ub[1];
    1.31 +    lb[0]=-INF;//-CPX_INFBOUND;
    1.32 +    ub[0]=INF;//CPX_INFBOUND;
    1.33 +    status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
    1.34 +    return i;
    1.35 +  }
    1.36 +  
    1.37 +  int LpCplex::_addRow() 
    1.38 +  {
    1.39 +    int i = CPXgetnumrows (env, lp);
    1.40 +    status = CPXnewrows (env, lp, 1, NULL, NULL, NULL, NULL, NULL);
    1.41 +    return i;
    1.42 +  }
    1.43 +  
    1.44 +  ///\warning Data at index 0 is ignored iin the arrays.
    1.45 +  void LpCplex::_setRowCoeffs(int i, 
    1.46 +			      int length,
    1.47 +			      int  const * indices, 
    1.48 +			      Value  const * values )
    1.49 +  {
    1.50 +    int rowlist[length+1];
    1.51 +    for (int k=1;k<=length;++k){
    1.52 +      rowlist[k]=i;
    1.53 +    }
    1.54 +    status = CPXchgcoeflist(env, lp, 
    1.55 +			    length, 
    1.56 +			    rowlist++, 
    1.57 +			    inices++, 
    1.58 +			    values++);
    1.59 +  }
    1.60 +  
    1.61 +  void LpCplex::_setColCoeffs(int i, 
    1.62 +			      int length,
    1.63 +			      int  const * indices, 
    1.64 +			      Value  const * values)
    1.65 +  {
    1.66 +    int collist[length+1];
    1.67 +    for (int k=1;k<=length;++k){
    1.68 +      collist[k]=i;
    1.69 +    }
    1.70 +    status = CPXchgcoeflist(env, lp, 
    1.71 +			    length, 
    1.72 +			    inices++, 
    1.73 +			    collist++, 
    1.74 +			    values++);
    1.75 +  }
    1.76 +  
    1.77 +  void LpCplex::_setColLowerBound(int i, Value value)
    1.78 +  {
    1.79 +  }
    1.80 +  
    1.81 +  void LpCplex::_setColUpperBound(int i, Value value)
    1.82 +  {
    1.83 +  }
    1.84 +  
    1.85 +  void LpCplex::_setRowLowerBound(int i, Value value)
    1.86 +  {
    1.87 +  }
    1.88 +  
    1.89 +  void LpCplex::_setRowUpperBound(int i, Value value)
    1.90 +  {
    1.91 +  }
    1.92 +  
    1.93 +  void LpCplex::_setObjCoeff(int i, Value obj_coef)
    1.94 +  {
    1.95 +  }
    1.96 +
    1.97 +  LpCplex::SolutionType LpCplex::_solve()
    1.98 +  {
    1.99 +    return OPTIMAL;
   1.100 +  }
   1.101 +
   1.102 +  LpCplex::Value LpCplex::_getSolution(int i)
   1.103 +  {
   1.104 +    return 0;
   1.105 +  }
   1.106 +  
   1.107 +} //namespace lemon
   1.108 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/athos/lp/lp_cplex.h	Mon Apr 04 14:46:08 2005 +0000
     2.3 @@ -0,0 +1,122 @@
     2.4 +/* -*- C++ -*-
     2.5 + * src/lemon/lp_cplex.h - Part of LEMON, a generic C++ optimization library
     2.6 + *
     2.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     2.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     2.9 + *
    2.10 + * Permission to use, modify and distribute this software is granted
    2.11 + * provided that this copyright notice appears in all copies. For
    2.12 + * precise terms see the accompanying LICENSE file.
    2.13 + *
    2.14 + * This software is provided "AS IS" with no warranty of any kind,
    2.15 + * express or implied, and with no claim as to its suitability for any
    2.16 + * purpose.
    2.17 + *
    2.18 + */
    2.19 +
    2.20 +#ifndef LEMON_LP_CPLEX_H
    2.21 +#define LEMON_LP_CPLEX_H
    2.22 +
    2.23 +///\file
    2.24 +///\brief Header of the LEMON-CPLEX lp solver interface.
    2.25 +
    2.26 +#include "lp_base.h"
    2.27 +extern "C" {
    2.28 +#include "ilcplex/cplex.h"
    2.29 +}
    2.30 +
    2.31 +namespace lemon {
    2.32 +
    2.33 +
    2.34 +  /// \brief Wrapper for GLPK solver
    2.35 +  /// 
    2.36 +  /// This class implements a lemon wrapper for GLPK.
    2.37 +  class LpCplex : public LpSolverBase {
    2.38 +
    2.39 +  public:
    2.40 +
    2.41 +    typedef LpSolverBase Parent;
    2.42 +    
    2.43 +    /// \e
    2.44 +    int status;
    2.45 +    CPXENVptr env;
    2.46 +    CPXLPptr lp;
    2.47 +
    2.48 +
    2.49 +    /// \e
    2.50 +    LpCplex() : Parent() {
    2.51 +      env = NULL;
    2.52 +      lp = NULL;
    2.53 +      env = CPXopenCPLEXdevelop(&status);     
    2.54 +//     if (Env == NULL)
    2.55 +//     {
    2.56 +//          fprintf(stderr,"A CPLEX környezet megnyitása sikertelen.\n");
    2.57 +// 	 CPXgeterrorstring(Env, Status, ErrorMsg);
    2.58 +// 	 fprintf(stderr,"%s",ErrorMsg);
    2.59 +// 	 goto Terminate;
    2.60 +//     }
    2.61 +
    2.62 +    // *** A problema létrehozása ***
    2.63 +    lp = CPXcreateprob(env, &status, "LP problem");
    2.64 + 
    2.65 +//    if (Problem == NULL)
    2.66 +//     {
    2.67 +// 	fprintf(stderr,"Az LP létrehozása sikertelen");
    2.68 +// 	goto Terminate;
    2.69 +//     }
    2.70 + 
    2.71 +    }
    2.72 +    /// \e
    2.73 +    ~LpCplex() {
    2.74 +      status = CPXfreeprob(env,&lp); 
    2.75 +//       if (Status != 0)
    2.76 +// 	{
    2.77 +// 	  fprintf(stderr,"A CPLEX feladat törlése sikertelen.\n");
    2.78 +// 	  CPXgeterrorstring(Env, Status, ErrorMsg);
    2.79 +// 	  fprintf(stderr,"%s",ErrorMsg);
    2.80 +// 	  goto Terminate;
    2.81 +// 	}
    2.82 +       
    2.83 +      status = CPXcloseCPLEX(&env); 
    2.84 +//       if (Status != 0)
    2.85 +// 	{
    2.86 +// 	  fprintf(stderr,"A CPLEX környezet bezárása sikertelen.\n");
    2.87 +// 	  CPXgeterrorstring(Env, Status, ErrorMsg);
    2.88 +// 	  fprintf(stderr,"%s",ErrorMsg);
    2.89 +// 	  goto Terminate;
    2.90 +// 	}
    2.91 +      
    2.92 +    }
    2.93 +
    2.94 +  protected:
    2.95 +    virtual int _addCol();
    2.96 +    virtual int _addRow();
    2.97 +    virtual void _setRowCoeffs(int i, 
    2.98 +			       int length,
    2.99 +                               const int   * indices, 
   2.100 +                               const Value   * values );
   2.101 +    virtual void _setColCoeffs(int i, 
   2.102 +			       int length,
   2.103 +                               const int   * indices, 
   2.104 +                               const Value   * values);
   2.105 +    virtual void _setColLowerBound(int i, Value value);
   2.106 +    virtual void _setColUpperBound(int i, Value value);
   2.107 +    virtual void _setRowLowerBound(int i, Value value);
   2.108 +    virtual void _setRowUpperBound(int i, Value value);
   2.109 +    virtual void _setObjCoeff(int i, Value obj_coef);
   2.110 +    ///\e
   2.111 +    
   2.112 +    ///\bug Unimplemented
   2.113 +    ///
   2.114 +    virtual SolutionType _solve();
   2.115 +    ///\e
   2.116 +    
   2.117 +    ///\bug Unimplemented
   2.118 +    ///
   2.119 +    virtual Value _getSolution(int i);
   2.120 +
   2.121 +  };
   2.122 +} //END OF NAMESPACE LEMON
   2.123 +
   2.124 +#endif //LEMON_LP_CPLEX_H
   2.125 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/work/athos/lp/lp_test_cplex.cc	Mon Apr 04 14:46:08 2005 +0000
     3.3 @@ -0,0 +1,185 @@
     3.4 +#include"lp_solver_skeleton.h"
     3.5 +#include"lp_cplex.h"
     3.6 +#include<lemon/list_graph.h>
     3.7 +
     3.8 +using namespace lemon;
     3.9 +
    3.10 +void lpTest(LpSolverBase & lp)
    3.11 +{
    3.12 +  typedef LpSolverBase LP;
    3.13 +
    3.14 +  std::vector<LP::Col> x;
    3.15 +  for(int i=0;i<10;i++) x.push_back(lp.addCol());
    3.16 +
    3.17 +  std::vector<LP::Col> y(10);
    3.18 +  lp.addColSet(y);
    3.19 +
    3.20 +  std::map<int,LP::Col> z;
    3.21 +  
    3.22 +  z.insert(std::make_pair(12,INVALID));
    3.23 +  z.insert(std::make_pair(2,INVALID));
    3.24 +  z.insert(std::make_pair(7,INVALID));
    3.25 +  z.insert(std::make_pair(5,INVALID));
    3.26 +  
    3.27 +  lp.addColSet(z);
    3.28 +
    3.29 +
    3.30 +  LP::Expr e,f,g;
    3.31 +  LP::Col p1,p2,p3,p4,p5;
    3.32 +  LP::Constr c;
    3.33 +  
    3.34 +  e[p1]=2;
    3.35 +  e.constComp()=12;
    3.36 +  e[p1]+=2;
    3.37 +  e.constComp()+=12;
    3.38 +  e[p1]-=2;
    3.39 +  e.constComp()-=12;
    3.40 +  
    3.41 +  e=2;
    3.42 +  e=2.2;
    3.43 +  e=p1;
    3.44 +  e=f;
    3.45 +
    3.46 +  e+=2;
    3.47 +  e+=2.2;
    3.48 +  e+=p1;
    3.49 +  e+=f;
    3.50 +
    3.51 +  e-=2;
    3.52 +  e-=2.2;
    3.53 +  e-=p1;
    3.54 +  e-=f;
    3.55 +
    3.56 +  e*=2;
    3.57 +  e*=2.2;
    3.58 +  e/=2;
    3.59 +  e/=2.2;
    3.60 +
    3.61 +  e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
    3.62 +      (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
    3.63 +      (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
    3.64 +      2.2*f+f*2.2+f/2.2+
    3.65 +      2*f+f*2+f/2+
    3.66 +      2.2*p1+p1*2.2+p1/2.2+
    3.67 +      2*p1+p1*2+p1/2
    3.68 +     );
    3.69 +  
    3.70 +
    3.71 +  c = (e  <= f  );
    3.72 +  c = (e  <= 2.2);
    3.73 +  c = (e  <= 2  );
    3.74 +  c = (e  <= p1 );
    3.75 +  c = (2.2<= f  );
    3.76 +  c = (2  <= f  );
    3.77 +  c = (p1 <= f  );
    3.78 +  c = (p1 <= p2 );
    3.79 +  c = (p1 <= 2.2);
    3.80 +  c = (p1 <= 2  );
    3.81 +  c = (2.2<= p2 );
    3.82 +  c = (2  <= p2 );
    3.83 +
    3.84 +  c = (e  >= f  );
    3.85 +  c = (e  >= 2.2);
    3.86 +  c = (e  >= 2  );
    3.87 +  c = (e  >= p1 );
    3.88 +  c = (2.2>= f  );
    3.89 +  c = (2  >= f  );
    3.90 +  c = (p1 >= f  );
    3.91 +  c = (p1 >= p2 );
    3.92 +  c = (p1 >= 2.2);
    3.93 +  c = (p1 >= 2  );
    3.94 +  c = (2.2>= p2 );
    3.95 +  c = (2  >= p2 );
    3.96 +
    3.97 +  c = (e  == f  );
    3.98 +  c = (e  == 2.2);
    3.99 +  c = (e  == 2  );
   3.100 +  c = (e  == p1 );
   3.101 +  c = (2.2== f  );
   3.102 +  c = (2  == f  );
   3.103 +  c = (p1 == f  );
   3.104 +  //c = (p1 == p2 );
   3.105 +  c = (p1 == 2.2);
   3.106 +  c = (p1 == 2  );
   3.107 +  c = (2.2== p2 );
   3.108 +  c = (2  == p2 );
   3.109 +
   3.110 +  c = (2 <= e <= 3);
   3.111 +  c = (2 <= p1<= 3);
   3.112 +
   3.113 +  c = (2 >= e >= 3);
   3.114 +  c = (2 >= p1>= 3);
   3.115 +
   3.116 +  e[x[3]]=2;
   3.117 +  e[x[3]]=4;
   3.118 +  e[x[3]]=1;
   3.119 +  e.constComp()=12;
   3.120 +  
   3.121 +  lp.addRow(LP::INF,e,23);
   3.122 +  lp.addRow(LP::INF,3.0*(p1+p2)-p3,23);
   3.123 +  lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
   3.124 +  lp.addRow(LP::INF,3.0*(p1+p2*2-5*p3+12-p4/3)+2*p4-4,23);
   3.125 +  lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
   3.126 +
   3.127 +  lp.addRow(x[1]+x[3]<=x[5]-3);
   3.128 +  lp.addRow(-7<=x[1]+x[3]-12<=3);
   3.129 +  //lp.addRow(x[1]<=x[5]);
   3.130 +
   3.131 +}
   3.132 +
   3.133 +
   3.134 +template<class G,class C>
   3.135 +double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
   3.136 +{
   3.137 +  LpGlpk lp;
   3.138 +  
   3.139 +  typedef G Graph;
   3.140 +  typedef typename G::Node Node;
   3.141 +  typedef typename G::NodeIt NodeIt;
   3.142 +  typedef typename G::Edge Edge;
   3.143 +  typedef typename G::EdgeIt EdgeIt;
   3.144 +  typedef typename G::OutEdgeIt OutEdgeIt;
   3.145 +  typedef typename G::InEdgeIt InEdgeIt;
   3.146 +  
   3.147 +  typename G::EdgeMap<LpGlpk::Col> x(g);
   3.148 +  lp.addColSet(x);
   3.149 +   //for(EdgeIt e(g);e!=INVALID;++e) x[e]=lp.addCol();
   3.150 +  
   3.151 +  for(EdgeIt e(g);e!=INVALID;++e) {
   3.152 +    lp.setColUpperBound(x[e],cap[e]);
   3.153 +    lp.setColLowerBound(x[e],0);
   3.154 +  }
   3.155 +
   3.156 +  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
   3.157 +    LpGlpk::Expr ex;
   3.158 +    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
   3.159 +    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
   3.160 +    lp.addRow(0,ex,0);
   3.161 +  }
   3.162 +  {
   3.163 +    LpGlpk::Expr ex;
   3.164 +    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
   3.165 +    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
   3.166 +    lp.setObj(ex);
   3.167 +  }
   3.168 +
   3.169 +  lp.solve();
   3.170 +
   3.171 +  return 0;
   3.172 +}
   3.173 +
   3.174 +int main() 
   3.175 +{
   3.176 +  LpSolverSkeleton lp_skel;
   3.177 +  LpGlpk lp_glpk;
   3.178 +  LpCplex lp_cplex;
   3.179 +
   3.180 +  lpTest(lp_skel);
   3.181 +  lpTest(lp_cplex);
   3.182 +
   3.183 +  ListGraph g;
   3.184 +  ListGraph::EdgeMap<double> cap(g);
   3.185 +  
   3.186 +  maxFlow(g,cap,ListGraph::NodeIt(g),ListGraph::NodeIt(g));
   3.187 +
   3.188 +}