| 1 | //----------------------------------------------------------------------------- |
| 2 | // name: OSI Interface for CPLEX |
| 3 | // author: Tobias Pfender |
| 4 | // Konrad-Zuse-Zentrum Berlin (Germany) |
| 5 | // email: [email protected] |
| 6 | // date: 09/25/2000 |
| 7 | // license: this file may be freely distributed under the terms of EPL |
| 8 | // comments: please scan this file for '???' and read the comments |
| 9 | //----------------------------------------------------------------------------- |
| 10 | // Copyright (C) 2000, Tobias Pfender, International Business Machines |
| 11 | // Corporation and others. All Rights Reserved. |
| 12 | |
| 13 | #ifndef OsiCpxSolverInterface_H |
| 14 | #define OsiCpxSolverInterface_H |
| 15 | |
| 16 | #include "OsiSolverInterface.hpp" |
| 17 | #include "CoinWarmStartBasis.hpp" |
| 18 | #include "OsiColCut.hpp" |
| 19 | #include "OsiRowCut.hpp" |
| 20 | |
| 21 | typedef struct cpxlp *CPXLPptr; |
| 22 | typedef struct cpxenv *CPXENVptr; |
| 23 | |
| 24 | /** CPLEX Solver Interface |
| 25 | |
| 26 | Instantiation of OsiCpxSolverInterface for CPLEX |
| 27 | */ |
| 28 | |
| 29 | class OsiCpxSolverInterface : virtual public OsiSolverInterface { |
| 30 | friend void OsiCpxSolverInterfaceUnitTest(const std::string &mpsDir, const std::string &netlibDir); |
| 31 | |
| 32 | public: |
| 33 | //--------------------------------------------------------------------------- |
| 34 | /**@name Solve methods */ |
| 35 | //@{ |
| 36 | /// Solve initial LP relaxation |
| 37 | virtual void initialSolve(); |
| 38 | |
| 39 | /// Resolve an LP relaxation after problem modification |
| 40 | virtual void resolve(); |
| 41 | |
| 42 | /// Invoke solver's built-in enumeration algorithm |
| 43 | virtual void branchAndBound(); |
| 44 | //@} |
| 45 | |
| 46 | //--------------------------------------------------------------------------- |
| 47 | /**@name Parameter set/get methods |
| 48 | |
| 49 | The set methods return true if the parameter was set to the given value, |
| 50 | false otherwise. There can be various reasons for failure: the given |
| 51 | parameter is not applicable for the solver (e.g., refactorization |
| 52 | frequency for the volume algorithm), the parameter is not yet implemented |
| 53 | for the solver or simply the value of the parameter is out of the range |
| 54 | the solver accepts. If a parameter setting call returns false check the |
| 55 | details of your solver. |
| 56 | |
| 57 | The get methods return true if the given parameter is applicable for the |
| 58 | solver and is implemented. In this case the value of the parameter is |
| 59 | returned in the second argument. Otherwise they return false. |
| 60 | */ |
| 61 | //@{ |
| 62 | // Set an integer parameter |
| 63 | bool setIntParam(OsiIntParam key, int value); |
| 64 | // Set an double parameter |
| 65 | bool setDblParam(OsiDblParam key, double value); |
| 66 | // Set a string parameter |
| 67 | bool setStrParam(OsiStrParam key, const std::string &value); |
| 68 | // Get an integer parameter |
| 69 | bool getIntParam(OsiIntParam key, int &value) const; |
| 70 | // Get an double parameter |
| 71 | bool getDblParam(OsiDblParam key, double &value) const; |
| 72 | // Get a string parameter |
| 73 | bool getStrParam(OsiStrParam key, std::string &value) const; |
| 74 | // Set mipstart option (pass column solution to CPLEX before MIP start) |
| 75 | void setMipStart(bool value) { domipstart = value; } |
| 76 | // Get mipstart option value |
| 77 | bool getMipStart() const { return domipstart; } |
| 78 | //@} |
| 79 | |
| 80 | //--------------------------------------------------------------------------- |
| 81 | ///@name Methods returning info on how the solution process terminated |
| 82 | //@{ |
| 83 | /// Are there a numerical difficulties? |
| 84 | virtual bool isAbandoned() const; |
| 85 | /// Is optimality proven? |
| 86 | virtual bool isProvenOptimal() const; |
| 87 | /// Is primal infeasiblity proven? |
| 88 | virtual bool isProvenPrimalInfeasible() const; |
| 89 | /// Is dual infeasiblity proven? |
| 90 | virtual bool isProvenDualInfeasible() const; |
| 91 | /// Is the given primal objective limit reached? |
| 92 | virtual bool isPrimalObjectiveLimitReached() const; |
| 93 | /// Is the given dual objective limit reached? |
| 94 | virtual bool isDualObjectiveLimitReached() const; |
| 95 | /// Iteration limit reached? |
| 96 | virtual bool isIterationLimitReached() const; |
| 97 | //@} |
| 98 | |
| 99 | //--------------------------------------------------------------------------- |
| 100 | /**@name WarmStart related methods */ |
| 101 | //@{ |
| 102 | |
| 103 | /*! \brief Get an empty warm start object |
| 104 | |
| 105 | This routine returns an empty CoinWarmStartBasis object. Its purpose is |
| 106 | to provide a way to give a client a warm start basis object of the |
| 107 | appropriate type, which can resized and modified as desired. |
| 108 | */ |
| 109 | CoinWarmStart *getEmptyWarmStart() const; |
| 110 | |
| 111 | /// Get warmstarting information |
| 112 | virtual CoinWarmStart *getWarmStart() const; |
| 113 | /** Set warmstarting information. Return true/false depending on whether |
| 114 | the warmstart information was accepted or not. */ |
| 115 | virtual bool setWarmStart(const CoinWarmStart *warmstart); |
| 116 | //@} |
| 117 | |
| 118 | //--------------------------------------------------------------------------- |
| 119 | /**@name Hotstart related methods (primarily used in strong branching). <br> |
| 120 | The user can create a hotstart (a snapshot) of the optimization process |
| 121 | then reoptimize over and over again always starting from there.<br> |
| 122 | <strong>NOTE</strong>: between hotstarted optimizations only |
| 123 | bound changes are allowed. */ |
| 124 | //@{ |
| 125 | /// Create a hotstart point of the optimization process |
| 126 | virtual void markHotStart(); |
| 127 | /// Optimize starting from the hotstart |
| 128 | virtual void solveFromHotStart(); |
| 129 | /// Delete the snapshot |
| 130 | virtual void unmarkHotStart(); |
| 131 | //@} |
| 132 | |
| 133 | //--------------------------------------------------------------------------- |
| 134 | /**@name Problem information methods |
| 135 | |
| 136 | These methods call the solver's query routines to return |
| 137 | information about the problem referred to by the current object. |
| 138 | Querying a problem that has no data associated with it result in |
| 139 | zeros for the number of rows and columns, and NULL pointers from |
| 140 | the methods that return vectors. |
| 141 | |
| 142 | Const pointers returned from any data-query method are valid as |
| 143 | long as the data is unchanged and the solver is not called. |
| 144 | */ |
| 145 | //@{ |
| 146 | /**@name Methods related to querying the input data */ |
| 147 | //@{ |
| 148 | /// Get number of columns |
| 149 | virtual int getNumCols() const; |
| 150 | |
| 151 | /// Get number of rows |
| 152 | virtual int getNumRows() const; |
| 153 | |
| 154 | /// Get number of nonzero elements |
| 155 | virtual int getNumElements() const; |
| 156 | |
| 157 | /// Get pointer to array[getNumCols()] of column lower bounds |
| 158 | virtual const double *getColLower() const; |
| 159 | |
| 160 | /// Get pointer to array[getNumCols()] of column upper bounds |
| 161 | virtual const double *getColUpper() const; |
| 162 | |
| 163 | /** Get pointer to array[getNumRows()] of row constraint senses. |
| 164 | <ul> |
| 165 | <li>'L': <= constraint |
| 166 | <li>'E': = constraint |
| 167 | <li>'G': >= constraint |
| 168 | <li>'R': ranged constraint |
| 169 | <li>'N': free constraint |
| 170 | </ul> |
| 171 | */ |
| 172 | virtual const char *getRowSense() const; |
| 173 | |
| 174 | /** Get pointer to array[getNumRows()] of rows right-hand sides |
| 175 | <ul> |
| 176 | <li> if rowsense()[i] == 'L' then rhs()[i] == rowupper()[i] |
| 177 | <li> if rowsense()[i] == 'G' then rhs()[i] == rowlower()[i] |
| 178 | <li> if rowsense()[i] == 'R' then rhs()[i] == rowupper()[i] |
| 179 | <li> if rowsense()[i] == 'N' then rhs()[i] == 0.0 |
| 180 | </ul> |
| 181 | */ |
| 182 | virtual const double *getRightHandSide() const; |
| 183 | |
| 184 | /** Get pointer to array[getNumRows()] of row ranges. |
| 185 | <ul> |
| 186 | <li> if rowsense()[i] == 'R' then |
| 187 | rowrange()[i] == rowupper()[i] - rowlower()[i] |
| 188 | <li> if rowsense()[i] != 'R' then |
| 189 | rowrange()[i] is 0.0 |
| 190 | </ul> |
| 191 | */ |
| 192 | virtual const double *getRowRange() const; |
| 193 | |
| 194 | /// Get pointer to array[getNumRows()] of row lower bounds |
| 195 | virtual const double *getRowLower() const; |
| 196 | |
| 197 | /// Get pointer to array[getNumRows()] of row upper bounds |
| 198 | virtual const double *getRowUpper() const; |
| 199 | |
| 200 | /// Get pointer to array[getNumCols()] of objective function coefficients |
| 201 | virtual const double *getObjCoefficients() const; |
| 202 | |
| 203 | /// Get objective function sense (1 for min (default), -1 for max) |
| 204 | virtual double getObjSense() const; |
| 205 | |
| 206 | /// Return true if column is continuous |
| 207 | virtual bool isContinuous(int colNumber) const; |
| 208 | |
| 209 | #if 0 |
| 210 | /// Return true if column is binary |
| 211 | virtual bool isBinary(int columnNumber) const; |
| 212 | |
| 213 | /** Return true if column is integer. |
| 214 | Note: This function returns true if the the column |
| 215 | is binary or a general integer. |
| 216 | */ |
| 217 | virtual bool isInteger(int columnNumber) const; |
| 218 | |
| 219 | /// Return true if column is general integer |
| 220 | virtual bool isIntegerNonBinary(int columnNumber) const; |
| 221 | |
| 222 | /// Return true if column is binary and not fixed at either bound |
| 223 | virtual bool isFreeBinary(int columnNumber) const; |
| 224 | #endif |
| 225 | |
| 226 | /// Get pointer to row-wise copy of matrix |
| 227 | virtual const CoinPackedMatrix *getMatrixByRow() const; |
| 228 | |
| 229 | /// Get pointer to column-wise copy of matrix |
| 230 | virtual const CoinPackedMatrix *getMatrixByCol() const; |
| 231 | |
| 232 | /// Get solver's value for infinity |
| 233 | virtual double getInfinity() const; |
| 234 | //@} |
| 235 | |
| 236 | /**@name Methods related to querying the solution */ |
| 237 | //@{ |
| 238 | /// Get pointer to array[getNumCols()] of primal solution vector |
| 239 | virtual const double *getColSolution() const; |
| 240 | |
| 241 | /// Get pointer to array[getNumRows()] of dual prices |
| 242 | virtual const double *getRowPrice() const; |
| 243 | |
| 244 | /// Get a pointer to array[getNumCols()] of reduced costs |
| 245 | virtual const double *getReducedCost() const; |
| 246 | |
| 247 | /** Get pointer to array[getNumRows()] of row activity levels (constraint |
| 248 | matrix times the solution vector */ |
| 249 | virtual const double *getRowActivity() const; |
| 250 | |
| 251 | /// Get objective function value |
| 252 | virtual double getObjValue() const; |
| 253 | |
| 254 | /** Get how many iterations it took to solve the problem (whatever |
| 255 | "iteration" mean to the solver. */ |
| 256 | virtual int getIterationCount() const; |
| 257 | |
| 258 | /** Get as many dual rays as the solver can provide. (In case of proven |
| 259 | primal infeasibility there should be at least one.) |
| 260 | |
| 261 | The first getNumRows() ray components will always be associated with |
| 262 | the row duals (as returned by getRowPrice()). If \c fullRay is true, |
| 263 | the final getNumCols() entries will correspond to the ray components |
| 264 | associated with the nonbasic variables. If the full ray is requested |
| 265 | and the method cannot provide it, it will throw an exception. |
| 266 | |
| 267 | <strong>NOTE for implementers of solver interfaces:</strong> <br> |
| 268 | The double pointers in the vector should point to arrays of length |
| 269 | getNumRows() and they should be allocated via new[]. <br> |
| 270 | |
| 271 | <strong>NOTE for users of solver interfaces:</strong> <br> |
| 272 | It is the user's responsibility to free the double pointers in the |
| 273 | vector using delete[]. |
| 274 | */ |
| 275 | virtual std::vector< double * > getDualRays(int maxNumRays, |
| 276 | bool fullRay = false) const; |
| 277 | /** Get as many primal rays as the solver can provide. (In case of proven |
| 278 | dual infeasibility there should be at least one.) |
| 279 | |
| 280 | <strong>NOTE for implementers of solver interfaces:</strong> <br> |
| 281 | The double pointers in the vector should point to arrays of length |
| 282 | getNumCols() and they should be allocated via new[]. <br> |
| 283 | |
| 284 | <strong>NOTE for users of solver interfaces:</strong> <br> |
| 285 | It is the user's responsibility to free the double pointers in the |
| 286 | vector using delete[]. |
| 287 | */ |
| 288 | virtual std::vector< double * > getPrimalRays(int maxNumRays) const; |
| 289 | |
| 290 | #if 0 |
| 291 | /** Get vector of indices of solution which are integer variables |
| 292 | presently at fractional values */ |
| 293 | virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05) |
| 294 | const; |
| 295 | #endif |
| 296 | //@} |
| 297 | //@} |
| 298 | |
| 299 | //--------------------------------------------------------------------------- |
| 300 | |
| 301 | /**@name Problem modifying methods */ |
| 302 | //@{ |
| 303 | //------------------------------------------------------------------------- |
| 304 | /**@name Changing bounds on variables and constraints */ |
| 305 | //@{ |
| 306 | /** Set an objective function coefficient */ |
| 307 | virtual void setObjCoeff(int elementIndex, double elementValue); |
| 308 | |
| 309 | /** Set a a set of objective function coefficients */ |
| 310 | virtual void setObjCoeffSet(const int *indexFirst, |
| 311 | const int *indexLast, |
| 312 | const double *coeffList); |
| 313 | |
| 314 | using OsiSolverInterface::setColLower; |
| 315 | /** Set a single column lower bound<br> |
| 316 | Use -COIN_DBL_MAX for -infinity. */ |
| 317 | virtual void setColLower(int elementIndex, double elementValue); |
| 318 | |
| 319 | using OsiSolverInterface::setColUpper; |
| 320 | /** Set a single column upper bound<br> |
| 321 | Use COIN_DBL_MAX for infinity. */ |
| 322 | virtual void setColUpper(int elementIndex, double elementValue); |
| 323 | |
| 324 | /** Set a single column lower and upper bound<br> |
| 325 | The default implementation just invokes <code>setColLower()</code> and |
| 326 | <code>setColUpper()</code> */ |
| 327 | virtual void setColBounds(int elementIndex, |
| 328 | double lower, double upper); |
| 329 | |
| 330 | /** Set the bounds on a number of columns simultaneously<br> |
| 331 | The default implementation just invokes <code>setCollower()</code> and |
| 332 | <code>setColupper()</code> over and over again. |
| 333 | @param <code>[indexfirst,indexLast]</code> contains the indices of |
| 334 | the constraints whose </em>either</em> bound changes |
| 335 | @param boundList the new lower/upper bound pairs for the variables |
| 336 | */ |
| 337 | virtual void setColSetBounds(const int *indexFirst, |
| 338 | const int *indexLast, |
| 339 | const double *boundList); |
| 340 | |
| 341 | /** Set a single row lower bound<br> |
| 342 | Use -COIN_DBL_MAX for -infinity. */ |
| 343 | virtual void setRowLower(int elementIndex, double elementValue); |
| 344 | |
| 345 | /** Set a single row upper bound<br> |
| 346 | Use COIN_DBL_MAX for infinity. */ |
| 347 | virtual void setRowUpper(int elementIndex, double elementValue); |
| 348 | |
| 349 | /** Set a single row lower and upper bound<br> |
| 350 | The default implementation just invokes <code>setRowLower()</code> and |
| 351 | <code>setRowUpper()</code> */ |
| 352 | virtual void setRowBounds(int elementIndex, |
| 353 | double lower, double upper); |
| 354 | |
| 355 | /** Set the type of a single row<br> */ |
| 356 | virtual void setRowType(int index, char sense, double rightHandSide, |
| 357 | double range); |
| 358 | |
| 359 | /** Set the bounds on a number of rows simultaneously<br> |
| 360 | The default implementation just invokes <code>setRowLower()</code> and |
| 361 | <code>setRowUpper()</code> over and over again. |
| 362 | @param <code>[indexfirst,indexLast]</code> contains the indices of |
| 363 | the constraints whose </em>either</em> bound changes |
| 364 | @param boundList the new lower/upper bound pairs for the constraints |
| 365 | */ |
| 366 | virtual void setRowSetBounds(const int *indexFirst, |
| 367 | const int *indexLast, |
| 368 | const double *boundList); |
| 369 | |
| 370 | /** Set the type of a number of rows simultaneously<br> |
| 371 | The default implementation just invokes <code>setRowType()</code> and |
| 372 | over and over again. |
| 373 | @param <code>[indexfirst,indexLast]</code> contains the indices of |
| 374 | the constraints whose type changes |
| 375 | @param senseList the new senses |
| 376 | @param rhsList the new right hand sides |
| 377 | @param rangeList the new ranges |
| 378 | */ |
| 379 | virtual void setRowSetTypes(const int *indexFirst, |
| 380 | const int *indexLast, |
| 381 | const char *senseList, |
| 382 | const double *rhsList, |
| 383 | const double *rangeList); |
| 384 | //@} |
| 385 | |
| 386 | //------------------------------------------------------------------------- |
| 387 | /**@name Integrality related changing methods */ |
| 388 | //@{ |
| 389 | /** Set the index-th variable to be a continuous variable */ |
| 390 | virtual void setContinuous(int index); |
| 391 | /** Set the index-th variable to be an integer variable */ |
| 392 | virtual void setInteger(int index); |
| 393 | /** Set the variables listed in indices (which is of length len) to be |
| 394 | continuous variables */ |
| 395 | virtual void setContinuous(const int *indices, int len); |
| 396 | /** Set the variables listed in indices (which is of length len) to be |
| 397 | integer variables */ |
| 398 | virtual void setInteger(const int *indices, int len); |
| 399 | //@} |
| 400 | |
| 401 | //------------------------------------------------------------------------- |
| 402 | /// Set objective function sense (1 for min (default), -1 for max,) |
| 403 | virtual void setObjSense(double s); |
| 404 | |
| 405 | /** Set the primal solution column values |
| 406 | |
| 407 | colsol[numcols()] is an array of values of the problem column |
| 408 | variables. These values are copied to memory owned by the |
| 409 | solver object or the solver. They will be returned as the |
| 410 | result of colsol() until changed by another call to |
| 411 | setColsol() or by a call to any solver routine. Whether the |
| 412 | solver makes use of the solution in any way is |
| 413 | solver-dependent. |
| 414 | */ |
| 415 | virtual void setColSolution(const double *colsol); |
| 416 | |
| 417 | /** Set dual solution vector |
| 418 | |
| 419 | rowprice[numrows()] is an array of values of the problem row |
| 420 | dual variables. These values are copied to memory owned by the |
| 421 | solver object or the solver. They will be returned as the |
| 422 | result of rowprice() until changed by another call to |
| 423 | setRowprice() or by a call to any solver routine. Whether the |
| 424 | solver makes use of the solution in any way is |
| 425 | solver-dependent. |
| 426 | */ |
| 427 | virtual void setRowPrice(const double *rowprice); |
| 428 | |
| 429 | //------------------------------------------------------------------------- |
| 430 | /**@name Methods to expand a problem.<br> |
| 431 | Note that if a column is added then by default it will correspond to a |
| 432 | continuous variable. */ |
| 433 | //@{ |
| 434 | using OsiSolverInterface::addCol; |
| 435 | /** */ |
| 436 | virtual void addCol(const CoinPackedVectorBase &vec, |
| 437 | const double collb, const double colub, |
| 438 | const double obj); |
| 439 | |
| 440 | using OsiSolverInterface::addCols; |
| 441 | /** */ |
| 442 | virtual void addCols(const int numcols, |
| 443 | const CoinPackedVectorBase *const *cols, |
| 444 | const double *collb, const double *colub, |
| 445 | const double *obj); |
| 446 | /** */ |
| 447 | virtual void deleteCols(const int num, const int *colIndices); |
| 448 | |
| 449 | using OsiSolverInterface::addRow; |
| 450 | /** */ |
| 451 | virtual void addRow(const CoinPackedVectorBase &vec, |
| 452 | const double rowlb, const double rowub); |
| 453 | /** */ |
| 454 | virtual void addRow(const CoinPackedVectorBase &vec, |
| 455 | const char rowsen, const double rowrhs, |
| 456 | const double rowrng); |
| 457 | |
| 458 | using OsiSolverInterface::addRows; |
| 459 | /** */ |
| 460 | virtual void addRows(const int numrows, |
| 461 | const CoinPackedVectorBase *const *rows, |
| 462 | const double *rowlb, const double *rowub); |
| 463 | /** */ |
| 464 | virtual void addRows(const int numrows, |
| 465 | const CoinPackedVectorBase *const *rows, |
| 466 | const char *rowsen, const double *rowrhs, |
| 467 | const double *rowrng); |
| 468 | /** */ |
| 469 | virtual void deleteRows(const int num, const int *rowIndices); |
| 470 | |
| 471 | #if 0 |
| 472 | // ??? implemented in OsiSolverInterface |
| 473 | //----------------------------------------------------------------------- |
| 474 | /** Apply a collection of cuts.<br> |
| 475 | Only cuts which have an <code>effectiveness >= effectivenessLb</code> |
| 476 | are applied. |
| 477 | <ul> |
| 478 | <li> ReturnCode.numberIneffective() -- number of cuts which were |
| 479 | not applied because they had an |
| 480 | <code>effectiveness < effectivenessLb</code> |
| 481 | <li> ReturnCode.numberInconsistent() -- number of invalid cuts |
| 482 | <li> ReturnCode.numberInconsistentWrtIntegerModel() -- number of |
| 483 | cuts that are invalid with respect to this integer model |
| 484 | <li> ReturnCode.numberInfeasible() -- number of cuts that would |
| 485 | make this integer model infeasible |
| 486 | <li> ReturnCode.numberApplied() -- number of integer cuts which |
| 487 | were applied to the integer model |
| 488 | <li> cs.size() == numberIneffective() + |
| 489 | numberInconsistent() + |
| 490 | numberInconsistentWrtIntegerModel() + |
| 491 | numberInfeasible() + |
| 492 | nubmerApplied() |
| 493 | </ul> |
| 494 | */ |
| 495 | virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs, |
| 496 | double effectivenessLb = 0.0); |
| 497 | #endif |
| 498 | //@} |
| 499 | //@} |
| 500 | |
| 501 | //--------------------------------------------------------------------------- |
| 502 | |
| 503 | /**@name Methods to input a problem */ |
| 504 | //@{ |
| 505 | /** Load in an problem by copying the arguments (the constraints on the |
| 506 | rows are given by lower and upper bounds). If a pointer is 0 then the |
| 507 | following values are the default: |
| 508 | <ul> |
| 509 | <li> <code>colub</code>: all columns have upper bound infinity |
| 510 | <li> <code>collb</code>: all columns have lower bound 0 |
| 511 | <li> <code>rowub</code>: all rows have upper bound infinity |
| 512 | <li> <code>rowlb</code>: all rows have lower bound -infinity |
| 513 | <li> <code>obj</code>: all variables have 0 objective coefficient |
| 514 | </ul> |
| 515 | */ |
| 516 | virtual void loadProblem(const CoinPackedMatrix &matrix, |
| 517 | const double *collb, const double *colub, |
| 518 | const double *obj, |
| 519 | const double *rowlb, const double *rowub); |
| 520 | |
| 521 | /** Load in an problem by assuming ownership of the arguments (the |
| 522 | constraints on the rows are given by lower and upper bounds). For |
| 523 | default values see the previous method. <br> |
| 524 | <strong>WARNING</strong>: The arguments passed to this method will be |
| 525 | freed using the C++ <code>delete</code> and <code>delete[]</code> |
| 526 | functions. |
| 527 | */ |
| 528 | virtual void assignProblem(CoinPackedMatrix *&matrix, |
| 529 | double *&collb, double *&colub, double *&obj, |
| 530 | double *&rowlb, double *&rowub); |
| 531 | |
| 532 | /** Load in an problem by copying the arguments (the constraints on the |
| 533 | rows are given by sense/rhs/range triplets). If a pointer is 0 then the |
| 534 | following values are the default: |
| 535 | <ul> |
| 536 | <li> <code>colub</code>: all columns have upper bound infinity |
| 537 | <li> <code>collb</code>: all columns have lower bound 0 |
| 538 | <li> <code>obj</code>: all variables have 0 objective coefficient |
| 539 | <li> <code>rowsen</code>: all rows are >= |
| 540 | <li> <code>rowrhs</code>: all right hand sides are 0 |
| 541 | <li> <code>rowrng</code>: 0 for the ranged rows |
| 542 | </ul> |
| 543 | */ |
| 544 | virtual void loadProblem(const CoinPackedMatrix &matrix, |
| 545 | const double *collb, const double *colub, |
| 546 | const double *obj, |
| 547 | const char *rowsen, const double *rowrhs, |
| 548 | const double *rowrng); |
| 549 | |
| 550 | /** Load in an problem by assuming ownership of the arguments (the |
| 551 | constraints on the rows are given by sense/rhs/range triplets). For |
| 552 | default values see the previous method. <br> |
| 553 | <strong>WARNING</strong>: The arguments passed to this method will be |
| 554 | freed using the C++ <code>delete</code> and <code>delete[]</code> |
| 555 | functions. |
| 556 | */ |
| 557 | virtual void assignProblem(CoinPackedMatrix *&matrix, |
| 558 | double *&collb, double *&colub, double *&obj, |
| 559 | char *&rowsen, double *&rowrhs, |
| 560 | double *&rowrng); |
| 561 | |
| 562 | /** Just like the other loadProblem() methods except that the matrix is |
| 563 | given in a standard column major ordered format (without gaps). */ |
| 564 | virtual void loadProblem(const int numcols, const int numrows, |
| 565 | const int *start, const int *index, |
| 566 | const double *value, |
| 567 | const double *collb, const double *colub, |
| 568 | const double *obj, |
| 569 | const double *rowlb, const double *rowub); |
| 570 | |
| 571 | /** Just like the other loadProblem() methods except that the matrix is |
| 572 | given in a standard column major ordered format (without gaps). */ |
| 573 | virtual void loadProblem(const int numcols, const int numrows, |
| 574 | const int *start, const int *index, |
| 575 | const double *value, |
| 576 | const double *collb, const double *colub, |
| 577 | const double *obj, |
| 578 | const char *rowsen, const double *rowrhs, |
| 579 | const double *rowrng); |
| 580 | |
| 581 | using OsiSolverInterface::readMps; |
| 582 | /** Read an mps file from the given filename */ |
| 583 | virtual int readMps(const char *filename, |
| 584 | const char *extension = "mps"); |
| 585 | |
| 586 | /** Write the problem into an mps file of the given filename. |
| 587 | If objSense is non zero then -1.0 forces the code to write a |
| 588 | maximization objective and +1.0 to write a minimization one. |
| 589 | If 0.0 then solver can do what it wants */ |
| 590 | virtual void writeMps(const char *filename, |
| 591 | const char *extension = "mps", |
| 592 | double objSense = 0.0) const; |
| 593 | |
| 594 | //@} |
| 595 | |
| 596 | /**@name Message handling */ |
| 597 | //@{ |
| 598 | /** Pass in a message handler |
| 599 | It is the client's responsibility to destroy a message handler installed |
| 600 | by this routine; it will not be destroyed when the solver interface is |
| 601 | destroyed. |
| 602 | */ |
| 603 | void passInMessageHandler(CoinMessageHandler *handler); |
| 604 | //@} |
| 605 | |
| 606 | //--------------------------------------------------------------------------- |
| 607 | |
| 608 | /**@name CPLEX specific public interfaces */ |
| 609 | //@{ |
| 610 | /** Get pointer to CPLEX model and free all specified cached data entries |
| 611 | (combined with logical or-operator '|' ): |
| 612 | */ |
| 613 | enum keepCachedFlag { |
| 614 | /// discard all cached data (default) |
| 615 | KEEPCACHED_NONE = 0, |
| 616 | /// column information: objective values, lower and upper bounds, variable types |
| 617 | KEEPCACHED_COLUMN = 1, |
| 618 | /// row information: right hand sides, ranges and senses, lower and upper bounds for row |
| 619 | KEEPCACHED_ROW = 2, |
| 620 | /// problem matrix: matrix ordered by column and by row |
| 621 | KEEPCACHED_MATRIX = 4, |
| 622 | /// LP solution: primal and dual solution, reduced costs, row activities |
| 623 | KEEPCACHED_RESULTS = 8, |
| 624 | /// only discard cached LP solution |
| 625 | KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX, |
| 626 | /// keep all cached data (similar to getMutableLpPtr()) |
| 627 | KEEPCACHED_ALL = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS, |
| 628 | /// free only cached column and LP solution information |
| 629 | FREECACHED_COLUMN = KEEPCACHED_PROBLEM & ~KEEPCACHED_COLUMN, |
| 630 | /// free only cached row and LP solution information |
| 631 | FREECACHED_ROW = KEEPCACHED_PROBLEM & ~KEEPCACHED_ROW, |
| 632 | /// free only cached matrix and LP solution information |
| 633 | FREECACHED_MATRIX = KEEPCACHED_PROBLEM & ~KEEPCACHED_MATRIX, |
| 634 | /// free only cached LP solution information |
| 635 | FREECACHED_RESULTS = KEEPCACHED_ALL & ~KEEPCACHED_RESULTS |
| 636 | }; |
| 637 | CPXLPptr getLpPtr(int keepCached = KEEPCACHED_NONE); |
| 638 | |
| 639 | //@{ |
| 640 | /// Method to access CPLEX environment pointer |
| 641 | CPXENVptr getEnvironmentPtr(); |
| 642 | //@} |
| 643 | |
| 644 | /// return a vector of variable types (continous, binary, integer) |
| 645 | const char *getCtype() const; |
| 646 | |
| 647 | /**@name Constructors and destructor */ |
| 648 | //@{ |
| 649 | /// Default Constructor |
| 650 | OsiCpxSolverInterface(); |
| 651 | |
| 652 | /// Clone |
| 653 | virtual OsiSolverInterface *clone(bool copyData = true) const; |
| 654 | |
| 655 | /// Copy constructor |
| 656 | OsiCpxSolverInterface(const OsiCpxSolverInterface &); |
| 657 | |
| 658 | /// Assignment operator |
| 659 | OsiCpxSolverInterface &operator=(const OsiCpxSolverInterface &rhs); |
| 660 | |
| 661 | /// Destructor |
| 662 | virtual ~OsiCpxSolverInterface(); |
| 663 | |
| 664 | /// Resets as if default constructor |
| 665 | virtual void reset(); |
| 666 | //@} |
| 667 | |
| 668 | /***************************************************************************/ |
| 669 | /**@name OsiSimplexInterface methods |
| 670 | Cplex adds a slack with coeff +1 in "<=" and "=" constraints, |
| 671 | with coeff -1 in ">=", |
| 672 | slack being non negative. We switch in order to get a "Clp tableau" |
| 673 | where all the slacks have coefficient +1 in the original tableau. |
| 674 | |
| 675 | If a slack for ">=" is non basic, invB is not changed; |
| 676 | column of the slack in the optimal tableau is flipped. |
| 677 | |
| 678 | If a slack for ">=" is basic, corresp. row of invB is flipped; |
| 679 | whole row of the optimal tableau is flipped; |
| 680 | then whole column for the slack in opt tableau is flipped. |
| 681 | |
| 682 | Ranged rows are not supported. It might work, but no garantee is given. |
| 683 | |
| 684 | Code implemented only for Cplex9.0 and higher, lower version number of |
| 685 | Cplex will abort the code. |
| 686 | */ |
| 687 | //@{ |
| 688 | |
| 689 | /** Returns 1 if can just do getBInv etc |
| 690 | 2 if has all OsiSimplex methods |
| 691 | and 0 if it has none */ |
| 692 | virtual int canDoSimplexInterface() const; |
| 693 | |
| 694 | using OsiSolverInterface::enableSimplexInterface; |
| 695 | /** Useless function, defined only for compatibility with |
| 696 | OsiSimplexInterface |
| 697 | */ |
| 698 | virtual void enableSimplexInterface(int doingPrimal) {}; |
| 699 | |
| 700 | /** Useless function, defined only for compatibility with |
| 701 | OsiSimplexInterface |
| 702 | */ |
| 703 | virtual void disableSimplexInterface() {}; |
| 704 | |
| 705 | /** Useless function, defined only for compatibility with |
| 706 | OsiSimplexInterface |
| 707 | */ |
| 708 | virtual void enableFactorization() const {}; |
| 709 | |
| 710 | /** Useless function, defined only for compatibility with |
| 711 | OsiSimplexInterface |
| 712 | */ |
| 713 | virtual void disableFactorization() const {}; |
| 714 | |
| 715 | ///Returns true if a basis is available |
| 716 | virtual bool basisIsAvailable() const; |
| 717 | |
| 718 | /** Returns a basis status of the structural/artificial variables |
| 719 | At present as warm start i.e 0: free, 1: basic, 2: upper, 3: lower |
| 720 | */ |
| 721 | virtual void getBasisStatus(int *cstat, int *rstat) const; |
| 722 | |
| 723 | ///Get a row of the tableau (slack part in slack if not NULL) |
| 724 | virtual void getBInvARow(int row, double *z, double *slack = NULL) const; |
| 725 | |
| 726 | ///Get a row of the basis inverse |
| 727 | virtual void getBInvRow(int row, double *z) const; |
| 728 | |
| 729 | ///Get a column of the tableau |
| 730 | virtual void getBInvACol(int col, double *vec) const; |
| 731 | |
| 732 | ///Get a column of the basis inverse |
| 733 | virtual void getBInvCol(int col, double *vec) const; |
| 734 | |
| 735 | /** Get indices of the pivot variable in each row |
| 736 | (order of indices corresponds to the |
| 737 | order of elements in a vector retured by getBInvACol() and |
| 738 | getBInvCol()). |
| 739 | */ |
| 740 | virtual void getBasics(int *index) const; |
| 741 | /// switches CPLEX to prob type LP |
| 742 | void switchToLP(); |
| 743 | |
| 744 | /// switches CPLEX to prob type MIP |
| 745 | void switchToMIP(); |
| 746 | |
| 747 | //@} |
| 748 | /***************************************************************************/ |
| 749 | |
| 750 | protected: |
| 751 | /// Get LP Pointer for const methods |
| 752 | CPXLPptr getMutableLpPtr() const; |
| 753 | |
| 754 | /// Get Environment Pointer for const methods |
| 755 | CPXENVptr getMutableEnvironmentPtr() const; |
| 756 | |
| 757 | /**@name Protected methods */ |
| 758 | //@{ |
| 759 | /// Apply a row cut. Return true if cut was applied. |
| 760 | virtual void applyRowCut(const OsiRowCut &rc); |
| 761 | |
| 762 | /** Apply a column cut (bound adjustment). |
| 763 | Return true if cut was applied. |
| 764 | */ |
| 765 | virtual void applyColCut(const OsiColCut &cc); |
| 766 | //@} |
| 767 | |
| 768 | private: |
| 769 | /**@name Private static class functions */ |
| 770 | //@{ |
| 771 | /// resizes coltype_ vector to be able to store at least minsize elements |
| 772 | void resizeColType(int minsize); |
| 773 | |
| 774 | /// frees colsize_ vector |
| 775 | void freeColType(); |
| 776 | //@} |
| 777 | |
| 778 | /**@name Private methods */ |
| 779 | //@{ |
| 780 | |
| 781 | /// The real work of a copy constructor (used by copy and assignment) |
| 782 | void gutsOfCopy(const OsiCpxSolverInterface &source); |
| 783 | |
| 784 | /// The real work of the constructor |
| 785 | void gutsOfConstructor(); |
| 786 | |
| 787 | /// The real work of the destructor |
| 788 | void gutsOfDestructor(); |
| 789 | |
| 790 | /// free cached column rim vectors |
| 791 | void freeCachedColRim(); |
| 792 | |
| 793 | /// free cached row rim vectors |
| 794 | void freeCachedRowRim(); |
| 795 | |
| 796 | /// free cached result vectors |
| 797 | void freeCachedResults(); |
| 798 | |
| 799 | /// free cached matrices |
| 800 | void freeCachedMatrix(); |
| 801 | |
| 802 | /// free all cached data (except specified entries, see getLpPtr()) |
| 803 | void freeCachedData(int keepCached = KEEPCACHED_NONE); |
| 804 | |
| 805 | /// free all allocated memory |
| 806 | void freeAllMemory(); |
| 807 | |
| 808 | //@} |
| 809 | |
| 810 | /**@name Private member data */ |
| 811 | //@{ |
| 812 | /// CPLEX environment used in this class instance |
| 813 | mutable CPXENVptr env_; |
| 814 | /// CPLEX model represented by this class instance |
| 815 | mutable CPXLPptr lp_; |
| 816 | |
| 817 | /// Hotstart information |
| 818 | int *hotStartCStat_; |
| 819 | int hotStartCStatSize_; |
| 820 | int *hotStartRStat_; |
| 821 | int hotStartRStatSize_; |
| 822 | int hotStartMaxIteration_; |
| 823 | |
| 824 | /**@name Cached information derived from the CPLEX model */ |
| 825 | //@{ |
| 826 | /// Pointer to objective vector |
| 827 | mutable double *obj_; |
| 828 | |
| 829 | /// Pointer to dense vector of variable lower bounds |
| 830 | mutable double *collower_; |
| 831 | |
| 832 | /// Pointer to dense vector of variable lower bounds |
| 833 | mutable double *colupper_; |
| 834 | |
| 835 | /// Pointer to dense vector of row sense indicators |
| 836 | mutable char *rowsense_; |
| 837 | |
| 838 | /// Pointer to dense vector of row right-hand side values |
| 839 | mutable double *rhs_; |
| 840 | |
| 841 | /// Pointer to dense vector of slack upper bounds for range constraints (undefined for non-range rows) |
| 842 | mutable double *rowrange_; |
| 843 | |
| 844 | /// Pointer to dense vector of row lower bounds |
| 845 | mutable double *rowlower_; |
| 846 | |
| 847 | /// Pointer to dense vector of row upper bounds |
| 848 | mutable double *rowupper_; |
| 849 | |
| 850 | /// Pointer to primal solution vector |
| 851 | mutable double *colsol_; |
| 852 | |
| 853 | /// Pointer to dual solution vector |
| 854 | mutable double *rowsol_; |
| 855 | |
| 856 | /// Pointer to reduced cost vector |
| 857 | mutable double *redcost_; |
| 858 | |
| 859 | /// Pointer to row activity (slack) vector |
| 860 | mutable double *rowact_; |
| 861 | |
| 862 | /// Pointer to row-wise copy of problem matrix coefficients. |
| 863 | mutable CoinPackedMatrix *matrixByRow_; |
| 864 | |
| 865 | /// Pointer to row-wise copy of problem matrix coefficients. |
| 866 | mutable CoinPackedMatrix *matrixByCol_; |
| 867 | //@} |
| 868 | |
| 869 | /**@name Additional information needed for storing MIP problems */ |
| 870 | //@{ |
| 871 | /// Pointer to dense vector of variable types (continous, binary, integer) |
| 872 | char *coltype_; |
| 873 | |
| 874 | /// Size of allocated memory for coltype_ |
| 875 | int coltypesize_; |
| 876 | |
| 877 | /// Stores whether CPLEX' prob type is currently set to MIP |
| 878 | mutable bool probtypemip_; |
| 879 | |
| 880 | /// Whether to pass a column solution to CPLEX before starting MIP solve (copymipstart) |
| 881 | bool domipstart; |
| 882 | |
| 883 | /// Whether to disable use of advanced basis (if given) |
| 884 | bool disableadvbasis; |
| 885 | |
| 886 | //@} |
| 887 | }; |
| 888 | |
| 889 | //############################################################################# |
| 890 | /** A function that tests the methods in the OsiCpxSolverInterface class. */ |
| 891 | void OsiCpxSolverInterfaceUnitTest(const std::string &mpsDir, const std::string &netlibDir); |
| 892 | |
| 893 | #endif |
| 894 | |
| 895 | /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 |
| 896 | */ |