src/lemon/lp_cplex.cc
changeset 1381 998e8def9676
child 1405 3626c7f10f14
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lemon/lp_cplex.cc	Fri Apr 22 17:47:01 2005 +0000
     1.3 @@ -0,0 +1,309 @@
     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 Research Group on Combinatorial Optimization, 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 +  LpCplex::LpCplex() : LpSolverBase() {
    1.28 +    env = NULL;
    1.29 +    lp = NULL;
    1.30 +    env = CPXopenCPLEXdevelop(&status);     
    1.31 +//     if (Env == NULL)
    1.32 +//     {
    1.33 +//          fprintf(stderr,"A CPLEX környezet megnyitása sikertelen.\n");
    1.34 +// 	 CPXgeterrorstring(Env, Status, ErrorMsg);
    1.35 +// 	 fprintf(stderr,"%s",ErrorMsg);
    1.36 +// 	 goto Terminate;
    1.37 +//     }
    1.38 +    
    1.39 +    // *** A problema létrehozása ***
    1.40 +    lp = CPXcreateprob(env, &status, "LP problem");
    1.41 +    
    1.42 +    //    if (Problem == NULL)
    1.43 +//     {
    1.44 +// 	fprintf(stderr,"Az LP létrehozása sikertelen");
    1.45 +// 	goto Terminate;
    1.46 +//     }
    1.47 +    
    1.48 +  }
    1.49 +  
    1.50 +  LpCplex::~LpCplex() {
    1.51 +    status = CPXfreeprob(env,&lp); 
    1.52 +    //       if (Status != 0)
    1.53 +    // 	{
    1.54 +// 	  fprintf(stderr,"A CPLEX feladat törlése sikertelen.\n");
    1.55 +// 	  CPXgeterrorstring(Env, Status, ErrorMsg);
    1.56 +// 	  fprintf(stderr,"%s",ErrorMsg);
    1.57 +// 	  goto Terminate;
    1.58 +// 	}
    1.59 +       
    1.60 +    status = CPXcloseCPLEX(&env); 
    1.61 +    //       if (Status != 0)
    1.62 +    // 	{
    1.63 +    // 	  fprintf(stderr,"A CPLEX környezet bezárása sikertelen.\n");
    1.64 +// 	  CPXgeterrorstring(Env, Status, ErrorMsg);
    1.65 +// 	  fprintf(stderr,"%s",ErrorMsg);
    1.66 +// 	  goto Terminate;
    1.67 +// 	}
    1.68 +      
    1.69 +  }
    1.70 +  
    1.71 +  LpSolverBase &LpCplex::_newLp() {return *(LpSolverBase*)0;}
    1.72 +  LpSolverBase &LpCplex::_copyLp() {return *(LpSolverBase*)0;}
    1.73 +
    1.74 +  int LpCplex::_addCol()
    1.75 +  {
    1.76 +    int i = CPXgetnumcols (env, lp);
    1.77 +    Value lb[1],ub[1];
    1.78 +    lb[0]=-INF;//-CPX_INFBOUND;
    1.79 +    ub[0]=INF;//CPX_INFBOUND;
    1.80 +    status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
    1.81 +    return i;
    1.82 +  }
    1.83 +  
    1.84 +  int LpCplex::_addRow() 
    1.85 +  {
    1.86 +    //We want a row that is not constrained
    1.87 +    char sense[1];
    1.88 +    sense[0]='L';//<= constraint
    1.89 +    Value rhs[1];
    1.90 +    rhs[0]=INF;
    1.91 +    int i = CPXgetnumrows (env, lp);
    1.92 +    status = CPXnewrows (env, lp, 1, rhs, sense, NULL, NULL);
    1.93 +    return i;
    1.94 +  }
    1.95 +  
    1.96 +  ///\warning Data at index 0 is ignored in the arrays.
    1.97 +  void LpCplex::_setRowCoeffs(int i, 
    1.98 +			      int length,
    1.99 +			      int  const * indices, 
   1.100 +			      Value  const * values )
   1.101 +  {
   1.102 +    int rowlist[length+1];
   1.103 +    int* p=rowlist;
   1.104 +    for (int k=1;k<=length;++k){
   1.105 +      rowlist[k]=i;
   1.106 +    }
   1.107 +    status = CPXchgcoeflist(env, lp, 
   1.108 +			    length, 
   1.109 +			    p+1, 
   1.110 +			    const_cast<int * >(indices+1), 
   1.111 +			    const_cast<Value * >(values+1));
   1.112 +  }
   1.113 +  
   1.114 +  void LpCplex::_setColCoeffs(int i, 
   1.115 +			      int length,
   1.116 +			      int  const * indices, 
   1.117 +			      Value  const * values)
   1.118 +  {
   1.119 +    int collist[length+1];
   1.120 +    int* p=collist;
   1.121 +    for (int k=1;k<=length;++k){
   1.122 +      collist[k]=i;
   1.123 +    }
   1.124 +    status = CPXchgcoeflist(env, lp, 
   1.125 +			    length, 
   1.126 +			    const_cast<int * >(indices+1), 
   1.127 +			    p+1, 
   1.128 +			    const_cast<Value * >(values+1));
   1.129 +  }
   1.130 +  
   1.131 +  void LpCplex::_setColLowerBound(int i, Value value)
   1.132 +  {
   1.133 +    int indices[1];
   1.134 +    indices[0]=i;
   1.135 +    char lu[1];
   1.136 +    lu[0]='L';
   1.137 +    Value bd[1];
   1.138 +    bd[0]=value;
   1.139 +    status = CPXchgbds (env, lp, 1, indices, lu, bd);
   1.140 + 
   1.141 +  }
   1.142 +  
   1.143 +  void LpCplex::_setColUpperBound(int i, Value value)
   1.144 +  {
   1.145 +    int indices[1];
   1.146 +    indices[0]=i;
   1.147 +    char lu[1];
   1.148 +    lu[0]='U';
   1.149 +    Value bd[1];
   1.150 +    bd[0]=value;
   1.151 +    status = CPXchgbds (env, lp, 1, indices, lu, bd);
   1.152 +  }
   1.153 +
   1.154 +  //This will be easier to implement
   1.155 +  void LpCplex::_setRowBounds(int i, Value lb, Value ub)
   1.156 +  {
   1.157 +    //Bad parameter
   1.158 +    if (lb==INF || ub==-INF) {
   1.159 +      //FIXME error
   1.160 +    }
   1.161 +
   1.162 +    int cnt=1;
   1.163 +    int indices[1];
   1.164 +    indices[0]=i;
   1.165 +    char sense[1];
   1.166 +
   1.167 +    if (lb==-INF){
   1.168 +      sense[0]='L';
   1.169 +      CPXchgsense (env, lp, cnt, indices, sense);
   1.170 +      CPXchgcoef (env, lp, i, -1, ub);
   1.171 +    }
   1.172 +    else{
   1.173 +      if (ub==INF){
   1.174 +	sense[0]='G';
   1.175 +	CPXchgsense (env, lp, cnt, indices, sense);
   1.176 +	CPXchgcoef (env, lp, i, -1, lb);
   1.177 +      }
   1.178 +      else{
   1.179 +	if (lb == ub){
   1.180 +	  sense[0]='E';
   1.181 +	  CPXchgsense (env, lp, cnt, indices, sense);
   1.182 +	  CPXchgcoef (env, lp, i, -1, lb);
   1.183 +	}
   1.184 +	else{
   1.185 +	  sense[0]='R';
   1.186 +	  CPXchgsense (env, lp, cnt, indices, sense);
   1.187 +	  CPXchgcoef (env, lp, i, -1, lb);
   1.188 +	  CPXchgcoef (env, lp, i, -2, ub-lb);	  
   1.189 +	}
   1.190 +      }
   1.191 +    }
   1.192 +  }
   1.193 +
   1.194 +  void LpCplex::_setRowLowerBound(int i, Value value)
   1.195 +  {
   1.196 +    //Not implemented, obsolete
   1.197 +  }
   1.198 +  
   1.199 +  void LpCplex::_setRowUpperBound(int i, Value value)
   1.200 +  {
   1.201 +    //Not implemented, obsolete
   1.202 +//     //TODO Ezt kell meg megirni
   1.203 +//     //type of the problem
   1.204 +//     char sense[1];
   1.205 +//     status = CPXgetsense (env, lp, sense, i, i);
   1.206 +//     Value rhs[1];
   1.207 +//     status = CPXgetrhs (env, lp, rhs, i, i);
   1.208 +
   1.209 +//     switch (sense[0]) {
   1.210 +//     case 'L'://<= constraint
   1.211 +//       break;
   1.212 +//     case 'E'://= constraint
   1.213 +//       break;
   1.214 +//     case 'G'://>= constraint
   1.215 +//       break;
   1.216 +//     case 'R'://ranged constraint
   1.217 +//       break;
   1.218 +//     default: ;
   1.219 +//       //FIXME error
   1.220 +//     }
   1.221 +
   1.222 +//     status = CPXchgcoef (env, lp, i, -2, value_rng);
   1.223 +  }
   1.224 +  
   1.225 +  void LpCplex::_setObjCoeff(int i, Value obj_coef)
   1.226 +  {
   1.227 +    CPXchgcoef (env, lp, -1, i, obj_coef);
   1.228 +  }
   1.229 +
   1.230 +  void LpCplex::_clearObj()
   1.231 +  {
   1.232 +    for (int i=0;i< CPXgetnumcols (env, lp);++i){
   1.233 +      CPXchgcoef (env, lp, -1, i, 0);
   1.234 +    }
   1.235 +    
   1.236 +  }
   1.237 +
   1.238 +  LpCplex::SolveExitStatus LpCplex::_solve()
   1.239 +  {
   1.240 +    
   1.241 +    status = CPXlpopt (env, lp);
   1.242 +    if (status == 0){
   1.243 +      return SOLVED;
   1.244 +    }
   1.245 +    else{
   1.246 +      return UNSOLVED;
   1.247 +    }
   1.248 +//     int i=  lpx_simplex(lp);
   1.249 +//     switch (i) {
   1.250 +//     case LPX_E_OK: 
   1.251 +//       return SOLVED;
   1.252 +//       break;
   1.253 +//     default:
   1.254 +//       return UNSOLVED;
   1.255 +//     }
   1.256 +  }
   1.257 +
   1.258 +  LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
   1.259 +  {
   1.260 +    //Unimplemented
   1.261 +    return OPTIMAL;
   1.262 +//     int stat=  lpx_get_status(lp);
   1.263 +//     switch (stat) {
   1.264 +//     case LPX_UNDEF://Undefined (no solve has been run yet)
   1.265 +//       return UNDEFINED;
   1.266 +//       break;
   1.267 +//     case LPX_NOFEAS://There is no feasible solution (primal, I guess)
   1.268 +//     case LPX_INFEAS://Infeasible 
   1.269 +//       return INFEASIBLE;
   1.270 +//       break;
   1.271 +//     case LPX_UNBND://Unbounded
   1.272 +//       return INFINITE;
   1.273 +//       break;
   1.274 +//     case LPX_FEAS://Feasible
   1.275 +//       return FEASIBLE;
   1.276 +//       break;
   1.277 +//     case LPX_OPT://Feasible
   1.278 +//       return OPTIMAL;
   1.279 +//       break;
   1.280 +//     default:
   1.281 +//       return UNDEFINED; //to avoid gcc warning
   1.282 +//       //FIXME error
   1.283 +//     }
   1.284 +  }
   1.285 +
   1.286 +  LpCplex::Value LpCplex::_getPrimal(int i)
   1.287 +  {
   1.288 +    Value x;
   1.289 +    CPXgetx (env, lp, &x, i, i);
   1.290 +    return x;
   1.291 +  }
   1.292 +  
   1.293 +  LpCplex::Value LpCplex::_getPrimalValue()
   1.294 +  {
   1.295 +    //Unimplemented
   1.296 +    return 0;
   1.297 +  }
   1.298 +  
   1.299 + 
   1.300 +
   1.301 +
   1.302 +  void LpCplex::_setMax()
   1.303 +  {
   1.304 +    CPXchgobjsen (env, lp, CPX_MAX);
   1.305 +   }
   1.306 +  void LpCplex::_setMin()
   1.307 +  {
   1.308 +    CPXchgobjsen (env, lp, CPX_MIN);
   1.309 +   }
   1.310 +  
   1.311 +} //namespace lemon
   1.312 +