/home/coin/SVN-release/Smi-0.81.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);
00285   
00286   //---------------------------------------------------------------------------
00293 
00294   virtual void markHotStart();
00296   virtual void solveFromHotStart();
00298   virtual void unmarkHotStart();
00300   
00301   //---------------------------------------------------------------------------
00316 
00317   virtual int getNumCols() const {
00318     return modelPtr_->numberColumns(); }
00319   
00321   virtual int getNumRows() const {
00322     return modelPtr_->numberRows(); }
00323   
00325   virtual int getNumElements() const {
00326     int retVal = 0;
00327     const CoinPackedMatrix * matrix =modelPtr_->matrix();
00328     if ( matrix != NULL ) retVal=matrix->getNumElements();
00329     return retVal; }
00330 
00333     virtual std::string getRowName(int rowIndex,
00334                                    unsigned maxLen = std::string::npos) const;
00335     
00338     virtual std::string getColName(int colIndex,
00339                                    unsigned maxLen = std::string::npos) const;
00340     
00341   
00343   virtual const double * getColLower() const { return modelPtr_->columnLower(); }
00344   
00346   virtual const double * getColUpper() const { return modelPtr_->columnUpper(); }
00347   
00357   virtual const char * getRowSense() const;
00358   
00367   virtual const double * getRightHandSide() const ;
00368   
00377   virtual const double * getRowRange() const ;
00378   
00380   virtual const double * getRowLower() const { return modelPtr_->rowLower(); }
00381   
00383   virtual const double * getRowUpper() const { return modelPtr_->rowUpper(); }
00384   
00386   virtual const double * getObjCoefficients() const 
00387   { return modelPtr_->objective(); }
00388   
00390   virtual double getObjSense() const 
00391   { return modelPtr_->optimizationDirection(); }
00392   
00394   virtual bool isContinuous(int colNumber) const;
00395   
00396   
00398   virtual const CoinPackedMatrix * getMatrixByRow() const;
00399   
00401   virtual const CoinPackedMatrix * getMatrixByCol() const;
00402   
00404   virtual CoinPackedMatrix * getMutableMatrixByCol() const;
00405   
00407   virtual double getInfinity() const { return OsiClpInfinity; }
00409   
00412 
00413   virtual const double * getColSolution() const; 
00414   
00416   virtual const double * getRowPrice() const;
00417   
00419   virtual const double * getReducedCost() const; 
00420   
00423   virtual const double * getRowActivity() const; 
00424   
00426   virtual double getObjValue() const;
00427   
00430   virtual int getIterationCount() const 
00431   { return modelPtr_->numberIterations(); }
00432   
00444   virtual std::vector<double*> getDualRays(int maxNumRays) const;
00456   virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
00457   
00459 
00460   
00461   //---------------------------------------------------------------------------
00462   
00465   //-------------------------------------------------------------------------
00469   virtual void setObjCoeff( int elementIndex, double elementValue );
00470   
00473   virtual void setColLower( int elementIndex, double elementValue );
00474   
00477   virtual void setColUpper( int elementIndex, double elementValue );
00478   
00480   virtual void setColBounds( int elementIndex,
00481                              double lower, double upper );
00482   
00491   virtual void setColSetBounds(const int* indexFirst,
00492                                const int* indexLast,
00493                                const double* boundList);
00494   
00497   virtual void setRowLower( int elementIndex, double elementValue );
00498   
00501   virtual void setRowUpper( int elementIndex, double elementValue ) ;
00502   
00504   virtual void setRowBounds( int elementIndex,
00505                              double lower, double upper ) ;
00506   
00508   virtual void setRowType(int index, char sense, double rightHandSide,
00509                           double range);
00510   
00519   virtual void setRowSetBounds(const int* indexFirst,
00520                                const int* indexLast,
00521                                const double* boundList);
00522   
00533   virtual void setRowSetTypes(const int* indexFirst,
00534                               const int* indexLast,
00535                               const char* senseList,
00536                               const double* rhsList,
00537                               const double* rangeList);
00542     virtual void setObjective(const double * array);
00543 
00548     virtual void setColLower(const double * array);
00549 
00554     virtual void setColUpper(const double * array);
00555 
00556 //    using OsiSolverInterface::setRowName ;
00558 //    virtual void setRowName(int rowIndex, std::string & name) ;
00559     virtual void setRowName(int rowIndex, std::string  name) ;
00560     
00561 //    using OsiSolverInterface::setColName ;
00563 //    virtual void setColName(int colIndex, std::string & name) ;
00564     virtual void setColName(int colIndex, std::string  name) ;
00565     
00567   
00568   //-------------------------------------------------------------------------
00572   virtual void setContinuous(int index);
00574   virtual void setInteger(int index);
00577   virtual void setContinuous(const int* indices, int len);
00580   virtual void setInteger(const int* indices, int len);
00582   inline int numberSOS() const
00583   { return numberSOS_;}
00585   inline const CoinSet * setInfo() const
00586   { return setInfo_;}
00596   virtual int findIntegersAndSOS(bool justCount);
00598   
00599   //-------------------------------------------------------------------------
00601   virtual void setObjSense(double s ) 
00602   { modelPtr_->setOptimizationDirection( s < 0 ? -1 : 1); }
00603   
00614   virtual void setColSolution(const double * colsol);
00615   
00626   virtual void setRowPrice(const double * rowprice);
00627   
00628   //-------------------------------------------------------------------------
00633 
00634   using OsiSolverInterface::addCol ;
00636   virtual void addCol(const CoinPackedVectorBase& vec,
00637                       const double collb, const double colub,   
00638                       const double obj);
00640   virtual void addCol(int numberElements, const int * rows, const double * elements,
00641                       const double collb, const double colub,   
00642                       const double obj) ;
00644   virtual void addCols(const int numcols,
00645                        const CoinPackedVectorBase * const * cols,
00646                        const double* collb, const double* colub,   
00647                        const double* obj);
00649   virtual void addCols(const int numcols,
00650                        const int * columnStarts, const int * rows, const double * elements,
00651                        const double* collb, const double* colub,   
00652                        const double* obj);
00654   virtual void deleteCols(const int num, const int * colIndices);
00655   
00656   using OsiSolverInterface::addRow ;
00658   virtual void addRow(const CoinPackedVectorBase& vec,
00659                       const double rowlb, const double rowub);
00661   virtual void addRow(const CoinPackedVectorBase& vec,
00662                       const char rowsen, const double rowrhs,   
00663                       const double rowrng);
00665   virtual void addRow(int numberElements, const int * columns, const double * element,
00666                       const double rowlb, const double rowub) ;
00668   virtual void addRows(const int numrows,
00669                        const CoinPackedVectorBase * const * rows,
00670                        const double* rowlb, const double* rowub);
00672   virtual void addRows(const int numrows,
00673                        const CoinPackedVectorBase * const * rows,
00674                        const char* rowsen, const double* rowrhs,   
00675                        const double* rowrng);
00676 
00678   virtual void addRows(const int numrows,
00679                        const int * rowStarts, const int * columns, const double * element,
00680                        const double* rowlb, const double* rowub);
00682   void modifyCoefficient(int row, int column, double newElement,
00683                         bool keepZero=false)
00684         {modelPtr_->modifyCoefficient(row,column,newElement, keepZero);}
00685 
00687   virtual void deleteRows(const int num, const int * rowIndices);
00688   
00689   //-----------------------------------------------------------------------
00693   virtual void applyRowCuts(int numberCuts, const OsiRowCut * cuts);
00698   virtual void applyRowCuts(int numberCuts, const OsiRowCut ** cuts);
00700 
00701   
00702   //---------------------------------------------------------------------------
00703   
00704 public:
00705   
00719   virtual void loadProblem(const CoinPackedMatrix& matrix,
00720                            const double* collb, const double* colub,   
00721                            const double* obj,
00722                            const double* rowlb, const double* rowub);
00723   
00731   virtual void assignProblem(CoinPackedMatrix*& matrix,
00732                              double*& collb, double*& colub, double*& obj,
00733                              double*& rowlb, double*& rowub);
00734   
00747   virtual void loadProblem(const CoinPackedMatrix& matrix,
00748                            const double* collb, const double* colub,
00749                            const double* obj,
00750                            const char* rowsen, const double* rowrhs,   
00751                            const double* rowrng);
00752   
00760   virtual void assignProblem(CoinPackedMatrix*& matrix,
00761                              double*& collb, double*& colub, double*& obj,
00762                              char*& rowsen, double*& rowrhs,
00763                              double*& rowrng);
00764   
00767   virtual void loadProblem(const int numcols, const int numrows,
00768                            const CoinBigIndex * start, const int* index,
00769                            const double* value,
00770                            const double* collb, const double* colub,   
00771                            const double* obj,
00772                            const double* rowlb, const double* rowub);
00773   
00776   virtual void loadProblem(const int numcols, const int numrows,
00777                            const CoinBigIndex * start, const int* index,
00778                            const double* value,
00779                            const double* collb, const double* colub,   
00780                            const double* obj,
00781                            const char* rowsen, const double* rowrhs,   
00782                            const double* rowrng);
00784   virtual int loadFromCoinModel (  CoinModel & modelObject, bool keepSolution=false);
00785 
00786   using OsiSolverInterface::readMps ;
00789   virtual int readMps(const char *filename,
00790                       const char *extension = "mps") ;
00793   int readMps(const char *filename,bool keepNames,bool allowErrors);
00794   
00799   virtual void writeMps(const char *filename,
00800                         const char *extension = "mps",
00801                         double objSense=0.0) const;
00810   virtual int writeMpsNative(const char *filename, 
00811                              const char ** rowNames, const char ** columnNames,
00812                              int formatType=0,int numberAcross=2,
00813                              double objSense=0.0) const ;
00815   virtual int readLp(const char *filename, const double epsilon = 1e-5);
00821   virtual void writeLp(const char *filename,
00822                        const char *extension = "lp",
00823                        double epsilon = 1e-5,
00824                        int numberAcross = 10,
00825                        int decimals = 5,
00826                        double objSense = 0.0,
00827                        bool useRowNames = true) const;
00832   virtual void writeLp(FILE *fp,
00833                double epsilon = 1e-5,
00834                int numberAcross = 10,
00835                int decimals = 5,
00836                double objSense = 0.0,
00837                bool useRowNames = true) const;
00843   virtual void replaceMatrixOptional(const CoinPackedMatrix & matrix);
00845   virtual void replaceMatrix(const CoinPackedMatrix & matrix) ;
00847   
00852 
00853   void newLanguage(CoinMessages::Language language);
00854   void setLanguage(CoinMessages::Language language)
00855   {newLanguage(language);}
00857     void generateCpp( FILE * fp);
00859   //---------------------------------------------------------------------------
00860   
00863 
00864   ClpSimplex * getModelPtr() const ;
00866   inline unsigned int specialOptions() const
00867   { return specialOptions_;}
00868   void setSpecialOptions(unsigned int value);
00870   inline int cleanupScaling() const
00871   { return cleanupScaling_;}
00884   inline void setCleanupScaling(int value)
00885   { cleanupScaling_=value;}
00888   inline double smallestElementInCut() const
00889   { return smallestElementInCut_;}
00892   inline void setSmallestElementInCut(double value)
00893   { smallestElementInCut_=value;}
00900   inline double smallestChangeInCut() const
00901   { return smallestChangeInCut_;}
00908   inline void setSmallestChangeInCut(double value)
00909   { smallestChangeInCut_=value;}
00911   inline void setSolveOptions(const ClpSolve & options)
00912   { solveOptions_ = options;}
00914   
00915   //---------------------------------------------------------------------------
00916   
00919 
00920   OsiClpSolverInterface ();
00921   
00923   virtual OsiSolverInterface * clone(bool copyData = true) const;
00924   
00926   OsiClpSolverInterface (const OsiClpSolverInterface &);
00927   
00929   OsiClpSolverInterface (ClpSimplex * rhs, bool reallyOwn=false);
00930   
00932   void releaseClp();
00933   
00935   OsiClpSolverInterface & operator=(const OsiClpSolverInterface& rhs);
00936   
00938   virtual ~OsiClpSolverInterface ();
00939   
00941   virtual void reset();
00943   
00944   //---------------------------------------------------------------------------
00945   
00946 protected:
00948 
00949 
00950   virtual void applyRowCut(const OsiRowCut& rc);
00951   
00953   virtual void applyColCut(const OsiColCut& cc);
00955   
00956   //---------------------------------------------------------------------------
00957   
00958 protected:
00961 
00962   void gutsOfDestructor();
00963   
00965   void freeCachedResults() const;
00966   
00968   void freeCachedResults0() const;
00969   
00971   void freeCachedResults1() const;
00972   
00974   void extractSenseRhsRange() const;
00975   
00977   void fillParamMaps();
00986   CoinWarmStartBasis getBasis(ClpSimplex * model) const;
00995   void setBasis( const CoinWarmStartBasis & basis, ClpSimplex * model);
00997   void crunch();
00999   void redoScaleFactors(int numberRows,const CoinBigIndex * starts,
01000                         const int * indices, const double * elements);
01001 public:
01004   void setBasis( const CoinWarmStartBasis & basis);
01006   void deleteScaleFactors();
01008   inline const double * upRange() const
01009   { return rowActivity_;}
01010   inline const double * downRange() const
01011   { return columnActivity_;}
01013   inline void passInRanges(int * array)
01014   { whichRange_=array;}
01016   void setSOSData(int numberSOS,const char * type,
01017                   const int * start,const int * indices, const double * weights=NULL);
01018 protected:
01020   
01023 
01024   mutable ClpSimplex * modelPtr_;
01026   double * linearObjective_;
01029 
01030   mutable char    *rowsense_;
01031   
01033   mutable double  *rhs_;
01034   
01038   mutable double  *rowrange_;
01039   
01042   mutable CoinWarmStartBasis* ws_;
01045   mutable double * rowActivity_;
01046   mutable double * columnActivity_;
01048   int numberSOS_;
01050   CoinSet * setInfo_;
01052   ClpSimplex * smallModel_;
01054   ClpFactorization * factorization_;
01057   double smallestElementInCut_;
01061   double smallestChangeInCut_;
01063   char * spareArrays_;
01065   CoinWarmStartBasis basis_;
01067   int itlimOrig_;
01068   
01070   mutable int lastAlgorithm_;
01071   
01073   bool notOwned_;
01074   
01076   mutable CoinPackedMatrix *matrixByRow_;  
01077   
01079   char * integerInformation_;
01080   
01085   int * whichRange_;
01086 
01087   //std::map<OsiIntParam, ClpIntParam> intParamMap_;
01088   //std::map<OsiDblParam, ClpDblParam> dblParamMap_;
01089   //std::map<OsiStrParam, ClpStrParam> strParamMap_;
01090   
01092   mutable ClpDataSave saveData_;
01094   ClpSolve solveOptions_;
01107   int cleanupScaling_;
01126   mutable unsigned int specialOptions_;
01128   ClpSimplex * baseModel_;
01130   int lastNumberRows_;
01132   CoinDoubleArrayWithLength rowScale_; 
01134   CoinDoubleArrayWithLength columnScale_; 
01136 };
01137   
01138 class OsiClpDisasterHandler : public ClpDisasterHandler {
01139 public:
01143 
01144   virtual void intoSimplex();
01146   virtual bool check() const ;
01148   virtual void saveInfo();
01150   
01151   
01156   OsiClpDisasterHandler(OsiClpSolverInterface * model = NULL);
01158   virtual ~OsiClpDisasterHandler();
01159   // Copy
01160   OsiClpDisasterHandler(const OsiClpDisasterHandler&);
01161   // Assignment
01162   OsiClpDisasterHandler& operator=(const OsiClpDisasterHandler&);
01164   virtual ClpDisasterHandler * clone() const;
01165 
01167   
01172   void setOsiModel(OsiClpSolverInterface * model);
01174   inline OsiClpSolverInterface * osiModel() const
01175   { return osiModel_;}
01177   inline void setWhereFrom(int value)
01178   { whereFrom_=value;}
01180   inline int whereFrom() const
01181   { return whereFrom_;}
01183   inline void setPhase(int value)
01184   { phase_=value;}
01186   inline int phase() const
01187   { return phase_;}
01189   inline bool inTrouble() const
01190   { return inTrouble_;}
01192   
01193   
01194 protected:
01198 
01199   OsiClpSolverInterface * osiModel_;
01207   int whereFrom_;
01215   int phase_;
01217   bool inTrouble_;
01218       
01220 };
01221 
01222 //#############################################################################
01230 void
01231 OsiClpSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
01232 #endif

Generated on Fri May 16 19:29:34 2008 by  doxygen 1.4.7