src/lemon/lp_cplex.cc
author athos
Thu, 05 May 2005 15:43:43 +0000
changeset 1405 3626c7f10f14
parent 1381 998e8def9676
child 1407 7152559e3d08
permissions -rw-r--r--
Deleted _setRowLowerBound() and _setRowUpperBound() functions. Cplex worked (now it does not because of _getPrimalStatus()).
alpar@1381
     1
/* -*- C++ -*-
alpar@1381
     2
 * src/lemon/lp_cplex.cc
alpar@1381
     3
 * - Part of LEMON, a generic C++ optimization library
alpar@1381
     4
 *
alpar@1381
     5
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1381
     6
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1381
     7
 *
alpar@1381
     8
 * Permission to use, modify and distribute this software is granted
alpar@1381
     9
 * provided that this copyright notice appears in all copies. For
alpar@1381
    10
 * precise terms see the accompanying LICENSE file.
alpar@1381
    11
 *
alpar@1381
    12
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1381
    13
 * express or implied, and with no claim as to its suitability for any
alpar@1381
    14
 * purpose.
alpar@1381
    15
 *
alpar@1381
    16
 */
athos@1405
    17
#include <iostream>
athos@1405
    18
#include<lemon/lp_cplex.h>
alpar@1381
    19
alpar@1381
    20
///\file
alpar@1381
    21
///\brief Implementation of the LEMON-CPLEX lp solver interface.
alpar@1381
    22
