src/work/athos/lp/lp_cplex.cc
author alpar
Fri, 15 Apr 2005 19:56:25 +0000
changeset 1359 1581f961cfaa
parent 1339 26a88d12d1a6
permissions -rw-r--r--
Correct the english name of EGRES.
athos@1299
     1
/* -*- C++ -*-
athos@1299
     2
 * src/lemon/lp_cplex.cc
athos@1299
     3
 * - Part of LEMON, a generic C++ optimization library
athos@1299
     4
 *
athos@1299
     5
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     6
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
athos@1299
     7
 *
athos@1299
     8
 * Permission to use, modify and distribute this software is granted
athos@1299
     9
 * provided that this copyright notice appears in all copies. For
athos@1299
    10
 * precise terms see the accompanying LICENSE file.
athos@1299
    11
 *
athos@1299
    12
 * This software is provided "AS IS" with no warranty of any kind,
athos@1299
    13
 * express or implied, and with no claim as to its suitability for any
athos@1299
    14
 * purpose.
athos@1299
    15
 *
athos@1299
    16
 */
athos@1299
    17
athos@1299
    18
#include"lp_cplex.h"
athos@1299
    19
athos@1299
    20
///\file
athos@1299
    21
///\brief Implementation of the LEMON-CPLEX lp solver interface.
athos@1299
    22
