00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef OsiGlpkSolverInterface_H
00010 #define OsiGlpkSolverInterface_H
00011
00012 #include <string>
00013 extern "C" {
00014 #include "glpk.h"
00015 }
00016 #include "OsiSolverInterface.hpp"
00017 #include "CoinPackedMatrix.hpp"
00018 #include "CoinWarmStartBasis.hpp"
00019
00025 class OsiGlpkSolverInterface : virtual public OsiSolverInterface {
00026 friend void OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00027
00028 public:
00029
00030
00033
00034 virtual void initialSolve();
00035
00037 virtual void resolve();
00038
00040 virtual void branchAndBound();
00042
00043
00059
00060 bool setIntParam(OsiIntParam key, int value);
00061
00062 bool setDblParam(OsiDblParam key, double value);
00063
00064 bool setStrParam(OsiStrParam key, const std::string & value);
00065
00066 bool setHintParam(OsiHintParam key, bool sense = true,
00067 OsiHintStrength strength = OsiHintTry, void *info = 0) ;
00068
00069 bool getIntParam(OsiIntParam key, int& value) const;
00070
00071 bool getDblParam(OsiDblParam key, double& value) const;
00072
00073 bool getStrParam(OsiStrParam key, std::string& value) const;
00075
00076
00078
00079
00080 virtual bool isAbandoned() const;
00082 virtual bool isProvenOptimal() const;
00084 virtual bool isProvenPrimalInfeasible() const;
00086 virtual bool isProvenDualInfeasible() const;
00088 virtual bool isPrimalObjectiveLimitReached() const;
00090 virtual bool isDualObjectiveLimitReached() const;
00092 virtual bool isIterationLimitReached() const;
00094 virtual bool isTimeLimitReached() const;
00096 virtual bool isFeasible() const;
00098
00099
00108 inline CoinWarmStart *getEmptyWarmStart () const
00109 { return (dynamic_cast<CoinWarmStart *>(new CoinWarmStartBasis())) ; }
00111 virtual CoinWarmStart* getWarmStart() const;
00114 virtual bool setWarmStart(const CoinWarmStart* warmstart);
00116
00117
00124
00125 virtual void markHotStart();
00127 virtual void solveFromHotStart();
00129 virtual void unmarkHotStart();
00131
00132
00147
00148 virtual int getNumCols() const;
00149
00151 virtual int getNumRows() const;
00152
00154 virtual int getNumElements() const;
00155
00157 virtual const double * getColLower() const;
00158
00160 virtual const double * getColUpper() const;
00161
00171 virtual const char * getRowSense() const;
00172
00181 virtual const double * getRightHandSide() const;
00182
00191 virtual const double * getRowRange() const;
00192
00194 virtual const double * getRowLower() const;
00195
00197 virtual const double * getRowUpper() const;
00198
00200 virtual const double * getObjCoefficients() const;
00201
00203 virtual double getObjSense() const;
00204
00206 virtual bool isContinuous(int colNumber) const;
00207
00208 #if 0
00210 virtual bool isBinary(int columnNumber) const;
00211
00216 virtual bool isInteger(int columnNumber) const;
00217
00219 virtual bool isIntegerNonBinary(int columnNumber) const;
00220
00222 virtual bool isFreeBinary(int columnNumber) const;
00223 #endif
00224
00226 virtual const CoinPackedMatrix * getMatrixByRow() const;
00227
00229 virtual const CoinPackedMatrix * getMatrixByCol() const;
00230
00232 virtual double getInfinity() const;
00234
00237
00238 virtual const double * getColSolution() const;
00239
00241 virtual const double * getRowPrice() const;
00242
00244 virtual const double * getReducedCost() const;
00245
00248 virtual const double * getRowActivity() const;
00249
00251 virtual double getObjValue() const;
00252
00255 virtual int getIterationCount() const;
00256
00268 virtual std::vector<double*> getDualRays(int maxNumRays) const;
00280 virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
00281
00282 #if 0
00283
00285 virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05)
00286 const;
00287 #endif
00288
00289
00290
00291
00292
00295
00299 virtual void setObjCoeff( int elementIndex, double elementValue );
00300
00301 using OsiSolverInterface::setColLower ;
00304 virtual void setColLower( int elementIndex, double elementValue );
00305
00306 using OsiSolverInterface::setColUpper ;
00309 virtual void setColUpper( int elementIndex, double elementValue );
00310
00314 virtual void setColBounds( int elementIndex,
00315 double lower, double upper );
00316
00325 virtual void setColSetBounds(const int* indexFirst,
00326 const int* indexLast,
00327 const double* boundList);
00328
00331 virtual void setRowLower( int elementIndex, double elementValue );
00332
00335 virtual void setRowUpper( int elementIndex, double elementValue );
00336
00340 virtual void setRowBounds( int elementIndex,
00341 double lower, double upper );
00342
00344 virtual void setRowType(int index, char sense, double rightHandSide,
00345 double range);
00346
00355 virtual void setRowSetBounds(const int* indexFirst,
00356 const int* indexLast,
00357 const double* boundList);
00358
00369 virtual void setRowSetTypes(const int* indexFirst,
00370 const int* indexLast,
00371 const char* senseList,
00372 const double* rhsList,
00373 const double* rangeList);
00375
00376
00380 virtual void setContinuous(int index);
00382 virtual void setInteger(int index);
00385 virtual void setContinuous(const int* indices, int len);
00388 virtual void setInteger(const int* indices, int len);
00390
00391
00393 virtual void setObjSense(double s);
00394
00405 virtual void setColSolution(const double * colsol);
00406
00417 virtual void setRowPrice(const double * rowprice);
00418
00419
00424
00425 using OsiSolverInterface::addCol ;
00427 virtual void addCol(const CoinPackedVectorBase& vec,
00428 const double collb, const double colub,
00429 const double obj);
00430
00431 using OsiSolverInterface::addCols ;
00433 virtual void addCols(const int numcols,
00434 const CoinPackedVectorBase * const * cols,
00435 const double* collb, const double* colub,
00436 const double* obj);
00438 virtual void deleteCols(const int num, const int * colIndices);
00439
00440 using OsiSolverInterface::addRow ;
00442 virtual void addRow(const CoinPackedVectorBase& vec,
00443 const double rowlb, const double rowub);
00445 virtual void addRow(const CoinPackedVectorBase& vec,
00446 const char rowsen, const double rowrhs,
00447 const double rowrng);
00448
00449 using OsiSolverInterface::addRows ;
00451 virtual void addRows(const int numrows,
00452 const CoinPackedVectorBase * const * rows,
00453 const double* rowlb, const double* rowub);
00455 virtual void addRows(const int numrows,
00456 const CoinPackedVectorBase * const * rows,
00457 const char* rowsen, const double* rowrhs,
00458 const double* rowrng);
00460 virtual void deleteRows(const int num, const int * rowIndices);
00461
00462 #if 0
00463
00464
00486 virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs,
00487 double effectivenessLb = 0.0);
00488 #endif
00489
00490
00491
00492
00493
00507 virtual void loadProblem(const CoinPackedMatrix& matrix,
00508 const double* collb, const double* colub,
00509 const double* obj,
00510 const double* rowlb, const double* rowub);
00511
00519 virtual void assignProblem(CoinPackedMatrix*& matrix,
00520 double*& collb, double*& colub, double*& obj,
00521 double*& rowlb, double*& rowub);
00522
00535 virtual void loadProblem(const CoinPackedMatrix& matrix,
00536 const double* collb, const double* colub,
00537 const double* obj,
00538 const char* rowsen, const double* rowrhs,
00539 const double* rowrng);
00540
00548 virtual void assignProblem(CoinPackedMatrix*& matrix,
00549 double*& collb, double*& colub, double*& obj,
00550 char*& rowsen, double*& rowrhs,
00551 double*& rowrng);
00552
00555 virtual void loadProblem(const int numcols, const int numrows,
00556 const int* start, const int* index,
00557 const double* value,
00558 const double* collb, const double* colub,
00559 const double* obj,
00560 const double* rowlb, const double* rowub);
00561
00564 virtual void loadProblem(const int numcols, const int numrows,
00565 const int* start, const int* index,
00566 const double* value,
00567 const double* collb, const double* colub,
00568 const double* obj,
00569 const char* rowsen, const double* rowrhs,
00570 const double* rowrng);
00571
00572 using OsiSolverInterface::readMps ;
00574 virtual int readMps(const char *filename,
00575 const char *extension = "mps");
00576
00581 virtual void writeMps(const char *filename,
00582 const char *extension = "mps",
00583 double objSense=0.0) const;
00585
00586
00587
00594
00597 void setObjName (std::string name) ;
00598
00604 void setRowName(int ndx, std::string name) ;
00605
00611 void setColName(int ndx, std::string name) ;
00612
00614
00615
00616
00619 enum keepCachedFlag
00620 {
00622 KEEPCACHED_NONE = 0,
00624 KEEPCACHED_COLUMN = 1,
00626 KEEPCACHED_ROW = 2,
00628 KEEPCACHED_MATRIX = 4,
00630 KEEPCACHED_RESULTS = 8,
00632 KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX,
00634 KEEPCACHED_ALL = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS,
00636 FREECACHED_COLUMN = KEEPCACHED_PROBLEM & ~KEEPCACHED_COLUMN,
00638 FREECACHED_ROW = KEEPCACHED_PROBLEM & ~KEEPCACHED_ROW,
00640 FREECACHED_MATRIX = KEEPCACHED_PROBLEM & ~KEEPCACHED_MATRIX,
00642 FREECACHED_RESULTS = KEEPCACHED_ALL & ~KEEPCACHED_RESULTS
00643 };
00644
00646 LPX * getModelPtr();
00647
00649
00652
00653 OsiGlpkSolverInterface();
00654
00656 virtual OsiSolverInterface * clone(bool copyData = true) const;
00657
00659 OsiGlpkSolverInterface( const OsiGlpkSolverInterface& );
00660
00662 OsiGlpkSolverInterface& operator=( const OsiGlpkSolverInterface& rhs );
00663
00665 virtual ~OsiGlpkSolverInterface();
00666
00668 virtual void reset();
00670
00671 protected:
00672
00675
00676 virtual void applyRowCut( const OsiRowCut & rc );
00677
00681 virtual void applyColCut( const OsiColCut & cc );
00682
00684 LPX * getMutableModelPtr() const;
00685
00687
00688 private:
00691
00693 void gutsOfCopy( const OsiGlpkSolverInterface & source );
00694
00696 void gutsOfConstructor();
00697
00699 void gutsOfDestructor();
00700
00702 void freeCachedColRim();
00703
00705 void freeCachedRowRim();
00706
00708 void freeCachedResults();
00709
00711 void freeCachedMatrix();
00712
00714 void freeCachedData( int keepCached = KEEPCACHED_NONE );
00715
00717 void freeAllMemory();
00718
00720 void printBounds();
00721
00723 void fillColBounds() const;
00725
00726
00729
00730 mutable LPX* lp_;
00731
00732
00733
00734 int bbWasLast_;
00735
00736
00738 int maxIteration_;
00740 int hotStartMaxIteration_;
00742 int nameDisc_;
00743
00744
00746 double dualObjectiveLimit_;
00748 double primalObjectiveLimit_;
00750 double dualTolerance_;
00752 double primalTolerance_;
00754 double objOffset_;
00755
00756
00758 std::string probName_;
00759
00761 mutable void *info_[OsiLastHintParam] ;
00762
00763
00765
00767 int hotStartCStatSize_;
00769 int *hotStartCStat_;
00771 double *hotStartCVal_;
00773 double *hotStartCDualVal_;
00774
00776 int hotStartRStatSize_;
00778 int *hotStartRStat_;
00780 double *hotStartRVal_;
00782 double *hotStartRDualVal_;
00783
00784
00786 bool isIterationLimitReached_;
00788 bool isTimeLimitReached_;
00790 bool isAbandoned_;
00796 bool isObjLowerLimitReached_;
00802 bool isObjUpperLimitReached_;
00804 bool isPrimInfeasible_;
00806 bool isDualInfeasible_;
00808 bool isFeasible_;
00809
00812
00814 mutable int iter_used_;
00815
00817 mutable double *obj_;
00818
00820 mutable double *collower_;
00821
00823 mutable double *colupper_;
00824
00826 mutable char *ctype_;
00827
00829 mutable char *rowsense_;
00830
00832 mutable double *rhs_;
00833
00835 mutable double *rowrange_;
00836
00838 mutable double *rowlower_;
00839
00841 mutable double *rowupper_;
00842
00844 mutable double *colsol_;
00845
00847 mutable double *rowsol_;
00848
00850 mutable double *redcost_;
00851
00853 mutable double *rowact_;
00854
00856 mutable CoinPackedMatrix *matrixByRow_;
00857
00859 mutable CoinPackedMatrix *matrixByCol_;
00861
00862 };
00863
00864
00870 void OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00871
00872 #endif // OsiGlpkSolverInterface_H