src/lemon/lp_cplex.cc
author athos
Fri, 06 May 2005 15:39:33 +0000
changeset 1407 7152559e3d08
parent 1405 3626c7f10f14
child 1431 ad44b1dd8013
permissions -rw-r--r--
Cplex works.
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@1407
   264
//7.5-os cplex statusai
athos@1407
   265
// #define CPX_OPTIMAL                      1
athos@1407
   266
// #define CPX_INFEASIBLE                   2
athos@1407
   267
// #define CPX_UNBOUNDED                    3
athos@1407
   268
// #define CPX_OBJ_LIM                      4
athos@1407
   269
// #define CPX_IT_LIM_FEAS                  5
athos@1407
   270
// #define CPX_IT_LIM_INFEAS                6
athos@1407
   271
// #define CPX_TIME_LIM_FEAS                7
athos@1407
   272
// #define CPX_TIME_LIM_INFEAS              8
athos@1407
   273
// #define CPX_NUM_BEST_FEAS                9
athos@1407
   274
// #define CPX_NUM_BEST_INFEAS             10
athos@1407
   275
// #define CPX_OPTIMAL_INFEAS              11
athos@1407
   276
// #define CPX_ABORT_FEAS                  12
athos@1407
   277
// #define CPX_ABORT_INFEAS                13
athos@1407
   278
// #define CPX_ABORT_DUAL_INFEAS           14
athos@1407
   279
// #define CPX_ABORT_PRIM_INFEAS           15
athos@1407
   280
// #define CPX_ABORT_PRIM_DUAL_INFEAS      16
athos@1407
   281
// #define CPX_ABORT_PRIM_DUAL_FEAS        17
athos@1407
   282
// #define CPX_ABORT_CROSSOVER             18
athos@1407
   283
// #define CPX_INForUNBD                   19
athos@1407
   284
// #define CPX_PIVOT                       20
athos@1407
   285
athos@1407
   286
//     Ezeket hova tegyem:
athos@1407
   287
// ??case CPX_ABORT_DUAL_INFEAS           
athos@1407
   288
// ??case CPX_ABORT_CROSSOVER             
athos@1407
   289
// ??case CPX_INForUNBD                   
athos@1407
   290
// ??case CPX_PIVOT                       
athos@1407
   291
athos@1407
   292
    int stat = CPXgetstat (env, lp);
athos@1407
   293
    switch (stat) {
athos@1407
   294
    case 0:
athos@1407
   295
      return UNDEFINED; //Undefined
athos@1407
   296
      break;      
athos@1407
   297
    case CPX_OPTIMAL://Optimal
athos@1407
   298
      return OPTIMAL;
athos@1407
   299
      break;
athos@1407
   300
    case CPX_UNBOUNDED://Unbounded
athos@1407
   301
      return INFINITE;
athos@1407
   302
      break;
athos@1407
   303
    case CPX_INFEASIBLE://Infeasible 
athos@1407
   304
    case CPX_IT_LIM_INFEAS:
athos@1407
   305
    case CPX_TIME_LIM_INFEAS:
athos@1407
   306
    case CPX_NUM_BEST_INFEAS:             
athos@1407
   307
    case CPX_OPTIMAL_INFEAS:              
athos@1407
   308
    case CPX_ABORT_INFEAS:                
athos@1407
   309
    case CPX_ABORT_PRIM_INFEAS:           
athos@1407
   310
    case CPX_ABORT_PRIM_DUAL_INFEAS:      
athos@1407
   311
      return INFEASIBLE;
athos@1407
   312
      break;
athos@1407
   313
    case CPX_OBJ_LIM:                    
athos@1407
   314
    case CPX_IT_LIM_FEAS:             
athos@1407
   315
    case CPX_TIME_LIM_FEAS:                
athos@1407
   316
    case CPX_NUM_BEST_FEAS:                
athos@1407
   317
    case CPX_ABORT_FEAS:                  
athos@1407
   318
    case CPX_ABORT_PRIM_DUAL_FEAS:        
athos@1407
   319
      return FEASIBLE;
athos@1407
   320
      break;
athos@1407
   321
    default:
athos@1407
   322
      return UNDEFINED; //Everything else comes here
athos@1407
   323
      //FIXME error
athos@1407
   324
    }
