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