/home/coin/SVN-release/Bcps-0.91.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 "ClpLinearObjective.hpp"
00013 #include "CoinPackedMatrix.hpp"
00014 #include "OsiSolverInterface.hpp"
00015 #include "CoinWarmStartBasis.hpp"
00016 #include "ClpEventHandler.hpp"
00017 #include "ClpNode.hpp"
00018 #include "CoinIndexedVector.hpp"
00019 
00020 class OsiRowCut;
00021 class OsiClpUserSolver;
00022 class OsiClpDisasterHandler;
00023 class CoinSet;
00024 #ifndef COIN_DBL_MAX
00025 static const double OsiClpInfinity = DBL_MAX;
00026 #else
00027 static const double OsiClpInfinity = COIN_DBL_MAX;
00028 #endif
00029 
00030 //#############################################################################
00031 
00038 class OsiClpSolverInterface :
00039   virtual public OsiSolverInterface {
00040   friend int OsiClpSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00041   
00042 public:
00043   //---------------------------------------------------------------------------
00046 
00047   virtual void initialSolve();
00048   
00050   virtual void resolve();
00051   
00053   virtual void branchAndBound();
00054 
00056   
00058 
00059   
00065   virtual void enableSimplexInterface(bool doingPrimal);
00066   
00068   virtual void disableSimplexInterface();
00072   virtual int canDoSimplexInterface() const;
00079   virtual void enableFactorization() const;
00081   virtual void disableFactorization() const;
00082   
00098   void setupForRepeatedUse(int senseOfAdventure=0, int printOut=0);
00100   virtual void synchronizeModel();
00101   // Sleazy methods to fool const requirements (no less safe as modelPtr_ mutable)
00102   void setSpecialOptionsMutable(unsigned int value) const;
00103 
00108   virtual bool basisIsAvailable() const;
00109   
00125   virtual void getBasisStatus(int* cstat, int* rstat) const;
00126   
00137   virtual int setBasisStatus(const int* cstat, const int* rstat);
00138   
00146   virtual int pivot(int colIn, int colOut, int outStatus);
00147   
00159   virtual int primalPivotResult(int colIn, int sign, 
00160                                 int& colOut, int& outStatus, 
00161                                 double& t, CoinPackedVector* dx);
00162   
00169   virtual int dualPivotResult(int& colIn, int& sign, 
00170                               int colOut, int outStatus, 
00171                               double& t, CoinPackedVector* dx);
00172   
00174   virtual void getReducedGradient(double* columnReducedCosts, 
00175                                   double * duals,
00176                                   const double * c);
00177   
00180   virtual void setObjectiveAndRefresh(double* c);
00181   
00183   virtual void getBInvARow(int row, double* z, double * slack=NULL) const;
00184   
00189   virtual void getBInvARow(int row, CoinIndexedVector * z, CoinIndexedVector * slack=NULL,
00190                            bool keepScaled=false) const;
00191   
00193   virtual void getBInvRow(int row, double* z) const;
00194   
00196   virtual void getBInvACol(int col, double* vec) const ;
00197   
00199   virtual void getBInvACol(int col, CoinIndexedVector * vec) const ;
00200   
00205   virtual void getBInvACol(CoinIndexedVector * vec) const ;
00206   
00208   virtual void getBInvCol(int col, double* vec) const ;
00209   
00214   virtual void getBasics(int* index) const;
00215   
00217   //---------------------------------------------------------------------------
00233   // Set an integer parameter
00234   bool setIntParam(OsiIntParam key, int value);
00235   // Set an double parameter
00236   bool setDblParam(OsiDblParam key, double value);
00237   // Set a string parameter
00238   bool setStrParam(OsiStrParam key, const std::string & value);
00239   // Get an integer parameter
00240   bool getIntParam(OsiIntParam key, int& value) const;
00241   // Get an double parameter
00242   bool getDblParam(OsiDblParam key, double& value) const;
00243   // Get a string parameter
00244   bool getStrParam(OsiStrParam key, std::string& value) const;
00245   // Set a hint parameter - overrides OsiSolverInterface
00246   virtual bool setHintParam(OsiHintParam key, bool yesNo=true,
00247                             OsiHintStrength strength=OsiHintTry,
00248                             void * otherInformation=NULL);
00250   
00251   //---------------------------------------------------------------------------
00253 
00254 
00255   virtual bool isAbandoned() const;
00257   virtual bool isProvenOptimal() const;
00259   virtual bool isProvenPrimalInfeasible() const;
00261   virtual bool isProvenDualInfeasible() const;
00263   virtual bool isPrimalObjectiveLimitReached() const;
00265   virtual bool isDualObjectiveLimitReached() const;
00267   virtual bool isIterationLimitReached() const;
00269   
00270   //---------------------------------------------------------------------------
00273   
00281   virtual CoinWarmStart *getEmptyWarmStart () const;
00282   
00284   virtual CoinWarmStart* getWarmStart() const;
00286   inline CoinWarmStartBasis* getPointerToWarmStart() 
00287   { return &basis_;}
00289   inline const CoinWarmStartBasis* getConstPointerToWarmStart() const 
00290   { return &basis_;}
00293   virtual bool setWarmStart(const CoinWarmStart* warmstart);
00303   virtual CoinWarmStart* getPointerToWarmStart(bool & mustDelete) ;
00304 
00306   
00307   //---------------------------------------------------------------------------
00314 
00315   virtual void markHotStart();
00317   virtual void solveFromHotStart();
00319   virtual void unmarkHotStart();
00328   int startFastDual(int options);
00330   void stopFastDual();
00332   void setStuff(double tolerance,double increment);
00334   
00335   //---------------------------------------------------------------------------
00350 
00351   virtual int getNumCols() const {
00352     return modelPtr_->numberColumns(); }
00353   
00355   virtual int getNumRows() const {
00356     return modelPtr_->numberRows(); }
00357   
00359   virtual int getNumElements() const {
00360     int retVal = 0;
00361     const CoinPackedMatrix * matrix =modelPtr_->matrix();
00362     if ( matrix != NULL ) retVal=matrix->getNumElements();
00363     return retVal; }
00364 
00367     virtual std::string getRowName(int rowIndex,
00368                                    unsigned maxLen = std::string::npos) const;
00369     
00372     virtual std::string getColName(int colIndex,
00373                                    unsigned maxLen = std::string::npos) const;
00374     
00375   
00377   virtual const double * getColLower() const { return modelPtr_->columnLower(); }
00378   
00380   virtual const double * getColUpper() const { return modelPtr_->columnUpper(); }
00381   
00391   virtual const char * getRowSense() const;
00392   
00401   virtual const double * getRightHandSide() const ;
00402   
00411   virtual const double * getRowRange() const ;
00412   
00414   virtual const double * getRowLower() const { return modelPtr_->rowLower(); }
00415   
00417   virtual const double * getRowUpper() const { return modelPtr_->rowUpper(); }
00418   
00420   virtual const double * getObjCoefficients() const 
00421   { return modelPtr_->objective(); }
00422   
00424   virtual double getObjSense() const 
00425   { return modelPtr_->optimizationDirection(); }
00426   
00428   virtual bool isContinuous(int colNumber) const;
00430   virtual bool isBinary(int colIndex) const;
00431   
00436   virtual bool isInteger(int colIndex) const;
00437   
00439   virtual bool isIntegerNonBinary(int colIndex) const;
00440   
00442   virtual bool isFreeBinary(int colIndex) const; 
00448   virtual const char * getColType(bool refresh=false) const;
00449   
00450   
00452   virtual const CoinPackedMatrix * getMatrixByRow() const;
00453   
00455   virtual const CoinPackedMatrix * getMatrixByCol() const;
00456   
00458   virtual CoinPackedMatrix * getMutableMatrixByCol() const;
00459   
00461   virtual double getInfinity() const { return OsiClpInfinity; }
00463   
00466 
00467   virtual const double * getColSolution() const; 
00468   
00470   virtual const double * getRowPrice() const;
00471   
00473   virtual const double * getReducedCost() const; 
00474   
00477   virtual const double * getRowActivity() const; 
00478   
00480   virtual double getObjValue() const;
00481   
00484   virtual int getIterationCount() const 
00485   { return modelPtr_->numberIterations(); }
00486   
00498   virtual std::vector<double*> getDualRays(int maxNumRays) const;
00510   virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
00511   
00513 
00514   
00515   //---------------------------------------------------------------------------
00516   
00519   //-------------------------------------------------------------------------
00523   virtual void setObjCoeff( int elementIndex, double elementValue );
00524   
00527   virtual void setColLower( int elementIndex, double elementValue );
00528   
00531   virtual void setColUpper( int elementIndex, double elementValue );
00532   
00534   virtual void setColBounds( int elementIndex,
00535                              double lower, double upper );
00536   
00545   virtual void setColSetBounds(const int* indexFirst,
00546                                const int* indexLast,
00547                                const double* boundList);
00548   
00551   virtual void setRowLower( int elementIndex, double elementValue );
00552   
00555   virtual void setRowUpper( int elementIndex, double elementValue ) ;
00556   
00558   virtual void setRowBounds( int elementIndex,
00559                              double lower, double upper ) ;
00560   
00562   virtual void setRowType(int index, char sense, double rightHandSide,
00563                           double range);
00564   
00573   virtual void setRowSetBounds(const int* indexFirst,
00574                                const int* indexLast,
00575                                const double* boundList);
00576   
00587   virtual void setRowSetTypes(const int* indexFirst,
00588                               const int* indexLast,
00589                               const char* senseList,
00590                               const double* rhsList,
00591                               const double* rangeList);
00596     virtual void setObjective(const double * array);
00597 
00602     virtual void setColLower(const double * array);
00603 
00608     virtual void setColUpper(const double * array);
00609 
00610 //    using OsiSolverInterface::setRowName ;
00612 //    virtual void setRowName(int rowIndex, std::string & name) ;
00613     virtual void setRowName(int rowIndex, std::string  name) ;
00614     
00615 //    using OsiSolverInterface::setColName ;
00617 //    virtual void setColName(int colIndex, std::string & name) ;
00618     virtual void setColName(int colIndex, std::string  name) ;
00619     
00621   
00622   //-------------------------------------------------------------------------
00626   virtual void setContinuous(int index);
00628   virtual void setInteger(int index);
00631   virtual void setContinuous(const int* indices, int len);
00634   virtual void setInteger(const int* indices, int len);
00636   inline int numberSOS() const
00637   { return numberSOS_;}
00639   inline const CoinSet * setInfo() const
00640   { return setInfo_;}
00650   virtual int findIntegersAndSOS(bool justCount);
00652   
00653   //-------------------------------------------------------------------------
00655   virtual void setObjSense(double s ) 
00656   { modelPtr_->setOptimizationDirection( s < 0 ? -1 : 1); }
00657   
00668   virtual void setColSolution(const double * colsol);
00669   
00680   virtual void setRowPrice(const double * rowprice);
00681   
00682   //-------------------------------------------------------------------------
00687 
00688   using OsiSolverInterface::addCol ;
00690   virtual void addCol(const CoinPackedVectorBase& vec,
00691                       const double collb, const double colub,   
00692                       const double obj);
00694   virtual void addCol(int numberElements, const int * rows, const double * elements,
00695                       const double collb, const double colub,   
00696                       const double obj) ;
00698   virtual void addCols(const int numcols,
00699                        const CoinPackedVectorBase * const * cols,
00700                        const double* collb, const double* colub,   
00701                        const double* obj);
00703   virtual void addCols(const int numcols,
00704                        const int * columnStarts, const int * rows, const double * elements,
00705                        const double* collb, const double* colub,   
00706                        const double* obj);
00708   virtual void deleteCols(const int num, const int * colIndices);
00709   
00710   using OsiSolverInterface::addRow ;
00712   virtual void addRow(const CoinPackedVectorBase& vec,
00713                       const double rowlb, const double rowub);
00715   virtual void addRow(const CoinPackedVectorBase& vec,
00716                       const char rowsen, const double rowrhs,   
00717                       const double rowrng);
00719   virtual void addRow(int numberElements, const int * columns, const double * element,
00720                       const double rowlb, const double rowub) ;
00722   virtual void addRows(const int numrows,
00723                        const CoinPackedVectorBase * const * rows,
00724                        const double* rowlb, const double* rowub);
00726   virtual void addRows(const int numrows,
00727                        const CoinPackedVectorBase * const * rows,
00728                        const char* rowsen, const double* rowrhs,   
00729                        const double* rowrng);
00730 
00732   virtual void addRows(const int numrows,
00733                        const int * rowStarts, const int * columns, const double * element,
00734                        const double* rowlb, const double* rowub);
00736   void modifyCoefficient(int row, int column, double newElement,
00737                         bool keepZero=false)
00738         {modelPtr_->modifyCoefficient(row,column,newElement, keepZero);}
00739 
00741   virtual void deleteRows(const int num, const int * rowIndices);
00744   virtual void saveBaseModel() ;
00748   virtual void restoreBaseModel(int numberRows);
00749   
00750   //-----------------------------------------------------------------------
00754   virtual void applyRowCuts(int numberCuts, const OsiRowCut * cuts);
00759   virtual void applyRowCuts(int numberCuts, const OsiRowCut ** cuts);
00761 
00762   
00763   //---------------------------------------------------------------------------
00764   
00765 public:
00766   
00780   virtual void loadProblem(const CoinPackedMatrix& matrix,
00781                            const double* collb, const double* colub,   
00782                            const double* obj,
00783                            const double* rowlb, const double* rowub);
00784   
00792   virtual void assignProblem(CoinPackedMatrix*& matrix,
00793                              double*& collb, double*& colub, double*& obj,
00794                              double*& rowlb, double*& rowub);
00795   
00808   virtual void loadProblem(const CoinPackedMatrix& matrix,
00809                            const double* collb, const double* colub,
00810                            const double* obj,
00811                            const char* rowsen, const double* rowrhs,   
00812                            const double* rowrng);
00813   
00821   virtual void assignProblem(CoinPackedMatrix*& matrix,
00822                              double*& collb, double*& colub, double*& obj,
00823                              char*& rowsen, double*& rowrhs,
00824                              double*& rowrng);
00825   
00828   virtual void loadProblem(const int numcols, const int numrows,
00829                            const CoinBigIndex * start, const int* index,
00830                            const double* value,
00831                            const double* collb, const double* colub,   
00832                            const double* obj,
00833                            const double* rowlb, const double* rowub);
00834   
00837   virtual void loadProblem(const int numcols, const int numrows,
00838                            const CoinBigIndex * start, const int* index,
00839                            const double* value,
00840                            const double* collb, const double* colub,   
00841                            const double* obj,
00842                            const char* rowsen, const double* rowrhs,   
00843                            const double* rowrng);
00845   virtual int loadFromCoinModel (  CoinModel & modelObject, bool keepSolution=false);
00846 
00847   using OsiSolverInterface::readMps ;
00850   virtual int readMps(const char *filename,
00851                       const char *extension = "mps") ;
00854   int readMps(const char *filename,bool keepNames,bool allowErrors);
00855   
00860   virtual void writeMps(const char *filename,
00861                         const char *extension = "mps",
00862                         double objSense=0.0) const;
00871   virtual int writeMpsNative(const char *filename, 
00872                              const char ** rowNames, const char ** columnNames,
00873                              int formatType=0,int numberAcross=2,
00874                              double objSense=0.0) const ;
00876   virtual int readLp(const char *filename, const double epsilon = 1e-5);
00882   virtual void writeLp(const char *filename,
00883                        const char *extension = "lp",
00884                        double epsilon = 1e-5,
00885                        int numberAcross = 10,
00886                        int decimals = 5,
00887                        double objSense = 0.0,
00888                        bool useRowNames = true) const;
00893   virtual void writeLp(FILE *fp,
00894                double epsilon = 1e-5,
00895                int numberAcross = 10,
00896                int decimals = 5,
00897                double objSense = 0.0,
00898                bool useRowNames = true) const;
00904   virtual void replaceMatrixOptional(const CoinPackedMatrix & matrix);
00906   virtual void replaceMatrix(const CoinPackedMatrix & matrix) ;
00908   
00919   virtual void passInMessageHandler(CoinMessageHandler * handler);
00921   void newLanguage(CoinMessages::Language language);
00922   void setLanguage(CoinMessages::Language language)
00923   {newLanguage(language);}
00925   void setLogLevel(int value);
00927   void generateCpp( FILE * fp);
00929   //---------------------------------------------------------------------------
00930   
00933 
00934   ClpSimplex * getModelPtr() const ;
00936   inline ClpSimplex * swapModelPtr(ClpSimplex * newModel)
00937   { ClpSimplex * model = modelPtr_; modelPtr_=newModel;return model;}
00939   inline unsigned int specialOptions() const
00940   { return specialOptions_;}
00941   void setSpecialOptions(unsigned int value);
00943   inline int lastAlgorithm() const
00944   { return lastAlgorithm_;}
00946   inline int cleanupScaling() const
00947   { return cleanupScaling_;}
00960   inline void setCleanupScaling(int value)
00961   { cleanupScaling_=value;}
00964   inline double smallestElementInCut() const
00965   { return smallestElementInCut_;}
00968   inline void setSmallestElementInCut(double value)
00969   { smallestElementInCut_=value;}
00976   inline double smallestChangeInCut() const
00977   { return smallestChangeInCut_;}
00984   inline void setSmallestChangeInCut(double value)
00985   { smallestChangeInCut_=value;}
00987   inline void setSolveOptions(const ClpSolve & options)
00988   { solveOptions_ = options;}
00992   virtual int tightenBounds(int lightweight=0);
00994   virtual CoinBigIndex getSizeL() const;
00996   virtual CoinBigIndex getSizeU() const;
00998   const OsiClpDisasterHandler * disasterHandler() const
00999   { return disasterHandler_;}
01001   void passInDisasterHandler(OsiClpDisasterHandler * handler);
01003   ClpLinearObjective * fakeObjective() const
01004   { return fakeObjective_;}
01006   void setFakeObjective(ClpLinearObjective * fakeObjective);
01008   void setFakeObjective(double * fakeObjective);
01010   
01011   //---------------------------------------------------------------------------
01012   
01015 
01016   OsiClpSolverInterface ();
01017   
01019   virtual OsiSolverInterface * clone(bool copyData = true) const;
01020   
01022   OsiClpSolverInterface (const OsiClpSolverInterface &);
01023   
01025   OsiClpSolverInterface (ClpSimplex * rhs, bool reallyOwn=false);
01026   
01028   void releaseClp();
01029   
01031   OsiClpSolverInterface & operator=(const OsiClpSolverInterface& rhs);
01032   
01034   virtual ~OsiClpSolverInterface ();
01035   
01037   virtual void reset();
01039   
01040   //---------------------------------------------------------------------------
01041   
01042 protected:
01044 
01045 
01046   virtual void applyRowCut(const OsiRowCut& rc);
01047   
01049   virtual void applyColCut(const OsiColCut& cc);
01051   
01052   //---------------------------------------------------------------------------
01053   
01054 protected:
01057 
01058   void gutsOfDestructor();
01059   
01061   void freeCachedResults() const;
01062   
01064   void freeCachedResults0() const;
01065   
01067   void freeCachedResults1() const;
01068   
01070   void extractSenseRhsRange() const;
01071   
01073   void fillParamMaps();
01082   CoinWarmStartBasis getBasis(ClpSimplex * model) const;
01091   void setBasis( const CoinWarmStartBasis & basis, ClpSimplex * model);
01093   void crunch();
01095   void redoScaleFactors(int numberRows,const CoinBigIndex * starts,
01096                         const int * indices, const double * elements);
01097 public:
01100   void setBasis( const CoinWarmStartBasis & basis);
01102   inline void setBasis( )
01103   { setBasis(basis_,modelPtr_);}
01105   CoinWarmStartDiff * getBasisDiff(const unsigned char * statusArray) const ;
01107   CoinWarmStartBasis * getBasis(const unsigned char * statusArray) const ;
01109   void deleteScaleFactors();
01111   inline const double * upRange() const
01112   { return rowActivity_;}
01113   inline const double * downRange() const
01114   { return columnActivity_;}
01116   inline void passInRanges(int * array)
01117   { whichRange_=array;}
01119   void setSOSData(int numberSOS,const char * type,
01120                   const int * start,const int * indices, const double * weights=NULL);
01122   void computeLargestAway();
01124   inline double largestAway() const
01125   { return largestAway_;}
01127   inline void setLargestAway(double value)
01128   { largestAway_ = value;}
01130   void lexSolve();
01131 protected:
01133   
01136 
01137   mutable ClpSimplex * modelPtr_;
01139   double * linearObjective_;
01142 
01143   mutable char    *rowsense_;
01144   
01146   mutable double  *rhs_;
01147   
01151   mutable double  *rowrange_;
01152   
01155   mutable CoinWarmStartBasis* ws_;
01158   mutable double * rowActivity_;
01159   mutable double * columnActivity_;
01161   ClpNodeStuff stuff_;
01163   int numberSOS_;
01165   CoinSet * setInfo_;
01167   ClpSimplex * smallModel_;
01169   ClpFactorization * factorization_;
01172   double smallestElementInCut_;
01176   double smallestChangeInCut_;
01178   double largestAway_;
01180   char * spareArrays_;
01182   CoinWarmStartBasis basis_;
01184   int itlimOrig_;
01185   
01187   mutable int lastAlgorithm_;
01188   
01190   bool notOwned_;
01191   
01193   mutable CoinPackedMatrix *matrixByRow_;  
01194   
01196   char * integerInformation_;
01197   
01202   int * whichRange_;
01203 
01204   //std::map<OsiIntParam, ClpIntParam> intParamMap_;
01205   //std::map<OsiDblParam, ClpDblParam> dblParamMap_;
01206   //std::map<OsiStrParam, ClpStrParam> strParamMap_;
01207   
01209   mutable ClpDataSave saveData_;
01211   ClpSolve solveOptions_;
01224   int cleanupScaling_;
01245   mutable unsigned int specialOptions_;
01247   ClpSimplex * baseModel_;
01249   int lastNumberRows_;
01251   ClpSimplex * continuousModel_;
01253   OsiClpDisasterHandler * disasterHandler_ ;
01255   ClpLinearObjective * fakeObjective_;
01257   CoinDoubleArrayWithLength rowScale_; 
01259   CoinDoubleArrayWithLength columnScale_; 
01261 };
01262   
01263 class OsiClpDisasterHandler : public ClpDisasterHandler {
01264 public:
01268 
01269   virtual void intoSimplex();
01271   virtual bool check() const ;
01273   virtual void saveInfo();
01275   virtual int typeOfDisaster();
01277   
01278   
01283   OsiClpDisasterHandler(OsiClpSolverInterface * model = NULL);
01285   virtual ~OsiClpDisasterHandler();
01286   // Copy
01287   OsiClpDisasterHandler(const OsiClpDisasterHandler&);
01288   // Assignment
01289   OsiClpDisasterHandler& operator=(const OsiClpDisasterHandler&);
01291   virtual ClpDisasterHandler * clone() const;
01292 
01294   
01299   void setOsiModel(OsiClpSolverInterface * model);
01301   inline OsiClpSolverInterface * osiModel() const
01302   { return osiModel_;}
01304   inline void setWhereFrom(int value)
01305   { whereFrom_=value;}
01307   inline int whereFrom() const
01308   { return whereFrom_;}
01310   inline void setPhase(int value)
01311   { phase_=value;}
01313   inline int phase() const
01314   { return phase_;}
01316   inline bool inTrouble() const
01317   { return inTrouble_;}
01318   
01320   
01321   
01322 protected:
01326 
01327   OsiClpSolverInterface * osiModel_;
01335   int whereFrom_;
01343   int phase_;
01345   bool inTrouble_;
01346       
01348 };
01349 
01350 //#############################################################################
01358 int
01359 OsiClpSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
01360 #endif

Generated on Mon Jun 8 03:02:38 2009 by  doxygen 1.4.7