athos@1407
   325
athos@1407
   326
athos@1407
   327
    //Nem tudom, hanyas cplex verzio statusai
athos@1405
   328
// CPX_STAT_ABORT_DUAL_OBJ_LIM
athos@1405
   329
// CPX_STAT_ABORT_IT_LIM
athos@1405
   330
// CPX_STAT_ABORT_OBJ_LIM
athos@1405
   331
// CPX_STAT_ABORT_PRIM_OBJ_LIM
athos@1405
   332
// CPX_STAT_ABORT_TIME_LIM
athos@1405
   333
// CPX_STAT_ABORT_USER
athos@1405
   334
// CPX_STAT_FEASIBLE_RELAXED
athos@1405
   335
// CPX_STAT_INFEASIBLE
athos@1405
   336
// CPX_STAT_INForUNBD
athos@1405
   337
// CPX_STAT_NUM_BEST
athos@1405
   338
// CPX_STAT_OPTIMAL
athos@1405
   339
// CPX_STAT_OPTIMAL_FACE_UNBOUNDED
athos@1405
   340
// CPX_STAT_OPTIMAL_INFEAS
athos@1405
   341
// CPX_STAT_OPTIMAL_RELAXED
athos@1405
   342
// CPX_STAT_UNBOUNDED
athos@1405
   343
athos@1407
   344
//     int stat = CPXgetstat (env, lp);
athos@1407
   345
//     switch (stat) {
athos@1407
   346
//     case CPX_STAT_OPTIMAL://Optimal
athos@1407
   347
//       return OPTIMAL;
athos@1407
   348
//       break;
athos@1407
   349
//     case CPX_STAT_INFEASIBLE://Infeasible 
athos@1407
   350
//       return INFEASIBLE;
athos@1407
   351
//       break;
athos@1407
   352
//     case CPX_STAT_UNBOUNDED://Unbounded
athos@1407
   353
//       return INFINITE;
athos@1407
   354
//       break;
athos@1407
   355
//     case CPX_STAT_NUM_BEST://Feasible
athos@1407
   356
//       return FEASIBLE;
athos@1407
   357
//       break;
athos@1407
   358
//     default:
athos@1407
   359
//       return UNDEFINED; //Everything else comes here
athos@1407
   360
//       //FIXME error
athos@1407
   361
//     }
athos@1407
   362
alpar@1381
   363
  }
alpar@1381
   364
alpar@1381
   365
  LpCplex::Value LpCplex::_getPrimal(int i)
alpar@1381
   366
  {
alpar@1381
   367
    Value x;
alpar@1381
   368
    CPXgetx (env, lp, &x, i, i);
alpar@1381
   369
    return x;
alpar@1381
   370
  }
alpar@1381
   371
  
alpar@1381
   372
  LpCplex::Value LpCplex::_getPrimalValue()
alpar@1381
   373
  {
athos@1405
   374
    Value objval;
athos@1405
   375
    //method = CPXgetmethod (env, lp);
athos@1405
   376
    status = CPXgetobjval (env, lp, &objval);
athos@1405
   377
    return objval;
alpar@1381
   378
  }
alpar@1381
   379
  
alpar@1381
   380
 
alpar@1381
   381
alpar@1381
   382
alpar@1381
   383
  void LpCplex::_setMax()
alpar@1381
   384
  {
alpar@1381
   385
    CPXchgobjsen (env, lp, CPX_MAX);
alpar@1381
   386
   }
alpar@1381
   387
  void LpCplex::_setMin()
alpar@1381
   388
  {
alpar@1381
   389
    CPXchgobjsen (env, lp, CPX_MIN);
alpar@1381
   390
   }
alpar@1381
   391
  
alpar@1381
   392
} //namespace lemon
alpar@1381
   393