Osi  trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
OsiGlpkSolverInterface.hpp
Go to the documentation of this file.
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 // Glpk < 4.48:
00031 typedef struct { double _opaque_prob[100]; } glp_prob;
00032 // Glpk 4.48: typedef struct glp_prob glp_prob;
00033 #endif
00034 
00035 class OsiGlpkSolverInterface : virtual public OsiSolverInterface {
00036   friend void OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00037   
00038 public:
00039   
00040   //---------------------------------------------------------------------------
00043 
00044   virtual void initialSolve();
00045   
00047   virtual void resolve();
00048   
00050   virtual void branchAndBound();
00052   
00053   //---------------------------------------------------------------------------
00069     // Set an integer parameter
00070     bool setIntParam(OsiIntParam key, int value);
00071     // Set an double parameter
00072     bool setDblParam(OsiDblParam key, double value);
00073     // Set a string parameter
00074     bool setStrParam(OsiStrParam key, const std::string & value);
00075     // Set a hint parameter
00076     bool setHintParam(OsiHintParam key, bool sense = true,
00077                       OsiHintStrength strength = OsiHintTry, void *info = 0) ;
00078     // Get an integer parameter
00079     bool getIntParam(OsiIntParam key, int& value) const;
00080     // Get an double parameter
00081     bool getDblParam(OsiDblParam key, double& value) const;
00082     // Get a string parameter
00083     bool getStrParam(OsiStrParam key, std::string& value) const;
00085 
00086   //---------------------------------------------------------------------------
00088 
00089 
00090     virtual bool isAbandoned() const;
00092     virtual bool isProvenOptimal() const;
00094     virtual bool isProvenPrimalInfeasible() const;
00096     virtual bool isProvenDualInfeasible() const;
00098     virtual bool isPrimalObjectiveLimitReached() const;
00100     virtual bool isDualObjectiveLimitReached() const;
00102     virtual bool isIterationLimitReached() const;
00104     virtual bool isTimeLimitReached() const;
00106     virtual bool isFeasible() const;
00108 
00109   //---------------------------------------------------------------------------
00118   inline CoinWarmStart *getEmptyWarmStart () const
00119   { return (dynamic_cast<CoinWarmStart *>(new CoinWarmStartBasis())) ; }
00121     virtual CoinWarmStart* getWarmStart() const;
00124     virtual bool setWarmStart(const CoinWarmStart* warmstart);
00126 
00127   //---------------------------------------------------------------------------
00134 
00135     virtual void markHotStart();
00137     virtual void solveFromHotStart();
00139     virtual void unmarkHotStart();
00141 
00142   //---------------------------------------------------------------------------
00157 
00158       virtual int getNumCols() const;
00159   
00161       virtual int getNumRows() const;
00162   
00164       virtual int getNumElements() const;
00165           
00167       virtual const double * getColLower() const;
00168   
00170       virtual const double * getColUpper() const;
00171   
00181       virtual const char * getRowSense() const;
00182   
00191       virtual const double * getRightHandSide() const;
00192   
00201       virtual const double * getRowRange() const;
00202   
00204       virtual const double * getRowLower() const;
00205   
00207       virtual const double * getRowUpper() const;
00208   
00210       virtual const double * getObjCoefficients() const;
00211   
00213       virtual double getObjSense() const;
00214 
00216       virtual bool isContinuous(int colNumber) const;
00217 
00218 #if 0
00219 
00220       virtual bool isBinary(int columnNumber) const;
00221   
00226       virtual bool isInteger(int columnNumber) const;
00227   
00229       virtual bool isIntegerNonBinary(int columnNumber) const;
00230   
00232       virtual bool isFreeBinary(int columnNumber) const;
00233 #endif
00234   
00236       virtual const CoinPackedMatrix * getMatrixByRow() const;
00237   
00239       virtual const CoinPackedMatrix * getMatrixByCol() const;
00240   
00242       virtual double getInfinity() const;
00244     
00247 
00248       virtual const double * getColSolution() const;
00249   
00251       virtual const double * getRowPrice() const;
00252   
00254       virtual const double * getReducedCost() const;
00255   
00258       virtual const double * getRowActivity() const;
00259   
00261       virtual double getObjValue() const;
00262   
00265       virtual int getIterationCount() const;
00266   
00278       virtual std::vector<double*> getDualRays(int maxNumRays,
00279                                                bool fullRay=false) const;
00297       virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
00298   
00299 #if 0
00300 
00302       virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05)
00303         const;
00304 #endif
00305 
00306 
00307   
00308   //---------------------------------------------------------------------------
00309 
00312     //-------------------------------------------------------------------------
00316       virtual void setObjCoeff( int elementIndex, double elementValue );
00317 
00318       using OsiSolverInterface::setColLower ;
00321       virtual void setColLower( int elementIndex, double elementValue );
00322       
00323       using OsiSolverInterface::setColUpper ;
00326       virtual void setColUpper( int elementIndex, double elementValue );
00327       
00331       virtual void setColBounds( int elementIndex,
00332                                  double lower, double upper );
00333     
00342       virtual void setColSetBounds(const int* indexFirst,
00343                                    const int* indexLast,
00344                                    const double* boundList);
00345       
00348       virtual void setRowLower( int elementIndex, double elementValue );
00349       
00352       virtual void setRowUpper( int elementIndex, double elementValue );
00353     
00357       virtual void setRowBounds( int elementIndex,
00358                                  double lower, double upper );
00359     
00361       virtual void setRowType(int index, char sense, double rightHandSide,
00362                               double range);
00363     
00372       virtual void setRowSetBounds(const int* indexFirst,
00373                                    const int* indexLast,
00374                                    const double* boundList);
00375     
00386       virtual void setRowSetTypes(const int* indexFirst,
00387                                   const int* indexLast,
00388                                   const char* senseList,
00389                                   const double* rhsList,
00390                                   const double* rangeList);
00392     
00393     //-------------------------------------------------------------------------
00397       virtual void setContinuous(int index);
00399       virtual void setInteger(int index);
00402       virtual void setContinuous(const int* indices, int len);
00405       virtual void setInteger(const int* indices, int len);
00407     
00408     //-------------------------------------------------------------------------
00410     virtual void setObjSense(double s);
00411     
00422     virtual void setColSolution(const double * colsol);
00423     
00434     virtual void setRowPrice(const double * rowprice);
00435     
00436     //-------------------------------------------------------------------------
00441 
00442       using OsiSolverInterface::addCol ;
00444       virtual void addCol(const CoinPackedVectorBase& vec,
00445                           const double collb, const double colub,   
00446                           const double obj);
00447 
00448       using OsiSolverInterface::addCols ;
00450       virtual void addCols(const int numcols,
00451                            const CoinPackedVectorBase * const * cols,
00452                            const double* collb, const double* colub,   
00453                            const double* obj);
00455       virtual void deleteCols(const int num, const int * colIndices);
00456     
00457       using OsiSolverInterface::addRow ;
00459       virtual void addRow(const CoinPackedVectorBase& vec,
00460                           const double rowlb, const double rowub);
00462       virtual void addRow(const CoinPackedVectorBase& vec,
00463                           const char rowsen, const double rowrhs,   
00464                           const double rowrng);
00465 
00466       using OsiSolverInterface::addRows ;
00468       virtual void addRows(const int numrows,
00469                            const CoinPackedVectorBase * const * rows,
00470                            const double* rowlb, const double* rowub);
00472       virtual void addRows(const int numrows,
00473                            const CoinPackedVectorBase * const * rows,
00474                            const char* rowsen, const double* rowrhs,   
00475                            const double* rowrng);
00477       virtual void deleteRows(const int num, const int * rowIndices);
00478     
00479 #if 0
00480   // ??? implemented in OsiSolverInterface
00481       //-----------------------------------------------------------------------
00503       virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs,
00504                                             double effectivenessLb = 0.0);
00505 #endif
00506 
00507 
00508 
00509   //---------------------------------------------------------------------------
00510 
00524     virtual void loadProblem(const CoinPackedMatrix& matrix,
00525                              const double* collb, const double* colub,   
00526                              const double* obj,
00527                              const double* rowlb, const double* rowub);
00528                             
00536     virtual void assignProblem(CoinPackedMatrix*& matrix,
00537                                double*& collb, double*& colub, double*& obj,
00538                                double*& rowlb, double*& rowub);
00539 
00552     virtual void loadProblem(const CoinPackedMatrix& matrix,
00553                              const double* collb, const double* colub,
00554                              const double* obj,
00555                              const char* rowsen, const double* rowrhs,   
00556                              const double* rowrng);
00557 
00565     virtual void assignProblem(CoinPackedMatrix*& matrix,
00566                                double*& collb, double*& colub, double*& obj,
00567                                char*& rowsen, double*& rowrhs,
00568                                double*& rowrng);
00569 
00572     virtual void loadProblem(const int numcols, const int numrows,
00573                              const int* start, const int* index,
00574                              const double* value,
00575                              const double* collb, const double* colub,   
00576                              const double* obj,
00577                              const double* rowlb, const double* rowub);
00578 
00581     virtual void loadProblem(const int numcols, const int numrows,
00582                              const int* start, const int* index,
00583                              const double* value,
00584                              const double* collb, const double* colub,   
00585                              const double* obj,
00586                              const char* rowsen, const double* rowrhs,   
00587                              const double* rowrng);
00588 
00589     using OsiSolverInterface::readMps ;
00591     virtual int readMps(const char *filename,
00592                          const char *extension = "mps");
00593 
00598     virtual void writeMps(const char *filename,
00599                           const char *extension = "mps",
00600                           double objSense=0.0) const;
00602 
00603   //---------------------------------------------------------------------------
00604 
00611 
00614     void setObjName (std::string name) ;
00615 
00621     void setRowName(int ndx, std::string name) ;
00622 
00628     void setColName(int ndx, std::string name) ;
00629 
00631 
00632   //---------------------------------------------------------------------------
00633 
00636   enum keepCachedFlag
00637   {
00639     KEEPCACHED_NONE    = 0,
00641     KEEPCACHED_COLUMN  = 1,
00643     KEEPCACHED_ROW     = 2,
00645     KEEPCACHED_MATRIX  = 4,
00647     KEEPCACHED_RESULTS = 8,
00649     KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX,
00651     KEEPCACHED_ALL     = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS,
00653     FREECACHED_COLUMN  = KEEPCACHED_PROBLEM & ~KEEPCACHED_COLUMN,
00655     FREECACHED_ROW     = KEEPCACHED_PROBLEM & ~KEEPCACHED_ROW,
00657     FREECACHED_MATRIX  = KEEPCACHED_PROBLEM & ~KEEPCACHED_MATRIX,
00659     FREECACHED_RESULTS = KEEPCACHED_ALL & ~KEEPCACHED_RESULTS
00660   };
00661 
00663   LPX * getModelPtr();
00664 
00666   
00675   static void incrementInstanceCounter() { ++numInstances_; }
00676     
00684   static void decrementInstanceCounter();
00685     
00687   static unsigned int getNumInstances() { return numInstances_; }
00689 
00690 
00693 
00694   OsiGlpkSolverInterface(); 
00695   
00697   virtual OsiSolverInterface * clone(bool copyData = true) const;
00698   
00700   OsiGlpkSolverInterface( const OsiGlpkSolverInterface& );
00701   
00703   OsiGlpkSolverInterface& operator=( const OsiGlpkSolverInterface& rhs );
00704   
00706   virtual ~OsiGlpkSolverInterface();
00707 
00709   virtual void reset();
00711   
00712 protected:
00713   
00716 
00717   virtual void applyRowCut( const OsiRowCut & rc );
00718   
00722   virtual void applyColCut( const OsiColCut & cc );
00723 
00725   LPX * getMutableModelPtr() const;
00726 
00728  
00729 private: 
00732     
00734   void gutsOfCopy( const OsiGlpkSolverInterface & source );
00735   
00737   void gutsOfConstructor();
00738   
00740   void gutsOfDestructor();
00741 
00743   void freeCachedColRim();
00744 
00746   void freeCachedRowRim();
00747 
00749   void freeCachedResults();
00750   
00752   void freeCachedMatrix();
00753 
00755   void freeCachedData( int keepCached = KEEPCACHED_NONE );
00756 
00758   void freeAllMemory();
00759 
00761   void printBounds(); 
00762 
00764   void fillColBounds() const;
00766   
00767   
00770 
00771   mutable LPX* lp_;
00772   
00774   static unsigned int numInstances_;
00775 
00776 
00777   // Remember whether simplex or b&b was most recently done
00778   // 0 = simplex;  1 = b&b
00779   int bbWasLast_; 
00780 
00781   // Int parameters.
00783   int maxIteration_;
00785   int hotStartMaxIteration_;
00787   int nameDisc_;
00788 
00789   // Double parameters.
00791   double dualObjectiveLimit_;
00793   double primalObjectiveLimit_;
00795   double dualTolerance_;
00797   double primalTolerance_;
00799   double objOffset_;
00800 
00801   // String parameters
00803   std::string probName_;
00804 
00806   mutable void *info_[OsiLastHintParam] ;
00807 
00808 
00810 
00812   int hotStartCStatSize_;
00814   int *hotStartCStat_;
00816   double *hotStartCVal_;
00818   double *hotStartCDualVal_;
00819 
00821   int hotStartRStatSize_;
00823   int *hotStartRStat_;
00825   double *hotStartRVal_;
00827   double *hotStartRDualVal_;
00828 
00829   // Status information
00831   bool isIterationLimitReached_;
00833   bool isTimeLimitReached_;
00835   bool isAbandoned_;
00841   bool isObjLowerLimitReached_;
00847   bool isObjUpperLimitReached_;
00849   bool isPrimInfeasible_;
00851   bool isDualInfeasible_;
00853   bool isFeasible_;
00854 
00857 
00859   mutable int iter_used_;
00860 
00862   mutable double  *obj_;
00863   
00865   mutable double  *collower_;
00866   
00868   mutable double  *colupper_;
00869   
00871   mutable char    *ctype_;
00872   
00874   mutable char    *rowsense_;
00875   
00877   mutable double  *rhs_;
00878   
00880   mutable double  *rowrange_;
00881   
00883   mutable double  *rowlower_;
00884   
00886   mutable double  *rowupper_;
00887   
00889   mutable double  *colsol_;
00890   
00892   mutable double  *rowsol_;
00893 
00895   mutable double  *redcost_;
00896 
00898   mutable double  *rowact_;
00899 
00901   mutable CoinPackedMatrix *matrixByRow_;  
00902   
00904   mutable CoinPackedMatrix *matrixByCol_;  
00906 
00907 };
00908 
00909 //#############################################################################
00911 void OsiGlpkSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00912 
00913 #endif // OsiGlpkSolverInterface_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines