1.1 --- a/src/lemon/lp_cplex.cc Sat May 21 21:04:57 2005 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,408 +0,0 @@
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 -#include <iostream>
1.21 -#include<lemon/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()
1.72 - {
1.73 - return *(LpSolverBase*)0;
1.74 - }
1.75 - LpSolverBase &LpCplex::_copyLp() {
1.76 - return *(LpSolverBase*)0;
1.77 - //Ez lesz majd CPXcloneprob (env, lp, &status);
1.78 - }
1.79 -
1.80 - int LpCplex::_addCol()
1.81 - {
1.82 - int i = CPXgetnumcols (env, lp);
1.83 - Value lb[1],ub[1];
1.84 - lb[0]=-INF;//-CPX_INFBOUND;
1.85 - ub[0]=INF;//CPX_INFBOUND;
1.86 - status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
1.87 - return i;
1.88 - }
1.89 -
1.90 - int LpCplex::_addRow()
1.91 - {
1.92 - //We want a row that is not constrained
1.93 - char sense[1];
1.94 - sense[0]='L';//<= constraint
1.95 - Value rhs[1];
1.96 - rhs[0]=INF;
1.97 - int i = CPXgetnumrows (env, lp);
1.98 - status = CPXnewrows (env, lp, 1, rhs, sense, NULL, NULL);
1.99 - return i;
1.100 - }
1.101 -
1.102 -
1.103 - void LpCplex::_eraseCol(int i) {
1.104 - ///\todo Not implemented yet
1.105 - }
1.106 -
1.107 - void LpCplex::_eraseRow(int i) {
1.108 - ///\todo Not implemented yet
1.109 - }
1.110 -
1.111 -
1.112 - ///\warning Data at index 0 is ignored in the arrays.
1.113 - void LpCplex::_setRowCoeffs(int i,
1.114 - int length,
1.115 - int const * indices,
1.116 - Value const * values )
1.117 - {
1.118 - int rowlist[length+1];
1.119 - int* p=rowlist;
1.120 - for (int k=1;k<=length;++k){
1.121 - rowlist[k]=i;
1.122 - }
1.123 - status = CPXchgcoeflist(env, lp,
1.124 - length,
1.125 - p+1,
1.126 - const_cast<int * >(indices+1),
1.127 - const_cast<Value * >(values+1));
1.128 - }
1.129 -
1.130 - void LpCplex::_setColCoeffs(int i,
1.131 - int length,
1.132 - int const * indices,
1.133 - Value const * values)
1.134 - {
1.135 - int collist[length+1];
1.136 - int* p=collist;
1.137 - for (int k=1;k<=length;++k){
1.138 - collist[k]=i;
1.139 - }
1.140 - status = CPXchgcoeflist(env, lp,
1.141 - length,
1.142 - const_cast<int * >(indices+1),
1.143 - p+1,
1.144 - const_cast<Value * >(values+1));
1.145 - }
1.146 -
1.147 - void LpCplex::_setCoeff(int row, int col, Value value)
1.148 - {
1.149 - CPXchgcoef (env, lp, row, col, value);
1.150 - }
1.151 -
1.152 - void LpCplex::_setColLowerBound(int i, Value value)
1.153 - {
1.154 - int indices[1];
1.155 - indices[0]=i;
1.156 - char lu[1];
1.157 - lu[0]='L';
1.158 - Value bd[1];
1.159 - bd[0]=value;
1.160 - status = CPXchgbds (env, lp, 1, indices, lu, bd);
1.161 -
1.162 - }
1.163 -
1.164 - void LpCplex::_setColUpperBound(int i, Value value)
1.165 - {
1.166 - int indices[1];
1.167 - indices[0]=i;
1.168 - char lu[1];
1.169 - lu[0]='U';
1.170 - Value bd[1];
1.171 - bd[0]=value;
1.172 - status = CPXchgbds (env, lp, 1, indices, lu, bd);
1.173 - }
1.174 -
1.175 - //This will be easier to implement
1.176 - void LpCplex::_setRowBounds(int i, Value lb, Value ub)
1.177 - {
1.178 - //Bad parameter
1.179 - if (lb==INF || ub==-INF) {
1.180 - //FIXME error
1.181 - }
1.182 -
1.183 - int cnt=1;
1.184 - int indices[1];
1.185 - indices[0]=i;
1.186 - char sense[1];
1.187 -
1.188 - if (lb==-INF){
1.189 - sense[0]='L';
1.190 - CPXchgsense (env, lp, cnt, indices, sense);
1.191 - CPXchgcoef (env, lp, i, -1, ub);
1.192 -
1.193 - }
1.194 - else{
1.195 - if (ub==INF){
1.196 - sense[0]='G';
1.197 - CPXchgsense (env, lp, cnt, indices, sense);
1.198 - CPXchgcoef (env, lp, i, -1, lb);
1.199 - }
1.200 - else{
1.201 - if (lb == ub){
1.202 - sense[0]='E';
1.203 - CPXchgsense (env, lp, cnt, indices, sense);
1.204 - CPXchgcoef (env, lp, i, -1, lb);
1.205 - }
1.206 - else{
1.207 - sense[0]='R';
1.208 - CPXchgsense (env, lp, cnt, indices, sense);
1.209 - CPXchgcoef (env, lp, i, -1, lb);
1.210 - CPXchgcoef (env, lp, i, -2, ub-lb);
1.211 - }
1.212 - }
1.213 - }
1.214 - }
1.215 -
1.216 -// void LpCplex::_setRowLowerBound(int i, Value value)
1.217 -// {
1.218 -// //Not implemented, obsolete
1.219 -// }
1.220 -
1.221 -// void LpCplex::_setRowUpperBound(int i, Value value)
1.222 -// {
1.223 -// //Not implemented, obsolete
1.224 -// // //TODO Ezt kell meg megirni
1.225 -// // //type of the problem
1.226 -// // char sense[1];
1.227 -// // status = CPXgetsense (env, lp, sense, i, i);
1.228 -// // Value rhs[1];
1.229 -// // status = CPXgetrhs (env, lp, rhs, i, i);
1.230 -
1.231 -// // switch (sense[0]) {
1.232 -// // case 'L'://<= constraint
1.233 -// // break;
1.234 -// // case 'E'://= constraint
1.235 -// // break;
1.236 -// // case 'G'://>= constraint
1.237 -// // break;
1.238 -// // case 'R'://ranged constraint
1.239 -// // break;
1.240 -// // default: ;
1.241 -// // //FIXME error
1.242 -// // }
1.243 -
1.244 -// // status = CPXchgcoef (env, lp, i, -2, value_rng);
1.245 -// }
1.246 -
1.247 - void LpCplex::_setObjCoeff(int i, Value obj_coef)
1.248 - {
1.249 - CPXchgcoef (env, lp, -1, i, obj_coef);
1.250 - }
1.251 -
1.252 - void LpCplex::_clearObj()
1.253 - {
1.254 - for (int i=0;i< CPXgetnumcols (env, lp);++i){
1.255 - CPXchgcoef (env, lp, -1, i, 0);
1.256 - }
1.257 -
1.258 - }
1.259 -
1.260 - LpCplex::SolveExitStatus LpCplex::_solve()
1.261 - {
1.262 -
1.263 - status = CPXlpopt (env, lp);
1.264 - if (status == 0){
1.265 - return SOLVED;
1.266 - }
1.267 - else{
1.268 - return UNSOLVED;
1.269 - }
1.270 -// int i= lpx_simplex(lp);
1.271 -// switch (i) {
1.272 -// case LPX_E_OK:
1.273 -// return SOLVED;
1.274 -// break;
1.275 -// default:
1.276 -// return UNSOLVED;
1.277 -// }
1.278 - }
1.279 -
1.280 - LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
1.281 - {
1.282 -//7.5-os cplex statusai
1.283 -// #define CPX_OPTIMAL 1
1.284 -// #define CPX_INFEASIBLE 2
1.285 -// #define CPX_UNBOUNDED 3
1.286 -// #define CPX_OBJ_LIM 4
1.287 -// #define CPX_IT_LIM_FEAS 5
1.288 -// #define CPX_IT_LIM_INFEAS 6
1.289 -// #define CPX_TIME_LIM_FEAS 7
1.290 -// #define CPX_TIME_LIM_INFEAS 8
1.291 -// #define CPX_NUM_BEST_FEAS 9
1.292 -// #define CPX_NUM_BEST_INFEAS 10
1.293 -// #define CPX_OPTIMAL_INFEAS 11
1.294 -// #define CPX_ABORT_FEAS 12
1.295 -// #define CPX_ABORT_INFEAS 13
1.296 -// #define CPX_ABORT_DUAL_INFEAS 14
1.297 -// #define CPX_ABORT_PRIM_INFEAS 15
1.298 -// #define CPX_ABORT_PRIM_DUAL_INFEAS 16
1.299 -// #define CPX_ABORT_PRIM_DUAL_FEAS 17
1.300 -// #define CPX_ABORT_CROSSOVER 18
1.301 -// #define CPX_INForUNBD 19
1.302 -// #define CPX_PIVOT 20
1.303 -
1.304 -// Ezeket hova tegyem:
1.305 -// ??case CPX_ABORT_DUAL_INFEAS
1.306 -// ??case CPX_ABORT_CROSSOVER
1.307 -// ??case CPX_INForUNBD
1.308 -// ??case CPX_PIVOT
1.309 -
1.310 - int stat = CPXgetstat (env, lp);
1.311 - switch (stat) {
1.312 - case 0:
1.313 - return UNDEFINED; //Undefined
1.314 - break;
1.315 - case CPX_OPTIMAL://Optimal
1.316 - return OPTIMAL;
1.317 - break;
1.318 - case CPX_UNBOUNDED://Unbounded
1.319 - return INFINITE;
1.320 - break;
1.321 - case CPX_INFEASIBLE://Infeasible
1.322 - case CPX_IT_LIM_INFEAS:
1.323 - case CPX_TIME_LIM_INFEAS:
1.324 - case CPX_NUM_BEST_INFEAS:
1.325 - case CPX_OPTIMAL_INFEAS:
1.326 - case CPX_ABORT_INFEAS:
1.327 - case CPX_ABORT_PRIM_INFEAS:
1.328 - case CPX_ABORT_PRIM_DUAL_INFEAS:
1.329 - return INFEASIBLE;
1.330 - break;
1.331 - case CPX_OBJ_LIM:
1.332 - case CPX_IT_LIM_FEAS:
1.333 - case CPX_TIME_LIM_FEAS:
1.334 - case CPX_NUM_BEST_FEAS:
1.335 - case CPX_ABORT_FEAS:
1.336 - case CPX_ABORT_PRIM_DUAL_FEAS:
1.337 - return FEASIBLE;
1.338 - break;
1.339 - default:
1.340 - return UNDEFINED; //Everything else comes here
1.341 - //FIXME error
1.342 - }
1.343 -
1.344 -
1.345 - //Nem tudom, hanyas cplex verzio statusai
1.346 -// CPX_STAT_ABORT_DUAL_OBJ_LIM
1.347 -// CPX_STAT_ABORT_IT_LIM
1.348 -// CPX_STAT_ABORT_OBJ_LIM
1.349 -// CPX_STAT_ABORT_PRIM_OBJ_LIM
1.350 -// CPX_STAT_ABORT_TIME_LIM
1.351 -// CPX_STAT_ABORT_USER
1.352 -// CPX_STAT_FEASIBLE_RELAXED
1.353 -// CPX_STAT_INFEASIBLE
1.354 -// CPX_STAT_INForUNBD
1.355 -// CPX_STAT_NUM_BEST
1.356 -// CPX_STAT_OPTIMAL
1.357 -// CPX_STAT_OPTIMAL_FACE_UNBOUNDED
1.358 -// CPX_STAT_OPTIMAL_INFEAS
1.359 -// CPX_STAT_OPTIMAL_RELAXED
1.360 -// CPX_STAT_UNBOUNDED
1.361 -
1.362 -// int stat = CPXgetstat (env, lp);
1.363 -// switch (stat) {
1.364 -// case CPX_STAT_OPTIMAL://Optimal
1.365 -// return OPTIMAL;
1.366 -// break;
1.367 -// case CPX_STAT_INFEASIBLE://Infeasible
1.368 -// return INFEASIBLE;
1.369 -// break;
1.370 -// case CPX_STAT_UNBOUNDED://Unbounded
1.371 -// return INFINITE;
1.372 -// break;
1.373 -// case CPX_STAT_NUM_BEST://Feasible
1.374 -// return FEASIBLE;
1.375 -// break;
1.376 -// default:
1.377 -// return UNDEFINED; //Everything else comes here
1.378 -// //FIXME error
1.379 -// }
1.380 -
1.381 - }
1.382 -
1.383 - LpCplex::Value LpCplex::_getPrimal(int i)
1.384 - {
1.385 - Value x;
1.386 - CPXgetx (env, lp, &x, i, i);
1.387 - return x;
1.388 - }
1.389 -
1.390 - LpCplex::Value LpCplex::_getPrimalValue()
1.391 - {
1.392 - Value objval;
1.393 - //method = CPXgetmethod (env, lp);
1.394 - status = CPXgetobjval (env, lp, &objval);
1.395 - return objval;
1.396 - }
1.397 -
1.398 -
1.399 -
1.400 -
1.401 - void LpCplex::_setMax()
1.402 - {
1.403 - CPXchgobjsen (env, lp, CPX_MAX);
1.404 - }
1.405 - void LpCplex::_setMin()
1.406 - {
1.407 - CPXchgobjsen (env, lp, CPX_MIN);
1.408 - }
1.409 -
1.410 -} //namespace lemon
1.411 -