namespace lemon {
athos@1299
    23
  
athos@1299
    24
  int LpCplex::_addCol()
athos@1299
    25
  {
athos@1299
    26
    int i = CPXgetnumcols (env, lp);
athos@1319
    27
    Value lb[1],ub[1];
athos@1299
    28
    lb[0]=-INF;//-CPX_INFBOUND;
athos@1299
    29
    ub[0]=INF;//CPX_INFBOUND;
athos@1299
    30
    status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
athos@1299
    31
    return i;
athos@1299
    32
  }
athos@1299
    33
  
athos@1299
    34
  int LpCplex::_addRow() 
athos@1299
    35
  {
athos@1319
    36
    //We want a ranged row
athos@1319
    37
    char sense[1];
athos@1319
    38
    sense[0]='R';
athos@1319
    39
athos@1299
    40
    int i = CPXgetnumrows (env, lp);
athos@1319
    41
    status = CPXnewrows (env, lp, 1, NULL, sense, NULL, NULL);
athos@1299
    42
    return i;
athos@1299
    43
  }
athos@1299
    44
  
athos@1299
    45
  ///\warning Data at index 0 is ignored iin the arrays.
athos@1299
    46
  void LpCplex::_setRowCoeffs(int i, 
athos@1299
    47
			      int length,
athos@1299
    48
			      int  const * indices, 
athos@1299
    49
			      Value  const * values )
athos@1299
    50
  {
athos@1299
    51
    int rowlist[length+1];
athos@1319
    52
    int* p=rowlist;
athos@1299
    53
    for (int k=1;k<=length;++k){
athos@1299
    54
      rowlist[k]=i;
athos@1299
    55
    }
athos@1299
    56
    status = CPXchgcoeflist(env, lp, 
athos@1299
    57
			    length, 
athos@1319
    58
			    p++, 
athos@1319
    59
			    const_cast<int * >(indices++), 
athos@1319
    60
			    const_cast<Value * >(values++));
athos@1299
    61
  }
athos@1299
    62
  
athos@1299
    63
  void LpCplex::_setColCoeffs(int i, 
athos@1299
    64
			      int length,
athos@1299
    65
			      int  const * indices, 
athos@1299
    66
			      Value  const * values)
athos@1299
    67
  {
athos@1299
    68
    int collist[length+1];
athos@1319
    69
    int* p=collist;
athos@1299
    70
    for (int k=1;k<=length;++k){
athos@1299
    71
      collist[k]=i;
athos@1299
    72
    }
athos@1299
    73
    status = CPXchgcoeflist(env, lp, 
athos@1299
    74
			    length, 
athos@1319
    75
			    const_cast<int * >(indices++), 
athos@1319
    76
			    p++, 
athos@1319
    77
			    const_cast<Value * >(values++));
athos@1299
    78
  }
athos@1299
    79
  
athos@1299
    80
  void LpCplex::_setColLowerBound(int i, Value value)
athos@1299
    81
  {
athos@1319
    82
    int indices[1];
athos@1319
    83
    indices[0]=i;
athos@1319
    84
    char lu[1];
athos@1319
    85
    lu[0]='L';
athos@1319
    86
    Value bd[1];
athos@1319
    87
    bd[0]=value;
athos@1319
    88
    status = CPXchgbds (env, lp, 1, indices, lu, bd);
athos@1319
    89
 
athos@1299
    90
  }
athos@1299
    91
  
athos@1299
    92
  void LpCplex::_setColUpperBound(int i, Value value)
athos@1299
    93
  {
athos@1319
    94
    int indices[1];
athos@1319
    95
    indices[0]=i;
athos@1319
    96
    char lu[1];
athos@1319
    97
    lu[0]='U';
athos@1319
    98
    Value bd[1];
athos@1319
    99
    bd[0]=value;
athos@1319
   100
    status = CPXchgbds (env, lp, 1, indices, lu, bd);
athos@1299
   101
  }
athos@1299
   102
  
athos@1299
   103
  void LpCplex::_setRowLowerBound(int i, Value value)
athos@1299
   104
  {
athos@1319
   105
    status = CPXchgcoef (env, lp, i, -1, value);
athos@1319
   106
athos@1299
   107
  }
athos@1299
   108
  
athos@1299
   109
  void LpCplex::_setRowUpperBound(int i, Value value)
athos@1299
   110
  {
athos@1319
   111
    //TODO Ezt kell meg megirni
athos@1339
   112
    //type of the problem
athos@1339
   113
    char sense[1];
athos@1339
   114
    status = CPXgetsense (env, lp, sense, i, i);
athos@1339
   115
    Value rhs[1];
athos@1339
   116
    status = CPXgetrhs (env, lp, rhs, i, i);
athos@1339
   117
athos@1339
   118
    switch (sense[0]) {
athos@1339
   119
    case 'L'://<= constraint
athos@1339
   120
      break;
athos@1339
   121
    case 'E'://= constraint
athos@1339
   122
      break;
athos@1339
   123
    case 'G'://>= constraint
athos@1339
   124
      break;
athos@1339
   125
    case 'R'://ranged constraint
athos@1339
   126
      break;
athos@1339
   127
    default: ;
athos@1339
   128
      //FIXME error
athos@1339
   129
    }
athos@1339
   130
athos@1339
   131
    status = CPXchgcoef (env, lp, i, -2, value_rng);
athos@1299
   132
  }
athos@1299
   133
  
athos@1299
   134
  void LpCplex::_setObjCoeff(int i, Value obj_coef)
athos@1299
   135
  {
athos@1319
   136
    status = CPXchgcoef (env, lp, -1, i, obj_coef);
athos@1319
   137
   }
athos@1319
   138
athos@1319
   139
  LpCplex::SolveExitStatus LpCplex::_solve()
athos@1319
   140
  {
athos@1319
   141
    return SOLVED;
athos@1319
   142
//     int i=  lpx_simplex(lp);
athos@1319
   143
//     switch (i) {
athos@1319
   144
//     case LPX_E_OK: 
athos@1319
   145
//       return SOLVED;
athos@1319
   146
//       break;
athos@1319
   147
//     default:
athos@1319
   148
//       return UNSOLVED;
athos@1319
   149
//     }
athos@1299
   150
  }
athos@1299
   151
athos@1319
   152
  LpCplex::Value LpCplex::_getPrimal(int i)
athos@1299
   153
  {
athos@1299
   154
    return 0;
athos@1299
   155
  }
athos@1299
   156
  
athos@1319
   157
  LpCplex::Value LpCplex::_getPrimalValue()
athos@1319
   158
  {
athos@1319
   159
    return 0;
athos@1319
   160
  }
athos@1319
   161
  
athos@1319
   162
 
athos@1319
   163
  LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
athos@1319
   164
  {
athos@1319
   165
    return OPTIMAL;
athos@1319
   166
//     int stat=  lpx_get_status(lp);
athos@1319
   167
//     switch (stat) {
athos@1319
   168
//     case LPX_UNDEF://Undefined (no solve has been run yet)
athos@1319
   169
//       return UNDEFINED;
athos@1319
   170
//       break;
athos@1319
   171
//     case LPX_NOFEAS://There is no feasible solution (primal, I guess)
athos@1319
   172
//     case LPX_INFEAS://Infeasible 
athos@1319
   173
//       return INFEASIBLE;
athos@1319
   174
//       break;
athos@1319
   175
//     case LPX_UNBND://Unbounded
athos@1319
   176
//       return INFINITE;
athos@1319
   177
//       break;
athos@1319
   178
//     case LPX_FEAS://Feasible
athos@1319
   179
//       return FEASIBLE;
athos@1319
   180
//       break;
athos@1319
   181
//     case LPX_OPT://Feasible
athos@1319
   182
//       return OPTIMAL;
athos@1319
   183
//       break;
athos@1319
   184
//     default:
athos@1319
   185
//       return UNDEFINED; //to avoid gcc warning
athos@1319
   186
//       //FIXME error
athos@1319
   187
//     }
athos@1319
   188
  }
athos@1319
   189
athos@1319
   190
athos@1319
   191
  void LpCplex::_setMax()
athos@1319
   192
  {
athos@1319
   193
    CPXchgobjsen (env, lp, CPX_MAX);
athos@1319
   194
   }
athos@1319
   195
  void LpCplex::_setMin()
athos@1319
   196
  {
athos@1319
   197
    CPXchgobjsen (env, lp, CPX_MIN);
athos@1319
   198
   }
athos@1319
   199
  
athos@1299
   200
} //namespace lemon
athos@1299
   201