namespace lemon {
alpar@1381
    23
  
alpar@1381
    24
  LpCplex::LpCplex() : LpSolverBase() {
alpar@1381
    25
    env = NULL;
alpar@1381
    26
    lp = NULL;
alpar@1381
    27
    env = CPXopenCPLEXdevelop(&status);     
alpar@1381
    28
//     if (Env == NULL)
alpar@1381
    29
//     {
alpar@1381
    30
//          fprintf(stderr,"A CPLEX környezet megnyitása sikertelen.\n");
alpar@1381
    31
// 	 CPXgeterrorstring(Env, Status, ErrorMsg);
alpar@1381
    32
// 	 fprintf(stderr,"%s",ErrorMsg);
alpar@1381
    33
// 	 goto Terminate;
alpar@1381
    34
//     }
alpar@1381
    35
    
alpar@1381
    36
    // *** A problema létrehozása ***
alpar@1381
    37
    lp = CPXcreateprob(env, &status, "LP problem");
alpar@1381
    38
    
alpar@1381
    39
    //    if (Problem == NULL)
alpar@1381
    40
//     {
alpar@1381
    41
// 	fprintf(stderr,"Az LP létrehozása sikertelen");
alpar@1381
    42
// 	goto Terminate;
alpar@1381
    43
//     }
alpar@1381
    44
    
alpar@1381
    45
  }
alpar@1381
    46
  
alpar@1381
    47
  LpCplex::~LpCplex() {
alpar@1381
    48
    status = CPXfreeprob(env,&lp); 
alpar@1381
    49
    //       if (Status != 0)
alpar@1381
    50
    // 	{
alpar@1381
    51
// 	  fprintf(stderr,"A CPLEX feladat törlése sikertelen.\n");
alpar@1381
    52
// 	  CPXgeterrorstring(Env, Status, ErrorMsg);
alpar@1381
    53
// 	  fprintf(stderr,"%s",ErrorMsg);
alpar@1381
    54
// 	  goto Terminate;
alpar@1381
    55
// 	}
alpar@1381
    56
       
alpar@1381
    57
    status = CPXcloseCPLEX(&env); 
alpar@1381
    58
    //       if (Status != 0)
alpar@1381
    59
    // 	{
alpar@1381
    60
    // 	  fprintf(stderr,"A CPLEX környezet bezárása sikertelen.\n");
alpar@1381
    61
// 	  CPXgeterrorstring(Env, Status, ErrorMsg);
alpar@1381
    62
// 	  fprintf(stderr,"%s",ErrorMsg);
alpar@1381
    63
// 	  goto Terminate;
alpar@1381
    64
// 	}
alpar@1381
    65
      
alpar@1381
    66
  }
alpar@1381
    67
  
athos@1405
    68
  LpSolverBase &LpCplex::_newLp() 
athos@1405
    69
  {
athos@1405
    70
    return *(LpSolverBase*)0;
athos@1405
    71
  }
athos@1405
    72
  LpSolverBase &LpCplex::_copyLp() {
athos@1405
    73
    return *(LpSolverBase*)0;
athos@1405
    74
    //Ez lesz majd CPXcloneprob (env, lp, &status);
athos@1405
    75
  }
alpar@1381
    76
alpar@1381
    77
  int LpCplex::_addCol()
alpar@1381
    78
  {
alpar@1381
    79
    int i = CPXgetnumcols (env, lp);
alpar@1381
    80
    Value lb[1],ub[1];
alpar@1381
    81
    lb[0]=-INF;//-CPX_INFBOUND;
alpar@1381
    82
    ub[0]=INF;//CPX_INFBOUND;
alpar@1381
    83
    status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
alpar@1381
    84
    return i;
alpar@1381
    85
  }
alpar@1381
    86
  
alpar@1381
    87
  int LpCplex::_addRow() 
alpar@1381
    88
  {
alpar@1381
    89
    //We want a row that is not constrained
alpar@1381
    90
    char sense[1];
alpar@1381
    91
    sense[0]='L';//<= constraint
alpar@1381
    92
    Value rhs[1];
alpar@1381
    93
    rhs[0]=INF;
alpar@1381
    94
    int i = CPXgetnumrows (env, lp);
alpar@1381
    95
    status = CPXnewrows (env, lp, 1, rhs, sense, NULL, NULL);
alpar@1381
    96
    return i;
alpar@1381
    97
  }
alpar@1381
    98
  
alpar@1381
    99
  ///\warning Data at index 0 is ignored in the arrays.
alpar@1381
   100
  void LpCplex::_setRowCoeffs(int i, 
alpar@1381
   101
			      int length,
alpar@1381
   102
			      int  const * indices, 
alpar@1381
   103
			      Value  const * values )
alpar@1381
   104
  {
alpar@1381
   105
    int rowlist[length+1];
alpar@1381
   106
    int* p=rowlist;
alpar@1381
   107
    for (int k=1;k<=length;++k){
alpar@1381
   108
      rowlist[k]=i;
alpar@1381
   109
    }
alpar@1381
   110
    status = CPXchgcoeflist(env, lp, 
alpar@1381
   111
			    length, 
alpar@1381
   112
			    p+1, 
alpar@1381
   113
			    const_cast<int * >(indices+1), 
alpar@1381
   114
			    const_cast<Value * >(values+1));
alpar@1381
   115
  }
alpar@1381
   116
  
alpar@1381
   117
  void LpCplex::_setColCoeffs(int i, 
alpar@1381
   118
			      int length,
alpar@1381
   119
			      int  const * indices, 
alpar@1381
   120
			      Value  const * values)
alpar@1381
   121
  {
alpar@1381
   122
    int collist[length+1];
alpar@1381
   123
    int* p=collist;
alpar@1381
   124
    for (int k=1;k<=length;++k){
alpar@1381
   125
      collist[k]=i;
alpar@1381
   126
    }
alpar@1381
   127
    status = CPXchgcoeflist(env, lp, 
alpar@1381
   128
			    length, 
alpar@1381
   129
			    const_cast<int * >(indices+1), 
alpar@1381
   130
			    p+1, 
alpar@1381
   131
			    const_cast<Value * >(values+1));
alpar@1381
   132
  }
alpar@1381
   133
  
alpar@1381
   134
  void LpCplex::_setColLowerBound(int i, Value value)
alpar@1381
   135
  {
alpar@1381
   136
    int indices[1];
alpar@1381
   137
    indices[0]=i;
alpar@1381
   138
    char lu[1];
alpar@1381
   139
    lu[0]='L';
alpar@1381
   140
    Value bd[1];
alpar@1381
   141
    bd[0]=value;
alpar@1381
   142
    status = CPXchgbds (env, lp, 1, indices, lu, bd);
alpar@1381
   143
 
alpar@1381
   144
  }
alpar@1381
   145
  
alpar@1381
   146
  void LpCplex::_setColUpperBound(int i, Value value)
alpar@1381
   147
  {
alpar@1381
   148
    int indices[1];
alpar@1381
   149
    indices[0]=i;
alpar@1381
   150
    char lu[1];
alpar@1381
   151
    lu[0]='U';
alpar@1381
   152
    Value bd[1];
alpar@1381
   153
    bd[0]=value;
alpar@1381
   154
    status = CPXchgbds (env, lp, 1, indices, lu, bd);
alpar@1381
   155
  }
alpar@1381
   156
alpar@1381
   157
  //This will be easier to implement
alpar@1381
   158
  void LpCplex::_setRowBounds(int i, Value lb, Value ub)
alpar@1381
   159
  {
alpar@1381
   160
    //Bad parameter
alpar@1381
   161
    if (lb==INF || ub==-INF) {
alpar@1381
   162
      //FIXME error
alpar@1381
   163
    }
athos@1405
   164
    
alpar@1381
   165
    int cnt=1;
alpar@1381
   166
    int indices[1];
alpar@1381
   167
    indices[0]=i;
alpar@1381
   168
    char sense[1];
alpar@1381
   169
alpar@1381
   170
    if (lb==-INF){
alpar@1381
   171
      sense[0]='L';
alpar@1381
   172
      CPXchgsense (env, lp, cnt, indices, sense);
alpar@1381
   173
      CPXchgcoef (env, lp, i, -1, ub);
athos@1405
   174
      
alpar@1381
   175
    }
alpar@1381
   176
    else{
alpar@1381
   177
      if (ub==INF){
alpar@1381
   178
	sense[0]='G';
alpar@1381
   179
	CPXchgsense (env, lp, cnt, indices, sense);
alpar@1381
   180
	CPXchgcoef (env, lp, i, -1, lb);
alpar@1381
   181
      }
alpar@1381
   182
      else{
alpar@1381
   183
	if (lb == ub){
alpar@1381
   184
	  sense[0]='E';
alpar@1381
   185
	  CPXchgsense (env, lp, cnt, indices, sense);
alpar@1381
   186
	  CPXchgcoef (env, lp, i, -1, lb);
alpar@1381
   187
	}
alpar@1381
   188
	else{
alpar@1381
   189
	  sense[0]='R';
alpar@1381
   190
	  CPXchgsense (env, lp, cnt, indices, sense);
alpar@1381
   191
	  CPXchgcoef (env, lp, i, -1, lb);
alpar@1381
   192
	  CPXchgcoef (env, lp, i, -2, ub-lb);	  
alpar@1381
   193
	}
alpar@1381
   194
      }
alpar@1381
   195
    }
alpar@1381
   196
  }
alpar@1381
   197
athos@1405
   198
//   void LpCplex::_setRowLowerBound(int i, Value value)
athos@1405
   199
//   {
athos@1405
   200
//     //Not implemented, obsolete
athos@1405
   201
//   }
alpar@1381
   202
  
athos@1405
   203
//   void LpCplex::_setRowUpperBound(int i, Value value)
athos@1405
   204
//   {
athos@1405
   205
//     //Not implemented, obsolete
athos@1405
   206
// //     //TODO Ezt kell meg megirni
athos@1405
   207
// //     //type of the problem
athos@1405
   208
// //     char sense[1];
athos@1405
   209
// //     status = CPXgetsense (env, lp, sense, i, i);
athos@1405
   210
// //     Value rhs[1];
athos@1405
   211
// //     status = CPXgetrhs (env, lp, rhs, i, i);
alpar@1381
   212
athos@1405
   213
// //     switch (sense[0]) {
athos@1405
   214
// //     case 'L'://<= constraint
athos@1405
   215
// //       break;
athos@1405
   216
// //     case 'E'://= constraint
athos@1405
   217
// //       break;
athos@1405
   218
// //     case 'G'://>= constraint
athos@1405
   219
// //       break;
athos@1405
   220
// //     case 'R'://ranged constraint
athos@1405
   221
// //       break;
athos@1405
   222
// //     default: ;
athos@1405
   223
// //       //FIXME error
athos@1405
   224
// //     }
alpar@1381
   225
athos@1405
   226
// //     status = CPXchgcoef (env, lp, i, -2, value_rng);
athos@1405
   227
//   }
alpar@1381
   228
  
alpar@1381
   229
  void LpCplex::_setObjCoeff(int i, Value obj_coef)
alpar@1381
   230
  {
alpar@1381
   231
    CPXchgcoef (env, lp, -1, i, obj_coef);
alpar@1381
   232
  }
alpar@1381
   233
alpar@1381
   234
  void LpCplex::_clearObj()
alpar@1381
   235
  {
alpar@1381
   236
    for (int i=0;i< CPXgetnumcols (env, lp);++i){
alpar@1381
   237
      CPXchgcoef (env, lp, -1, i, 0);
alpar@1381
   238
    }
alpar@1381
   239
    
alpar@1381
   240
  }
alpar@1381
   241
alpar@1381
   242
  LpCplex::SolveExitStatus LpCplex::_solve()
alpar@1381
   243
  {
alpar@1381
   244
    
alpar@1381
   245
    status = CPXlpopt (env, lp);
alpar@1381
   246
    if (status == 0){
alpar@1381
   247
      return SOLVED;
alpar@1381
   248
    }
alpar@1381
   249
    else{
alpar@1381
   250
      return UNSOLVED;
alpar@1381
   251
    }
alpar@1381
   252
//     int i=  lpx_simplex(lp);
alpar@1381
   253
//     switch (i) {
alpar@1381
   254
//     case LPX_E_OK: 
alpar@1381
   255
//       return SOLVED;
alpar@1381
   256
//       break;
alpar@1381
   257
//     default:
alpar@1381
   258
//       return UNSOLVED;
alpar@1381
   259
//     }
alpar@1381
   260
  }
alpar@1381
   261
alpar@1381
   262
  LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
alpar@1381
   263
  {
athos@1405
   264
// CPX_STAT_ABORT_DUAL_OBJ_LIM
athos@1405
   265
// CPX_STAT_ABORT_IT_LIM
athos@1405
   266
// CPX_STAT_ABORT_OBJ_LIM
athos@1405
   267
// CPX_STAT_ABORT_PRIM_OBJ_LIM
athos@1405
   268
// CPX_STAT_ABORT_TIME_LIM
athos@1405
   269
// CPX_STAT_ABORT_USER
athos@1405
   270
// CPX_STAT_FEASIBLE_RELAXED
athos@1405
   271
// CPX_STAT_INFEASIBLE
athos@1405
   272
// CPX_STAT_INForUNBD
athos@1405
   273
// CPX_STAT_NUM_BEST
athos@1405
   274
// CPX_STAT_OPTIMAL
athos@1405
   275
// CPX_STAT_OPTIMAL_FACE_UNBOUNDED
athos@1405
   276
// CPX_STAT_OPTIMAL_INFEAS
athos@1405
   277
// CPX_STAT_OPTIMAL_RELAXED
athos@1405
   278
// CPX_STAT_UNBOUNDED
athos@1405
   279
alpar@1381
   280
    //Unimplemented
athos@1405
   281
    int stat = CPXgetstat (env, lp);
athos@1405
   282
    switch (stat) {
athos@1405
   283
    case CPX_STAT_OPTIMAL://Optimal
athos@1405
   284
      return OPTIMAL;
athos@1405
   285
      break;
athos@1405
   286
    case CPX_STAT_INFEASIBLE://Infeasible 
athos@1405
   287
      return INFEASIBLE;
athos@1405
   288
      break;
athos@1405
   289
    case CPX_STAT_UNBOUNDED://Unbounded
athos@1405
   290
      return INFINITE;
athos@1405
   291
      break;
athos@1405
   292
    case CPX_STAT_NUM_BEST://Feasible
athos@1405
   293
      return FEASIBLE;
athos@1405
   294
      break;
athos@1405
   295
    default:
athos@1405
   296
      return UNDEFINED; //Everything else comes here
athos@1405
   297
      //FIXME error
athos@1405
   298
    }
alpar@1381
   299
  }
alpar@1381
   300
alpar@1381
   301
  LpCplex::Value LpCplex::_getPrimal(int i)
alpar@1381
   302
  {
alpar@1381
   303
    Value x;
alpar@1381
   304
    CPXgetx (env, lp, &x, i, i);
alpar@1381
   305
    return x;
alpar@1381
   306
  }
alpar@1381
   307
  
alpar@1381
   308
  LpCplex::Value LpCplex::_getPrimalValue()
alpar@1381
   309
  {
athos@1405
   310
    Value objval;
athos@1405
   311
    //method = CPXgetmethod (env, lp);
athos@1405
   312
    status = CPXgetobjval (env, lp, &objval);
athos@1405
   313
    return objval;
alpar@1381
   314
  }
alpar@1381
   315
  
alpar@1381
   316
 
alpar@1381
   317
alpar@1381
   318
alpar@1381
   319
  void LpCplex::_setMax()
alpar@1381
   320
  {
alpar@1381
   321
    CPXchgobjsen (env, lp, CPX_MAX);
alpar@1381
   322
   }
alpar@1381
   323
  void LpCplex::_setMin()
alpar@1381
   324
  {
alpar@1381
   325
    CPXchgobjsen (env, lp, CPX_MIN);
alpar@1381
   326
   }
alpar@1381
   327
  
alpar@1381
   328
} //namespace lemon
alpar@1381
   329