Osi
trunk
|
00001 //----------------------------------------------------------------------------- 00002 // name: OSI Interface for GLPK 00003 //----------------------------------------------------------------------------- 00004 // Copyright (C) 2001, Vivian De Smedt, Braden Hunsaker 00005 // Copyright (C) 2003 University of Pittsburgh 00006 // University of Pittsburgh coding done by Brady Hunsaker 00007 // All Rights Reserved. 00008 // This code is licensed under the terms of the Eclipse Public License (EPL). 00009 00010 #ifndef OsiGlpkSolverInterface_H 00011 #define OsiGlpkSolverInterface_H 00012 00013 #include <string> 00014 #include <glpk.h> 00015 #include "OsiSolverInterface.hpp" 00016 #include "CoinPackedMatrix.hpp" 00017 #include "CoinWarmStartBasis.hpp" 00018 00024 #ifndef LPX 00025 #define LPX glp_prob 00026 #endif 00027 00028 // ifndef GLP_PROB_DEFINED 00029 // define GLP_PROB_DEFINED 00030 // typedef struct { double _opaque_prob[100]; } glp_prob; 00031 // endif 00032 00033 class OsiGlpkSolverInterface : virtual public OsiSolverInterface { 00034 friend void OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir); 00035 00036 public: 00037 00038 //--------------------------------------------------------------------------- 00041 00042 virtual void initialSolve(); 00043 00045 virtual void resolve(); 00046 00048 virtual void branchAndBound(); 00050 00051 //--------------------------------------------------------------------------- 00067 // Set an integer parameter 00068 bool setIntParam(OsiIntParam key, int value); 00069 // Set an double parameter 00070 bool setDblParam(OsiDblParam key, double value); 00071 // Set a string parameter 00072 bool setStrParam(OsiStrParam key, const std::string & value); 00073 // Set a hint parameter 00074 bool setHintParam(OsiHintParam key, bool sense = true, 00075 OsiHintStrength strength = OsiHintTry, void *info = 0) ; 00076 // Get an integer parameter 00077 bool getIntParam(OsiIntParam key, int& value) const; 00078 // Get an double parameter 00079 bool getDblParam(OsiDblParam key, double& value) const; 00080 // Get a string parameter 00081 bool getStrParam(OsiStrParam key, std::string& value) const; 00083 00084 //--------------------------------------------------------------------------- 00086 00087 00088 virtual bool isAbandoned() const; 00090 virtual bool isProvenOptimal() const; 00092 virtual bool isProvenPrimalInfeasible() const; 00094 virtual bool isProvenDualInfeasible() const; 00096 virtual bool isPrimalObjectiveLimitReached() const; 00098 virtual bool isDualObjectiveLimitReached() const; 00100 virtual bool isIterationLimitReached() const; 00102 virtual bool isTimeLimitReached() const; 00104 virtual bool isFeasible() const; 00106 00107 //--------------------------------------------------------------------------- 00116 inline CoinWarmStart *getEmptyWarmStart () const 00117 { return (dynamic_cast<CoinWarmStart *>(new CoinWarmStartBasis())) ; } 00119 virtual CoinWarmStart* getWarmStart() const; 00122 virtual bool setWarmStart(const CoinWarmStart* warmstart); 00124 00125 //--------------------------------------------------------------------------- 00132 00133 virtual void markHotStart(); 00135 virtual void solveFromHotStart(); 00137 virtual void unmarkHotStart(); 00139 00140 //--------------------------------------------------------------------------- 00155 00156 virtual int getNumCols() const; 00157 00159 virtual int getNumRows() const; 00160 00162 virtual int getNumElements() const; 00163 00165 virtual const double * getColLower() const; 00166 00168 virtual const double * getColUpper() const; 00169 00179 virtual const char * getRowSense() const; 00180 00189 virtual const double * getRightHandSide() const; 00190 00199 virtual const double * getRowRange() const; 00200 00202 virtual const double * getRowLower() const; 00203 00205 virtual const double * getRowUpper() const; 00206 00208 virtual const double * getObjCoefficients() const; 00209 00211 virtual double getObjSense() const; 00212 00214 virtual bool isContinuous(int colNumber) const; 00215 00216 #if 0 00217 00218 virtual bool isBinary(int columnNumber) const; 00219 00224 virtual bool isInteger(int columnNumber) const; 00225 00227 virtual bool isIntegerNonBinary(int columnNumber) const; 00228 00230 virtual bool isFreeBinary(int columnNumber) const; 00231 #endif 00232 00234 virtual const CoinPackedMatrix * getMatrixByRow() const; 00235 00237 virtual const CoinPackedMatrix * getMatrixByCol() const; 00238 00240 virtual double getInfinity() const; 00242 00245 00246 virtual const double * getColSolution() const; 00247 00249 virtual const double * getRowPrice() const; 00250 00252 virtual const double * getReducedCost() const; 00253 00256 virtual const double * getRowActivity() const; 00257 00259 virtual double getObjValue() const; 00260 00263 virtual int getIterationCount() const; 00264 00276 virtual std::vector<double*> getDualRays(int maxNumRays, 00277 bool fullRay=false) const; 00295 virtual std::vector<double*> getPrimalRays(int maxNumRays) const; 00296 00297 #if 0 00298 00300 virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05) 00301 const; 00302 #endif 00303 00304 00305 00306 //--------------------------------------------------------------------------- 00307 00310 //------------------------------------------------------------------------- 00314 virtual void setObjCoeff( int elementIndex, double elementValue ); 00315 00316 using OsiSolverInterface::setColLower ; 00319 virtual void setColLower( int elementIndex, double elementValue ); 00320 00321 using OsiSolverInterface::setColUpper ; 00324 virtual void setColUpper( int elementIndex, double elementValue ); 00325 00329 virtual void setColBounds( int elementIndex, 00330 double lower, double upper ); 00331 00340 virtual void setColSetBounds(const int* indexFirst, 00341 const int* indexLast, 00342 const double* boundList); 00343 00346 virtual void setRowLower( int elementIndex, double elementValue ); 00347 00350 virtual void setRowUpper( int elementIndex, double elementValue ); 00351 00355 virtual void setRowBounds( int elementIndex, 00356 double lower, double upper ); 00357 00359 virtual void setRowType(int index, char sense, double rightHandSide, 00360 double range); 00361 00370 virtual void setRowSetBounds(const int* indexFirst, 00371 const int* indexLast, 00372 const double* boundList); 00373 00384 virtual void setRowSetTypes(const int* indexFirst, 00385 const int* indexLast, 00386 const char* senseList, 00387 const double* rhsList, 00388 const double* rangeList); 00390 00391 //------------------------------------------------------------------------- 00395 virtual void setContinuous(int index); 00397 virtual void setInteger(int index); 00400 virtual void setContinuous(const int* indices, int len); 00403 virtual void setInteger(const int* indices, int len); 00405 00406 //------------------------------------------------------------------------- 00408 virtual void setObjSense(double s); 00409 00420 virtual void setColSolution(const double * colsol); 00421 00432 virtual void setRowPrice(const double * rowprice); 00433 00434 //------------------------------------------------------------------------- 00439 00440 using OsiSolverInterface::addCol ; 00442 virtual void addCol(const CoinPackedVectorBase& vec, 00443 const double collb, const double colub, 00444 const double obj); 00445 00446 using OsiSolverInterface::addCols ; 00448 virtual void addCols(const int numcols, 00449 const CoinPackedVectorBase * const * cols, 00450 const double* collb, const double* colub, 00451 const double* obj); 00453 virtual void deleteCols(const int num, const int * colIndices); 00454 00455 using OsiSolverInterface::addRow ; 00457 virtual void addRow(const CoinPackedVectorBase& vec, 00458 const double rowlb, const double rowub); 00460 virtual void addRow(const CoinPackedVectorBase& vec, 00461 const char rowsen, const double rowrhs, 00462 const double rowrng); 00463 00464 using OsiSolverInterface::addRows ; 00466 virtual void addRows(const int numrows, 00467 const CoinPackedVectorBase * const * rows, 00468 const double* rowlb, const double* rowub); 00470 virtual void addRows(const int numrows, 00471 const CoinPackedVectorBase * const * rows, 00472 const char* rowsen, const double* rowrhs, 00473 const double* rowrng); 00475 virtual void deleteRows(const int num, const int * rowIndices); 00476 00477 #if 0 00478 // ??? implemented in OsiSolverInterface 00479 //----------------------------------------------------------------------- 00501 virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs, 00502 double effectivenessLb = 0.0); 00503 #endif 00504 00505 00506 00507 //--------------------------------------------------------------------------- 00508 00522 virtual void loadProblem(const CoinPackedMatrix& matrix, 00523 const double* collb, const double* colub, 00524 const double* obj, 00525 const double* rowlb, const double* rowub); 00526 00534 virtual void assignProblem(CoinPackedMatrix*& matrix, 00535 double*& collb, double*& colub, double*& obj, 00536 double*& rowlb, double*& rowub); 00537 00550 virtual void loadProblem(const CoinPackedMatrix& matrix, 00551 const double* collb, const double* colub, 00552 const double* obj, 00553 const char* rowsen, const double* rowrhs, 00554 const double* rowrng); 00555 00563 virtual void assignProblem(CoinPackedMatrix*& matrix, 00564 double*& collb, double*& colub, double*& obj, 00565 char*& rowsen, double*& rowrhs, 00566 double*& rowrng); 00567 00570 virtual void loadProblem(const int numcols, const int numrows, 00571 const int* start, const int* index, 00572 const double* value, 00573 const double* collb, const double* colub, 00574 const double* obj, 00575 const double* rowlb, const double* rowub); 00576 00579 virtual void loadProblem(const int numcols, const int numrows, 00580 const int* start, const int* index, 00581 const double* value, 00582 const double* collb, const double* colub, 00583 const double* obj, 00584 const char* rowsen, const double* rowrhs, 00585 const double* rowrng); 00586 00587 using OsiSolverInterface::readMps ; 00589 virtual int readMps(const char *filename, 00590 const char *extension = "mps"); 00591 00596 virtual void writeMps(const char *filename, 00597 const char *extension = "mps", 00598 double objSense=0.0) const; 00600 00601 //--------------------------------------------------------------------------- 00602 00609 00612 void setObjName (std::string name) ; 00613 00619 void setRowName(int ndx, std::string name) ; 00620 00626 void setColName(int ndx, std::string name) ; 00627 00629 00630 //--------------------------------------------------------------------------- 00631 00634 enum keepCachedFlag 00635 { 00637 KEEPCACHED_NONE = 0, 00639 KEEPCACHED_COLUMN = 1, 00641 KEEPCACHED_ROW = 2, 00643 KEEPCACHED_MATRIX = 4, 00645 KEEPCACHED_RESULTS = 8, 00647 KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX, 00649 KEEPCACHED_ALL = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS, 00651 FREECACHED_COLUMN = KEEPCACHED_PROBLEM & ~KEEPCACHED_COLUMN, 00653 FREECACHED_ROW = KEEPCACHED_PROBLEM & ~KEEPCACHED_ROW, 00655 FREECACHED_MATRIX = KEEPCACHED_PROBLEM & ~KEEPCACHED_MATRIX, 00657 FREECACHED_RESULTS = KEEPCACHED_ALL & ~KEEPCACHED_RESULTS 00658 }; 00659 00661 LPX * getModelPtr(); 00662 00664 00673 static void incrementInstanceCounter() { ++numInstances_; } 00674 00682 static void decrementInstanceCounter(); 00683 00685 static unsigned int getNumInstances() { return numInstances_; } 00687 00688 00691 00692 OsiGlpkSolverInterface(); 00693 00695 virtual OsiSolverInterface * clone(bool copyData = true) const; 00696 00698 OsiGlpkSolverInterface( const OsiGlpkSolverInterface& ); 00699 00701 OsiGlpkSolverInterface& operator=( const OsiGlpkSolverInterface& rhs ); 00702 00704 virtual ~OsiGlpkSolverInterface(); 00705 00707 virtual void reset(); 00709 00710 protected: 00711 00714 00715 virtual void applyRowCut( const OsiRowCut & rc ); 00716 00720 virtual void applyColCut( const OsiColCut & cc ); 00721 00723 LPX * getMutableModelPtr() const; 00724 00726 00727 private: 00730 00732 void gutsOfCopy( const OsiGlpkSolverInterface & source ); 00733 00735 void gutsOfConstructor(); 00736 00738 void gutsOfDestructor(); 00739 00741 void freeCachedColRim(); 00742 00744 void freeCachedRowRim(); 00745 00747 void freeCachedResults(); 00748 00750 void freeCachedMatrix(); 00751 00753 void freeCachedData( int keepCached = KEEPCACHED_NONE ); 00754 00756 void freeAllMemory(); 00757 00759 void printBounds(); 00760 00762 void fillColBounds() const; 00764 00765 00768 00769 mutable LPX* lp_; 00770 00772 static unsigned int numInstances_; 00773 00774 00775 // Remember whether simplex or b&b was most recently done 00776 // 0 = simplex; 1 = b&b 00777 int bbWasLast_; 00778 00779 // Int parameters. 00781 int maxIteration_; 00783 int hotStartMaxIteration_; 00785 int nameDisc_; 00786 00787 // Double parameters. 00789 double dualObjectiveLimit_; 00791 double primalObjectiveLimit_; 00793 double dualTolerance_; 00795 double primalTolerance_; 00797 double objOffset_; 00798 00799 // String parameters 00801 std::string probName_; 00802 00804 mutable void *info_[OsiLastHintParam] ; 00805 00806 00808 00810 int hotStartCStatSize_; 00812 int *hotStartCStat_; 00814 double *hotStartCVal_; 00816 double *hotStartCDualVal_; 00817 00819 int hotStartRStatSize_; 00821 int *hotStartRStat_; 00823 double *hotStartRVal_; 00825 double *hotStartRDualVal_; 00826 00827 // Status information 00829 bool isIterationLimitReached_; 00831 bool isTimeLimitReached_; 00833 bool isAbandoned_; 00839 bool isObjLowerLimitReached_; 00845 bool isObjUpperLimitReached_; 00847 bool isPrimInfeasible_; 00849 bool isDualInfeasible_; 00851 bool isFeasible_; 00852 00855 00857 mutable int iter_used_; 00858 00860 mutable double *obj_; 00861 00863 mutable double *collower_; 00864 00866 mutable double *colupper_; 00867 00869 mutable char *ctype_; 00870 00872 mutable char *rowsense_; 00873 00875 mutable double *rhs_; 00876 00878 mutable double *rowrange_; 00879 00881 mutable double *rowlower_; 00882 00884 mutable double *rowupper_; 00885 00887 mutable double *colsol_; 00888 00890 mutable double *rowsol_; 00891 00893 mutable double *redcost_; 00894 00896 mutable double *rowact_; 00897 00899 mutable CoinPackedMatrix *matrixByRow_; 00900 00902 mutable CoinPackedMatrix *matrixByCol_; 00904 00905 }; 00906 00907 //############################################################################# 00909 void OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir); 00910 00911 #endif // OsiGlpkSolverInterface_H