Osi
trunk
|
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; 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 /***************************************************************************/ 00726 00749 virtual OsiSolverInterface::ApplyCutsReturnCode applyCuts(const OsiCuts & cs, 00750 double effectivenessLb = 0.0); 00751 00752 protected: 00753 00756 00757 virtual void applyRowCut(const OsiRowCut & rc); 00758 00762 virtual void applyColCut(const OsiColCut & cc); 00764 00765 private: 00768 00769 void resizeColSpace(int minsize); 00770 00772 void freeColSpace(); 00773 00775 void resizeAuxColSpace(int minsize); 00776 00778 void resizeAuxColIndSpace(); 00780 00783 00784 static GRBenv* globalenv_; 00786 static bool globalenv_is_ours; 00788 static unsigned int numInstances_; 00790 00793 00795 GRBmodel* getMutableLpPtr() const; 00796 00798 void gutsOfCopy(const OsiGrbSolverInterface & source); 00799 00801 void gutsOfConstructor(); 00802 00804 void gutsOfDestructor(); 00805 00807 void freeCachedColRim(); 00808 00810 void freeCachedRowRim(); 00811 00813 void freeCachedResults(); 00814 00816 void freeCachedMatrix(); 00817 00819 void freeCachedData(int keepCached = KEEPCACHED_NONE); 00820 00822 void freeAllMemory(); 00823 00825 void convertToRangedRow(int rowidx, double rhs, double range); 00826 00828 void convertToNormalRow(int rowidx, char sense, double rhs); 00830 00831 00834 00835 mutable GRBenv* localenv_; 00836 00838 mutable GRBmodel* lp_; 00839 00841 int *hotStartCStat_; 00842 int hotStartCStatSize_; 00843 int *hotStartRStat_; 00844 int hotStartRStatSize_; 00845 int hotStartMaxIteration_; 00846 00848 int nameDisc_; 00849 00852 00853 mutable double *obj_; 00854 00856 mutable double *collower_; 00857 00859 mutable double *colupper_; 00860 00862 mutable char *rowsense_; 00863 00865 mutable double *rhs_; 00866 00868 mutable double *rowrange_; 00869 00871 mutable double *rowlower_; 00872 00874 mutable double *rowupper_; 00875 00877 mutable double *colsol_; 00878 00880 mutable double *rowsol_; 00881 00883 mutable double *redcost_; 00884 00886 mutable double *rowact_; 00887 00889 mutable CoinPackedMatrix *matrixByRow_; 00890 00892 mutable CoinPackedMatrix *matrixByCol_; 00894 00897 00898 mutable bool probtypemip_; 00899 00901 bool domipstart; 00902 00904 int colspace_; 00905 00907 char *coltype_; 00908 00910 int nauxcols; 00911 00913 int auxcolspace; 00914 00917 int* colmap_O2G; 00918 00923 int* colmap_G2O; 00924 00926 int auxcolindspace; 00927 00931 int* auxcolind; 00933 }; 00934 00935 //############################################################################# 00937 void OsiGrbSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir); 00938 00939 #endif