- Main Page
- Namespaces
- Classes
- Files
- Directories

- Search for |

- File List
- File Members

# 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: OsiGrbSolverInterface.hpp 1754 2011-06-19 16:07:57Z stefan $
    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;
    00083   // Set mipstart option (pass column solution to CPLEX before MIP start)
    00084   void setMipStart(bool value) { domipstart = value; }
    00085   // Get mipstart option value
    00086   bool getMipStart() const { return domipstart; }
    00088
    00089         //---------------------------------------------------------------------------
    00091
    00092
    00093         virtual bool isAbandoned() const;
    00095         virtual bool isProvenOptimal() const;
    00097         virtual bool isProvenPrimalInfeasible() const;
    00099         virtual bool isProvenDualInfeasible() const;
    00101         virtual bool isPrimalObjectiveLimitReached() const;
    00103         virtual bool isDualObjectiveLimitReached() const;
    00105         virtual bool isIterationLimitReached() const;
    00107
    00108         //---------------------------------------------------------------------------
    00111
    00118         CoinWarmStart* getEmptyWarmStart() const;
    00119
    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
    00219         virtual const CoinPackedMatrix * getMatrixByRow() const;
    00220
    00222         virtual const CoinPackedMatrix * getMatrixByCol() const;
    00223
    00225         virtual double getInfinity() const;
    00227
    00230
    00231         virtual const double * getColSolution() const;
    00232
    00234         virtual const double * getRowPrice() const;
    00235
    00237         virtual const double * getReducedCost() const;
    00238
    00241         virtual const double * getRowActivity() const;
    00242
    00244         virtual double getObjValue() const;
    00245
    00248         virtual int getIterationCount() const;
    00249
    00267         virtual std::vector<double*> getDualRays(int maxNumRays,
    00268                                                  bool fullRay=false) const;
    00280         virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
    00282
    00283
    00284         //---------------------------------------------------------------------------
    00285
    00288         //-------------------------------------------------------------------------
    00292         virtual void setObjCoeff(int elementIndex, double elementValue);
    00293
    00295         virtual void setObjCoeffSet(const int* indexFirst, const int* indexLast,
    00296                         const double* coeffList);
    00297
    00298         using OsiSolverInterface::setColLower;
    00301         virtual void setColLower(int elementIndex, double elementValue);
    00302
    00303         using OsiSolverInterface::setColUpper;
    00306         virtual void setColUpper(int elementIndex, double elementValue);
    00307
    00311         virtual void setColBounds(int elementIndex, double lower, double upper);
    00312
    00320         virtual void setColSetBounds(const int* indexFirst, const int* indexLast,
    00321                         const double* boundList);
    00322
    00325         virtual void setRowLower(int elementIndex, double elementValue);
    00326
    00329         virtual void setRowUpper(int elementIndex, double elementValue);
    00330
    00334         virtual void setRowBounds(int elementIndex, double lower, double upper);
    00335
    00337         virtual void setRowType(int index, char sense, double rightHandSide,
    00338                         double range);
    00339
    00347         virtual void setRowSetBounds(const int* indexFirst, const int* indexLast,
    00348                         const double* boundList);
    00349
    00359         virtual void setRowSetTypes(const int* indexFirst, const int* indexLast,
    00360                         const char* senseList, const double* rhsList, const double* rangeList);
    00362
    00363         //-------------------------------------------------------------------------
    00367         virtual void setContinuous(int index);
    00369         virtual void setInteger(int index);
    00372         virtual void setContinuous(const int* indices, int len);
    00375         virtual void setInteger(const int* indices, int len);
    00377
    00378   //-------------------------------------------------------------------------
    00382   virtual void setRowName(int ndx, std::string name) ;
    00383
    00385   virtual void setColName(int ndx, std::string name) ;
    00387
    00388         //-------------------------------------------------------------------------
    00390         virtual void setObjSense(double s);
    00391
    00402         virtual void setColSolution(const double * colsol);
    00403
    00414         virtual void setRowPrice(const double * rowprice);
    00415
    00416         //-------------------------------------------------------------------------
    00421         using OsiSolverInterface::addCol;
    00423         virtual void addCol(const CoinPackedVectorBase& vec, const double collb,
    00424                         const double colub, const double obj);
    00425
    00426         using OsiSolverInterface::addCols;
    00428         virtual void addCols(const int numcols,
    00429                         const CoinPackedVectorBase * const * cols, const double* collb,
    00430                         const double* colub, const double* obj);
    00432         virtual void deleteCols(const int num, const int * colIndices);
    00433
    00434         using OsiSolverInterface::addRow;
    00436         virtual void addRow(const CoinPackedVectorBase& vec, const double rowlb,
    00437                         const double rowub);
    00439         virtual void addRow(const CoinPackedVectorBase& vec, const char rowsen,
    00440                         const double rowrhs, const double rowrng);
    00441
    00442         using OsiSolverInterface::addRows;
    00444         virtual void addRows(const int numrows,
    00445                         const CoinPackedVectorBase * const * rows, const double* rowlb,
    00446                         const double* rowub);
    00448         virtual void addRows(const int numrows,
    00449                         const CoinPackedVectorBase * const * rows, const char* rowsen,
    00450                         const double* rowrhs, const double* rowrng);
    00452         virtual void deleteRows(const int num, const int * rowIndices);
    00454
    00455
    00456         //---------------------------------------------------------------------------
    00457
    00471         virtual void loadProblem(const CoinPackedMatrix& matrix, const double* collb,
    00472                         const double* colub, const double* obj, const double* rowlb,
    00473                         const double* rowub);
    00474
    00482         virtual void assignProblem(CoinPackedMatrix*& matrix, double*& collb,
    00483                         double*& colub, double*& obj, double*& rowlb, double*& rowub);
    00484
    00497         virtual void loadProblem(const CoinPackedMatrix& matrix, const double* collb,
    00498                         const double* colub, const double* obj, const char* rowsen,
    00499                         const double* rowrhs, const double* rowrng);
    00500
    00508         virtual void assignProblem(CoinPackedMatrix*& matrix, double*& collb,
    00509                         double*& colub, double*& obj, char*& rowsen, double*& rowrhs,
    00510                         double*& rowrng);
    00511
    00514         virtual void loadProblem(const int numcols, const int numrows,
    00515                         const int* start, const int* index, const double* value,
    00516                         const double* collb, const double* colub, const double* obj,
    00517                         const double* rowlb, const double* rowub);
    00518
    00521         virtual void loadProblem(const int numcols, const int numrows,
    00522                         const int* start, const int* index, const double* value,
    00523                         const double* collb, const double* colub, const double* obj,
    00524                         const char* rowsen, const double* rowrhs, const double* rowrng);
    00525
    00526         using OsiSolverInterface::readMps;
    00528         virtual int readMps(const char *filename, const char *extension = "mps");
    00529
    00534         virtual void writeMps(const char *filename, const char *extension = "mps",
    00535                         double objSense=0.0) const;
    00536
    00538
    00539         //---------------------------------------------------------------------------
    00540
    00546         enum keepCachedFlag
    00547         {
    00549                 KEEPCACHED_NONE = 0,
    00551                 KEEPCACHED_COLUMN = 1,
    00553                 KEEPCACHED_ROW = 2,
    00555                 KEEPCACHED_MATRIX = 4,
    00557                 KEEPCACHED_RESULTS = 8,
    00559                 KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX,
    00561                 KEEPCACHED_ALL = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS,
    00563                 FREECACHED_COLUMN = KEEPCACHED_PROBLEM & ~KEEPCACHED_COLUMN,
    00565                 FREECACHED_ROW = KEEPCACHED_PROBLEM & ~KEEPCACHED_ROW,
    00567                 FREECACHED_MATRIX = KEEPCACHED_PROBLEM & ~KEEPCACHED_MATRIX,
    00569                 FREECACHED_RESULTS = KEEPCACHED_ALL & ~KEEPCACHED_RESULTS
    00570         };
    00571
    00572         GRBmodel* getLpPtr(int keepCached = KEEPCACHED_NONE);
    00573
    00575
    00576         GRBenv* getEnvironmentPtr() const;
    00577
    00579   bool isDemoLicense() const;
    00581
    00583         const char* getCtype() const;
    00584
    00594         static void incrementInstanceCounter();
    00595
    00604         static void decrementInstanceCounter();
    00605
    00607         static void setEnvironment(GRBenv* globalenv);
    00608
    00610         static unsigned int getNumInstances();
    00612
    00613
    00616
    00617         OsiGrbSolverInterface(bool use_local_env = false);
    00618
    00620         OsiGrbSolverInterface(GRBenv* localgrbenv);
    00621
    00623         virtual OsiSolverInterface * clone(bool copyData = true) const;
    00624
    00626         OsiGrbSolverInterface(const OsiGrbSolverInterface&);
    00627
    00629         OsiGrbSolverInterface& operator=(const OsiGrbSolverInterface& rhs);
    00630
    00632         virtual ~OsiGrbSolverInterface();
    00633
    00635         virtual void reset();
    00637
    00638         /***************************************************************************/
    00655
    00659         virtual int canDoSimplexInterface() const;
    00660
    00661         using OsiSolverInterface::enableSimplexInterface;
    00665         virtual void enableSimplexInterface(int doingPrimal) {
    00666         }
    00667         ;
    00668
    00672         virtual void disableSimplexInterface() {
    00673         }
    00674         ;
    00675
    00679         virtual void enableFactorization() const {
    00680         }
    00681         ;
    00682
    00686         virtual void disableFactorization() const {
    00687         }
    00688         ;
    00689
    00691         virtual bool basisIsAvailable() const;
    00692
    00696         virtual void getBasisStatus(int* cstat, int* rstat) const;
    00697
    00698         //  ///Get a row of the tableau (slack part in slack if not NULL)
    00699         //  virtual void getBInvARow(int row, double* z, double * slack=NULL) const;
    00700         //
    00701         //  ///Get a row of the basis inverse
    00702         //  virtual void getBInvRow(int row, double* z) const;
    00703         //
    00704         //  ///Get a column of the tableau
    00705         //  virtual void getBInvACol(int col, double* vec) const;
    00706         //
    00707         //  ///Get a column of the basis inverse
    00708         //  virtual void getBInvCol(int col, double* vec) const;
    00709         //
    00710         //  /**  Get indices of the pivot variable in each row
    00711         //      (order of indices corresponds to the
    00712         //      order of elements in a vector retured by getBInvACol() and
    00713         //      getBInvCol()).
    00714         //  */
    00715         //  virtual void getBasics(int* index) const;
    00717         void switchToLP();
    00718
    00720         void switchToMIP();
    00721
    00723         /***************************************************************************/
    00724
    00725 protected:
    00726
    00729
    00730         virtual void applyRowCut(const OsiRowCut & rc);
    00731
    00735         virtual void applyColCut(const OsiColCut & cc);
    00737
    00738 private:
    00741
    00742         void resizeColSpace(int minsize);
    00743
    00745         void freeColSpace();
    00746
    00748         void resizeAuxColSpace(int minsize);
    00749
    00751         void resizeAuxColIndSpace();
    00753
    00756
    00757         static GRBenv* globalenv_;
    00759         static bool globalenv_is_ours;
    00761         static unsigned int numInstances_;
    00763
    00766
    00768         GRBmodel* getMutableLpPtr() const;
    00769
    00771         void gutsOfCopy(const OsiGrbSolverInterface & source);
    00772
    00774         void gutsOfConstructor();
    00775
    00777         void gutsOfDestructor();
    00778
    00780         void freeCachedColRim();
    00781
    00783         void freeCachedRowRim();
    00784
    00786         void freeCachedResults();
    00787
    00789         void freeCachedMatrix();
    00790
    00792         void freeCachedData(int keepCached = KEEPCACHED_NONE);
    00793
    00795         void freeAllMemory();
    00796
    00798         void convertToRangedRow(int rowidx, double rhs, double range);
    00799
    00801         void convertToNormalRow(int rowidx, char sense, double rhs);
    00803
    00804
    00807
    00808         mutable GRBenv* localenv_;
    00809
    00811         mutable GRBmodel* lp_;
    00812
    00814         int *hotStartCStat_;
    00815         int hotStartCStatSize_;
    00816         int *hotStartRStat_;
    00817         int hotStartRStatSize_;
    00818         int hotStartMaxIteration_;
    00819
    00821   int nameDisc_;
    00822
    00825
    00826         mutable double *obj_;
    00827
    00829         mutable double *collower_;
    00830
    00832         mutable double *colupper_;
    00833
    00835         mutable char *rowsense_;
    00836
    00838         mutable double *rhs_;
    00839
    00841         mutable double *rowrange_;
    00842
    00844         mutable double *rowlower_;
    00845
    00847         mutable double *rowupper_;
    00848
    00850         mutable double *colsol_;
    00851
    00853         mutable double *rowsol_;
    00854
    00856         mutable double *redcost_;
    00857
    00859         mutable double *rowact_;
    00860
    00862         mutable CoinPackedMatrix *matrixByRow_;
    00863
    00865         mutable CoinPackedMatrix *matrixByCol_;
    00867
    00870
    00871   mutable bool probtypemip_;
    00872
    00874   bool domipstart;
    00875
    00877   int colspace_;
    00878
    00880         char *coltype_;
    00881
    00883         int nauxcols;
    00884
    00886         int auxcolspace;
    00887
    00890         int* colmap_O2G;
    00891
    00896         int* colmap_G2O;
    00897
    00899         int auxcolindspace;
    00900
    00904         int* auxcolind;
    00906 };
    00907
    00908 //#############################################################################
    00910 void OsiGrbSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
    00911
    00912 #endif