Osi trunk
OsiGrbSolverInterface.hpp
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 // name:     OSI Interface for Gurobi
00003 // template: OSI Cplex Interface written by T. Achterberg
00004 // author:   Stefan Vigerske
00005 //           Humboldt University Berlin
00006 // date:     09/02/2009
00007 // license:  this file may be freely distributed under the terms of EPL
00008 // comments: please scan this file for '???' and read the comments
00009 //-----------------------------------------------------------------------------
00010 // Copyright (C) 2009 Humboldt University Berlin and others.
00011 // All Rights Reserved.
00012 
00013 // $Id$
00014 
00015 #ifndef OsiGrbSolverInterface_H
00016 #define OsiGrbSolverInterface_H
00017 
00018 #include <string>
00019 #include "OsiSolverInterface.hpp"
00020 
00021 typedef struct _GRBmodel GRBmodel;
00022 typedef struct _GRBenv GRBenv;
00023 
00029 class OsiGrbSolverInterface : virtual public OsiSolverInterface {
00030   friend void OsiGrbSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00031 
00032 public:
00033 
00034         //---------------------------------------------------------------------------
00037 
00038         virtual void initialSolve();
00039 
00041         virtual void resolve();
00042 
00044         virtual void branchAndBound();
00046 
00047         //---------------------------------------------------------------------------
00063         // Set an integer parameter
00064         bool setIntParam(OsiIntParam key, int value);
00065         // Set an double parameter
00066         bool setDblParam(OsiDblParam key, double value);
00067         // Set a string parameter
00068         bool setStrParam(OsiStrParam key, const std::string & value);
00069         // Set a hint parameter
00070         bool setHintParam(OsiHintParam key, bool yesNo = true, OsiHintStrength strength = OsiHintTry, void* = NULL);
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;
00077         // Get a hint parameter
00078         bool getHintParam(OsiHintParam key, bool& yesNo, OsiHintStrength& strength, void*& otherInformation) const;
00079         // Get a hint parameter
00080         bool getHintParam(OsiHintParam key, bool& yesNo, OsiHintStrength& strength) const;
00081         // Get a hint parameter
00082         bool getHintParam(OsiHintParam key, bool& yesNo) const;
00084 
00085         //---------------------------------------------------------------------------
00087 
00088 
00089         virtual bool isAbandoned() const;
00091         virtual bool isProvenOptimal() const;
00093         virtual bool isProvenPrimalInfeasible() const;
00095         virtual bool isProvenDualInfeasible() const;
00097         virtual bool isPrimalObjectiveLimitReached() const;
00099         virtual bool isDualObjectiveLimitReached() const;
00101         virtual bool isIterationLimitReached() const;
00103 
00104         //---------------------------------------------------------------------------
00107 
00114         CoinWarmStart* getEmptyWarmStart() const;
00115 
00117         virtual CoinWarmStart* getWarmStart() const;
00120         virtual bool setWarmStart(const CoinWarmStart* warmstart);
00122 
00123         //---------------------------------------------------------------------------
00130 
00131         virtual void markHotStart();
00133         virtual void solveFromHotStart();
00135         virtual void unmarkHotStart();
00137 
00138         //---------------------------------------------------------------------------
00153 
00154         virtual int getNumCols() const;
00155 
00157         virtual int getNumRows() const;
00158 
00160         virtual int getNumElements() const;
00161 
00163         virtual const double * getColLower() const;
00164 
00166         virtual const double * getColUpper() const;
00167 
00177         virtual const char * getRowSense() const;
00178 
00187         virtual const double * getRightHandSide() const;
00188 
00197         virtual const double * getRowRange() const;
00198 
00200         virtual const double * getRowLower() const;
00201 
00203         virtual const double * getRowUpper() const;
00204 
00206         virtual const double * getObjCoefficients() const;
00207 
00209         virtual double getObjSense() const;
00210 
00212         virtual bool isContinuous(int colNumber) const;
00213 
00215         virtual const CoinPackedMatrix * getMatrixByRow() const;
00216 
00218         virtual const CoinPackedMatrix * getMatrixByCol() const;
00219 
00221         virtual double getInfinity() const;
00223 
00226 
00227         virtual const double * getColSolution() const;
00228 
00230         virtual const double * getRowPrice() const;
00231 
00233         virtual const double * getReducedCost() const;
00234 
00237         virtual const double * getRowActivity() const;
00238 
00240         virtual double getObjValue() const;
00241 
00244         virtual int getIterationCount() const;
00245 
00263         virtual std::vector<double*> getDualRays(int maxNumRays,
00264                                                  bool fullRay=false) const;
00276         virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
00278 
00279 
00280         //---------------------------------------------------------------------------
00281 
00284         //-------------------------------------------------------------------------
00288         virtual void setObjCoeff(int elementIndex, double elementValue);
00289 
00291         virtual void setObjCoeffSet(const int* indexFirst, const int* indexLast,
00292                         const double* coeffList);
00293 
00294         using OsiSolverInterface::setColLower;
00297         virtual void setColLower(int elementIndex, double elementValue);
00298 
00299         using OsiSolverInterface::setColUpper;
00302         virtual void setColUpper(int elementIndex, double elementValue);
00303 
00307         virtual void setColBounds(int elementIndex, double lower, double upper);
00308 
00316         virtual void setColSetBounds(const int* indexFirst, const int* indexLast,
00317                         const double* boundList);
00318 
00321         virtual void setRowLower(int elementIndex, double elementValue);
00322 
00325         virtual void setRowUpper(int elementIndex, double elementValue);
00326 
00330         virtual void setRowBounds(int elementIndex, double lower, double upper);
00331 
00333         virtual void setRowType(int index, char sense, double rightHandSide,
00334                         double range);
00335 
00343         virtual void setRowSetBounds(const int* indexFirst, const int* indexLast,
00344                         const double* boundList);
00345 
00355         virtual void setRowSetTypes(const int* indexFirst, const int* indexLast,
00356                         const char* senseList, const double* rhsList, const double* rangeList);
00358 
00359         //-------------------------------------------------------------------------
00363         virtual void setContinuous(int index);
00365         virtual void setInteger(int index);
00368         virtual void setContinuous(const int* indices, int len);
00371         virtual void setInteger(const int* indices, int len);
00373 
00374   //-------------------------------------------------------------------------
00378   virtual void setRowName(int ndx, std::string name) ;
00379 
00381   virtual void setColName(int ndx, std::string name) ;
00383 
00384         //-------------------------------------------------------------------------
00386         virtual void setObjSense(double s);
00387 
00398         virtual void setColSolution(const double * colsol);
00399 
00410         virtual void setRowPrice(const double * rowprice);
00411 
00412         //-------------------------------------------------------------------------
00417         using OsiSolverInterface::addCol;
00419         virtual void addCol(const CoinPackedVectorBase& vec, const double collb,
00420                         const double colub, const double obj);
00421 
00422         using OsiSolverInterface::addCols;
00424         virtual void addCols(const int numcols,
00425                         const CoinPackedVectorBase * const * cols, const double* collb,
00426                         const double* colub, const double* obj);
00428         virtual void deleteCols(const int num, const int * colIndices);
00429 
00430         using OsiSolverInterface::addRow;
00432         virtual void addRow(const CoinPackedVectorBase& vec, const double rowlb,
00433                         const double rowub);
00435         virtual void addRow(const CoinPackedVectorBase& vec, const char rowsen,
00436                         const double rowrhs, const double rowrng);
00437 
00438         using OsiSolverInterface::addRows;
00440         virtual void addRows(const int numrows,
00441                         const CoinPackedVectorBase * const * rows, const double* rowlb,
00442                         const double* rowub);
00444         virtual void addRows(const int numrows,
00445                         const CoinPackedVectorBase * const * rows, const char* rowsen,
00446                         const double* rowrhs, const double* rowrng);
00448         virtual void deleteRows(const int num, const int * rowIndices);
00450 
00451 
00452         //---------------------------------------------------------------------------
00453 
00467         virtual void loadProblem(const CoinPackedMatrix& matrix, const double* collb,
00468                         const double* colub, const double* obj, const double* rowlb,
00469                         const double* rowub);
00470 
00478         virtual void assignProblem(CoinPackedMatrix*& matrix, double*& collb,
00479                         double*& colub, double*& obj, double*& rowlb, double*& rowub);
00480 
00493         virtual void loadProblem(const CoinPackedMatrix& matrix, const double* collb,
00494                         const double* colub, const double* obj, const char* rowsen,
00495                         const double* rowrhs, const double* rowrng);
00496 
00504         virtual void assignProblem(CoinPackedMatrix*& matrix, double*& collb,
00505                         double*& colub, double*& obj, char*& rowsen, double*& rowrhs,
00506                         double*& rowrng);
00507 
00510         virtual void loadProblem(const int numcols, const int numrows,
00511                         const int* start, const int* index, const double* value,
00512                         const double* collb, const double* colub, const double* obj,
00513                         const double* rowlb, const double* rowub);
00514 
00517         virtual void loadProblem(const int numcols, const int numrows,
00518                         const int* start, const int* index, const double* value,
00519                         const double* collb, const double* colub, const double* obj,
00520                         const char* rowsen, const double* rowrhs, const double* rowrng);
00521 
00522         using OsiSolverInterface::readMps;
00524         virtual int readMps(const char *filename, const char *extension = "mps");
00525 
00530         virtual void writeMps(const char *filename, const char *extension = "mps",
00531                         double objSense=0.0) const;
00532 
00534 
00535         //---------------------------------------------------------------------------
00536 
00542         enum keepCachedFlag
00543         {
00545                 KEEPCACHED_NONE = 0,
00547                 KEEPCACHED_COLUMN = 1,
00549                 KEEPCACHED_ROW = 2,
00551                 KEEPCACHED_MATRIX = 4,
00553                 KEEPCACHED_RESULTS = 8,
00555                 KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX,
00557                 KEEPCACHED_ALL = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS,
00559                 FREECACHED_COLUMN = KEEPCACHED_PROBLEM & ~KEEPCACHED_COLUMN,
00561                 FREECACHED_ROW = KEEPCACHED_PROBLEM & ~KEEPCACHED_ROW,
00563                 FREECACHED_MATRIX = KEEPCACHED_PROBLEM & ~KEEPCACHED_MATRIX,
00565                 FREECACHED_RESULTS = KEEPCACHED_ALL & ~KEEPCACHED_RESULTS
00566         };
00567 
00568         GRBmodel* getLpPtr(int keepCached = KEEPCACHED_NONE);
00569 
00571 
00572         GRBenv* getEnvironmentPtr() const;
00573 
00575   bool isDemoLicense() const;
00577 
00579         const char* getCtype() const;
00580 
00590         static void incrementInstanceCounter();
00591 
00600         static void decrementInstanceCounter();
00601         
00603         static void setEnvironment(GRBenv* globalenv);
00604 
00606         static unsigned int getNumInstances();
00608 
00609 
00612 
00613         OsiGrbSolverInterface(bool use_local_env = false);
00614 
00616         OsiGrbSolverInterface(GRBenv* localgrbenv);
00617 
00619         virtual OsiSolverInterface * clone(bool copyData = true) const;
00620 
00622         OsiGrbSolverInterface(const OsiGrbSolverInterface&);
00623 
00625         OsiGrbSolverInterface& operator=(const OsiGrbSolverInterface& rhs);
00626 
00628         virtual ~OsiGrbSolverInterface();
00629 
00631         virtual void reset();
00633 
00634         /***************************************************************************/
00651 
00655         virtual int canDoSimplexInterface() const;
00656 
00657         using OsiSolverInterface::enableSimplexInterface;
00661         virtual void enableSimplexInterface(int doingPrimal) {
00662         }
00663         ;
00664 
00668         virtual void disableSimplexInterface() {
00669         }
00670         ;
00671 
00675         virtual void enableFactorization() const {
00676         }
00677         ;
00678 
00682         virtual void disableFactorization() const {
00683         }
00684         ;
00685 
00687         virtual bool basisIsAvailable() const;
00688 
00692         virtual void getBasisStatus(int* cstat, int* rstat) const;
00693 
00694         //  ///Get a row of the tableau (slack part in slack if not NULL)
00695         //  virtual void getBInvARow(int row, double* z, double * slack=NULL) const;
00696         //
00697         //  ///Get a row of the basis inverse
00698         //  virtual void getBInvRow(int row, double* z) const;
00699         //
00700         //  ///Get a column of the tableau
00701         //  virtual void getBInvACol(int col, double* vec) const;
00702         //
00703         //  ///Get a column of the basis inverse
00704         //  virtual void getBInvCol(int col, double* vec) const;
00705         //
00706         //  /**  Get indices of the pivot variable in each row
00707         //      (order of indices corresponds to the
00708         //      order of elements in a vector retured by getBInvACol() and
00709         //      getBInvCol()).
00710         //  */
00711         //  virtual void getBasics(int* index) const;
00713         void switchToLP();
00714 
00716         void switchToMIP();
00717 
00719         /***************************************************************************/
00720 
00721 protected:
00722 
00725 
00726         virtual void applyRowCut(const OsiRowCut & rc);
00727 
00731         virtual void applyColCut(const OsiColCut & cc);
00733 
00734 private:
00737 
00738         void resizeColSpace(int minsize);
00739 
00741         void freeColSpace();
00742 
00744         void resizeAuxColSpace(int minsize);
00745 
00747         void resizeAuxColIndSpace();
00749 
00752 
00753         static GRBenv* globalenv_;
00755         static bool globalenv_is_ours;
00757         static unsigned int numInstances_;
00759 
00762 
00764         GRBmodel* getMutableLpPtr() const;
00765 
00767         void gutsOfCopy(const OsiGrbSolverInterface & source);
00768 
00770         void gutsOfConstructor();
00771 
00773         void gutsOfDestructor();
00774 
00776         void freeCachedColRim();
00777 
00779         void freeCachedRowRim();
00780 
00782         void freeCachedResults();
00783 
00785         void freeCachedMatrix();
00786 
00788         void freeCachedData(int keepCached = KEEPCACHED_NONE);
00789 
00791         void freeAllMemory();
00792 
00794         void convertToRangedRow(int rowidx, double rhs, double range);
00795 
00797         void convertToNormalRow(int rowidx, char sense, double rhs);
00799 
00800 
00803 
00804         mutable GRBenv* localenv_;
00805         
00807         mutable GRBmodel* lp_;  
00808 
00810         int *hotStartCStat_;
00811         int hotStartCStatSize_;
00812         int *hotStartRStat_;
00813         int hotStartRStatSize_;
00814         int hotStartMaxIteration_;
00815 
00817   int nameDisc_;
00818 
00821 
00822         mutable double *obj_;
00823 
00825         mutable double *collower_;
00826 
00828         mutable double *colupper_;
00829 
00831         mutable char *rowsense_;
00832 
00834         mutable double *rhs_;
00835 
00837         mutable double *rowrange_;
00838 
00840         mutable double *rowlower_;
00841 
00843         mutable double *rowupper_;
00844 
00846         mutable double *colsol_;
00847 
00849         mutable double *rowsol_;
00850 
00852         mutable double *redcost_;
00853 
00855         mutable double *rowact_;
00856 
00858         mutable CoinPackedMatrix *matrixByRow_;
00859 
00861         mutable CoinPackedMatrix *matrixByCol_;
00863 
00866 
00867   mutable bool probtypemip_;
00868 
00870   int colspace_;
00871 
00873         char *coltype_;
00874 
00876         int nauxcols;
00877 
00879         int auxcolspace;
00880 
00883         int* colmap_O2G;
00884 
00889         int* colmap_G2O;
00890 
00892         int auxcolindspace;
00893 
00897         int* auxcolind;
00899 };
00900 
00901 //#############################################################################
00907 void OsiGrbSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00908 
00909 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines