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 "OsiSolverInterface.hpp" 00015 #include "CoinPackedMatrix.hpp" 00016 #include "CoinWarmStartBasis.hpp" 00017 00023 #ifndef LPX 00024 #define LPX glp_prob 00025 typedef struct { double _opaque_prob[100]; } glp_prob; 00026 #endif 00027 00028 class OsiGlpkSolverInterface : virtual public OsiSolverInterface { 00029 friend int OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir); 00030 00031 public: 00032 00033 //--------------------------------------------------------------------------- 00036 00037 virtual void initialSolve(); 00038 00040 virtual void resolve(); 00041 00043 virtual void branchAndBound(); 00045 00046 //--------------------------------------------------------------------------- 00062 // Set an integer parameter 00063 bool setIntParam(OsiIntParam key, int value); 00064 // Set an double parameter 00065 bool setDblParam(OsiDblParam key, double value); 00066 // Set a string parameter 00067 bool setStrParam(OsiStrParam key, const std::string & value); 00068 // Set a hint parameter 00069 bool setHintParam(OsiHintParam key, bool sense = true, 00070 OsiHintStrength strength = OsiHintTry, void *info = 0) ; 00071 // Get an integer parameter 00072 bool getIntParam(OsiIntParam key, int& value) const; 00073 // Get an double parameter 00074 bool getDblParam(OsiDblParam key, double& value) const; 00075 // Get a string parameter 00076 bool getStrParam(OsiStrParam key, std::string& value) const; 00078 00079 //--------------------------------------------------------------------------- 00081 00082 00083 virtual bool isAbandoned() const; 00085 virtual bool isProvenOptimal() const; 00087 virtual bool isProvenPrimalInfeasible() const; 00089 virtual bool isProvenDualInfeasible() const; 00091 virtual bool isPrimalObjectiveLimitReached() const; 00093 virtual bool isDualObjectiveLimitReached() const; 00095 virtual bool isIterationLimitReached() const; 00097 virtual bool isTimeLimitReached() const; 00099 virtual bool isFeasible() const; 00101 00102 //--------------------------------------------------------------------------- 00111 inline CoinWarmStart *getEmptyWarmStart () const 00112 { return (dynamic_cast<CoinWarmStart *>(new CoinWarmStartBasis())) ; } 00114 virtual CoinWarmStart* getWarmStart() const; 00117 virtual bool setWarmStart(const CoinWarmStart* warmstart); 00119 00120 //--------------------------------------------------------------------------- 00127 00128 virtual void markHotStart(); 00130 virtual void solveFromHotStart(); 00132 virtual void unmarkHotStart(); 00134 00135 //--------------------------------------------------------------------------- 00150 00151 virtual int getNumCols() const; 00152 00154 virtual int getNumRows() const; 00155 00157 virtual int getNumElements() const; 00158 00160 virtual const double * getColLower() const; 00161 00163 virtual const double * getColUpper() const; 00164 00174 virtual const char * getRowSense() const; 00175 00184 virtual const double * getRightHandSide() const; 00185 00194 virtual const double * getRowRange() const; 00195 00197 virtual const double * getRowLower() const; 00198 00200 virtual const double * getRowUpper() const; 00201 00203 virtual const double * getObjCoefficients() const; 00204 00206 virtual double getObjSense() const; 00207 00209 virtual bool isContinuous(int colNumber) const; 00210 00211 #if 0 00212 00213 virtual bool isBinary(int columnNumber) const; 00214 00219 virtual bool isInteger(int columnNumber) const; 00220 00222 virtual bool isIntegerNonBinary(int columnNumber) const; 00223 00225 virtual bool isFreeBinary(int columnNumber) const; 00226 #endif 00227 00229 virtual const CoinPackedMatrix * getMatrixByRow() const; 00230 00232 virtual const CoinPackedMatrix * getMatrixByCol() const; 00233 00235 virtual double getInfinity() const; 00237 00240 00241 virtual const double * getColSolution() const; 00242 00244 virtual const double * getRowPrice() const; 00245 00247 virtual const double * getReducedCost() const; 00248 00251 virtual const double * getRowActivity() const; 00252 00254 virtual double getObjValue() const; 00255 00258 virtual int getIterationCount() const; 00259 00271 virtual std::vector<double*> getDualRays(int maxNumRays, 00272 bool fullRay=false) const; 00290 virtual std::vector<double*> getPrimalRays(int maxNumRays) const; 00291 00292 #if 0 00293 00295 virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05) 00296 const; 00297 #endif 00298 00299 00300 00301 //--------------------------------------------------------------------------- 00302 00305 //------------------------------------------------------------------------- 00309 virtual void setObjCoeff( int elementIndex, double elementValue ); 00310 00311 using OsiSolverInterface::setColLower ; 00314 virtual void setColLower( int elementIndex, double elementValue ); 00315 00316 using OsiSolverInterface::setColUpper ; 00319 virtual void setColUpper( int elementIndex, double elementValue ); 00320 00324 virtual void setColBounds( int elementIndex, 00325 double lower, double upper ); 00326 00335 virtual void setColSetBounds(const int* indexFirst, 00336 const int* indexLast, 00337 const double* boundList); 00338 00341 virtual void setRowLower( int elementIndex, double elementValue ); 00342 00345 virtual void setRowUpper( int elementIndex, double elementValue ); 00346 00350 virtual void setRowBounds( int elementIndex, 00351 double lower, double upper ); 00352 00354 virtual void setRowType(int index, char sense, double rightHandSide, 00355 double range); 00356 00365 virtual void setRowSetBounds(const int* indexFirst, 00366 const int* indexLast, 00367 const double* boundList); 00368 00379 virtual void setRowSetTypes(const int* indexFirst, 00380 const int* indexLast, 00381 const char* senseList, 00382 const double* rhsList, 00383 const double* rangeList); 00385 00386 //------------------------------------------------------------------------- 00390 virtual void setContinuous(int index); 00392 virtual void setInteger(int index); 00395 virtual void setContinuous(const int* indices, int len); 00398 virtual void setInteger(const int* indices, int len); 00400 00401 //------------------------------------------------------------------------- 00403 virtual void setObjSense(double s); 00404 00415 virtual void setColSolution(const double * colsol); 00416 00427 virtual void setRowPrice(const double * rowprice); 00428 00429 //------------------------------------------------------------------------- 00434 00435 using OsiSolverInterface::addCol ; 00437 virtual void addCol(const CoinPackedVectorBase& vec, 00438 const double collb, const double colub, 00439 const double obj); 00440 00441 using OsiSolverInterface::addCols ; 00443 virtual void addCols(const int numcols, 00444 const CoinPackedVectorBase * const * cols, 00445 const double* collb, const double* colub, 00446 const double* obj); 00448 virtual void deleteCols(const int num, const int * colIndices); 00449 00450 using OsiSolverInterface::addRow ; 00452 virtual void addRow(const CoinPackedVectorBase& vec, 00453 const double rowlb, const double rowub); 00455 virtual void addRow(const CoinPackedVectorBase& vec, 00456 const char rowsen, const double rowrhs, 00457 const double rowrng); 00458 00459 using OsiSolverInterface::addRows ; 00461 virtual void addRows(const int numrows, 00462 const CoinPackedVectorBase * const * rows, 00463 const double* rowlb, const double* rowub); 00465 virtual void addRows(const int numrows, 00466 const CoinPackedVectorBase * const * rows, 00467 const char* rowsen, const double* rowrhs, 00468 const double* rowrng); 00470 virtual void deleteRows(const int num, const int * rowIndices); 00471 00472 #if 0 00473 // ??? implemented in OsiSolverInterface 00474 //----------------------------------------------------------------------- 00496 virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs, 00497 double effectivenessLb = 0.0); 00498 #endif 00499 00500 00501 00502 //--------------------------------------------------------------------------- 00503 00517 virtual void loadProblem(const CoinPackedMatrix& matrix, 00518 const double* collb, const double* colub, 00519 const double* obj, 00520 const double* rowlb, const double* rowub); 00521 00529 virtual void assignProblem(CoinPackedMatrix*& matrix, 00530 double*& collb, double*& colub, double*& obj, 00531 double*& rowlb, double*& rowub); 00532 00545 virtual void loadProblem(const CoinPackedMatrix& matrix, 00546 const double* collb, const double* colub, 00547 const double* obj, 00548 const char* rowsen, const double* rowrhs, 00549 const double* rowrng); 00550 00558 virtual void assignProblem(CoinPackedMatrix*& matrix, 00559 double*& collb, double*& colub, double*& obj, 00560 char*& rowsen, double*& rowrhs, 00561 double*& rowrng); 00562 00565 virtual void loadProblem(const int numcols, const int numrows, 00566 const int* start, const int* index, 00567 const double* value, 00568 const double* collb, const double* colub, 00569 const double* obj, 00570 const double* rowlb, const double* rowub); 00571 00574 virtual void loadProblem(const int numcols, const int numrows, 00575 const int* start, const int* index, 00576 const double* value, 00577 const double* collb, const double* colub, 00578 const double* obj, 00579 const char* rowsen, const double* rowrhs, 00580 const double* rowrng); 00581 00582 using OsiSolverInterface::readMps ; 00584 virtual int readMps(const char *filename, 00585 const char *extension = "mps"); 00586 00591 virtual void writeMps(const char *filename, 00592 const char *extension = "mps", 00593 double objSense=0.0) const; 00595 00596 //--------------------------------------------------------------------------- 00597 00604 00607 void setObjName (std::string name) ; 00608 00614 void setRowName(int ndx, std::string name) ; 00615 00621 void setColName(int ndx, std::string name) ; 00622 00624 00625 //--------------------------------------------------------------------------- 00626 00629 enum keepCachedFlag 00630 { 00632 KEEPCACHED_NONE = 0, 00634 KEEPCACHED_COLUMN = 1, 00636 KEEPCACHED_ROW = 2, 00638 KEEPCACHED_MATRIX = 4, 00640 KEEPCACHED_RESULTS = 8, 00642 KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX, 00644 KEEPCACHED_ALL = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS, 00646 FREECACHED_COLUMN = KEEPCACHED_PROBLEM & ~KEEPCACHED_COLUMN, 00648 FREECACHED_ROW = KEEPCACHED_PROBLEM & ~KEEPCACHED_ROW, 00650 FREECACHED_MATRIX = KEEPCACHED_PROBLEM & ~KEEPCACHED_MATRIX, 00652 FREECACHED_RESULTS = KEEPCACHED_ALL & ~KEEPCACHED_RESULTS 00653 }; 00654 00656 LPX * getModelPtr(); 00657 00659 00668 static void incrementInstanceCounter() { ++numInstances_; } 00669 00677 static void decrementInstanceCounter(); 00678 00680 static unsigned int getNumInstances() { return numInstances_; } 00682 00683 00686 00687 OsiGlpkSolverInterface(); 00688 00690 virtual OsiSolverInterface * clone(bool copyData = true) const; 00691 00693 OsiGlpkSolverInterface( const OsiGlpkSolverInterface& ); 00694 00696 OsiGlpkSolverInterface& operator=( const OsiGlpkSolverInterface& rhs ); 00697 00699 virtual ~OsiGlpkSolverInterface(); 00700 00702 virtual void reset(); 00704 00705 protected: 00706 00709 00710 virtual void applyRowCut( const OsiRowCut & rc ); 00711 00715 virtual void applyColCut( const OsiColCut & cc ); 00716 00718 LPX * getMutableModelPtr() const; 00719 00721 00722 private: 00725 00727 void gutsOfCopy( const OsiGlpkSolverInterface & source ); 00728 00730 void gutsOfConstructor(); 00731 00733 void gutsOfDestructor(); 00734 00736 void freeCachedColRim(); 00737 00739 void freeCachedRowRim(); 00740 00742 void freeCachedResults(); 00743 00745 void freeCachedMatrix(); 00746 00748 void freeCachedData( int keepCached = KEEPCACHED_NONE ); 00749 00751 void freeAllMemory(); 00752 00754 void printBounds(); 00755 00757 void fillColBounds() const; 00759 00760 00763 00764 mutable LPX* lp_; 00765 00767 static unsigned int numInstances_; 00768 00769 00770 // Remember whether simplex or b&b was most recently done 00771 // 0 = simplex; 1 = b&b 00772 int bbWasLast_; 00773 00774 // Int parameters. 00776 int maxIteration_; 00778 int hotStartMaxIteration_; 00780 int nameDisc_; 00781 00782 // Double parameters. 00784 double dualObjectiveLimit_; 00786 double primalObjectiveLimit_; 00788 double dualTolerance_; 00790 double primalTolerance_; 00792 double objOffset_; 00793 00794 // String parameters 00796 std::string probName_; 00797 00799 mutable void *info_[OsiLastHintParam] ; 00800 00801 00803 00805 int hotStartCStatSize_; 00807 int *hotStartCStat_; 00809 double *hotStartCVal_; 00811 double *hotStartCDualVal_; 00812 00814 int hotStartRStatSize_; 00816 int *hotStartRStat_; 00818 double *hotStartRVal_; 00820 double *hotStartRDualVal_; 00821 00822 // Status information 00824 bool isIterationLimitReached_; 00826 bool isTimeLimitReached_; 00828 bool isAbandoned_; 00834 bool isObjLowerLimitReached_; 00840 bool isObjUpperLimitReached_; 00842 bool isPrimInfeasible_; 00844 bool isDualInfeasible_; 00846 bool isFeasible_; 00847 00850 00852 mutable int iter_used_; 00853 00855 mutable double *obj_; 00856 00858 mutable double *collower_; 00859 00861 mutable double *colupper_; 00862 00864 mutable char *ctype_; 00865 00867 mutable char *rowsense_; 00868 00870 mutable double *rhs_; 00871 00873 mutable double *rowrange_; 00874 00876 mutable double *rowlower_; 00877 00879 mutable double *rowupper_; 00880 00882 mutable double *colsol_; 00883 00885 mutable double *rowsol_; 00886 00888 mutable double *redcost_; 00889 00891 mutable double *rowact_; 00892 00894 mutable CoinPackedMatrix *matrixByRow_; 00895 00897 mutable CoinPackedMatrix *matrixByCol_; 00899 00900 }; 00901 00902 //############################################################################# 00908 int OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir); 00909 00910 #endif // OsiGlpkSolverInterface_H