| 1 | /* $Id: CoinSnapshot.hpp 1448 2011-06-19 15:34:41Z stefan $ */ |
| 2 | // Copyright (C) 2006, International Business Machines |
| 3 | // Corporation and others. All Rights Reserved. |
| 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
| 5 | |
| 6 | #ifndef CoinSnapshot_H |
| 7 | #define CoinSnapshot_H |
| 8 | |
| 9 | class CoinPackedMatrix; |
| 10 | #include "CoinTypes.hpp" |
| 11 | |
| 12 | //############################################################################# |
| 13 | |
| 14 | /** NON Abstract Base Class for interfacing with cut generators or branching code or .. |
| 15 | It is designed to be snapshot of a problem at a node in tree |
| 16 | |
| 17 | The class may or may not own the arrays - see owned_ |
| 18 | |
| 19 | |
| 20 | Querying a problem that has no data associated with it will result in |
| 21 | zeros for the number of rows and columns, and NULL pointers from |
| 22 | the methods that return arrays. |
| 23 | */ |
| 24 | |
| 25 | class CoinSnapshot { |
| 26 | |
| 27 | public: |
| 28 | |
| 29 | //--------------------------------------------------------------------------- |
| 30 | /**@name Problem query methods |
| 31 | |
| 32 | The Matrix pointers may be NULL |
| 33 | */ |
| 34 | //@{ |
| 35 | /// Get number of columns |
| 36 | inline int getNumCols() const |
| 37 | { return numCols_;} |
| 38 | |
| 39 | /// Get number of rows |
| 40 | inline int getNumRows() const |
| 41 | { return numRows_;} |
| 42 | |
| 43 | /// Get number of nonzero elements |
| 44 | inline int getNumElements() const |
| 45 | { return numElements_;} |
| 46 | |
| 47 | /// Get number of integer variables |
| 48 | inline int getNumIntegers() const |
| 49 | { return numIntegers_;} |
| 50 | |
| 51 | /// Get pointer to array[getNumCols()] of column lower bounds |
| 52 | inline const double * getColLower() const |
| 53 | { return colLower_;} |
| 54 | |
| 55 | /// Get pointer to array[getNumCols()] of column upper bounds |
| 56 | inline const double * getColUpper() const |
| 57 | { return colUpper_;} |
| 58 | |
| 59 | /// Get pointer to array[getNumRows()] of row lower bounds |
| 60 | inline const double * getRowLower() const |
| 61 | { return rowLower_;} |
| 62 | |
| 63 | /// Get pointer to array[getNumRows()] of row upper bounds |
| 64 | inline const double * getRowUpper() const |
| 65 | { return rowUpper_;} |
| 66 | |
| 67 | /** Get pointer to array[getNumRows()] of row right-hand sides |
| 68 | This gives same results as OsiSolverInterface for useful cases |
| 69 | If getRowUpper()[i] != infinity then |
| 70 | getRightHandSide()[i] == getRowUpper()[i] |
| 71 | else |
| 72 | getRightHandSide()[i] == getRowLower()[i] |
| 73 | */ |
| 74 | inline const double * getRightHandSide() const |
| 75 | { return rightHandSide_;} |
| 76 | |
| 77 | /// Get pointer to array[getNumCols()] of objective function coefficients |
| 78 | inline const double * getObjCoefficients() const |
| 79 | { return objCoefficients_;} |
| 80 | |
| 81 | /// Get objective function sense (1 for min (default), -1 for max) |
| 82 | inline double getObjSense() const |
| 83 | { return objSense_;} |
| 84 | |
| 85 | /// Return true if variable is continuous |
| 86 | inline bool isContinuous(int colIndex) const |
| 87 | { return colType_[colIndex]=='C';} |
| 88 | |
| 89 | /// Return true if variable is binary |
| 90 | inline bool isBinary(int colIndex) const |
| 91 | { return colType_[colIndex]=='B';} |
| 92 | |
| 93 | /// Return true if column is integer. |
| 94 | inline bool isInteger(int colIndex) const |
| 95 | { return colType_[colIndex]=='B'||colType_[colIndex]=='I';} |
| 96 | |
| 97 | /// Return true if variable is general integer |
| 98 | inline bool isIntegerNonBinary(int colIndex) const |
| 99 | { return colType_[colIndex]=='I';} |
| 100 | |
| 101 | /// Return true if variable is binary and not fixed at either bound |
| 102 | inline bool isFreeBinary(int colIndex) const |
| 103 | { return colType_[colIndex]=='B'&&colUpper_[colIndex]>colLower_[colIndex];} |
| 104 | |
| 105 | /// Get colType array ('B', 'I', or 'C' for Binary, Integer and Continuous) |
| 106 | inline const char * getColType() const |
| 107 | { return colType_;} |
| 108 | |
| 109 | /// Get pointer to row-wise copy of current matrix |
| 110 | inline const CoinPackedMatrix * getMatrixByRow() const |
| 111 | { return matrixByRow_;} |
| 112 | |
| 113 | /// Get pointer to column-wise copy of current matrix |
| 114 | inline const CoinPackedMatrix * getMatrixByCol() const |
| 115 | { return matrixByCol_;} |
| 116 | |
| 117 | /// Get pointer to row-wise copy of "original" matrix |
| 118 | inline const CoinPackedMatrix * getOriginalMatrixByRow() const |
| 119 | { return originalMatrixByRow_;} |
| 120 | |
| 121 | /// Get pointer to column-wise copy of "original" matrix |
| 122 | inline const CoinPackedMatrix * getOriginalMatrixByCol() const |
| 123 | { return originalMatrixByCol_;} |
| 124 | //@} |
| 125 | |
| 126 | /**@name Solution query methods */ |
| 127 | //@{ |
| 128 | /// Get pointer to array[getNumCols()] of primal variable values |
| 129 | inline const double * getColSolution() const |
| 130 | { return colSolution_;} |
| 131 | |
| 132 | /// Get pointer to array[getNumRows()] of dual variable values |
| 133 | inline const double * getRowPrice() const |
| 134 | { return rowPrice_;} |
| 135 | |
| 136 | /// Get a pointer to array[getNumCols()] of reduced costs |
| 137 | inline const double * getReducedCost() const |
| 138 | { return reducedCost_;} |
| 139 | |
| 140 | /// Get pointer to array[getNumRows()] of row activity levels (constraint matrix times the solution vector). |
| 141 | inline const double * getRowActivity() const |
| 142 | { return rowActivity_;} |
| 143 | |
| 144 | /// Get pointer to array[getNumCols()] of primal variable values which should not be separated (for debug) |
| 145 | inline const double * getDoNotSeparateThis() const |
| 146 | { return doNotSeparateThis_;} |
| 147 | //@} |
| 148 | |
| 149 | /**@name Other scalar get methods */ |
| 150 | //@{ |
| 151 | /// Get solver's value for infinity |
| 152 | inline double getInfinity() const |
| 153 | { return infinity_;} |
| 154 | |
| 155 | /** Get objective function value - includinbg any offset i.e. |
| 156 | sum c sub j * x subj - objValue = objOffset */ |
| 157 | inline double getObjValue() const |
| 158 | { return objValue_;} |
| 159 | |
| 160 | /// Get objective offset i.e. sum c sub j * x subj -objValue = objOffset |
| 161 | inline double getObjOffset() const |
| 162 | { return objOffset_;} |
| 163 | |
| 164 | /// Get dual tolerance |
| 165 | inline double getDualTolerance() const |
| 166 | { return dualTolerance_;} |
| 167 | |
| 168 | /// Get primal tolerance |
| 169 | inline double getPrimalTolerance() const |
| 170 | { return primalTolerance_;} |
| 171 | |
| 172 | /// Get integer tolerance |
| 173 | inline double getIntegerTolerance() const |
| 174 | { return integerTolerance_;} |
| 175 | |
| 176 | /// Get integer upper bound i.e. best solution * getObjSense |
| 177 | inline double getIntegerUpperBound() const |
| 178 | { return integerUpperBound_;} |
| 179 | |
| 180 | /// Get integer lower bound i.e. best possible solution * getObjSense |
| 181 | inline double getIntegerLowerBound() const |
| 182 | { return integerLowerBound_;} |
| 183 | //@} |
| 184 | |
| 185 | //--------------------------------------------------------------------------- |
| 186 | |
| 187 | /**@name Method to input a problem */ |
| 188 | //@{ |
| 189 | /** Load in an problem by copying the arguments (the constraints on the |
| 190 | rows are given by lower and upper bounds). If a pointer is NULL then the |
| 191 | following values are the default: |
| 192 | <ul> |
| 193 | <li> <code>colub</code>: all columns have upper bound infinity |
| 194 | <li> <code>collb</code>: all columns have lower bound 0 |
| 195 | <li> <code>rowub</code>: all rows have upper bound infinity |
| 196 | <li> <code>rowlb</code>: all rows have lower bound -infinity |
| 197 | <li> <code>obj</code>: all variables have 0 objective coefficient |
| 198 | </ul> |
| 199 | All solution type arrays will be deleted |
| 200 | */ |
| 201 | void loadProblem(const CoinPackedMatrix& matrix, |
| 202 | const double* collb, const double* colub, |
| 203 | const double* obj, |
| 204 | const double* rowlb, const double* rowub, |
| 205 | bool makeRowCopy=false); |
| 206 | |
| 207 | //@} |
| 208 | |
| 209 | //--------------------------------------------------------------------------- |
| 210 | |
| 211 | /**@name Methods to set data */ |
| 212 | //@{ |
| 213 | /// Set number of columns |
| 214 | inline void setNumCols(int value) |
| 215 | { numCols_ = value;} |
| 216 | |
| 217 | /// Set number of rows |
| 218 | inline void setNumRows(int value) |
| 219 | { numRows_ = value;} |
| 220 | |
| 221 | /// Set number of nonzero elements |
| 222 | inline void setNumElements(int value) |
| 223 | { numElements_ = value;} |
| 224 | |
| 225 | /// Set number of integer variables |
| 226 | inline void setNumIntegers(int value) |
| 227 | { numIntegers_ = value;} |
| 228 | |
| 229 | /// Set pointer to array[getNumCols()] of column lower bounds |
| 230 | void setColLower(const double * array, bool copyIn=true); |
| 231 | |
| 232 | /// Set pointer to array[getNumCols()] of column upper bounds |
| 233 | void setColUpper(const double * array, bool copyIn=true); |
| 234 | |
| 235 | /// Set pointer to array[getNumRows()] of row lower bounds |
| 236 | void setRowLower(const double * array, bool copyIn=true); |
| 237 | |
| 238 | /// Set pointer to array[getNumRows()] of row upper bounds |
| 239 | void setRowUpper(const double * array, bool copyIn=true); |
| 240 | |
| 241 | /** Set pointer to array[getNumRows()] of row right-hand sides |
| 242 | This gives same results as OsiSolverInterface for useful cases |
| 243 | If getRowUpper()[i] != infinity then |
| 244 | getRightHandSide()[i] == getRowUpper()[i] |
| 245 | else |
| 246 | getRightHandSide()[i] == getRowLower()[i] |
| 247 | */ |
| 248 | void setRightHandSide(const double * array, bool copyIn=true); |
| 249 | |
| 250 | /** Create array[getNumRows()] of row right-hand sides |
| 251 | using existing information |
| 252 | This gives same results as OsiSolverInterface for useful cases |
| 253 | If getRowUpper()[i] != infinity then |
| 254 | getRightHandSide()[i] == getRowUpper()[i] |
| 255 | else |
| 256 | getRightHandSide()[i] == getRowLower()[i] |
| 257 | */ |
| 258 | void createRightHandSide(); |
| 259 | |
| 260 | /// Set pointer to array[getNumCols()] of objective function coefficients |
| 261 | void setObjCoefficients(const double * array, bool copyIn=true); |
| 262 | |
| 263 | /// Set objective function sense (1 for min (default), -1 for max) |
| 264 | inline void setObjSense(double value) |
| 265 | { objSense_ = value;} |
| 266 | |
| 267 | /// Set colType array ('B', 'I', or 'C' for Binary, Integer and Continuous) |
| 268 | void setColType(const char *array, bool copyIn=true); |
| 269 | |
| 270 | /// Set pointer to row-wise copy of current matrix |
| 271 | void setMatrixByRow(const CoinPackedMatrix * matrix, bool copyIn=true); |
| 272 | |
| 273 | /// Create row-wise copy from MatrixByCol |
| 274 | void createMatrixByRow(); |
| 275 | |
| 276 | /// Set pointer to column-wise copy of current matrix |
| 277 | void setMatrixByCol(const CoinPackedMatrix * matrix, bool copyIn=true); |
| 278 | |
| 279 | /// Set pointer to row-wise copy of "original" matrix |
| 280 | void setOriginalMatrixByRow(const CoinPackedMatrix * matrix, bool copyIn=true); |
| 281 | |
| 282 | /// Set pointer to column-wise copy of "original" matrix |
| 283 | void setOriginalMatrixByCol(const CoinPackedMatrix * matrix, bool copyIn=true); |
| 284 | |
| 285 | /// Set pointer to array[getNumCols()] of primal variable values |
| 286 | void setColSolution(const double * array, bool copyIn=true); |
| 287 | |
| 288 | /// Set pointer to array[getNumRows()] of dual variable values |
| 289 | void setRowPrice(const double * array, bool copyIn=true); |
| 290 | |
| 291 | /// Set a pointer to array[getNumCols()] of reduced costs |
| 292 | void setReducedCost(const double * array, bool copyIn=true); |
| 293 | |
| 294 | /// Set pointer to array[getNumRows()] of row activity levels (constraint matrix times the solution vector). |
| 295 | void setRowActivity(const double * array, bool copyIn=true); |
| 296 | |
| 297 | /// Set pointer to array[getNumCols()] of primal variable values which should not be separated (for debug) |
| 298 | void setDoNotSeparateThis(const double * array, bool copyIn=true); |
| 299 | |
| 300 | /// Set solver's value for infinity |
| 301 | inline void setInfinity(double value) |
| 302 | { infinity_ = value;} |
| 303 | |
| 304 | /// Set objective function value (including any rhs offset) |
| 305 | inline void setObjValue(double value) |
| 306 | { objValue_ = value;} |
| 307 | |
| 308 | /// Set objective offset i.e. sum c sub j * x subj -objValue = objOffset |
| 309 | inline void setObjOffset(double value) |
| 310 | { objOffset_ = value;} |
| 311 | |
| 312 | /// Set dual tolerance |
| 313 | inline void setDualTolerance(double value) |
| 314 | { dualTolerance_ = value;} |
| 315 | |
| 316 | /// Set primal tolerance |
| 317 | inline void setPrimalTolerance(double value) |
| 318 | { primalTolerance_ = value;} |
| 319 | |
| 320 | /// Set integer tolerance |
| 321 | inline void setIntegerTolerance(double value) |
| 322 | { integerTolerance_ = value;} |
| 323 | |
| 324 | /// Set integer upper bound i.e. best solution * getObjSense |
| 325 | inline void setIntegerUpperBound(double value) |
| 326 | { integerUpperBound_ = value;} |
| 327 | |
| 328 | /// Set integer lower bound i.e. best possible solution * getObjSense |
| 329 | inline void setIntegerLowerBound(double value) |
| 330 | { integerLowerBound_ = value;} |
| 331 | //@} |
| 332 | |
| 333 | //--------------------------------------------------------------------------- |
| 334 | |
| 335 | ///@name Constructors and destructors |
| 336 | //@{ |
| 337 | /// Default Constructor |
| 338 | CoinSnapshot(); |
| 339 | |
| 340 | /// Copy constructor |
| 341 | CoinSnapshot(const CoinSnapshot &); |
| 342 | |
| 343 | /// Assignment operator |
| 344 | CoinSnapshot & operator=(const CoinSnapshot& rhs); |
| 345 | |
| 346 | /// Destructor |
| 347 | virtual ~CoinSnapshot (); |
| 348 | |
| 349 | //@} |
| 350 | |
| 351 | private: |
| 352 | ///@name private functions |
| 353 | //@{ |
| 354 | /** Does main work of destructor - type (or'ed) |
| 355 | 1 - NULLify pointers |
| 356 | 2 - delete pointers |
| 357 | 4 - initialize scalars (tolerances etc) |
| 358 | 8 - initialize scalars (objValue etc0 |
| 359 | */ |
| 360 | void gutsOfDestructor(int type); |
| 361 | /// Does main work of copy |
| 362 | void gutsOfCopy(const CoinSnapshot & rhs); |
| 363 | //@} |
| 364 | |
| 365 | ///@name Private member data |
| 366 | |
| 367 | /// objective function sense (1 for min (default), -1 for max) |
| 368 | double objSense_; |
| 369 | |
| 370 | /// solver's value for infinity |
| 371 | double infinity_; |
| 372 | |
| 373 | /// objective function value (including any rhs offset) |
| 374 | double objValue_; |
| 375 | |
| 376 | /// objective offset i.e. sum c sub j * x subj -objValue = objOffset |
| 377 | double objOffset_; |
| 378 | |
| 379 | /// dual tolerance |
| 380 | double dualTolerance_; |
| 381 | |
| 382 | /// primal tolerance |
| 383 | double primalTolerance_; |
| 384 | |
| 385 | /// integer tolerance |
| 386 | double integerTolerance_; |
| 387 | |
| 388 | /// integer upper bound i.e. best solution * getObjSense |
| 389 | double integerUpperBound_; |
| 390 | |
| 391 | /// integer lower bound i.e. best possible solution * getObjSense |
| 392 | double integerLowerBound_; |
| 393 | |
| 394 | /// pointer to array[getNumCols()] of column lower bounds |
| 395 | const double * colLower_; |
| 396 | |
| 397 | /// pointer to array[getNumCols()] of column upper bounds |
| 398 | const double * colUpper_; |
| 399 | |
| 400 | /// pointer to array[getNumRows()] of row lower bounds |
| 401 | const double * rowLower_; |
| 402 | |
| 403 | /// pointer to array[getNumRows()] of row upper bounds |
| 404 | const double * rowUpper_; |
| 405 | |
| 406 | /// pointer to array[getNumRows()] of rhs side values |
| 407 | const double * rightHandSide_; |
| 408 | |
| 409 | /// pointer to array[getNumCols()] of objective function coefficients |
| 410 | const double * objCoefficients_; |
| 411 | |
| 412 | /// colType array ('B', 'I', or 'C' for Binary, Integer and Continuous) |
| 413 | const char * colType_; |
| 414 | |
| 415 | /// pointer to row-wise copy of current matrix |
| 416 | const CoinPackedMatrix * matrixByRow_; |
| 417 | |
| 418 | /// pointer to column-wise copy of current matrix |
| 419 | const CoinPackedMatrix * matrixByCol_; |
| 420 | |
| 421 | /// pointer to row-wise copy of "original" matrix |
| 422 | const CoinPackedMatrix * originalMatrixByRow_; |
| 423 | |
| 424 | /// pointer to column-wise copy of "original" matrix |
| 425 | const CoinPackedMatrix * originalMatrixByCol_; |
| 426 | |
| 427 | /// pointer to array[getNumCols()] of primal variable values |
| 428 | const double * colSolution_; |
| 429 | |
| 430 | /// pointer to array[getNumRows()] of dual variable values |
| 431 | const double * rowPrice_; |
| 432 | |
| 433 | /// a pointer to array[getNumCols()] of reduced costs |
| 434 | const double * reducedCost_; |
| 435 | |
| 436 | /// pointer to array[getNumRows()] of row activity levels (constraint matrix times the solution vector). |
| 437 | const double * rowActivity_; |
| 438 | |
| 439 | /// pointer to array[getNumCols()] of primal variable values which should not be separated (for debug) |
| 440 | const double * doNotSeparateThis_; |
| 441 | |
| 442 | /// number of columns |
| 443 | int numCols_; |
| 444 | |
| 445 | /// number of rows |
| 446 | int numRows_; |
| 447 | |
| 448 | /// number of nonzero elements |
| 449 | int numElements_; |
| 450 | |
| 451 | /// number of integer variables |
| 452 | int numIntegers_; |
| 453 | |
| 454 | /// To say whether arrays etc are owned by CoinSnapshot |
| 455 | typedef struct { |
| 456 | unsigned int colLower:1; |
| 457 | unsigned int colUpper:1; |
| 458 | unsigned int rowLower:1; |
| 459 | unsigned int rowUpper:1; |
| 460 | unsigned int rightHandSide:1; |
| 461 | unsigned int objCoefficients:1; |
| 462 | unsigned int colType:1; |
| 463 | unsigned int matrixByRow:1; |
| 464 | unsigned int matrixByCol:1; |
| 465 | unsigned int originalMatrixByRow:1; |
| 466 | unsigned int originalMatrixByCol:1; |
| 467 | unsigned int colSolution:1; |
| 468 | unsigned int rowPrice:1; |
| 469 | unsigned int reducedCost:1; |
| 470 | unsigned int rowActivity:1; |
| 471 | unsigned int doNotSeparateThis:1; |
| 472 | } coinOwned; |
| 473 | coinOwned owned_; |
| 474 | //@} |
| 475 | }; |
| 476 | #endif |
| 477 | |