- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/Cgl-0.52.0/Osi/src/OsiClp/OsiClpSolverInterface.hpp

Go to the documentation of this file.

    00001 // Copyright (C) 2000, International Business Machines
    00002 // Corporation and others.  All Rights Reserved.
    00003
    00004 #ifndef OsiClpSolverInterface_H
    00005 #define OsiClpSolverInterface_H
    00006
    00007 #include <string>
    00008 #include <cfloat>
    00009 #include <map>
    00010
    00011 #include "ClpSimplex.hpp"
    00012 #include "CoinPackedMatrix.hpp"
    00013 #include "OsiSolverInterface.hpp"
    00014 #include "CoinWarmStartBasis.hpp"
    00015 #include "ClpEventHandler.hpp"
    00016 #include "CoinIndexedVector.hpp"
    00017
    00018 class OsiRowCut;
    00019 class OsiClpUserSolver;
    00020 class CoinSet;
    00021 #ifndef COIN_DBL_MAX
    00022 static const double OsiClpInfinity = DBL_MAX;
    00023 #else
    00024 static const double OsiClpInfinity = COIN_DBL_MAX;
    00025 #endif
    00026
    00027 //#############################################################################
    00028
    00035 class OsiClpSolverInterface :
    00036   virtual public OsiSolverInterface {
    00037   friend void OsiClpSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
    00038
    00039 public:
    00040   //---------------------------------------------------------------------------
    00043
    00044   virtual void initialSolve();
    00045
    00047   virtual void resolve();
    00048
    00050   virtual void branchAndBound();
    00052
    00054
    00055
    00061   virtual void enableSimplexInterface(bool doingPrimal);
    00062
    00064   virtual void disableSimplexInterface();
    00068   virtual int canDoSimplexInterface() const;
    00075   virtual void enableFactorization() const;
    00077   virtual void disableFactorization() const;
    00078
    00094   void setupForRepeatedUse(int senseOfAdventure=0, int printOut=0);
    00096   virtual void synchronizeModel();
    00097   // Sleazy methods to fool const requirements (no less safe as modelPtr_ mutable)
    00098   void setSpecialOptionsMutable(unsigned int value) const;
    00099
    00104   virtual bool basisIsAvailable() const;
    00105
    00121   virtual void getBasisStatus(int* cstat, int* rstat) const;
    00122
    00133   virtual int setBasisStatus(const int* cstat, const int* rstat);
    00134
    00142   virtual int pivot(int colIn, int colOut, int outStatus);
    00143
    00155   virtual int primalPivotResult(int colIn, int sign,
    00156                                 int& colOut, int& outStatus,
    00157                                 double& t, CoinPackedVector* dx);
    00158
    00165   virtual int dualPivotResult(int& colIn, int& sign,
    00166                               int colOut, int outStatus,
    00167                               double& t, CoinPackedVector* dx);
    00168
    00170   virtual void getReducedGradient(double* columnReducedCosts,
    00171                                   double * duals,
    00172                                   const double * c);
    00173
    00176   virtual void setObjectiveAndRefresh(double* c);
    00177
    00179   virtual void getBInvARow(int row, double* z, double * slack=NULL) const;
    00180
    00185   virtual void getBInvARow(int row, CoinIndexedVector * z, CoinIndexedVector * slack=NULL,
    00186                            bool keepScaled=false) const;
    00187
    00189   virtual void getBInvRow(int row, double* z) const;
    00190
    00192   virtual void getBInvACol(int col, double* vec) const ;
    00193
    00195   virtual void getBInvACol(int col, CoinIndexedVector * vec) const ;
    00196
    00201   virtual void getBInvACol(CoinIndexedVector * vec) const ;
    00202
    00204   virtual void getBInvCol(int col, double* vec) const ;
    00205
    00210   virtual void getBasics(int* index) const;
    00211
    00213   //---------------------------------------------------------------------------
    00229   // Set an integer parameter
    00230   bool setIntParam(OsiIntParam key, int value);
    00231   // Set an double parameter
    00232   bool setDblParam(OsiDblParam key, double value);
    00233   // Set a string parameter
    00234   bool setStrParam(OsiStrParam key, const std::string & value);
    00235   // Get an integer parameter
    00236   bool getIntParam(OsiIntParam key, int& value) const;
    00237   // Get an double parameter
    00238   bool getDblParam(OsiDblParam key, double& value) const;
    00239   // Get a string parameter
    00240   bool getStrParam(OsiStrParam key, std::string& value) const;
    00241   // Set a hint parameter - overrides OsiSolverInterface
    00242   virtual bool setHintParam(OsiHintParam key, bool yesNo=true,
    00243                             OsiHintStrength strength=OsiHintTry,
    00244                             void * otherInformation=NULL);
    00246
    00247   //---------------------------------------------------------------------------
    00249
    00250
    00251   virtual bool isAbandoned() const;
    00253   virtual bool isProvenOptimal() const;
    00255   virtual bool isProvenPrimalInfeasible() const;
    00257   virtual bool isProvenDualInfeasible() const;
    00259   virtual bool isPrimalObjectiveLimitReached() const;
    00261   virtual bool isDualObjectiveLimitReached() const;
    00263   virtual bool isIterationLimitReached() const;
    00265
    00266   //---------------------------------------------------------------------------
    00269
    00277   virtual CoinWarmStart *getEmptyWarmStart () const;
    00278
    00280   virtual CoinWarmStart* getWarmStart() const;
    00283   virtual bool setWarmStart(const CoinWarmStart* warmstart);
    00293   virtual CoinWarmStart* getPointerToWarmStart(bool & mustDelete) ;
    00294
    00296
    00297   //---------------------------------------------------------------------------
    00304
    00305   virtual void markHotStart();
    00307   virtual void solveFromHotStart();
    00309   virtual void unmarkHotStart();
    00311
    00312   //---------------------------------------------------------------------------
    00327
    00328   virtual int getNumCols() const {
    00329     return modelPtr_->numberColumns(); }
    00330
    00332   virtual int getNumRows() const {
    00333     return modelPtr_->numberRows(); }
    00334
    00336   virtual int getNumElements() const {
    00337     int retVal = 0;
    00338     const CoinPackedMatrix * matrix =modelPtr_->matrix();
    00339     if ( matrix != NULL ) retVal=matrix->getNumElements();
    00340     return retVal; }
    00341
    00344     virtual std::string getRowName(int rowIndex,
    00345                                    unsigned maxLen = std::string::npos) const;
    00346
    00349     virtual std::string getColName(int colIndex,
    00350                                    unsigned maxLen = std::string::npos) const;
    00351
    00352
    00354   virtual const double * getColLower() const { return modelPtr_->columnLower(); }
    00355
    00357   virtual const double * getColUpper() const { return modelPtr_->columnUpper(); }
    00358
    00368   virtual const char * getRowSense() const;
    00369
    00378   virtual const double * getRightHandSide() const ;
    00379
    00388   virtual const double * getRowRange() const ;
    00389
    00391   virtual const double * getRowLower() const { return modelPtr_->rowLower(); }
    00392
    00394   virtual const double * getRowUpper() const { return modelPtr_->rowUpper(); }
    00395
    00397   virtual const double * getObjCoefficients() const
    00398   { return modelPtr_->objective(); }
    00399
    00401   virtual double getObjSense() const
    00402   { return modelPtr_->optimizationDirection(); }
    00403
    00405   virtual bool isContinuous(int colNumber) const;
    00407   virtual bool isBinary(int colIndex) const;
    00408
    00413   virtual bool isInteger(int colIndex) const;
    00414
    00416   virtual bool isIntegerNonBinary(int colIndex) const;
    00417
    00419   virtual bool isFreeBinary(int colIndex) const;
    00425   virtual const char * getColType(bool refresh=false) const;
    00426
    00427
    00429   virtual const CoinPackedMatrix * getMatrixByRow() const;
    00430
    00432   virtual const CoinPackedMatrix * getMatrixByCol() const;
    00433
    00435   virtual CoinPackedMatrix * getMutableMatrixByCol() const;
    00436
    00438   virtual double getInfinity() const { return OsiClpInfinity; }
    00440
    00443
    00444   virtual const double * getColSolution() const;
    00445
    00447   virtual const double * getRowPrice() const;
    00448
    00450   virtual const double * getReducedCost() const;
    00451
    00454   virtual const double * getRowActivity() const;
    00455
    00457   virtual double getObjValue() const;
    00458
    00461   virtual int getIterationCount() const
    00462   { return modelPtr_->numberIterations(); }
    00463
    00475   virtual std::vector<double*> getDualRays(int maxNumRays) const;
    00487   virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
    00488
    00490
    00491
    00492   //---------------------------------------------------------------------------
    00493
    00496   //-------------------------------------------------------------------------
    00500   virtual void setObjCoeff( int elementIndex, double elementValue );
    00501
    00504   virtual void setColLower( int elementIndex, double elementValue );
    00505
    00508   virtual void setColUpper( int elementIndex, double elementValue );
    00509
    00511   virtual void setColBounds( int elementIndex,
    00512                              double lower, double upper );
    00513
    00522   virtual void setColSetBounds(const int* indexFirst,
    00523                                const int* indexLast,
    00524                                const double* boundList);
    00525
    00528   virtual void setRowLower( int elementIndex, double elementValue );
    00529
    00532   virtual void setRowUpper( int elementIndex, double elementValue ) ;
    00533
    00535   virtual void setRowBounds( int elementIndex,
    00536                              double lower, double upper ) ;
    00537
    00539   virtual void setRowType(int index, char sense, double rightHandSide,
    00540                           double range);
    00541
    00550   virtual void setRowSetBounds(const int* indexFirst,
    00551                                const int* indexLast,
    00552                                const double* boundList);
    00553
    00564   virtual void setRowSetTypes(const int* indexFirst,
    00565                               const int* indexLast,
    00566                               const char* senseList,
    00567                               const double* rhsList,
    00568                               const double* rangeList);
    00573     virtual void setObjective(const double * array);
    00574
    00579     virtual void setColLower(const double * array);
    00580
    00585     virtual void setColUpper(const double * array);
    00586
    00587 //    using OsiSolverInterface::setRowName ;
    00589 //    virtual void setRowName(int rowIndex, std::string & name) ;
    00590     virtual void setRowName(int rowIndex, std::string  name) ;
    00591
    00592 //    using OsiSolverInterface::setColName ;
    00594 //    virtual void setColName(int colIndex, std::string & name) ;
    00595     virtual void setColName(int colIndex, std::string  name) ;
    00596
    00598
    00599   //-------------------------------------------------------------------------
    00603   virtual void setContinuous(int index);
    00605   virtual void setInteger(int index);
    00608   virtual void setContinuous(const int* indices, int len);
    00611   virtual void setInteger(const int* indices, int len);
    00613   inline int numberSOS() const
    00614   { return numberSOS_;}
    00616   inline const CoinSet * setInfo() const
    00617   { return setInfo_;}
    00627   virtual int findIntegersAndSOS(bool justCount);
    00629
    00630   //-------------------------------------------------------------------------
    00632   virtual void setObjSense(double s )
    00633   { modelPtr_->setOptimizationDirection( s < 0 ? -1 : 1); }
    00634
    00645   virtual void setColSolution(const double * colsol);
    00646
    00657   virtual void setRowPrice(const double * rowprice);
    00658
    00659   //-------------------------------------------------------------------------
    00664
    00665   using OsiSolverInterface::addCol ;
    00667   virtual void addCol(const CoinPackedVectorBase& vec,
    00668                       const double collb, const double colub,
    00669                       const double obj);
    00671   virtual void addCol(int numberElements, const int * rows, const double * elements,
    00672                       const double collb, const double colub,
    00673                       const double obj) ;
    00675   virtual void addCols(const int numcols,
    00676                        const CoinPackedVectorBase * const * cols,
    00677                        const double* collb, const double* colub,
    00678                        const double* obj);
    00680   virtual void addCols(const int numcols,
    00681                        const int * columnStarts, const int * rows, const double * elements,
    00682                        const double* collb, const double* colub,
    00683                        const double* obj);
    00685   virtual void deleteCols(const int num, const int * colIndices);
    00686
    00687   using OsiSolverInterface::addRow ;
    00689   virtual void addRow(const CoinPackedVectorBase& vec,
    00690                       const double rowlb, const double rowub);
    00692   virtual void addRow(const CoinPackedVectorBase& vec,
    00693                       const char rowsen, const double rowrhs,
    00694                       const double rowrng);
    00696   virtual void addRow(int numberElements, const int * columns, const double * element,
    00697                       const double rowlb, const double rowub) ;
    00699   virtual void addRows(const int numrows,
    00700                        const CoinPackedVectorBase * const * rows,
    00701                        const double* rowlb, const double* rowub);
    00703   virtual void addRows(const int numrows,
    00704                        const CoinPackedVectorBase * const * rows,
    00705                        const char* rowsen, const double* rowrhs,
    00706                        const double* rowrng);
    00707
    00709   virtual void addRows(const int numrows,
    00710                        const int * rowStarts, const int * columns, const double * element,
    00711                        const double* rowlb, const double* rowub);
    00713   void modifyCoefficient(int row, int column, double newElement,
    00714                         bool keepZero=false)
    00715         {modelPtr_->modifyCoefficient(row,column,newElement, keepZero);}
    00716
    00718   virtual void deleteRows(const int num, const int * rowIndices);
    00721   virtual void saveBaseModel() ;
    00725   virtual void restoreBaseModel(int numberRows);
    00726
    00727   //-----------------------------------------------------------------------
    00731   virtual void applyRowCuts(int numberCuts, const OsiRowCut * cuts);
    00736   virtual void applyRowCuts(int numberCuts, const OsiRowCut ** cuts);
    00738
    00739
    00740   //---------------------------------------------------------------------------
    00741
    00742 public:
    00743
    00757   virtual void loadProblem(const CoinPackedMatrix& matrix,
    00758                            const double* collb, const double* colub,
    00759                            const double* obj,
    00760                            const double* rowlb, const double* rowub);
    00761
    00769   virtual void assignProblem(CoinPackedMatrix*& matrix,
    00770                              double*& collb, double*& colub, double*& obj,
    00771                              double*& rowlb, double*& rowub);
    00772
    00785   virtual void loadProblem(const CoinPackedMatrix& matrix,
    00786                            const double* collb, const double* colub,
    00787                            const double* obj,
    00788                            const char* rowsen, const double* rowrhs,
    00789                            const double* rowrng);
    00790
    00798   virtual void assignProblem(CoinPackedMatrix*& matrix,
    00799                              double*& collb, double*& colub, double*& obj,
    00800                              char*& rowsen, double*& rowrhs,
    00801                              double*& rowrng);
    00802
    00805   virtual void loadProblem(const int numcols, const int numrows,
    00806                            const CoinBigIndex * start, const int* index,
    00807                            const double* value,
    00808                            const double* collb, const double* colub,
    00809                            const double* obj,
    00810                            const double* rowlb, const double* rowub);
    00811
    00814   virtual void loadProblem(const int numcols, const int numrows,
    00815                            const CoinBigIndex * start, const int* index,
    00816                            const double* value,
    00817                            const double* collb, const double* colub,
    00818                            const double* obj,
    00819                            const char* rowsen, const double* rowrhs,
    00820                            const double* rowrng);
    00822   virtual int loadFromCoinModel (  CoinModel & modelObject, bool keepSolution=false);
    00823
    00824   using OsiSolverInterface::readMps ;
    00827   virtual int readMps(const char *filename,
    00828                       const char *extension = "mps") ;
    00831   int readMps(const char *filename,bool keepNames,bool allowErrors);
    00832
    00837   virtual void writeMps(const char *filename,
    00838                         const char *extension = "mps",
    00839                         double objSense=0.0) const;
    00848   virtual int writeMpsNative(const char *filename,
    00849                              const char ** rowNames, const char ** columnNames,
    00850                              int formatType=0,int numberAcross=2,
    00851                              double objSense=0.0) const ;
    00853   virtual int readLp(const char *filename, const double epsilon = 1e-5);
    00859   virtual void writeLp(const char *filename,
    00860                        const char *extension = "lp",
    00861                        double epsilon = 1e-5,
    00862                        int numberAcross = 10,
    00863                        int decimals = 5,
    00864                        double objSense = 0.0,
    00865                        bool useRowNames = true) const;
    00870   virtual void writeLp(FILE *fp,
    00871                double epsilon = 1e-5,
    00872                int numberAcross = 10,
    00873                int decimals = 5,
    00874                double objSense = 0.0,
    00875                bool useRowNames = true) const;
    00881   virtual void replaceMatrixOptional(const CoinPackedMatrix & matrix);
    00883   virtual void replaceMatrix(const CoinPackedMatrix & matrix) ;
    00885
    00890
    00891   void newLanguage(CoinMessages::Language language);
    00892   void setLanguage(CoinMessages::Language language)
    00893   {newLanguage(language);}
    00895     void generateCpp( FILE * fp);
    00897   //---------------------------------------------------------------------------
    00898
    00901
    00902   ClpSimplex * getModelPtr() const ;
    00904   inline ClpSimplex * swapModelPtr(ClpSimplex * newModel)
    00905   { ClpSimplex * model = modelPtr_; modelPtr_=newModel;return model;}
    00907   inline unsigned int specialOptions() const
    00908   { return specialOptions_;}
    00909   void setSpecialOptions(unsigned int value);
    00911   inline int cleanupScaling() const
    00912   { return cleanupScaling_;}
    00925   inline void setCleanupScaling(int value)
    00926   { cleanupScaling_=value;}
    00929   inline double smallestElementInCut() const
    00930   { return smallestElementInCut_;}
    00933   inline void setSmallestElementInCut(double value)
    00934   { smallestElementInCut_=value;}
    00941   inline double smallestChangeInCut() const
    00942   { return smallestChangeInCut_;}
    00949   inline void setSmallestChangeInCut(double value)
    00950   { smallestChangeInCut_=value;}
    00952   inline void setSolveOptions(const ClpSolve & options)
    00953   { solveOptions_ = options;}
    00955   int tightenBounds();
    00957
    00958   //---------------------------------------------------------------------------
    00959
    00962
    00963   OsiClpSolverInterface ();
    00964
    00966   virtual OsiSolverInterface * clone(bool copyData = true) const;
    00967
    00969   OsiClpSolverInterface (const OsiClpSolverInterface &);
    00970
    00972   OsiClpSolverInterface (ClpSimplex * rhs, bool reallyOwn=false);
    00973
    00975   void releaseClp();
    00976
    00978   OsiClpSolverInterface & operator=(const OsiClpSolverInterface& rhs);
    00979
    00981   virtual ~OsiClpSolverInterface ();
    00982
    00984   virtual void reset();
    00986
    00987   //---------------------------------------------------------------------------
    00988
    00989 protected:
    00991
    00992
    00993   virtual void applyRowCut(const OsiRowCut& rc);
    00994
    00996   virtual void applyColCut(const OsiColCut& cc);
    00998
    00999   //---------------------------------------------------------------------------
    01000
    01001 protected:
    01004
    01005   void gutsOfDestructor();
    01006
    01008   void freeCachedResults() const;
    01009
    01011   void freeCachedResults0() const;
    01012
    01014   void freeCachedResults1() const;
    01015
    01017   void extractSenseRhsRange() const;
    01018
    01020   void fillParamMaps();
    01029   CoinWarmStartBasis getBasis(ClpSimplex * model) const;
    01038   void setBasis( const CoinWarmStartBasis & basis, ClpSimplex * model);
    01040   void crunch();
    01042   void redoScaleFactors(int numberRows,const CoinBigIndex * starts,
    01043                         const int * indices, const double * elements);
    01044 public:
    01047   void setBasis( const CoinWarmStartBasis & basis);
    01049   inline void setBasis( )
    01050   { setBasis(basis_,modelPtr_);}
    01052   void deleteScaleFactors();
    01054   inline const double * upRange() const
    01055   { return rowActivity_;}
    01056   inline const double * downRange() const
    01057   { return columnActivity_;}
    01059   inline void passInRanges(int * array)
    01060   { whichRange_=array;}
    01062   void setSOSData(int numberSOS,const char * type,
    01063                   const int * start,const int * indices, const double * weights=NULL);
    01064 protected:
    01066
    01069
    01070   mutable ClpSimplex * modelPtr_;
    01072   double * linearObjective_;
    01075
    01076   mutable char    *rowsense_;
    01077
    01079   mutable double  *rhs_;
    01080
    01084   mutable double  *rowrange_;
    01085
    01088   mutable CoinWarmStartBasis* ws_;
    01091   mutable double * rowActivity_;
    01092   mutable double * columnActivity_;
    01094   int numberSOS_;
    01096   CoinSet * setInfo_;
    01098   ClpSimplex * smallModel_;
    01100   ClpFactorization * factorization_;
    01103   double smallestElementInCut_;
    01107   double smallestChangeInCut_;
    01109   char * spareArrays_;
    01111   CoinWarmStartBasis basis_;
    01113   int itlimOrig_;
    01114
    01116   mutable int lastAlgorithm_;
    01117
    01119   bool notOwned_;
    01120
    01122   mutable CoinPackedMatrix *matrixByRow_;
    01123
    01125   char * integerInformation_;
    01126
    01131   int * whichRange_;
    01132
    01133   //std::map<OsiIntParam, ClpIntParam> intParamMap_;
    01134   //std::map<OsiDblParam, ClpDblParam> dblParamMap_;
    01135   //std::map<OsiStrParam, ClpStrParam> strParamMap_;
    01136
    01138   mutable ClpDataSave saveData_;
    01140   ClpSolve solveOptions_;
    01153   int cleanupScaling_;
    01172   mutable unsigned int specialOptions_;
    01174   ClpSimplex * baseModel_;
    01176   int lastNumberRows_;
    01178   ClpSimplex * continuousModel_;
    01180   CoinDoubleArrayWithLength rowScale_;
    01182   CoinDoubleArrayWithLength columnScale_;
    01184 };
    01185
    01186 class OsiClpDisasterHandler : public ClpDisasterHandler {
    01187 public:
    01191
    01192   virtual void intoSimplex();
    01194   virtual bool check() const ;
    01196   virtual void saveInfo();
    01198
    01199
    01204   OsiClpDisasterHandler(OsiClpSolverInterface * model = NULL);
    01206   virtual ~OsiClpDisasterHandler();
    01207   // Copy
    01208   OsiClpDisasterHandler(const OsiClpDisasterHandler&);
    01209   // Assignment
    01210   OsiClpDisasterHandler& operator=(const OsiClpDisasterHandler&);
    01212   virtual ClpDisasterHandler * clone() const;
    01213
    01215
    01220   void setOsiModel(OsiClpSolverInterface * model);
    01222   inline OsiClpSolverInterface * osiModel() const
    01223   { return osiModel_;}
    01225   inline void setWhereFrom(int value)
    01226   { whereFrom_=value;}
    01228   inline int whereFrom() const
    01229   { return whereFrom_;}
    01231   inline void setPhase(int value)
    01232   { phase_=value;}
    01234   inline int phase() const
    01235   { return phase_;}
    01237   inline bool inTrouble() const
    01238   { return inTrouble_;}
    01240
    01241
    01242 protected:
    01246
    01247   OsiClpSolverInterface * osiModel_;
    01255   int whereFrom_;
    01263   int phase_;
    01265   bool inTrouble_;
    01266
    01268 };
    01269
    01270 //#############################################################################
    01278 void
    01279 OsiClpSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
    01280 #endif