/home/coin/SVN-release/Vol-1.2.0/Osi/test/OsiTestSolverInterface.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 // this is a copy of OsiVolSolverInterface (trunk rev. 1466) renamed to OsiTestSolverInterface
00005 
00006 #ifndef OsiTestSolverInterface_H
00007 #define OsiTestSolverInterface_H
00008 
00009 #include <string>
00010 
00011 #include "OsiTestSolver.hpp"
00012 
00013 #include "CoinPackedMatrix.hpp"
00014 
00015 #include "OsiSolverInterface.hpp"
00016 
00017 static const double OsiTestInfinity = 1.0e31;
00018 
00019 //#############################################################################
00020 
00026 class OsiTestSolverInterface :
00027    virtual public OsiSolverInterface, public VOL_user_hooks {
00028    friend int OsiTestSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00029 
00030 private:
00031   class OsiVolMatrixOneMinusOne_ {
00032     int majorDim_;
00033     int minorDim_;
00034 
00035     int plusSize_;
00036     int * plusInd_;
00037     int * plusStart_;
00038     int * plusLength_;
00039 
00040     int minusSize_;
00041     int * minusInd_;
00042     int * minusStart_;
00043     int * minusLength_;
00044 
00045   public:
00046     OsiVolMatrixOneMinusOne_(const CoinPackedMatrix& m);
00047     ~OsiVolMatrixOneMinusOne_();
00048     void timesMajor(const double* x, double* y) const;
00049   };
00050 
00051 public:
00052   //---------------------------------------------------------------------------
00055 
00056     virtual void initialSolve();
00057 
00059     virtual void resolve();
00060 
00062     virtual void branchAndBound() {
00063       throw CoinError("Sorry, the Volume Algorithm doesn't implement B&B",
00064                      "branchAndBound", "OsiTestSolverInterface");
00065     }
00067 
00068   //---------------------------------------------------------------------------
00084     // Set an integer parameter
00085     bool setIntParam(OsiIntParam key, int value);
00086     // Set an double parameter
00087     bool setDblParam(OsiDblParam key, double value);
00088     // Set a string parameter
00089     bool setStrParam(OsiStrParam key, const std::string & value);
00090     // Get an integer parameter
00091     bool getIntParam(OsiIntParam key, int& value) const;
00092     // Get an double parameter
00093     bool getDblParam(OsiDblParam key, double& value) const;
00094     // Get a string parameter
00095     bool getStrParam(OsiStrParam key, std::string& value) const;
00097 
00098   //---------------------------------------------------------------------------
00100 
00101 
00102     virtual bool isAbandoned() const;
00104     virtual bool isProvenOptimal() const;
00106     virtual bool isProvenPrimalInfeasible() const;
00108     virtual bool isProvenDualInfeasible() const;
00110     virtual bool isPrimalObjectiveLimitReached() const;
00112     virtual bool isDualObjectiveLimitReached() const;
00114     virtual bool isIterationLimitReached() const;
00116 
00117   //---------------------------------------------------------------------------
00126     virtual CoinWarmStart *getEmptyWarmStart () const ;
00127 
00129     virtual CoinWarmStart* getWarmStart() const;
00132     virtual bool setWarmStart(const CoinWarmStart* warmstart);
00134 
00135   //---------------------------------------------------------------------------
00142 
00143     virtual void markHotStart();
00145     virtual void solveFromHotStart();
00147     virtual void unmarkHotStart();
00149 
00150   //---------------------------------------------------------------------------
00165 
00166       virtual int getNumCols() const {
00167         return rowMatrixCurrent_?
00168         rowMatrix_.getNumCols() : colMatrix_.getNumCols(); }
00169   
00171       virtual int getNumRows() const {
00172         return rowMatrixCurrent_?
00173         rowMatrix_.getNumRows() : colMatrix_.getNumRows(); }
00174   
00176       virtual int getNumElements() const {
00177         return rowMatrixCurrent_?
00178         rowMatrix_.getNumElements() : colMatrix_.getNumElements(); }
00179   
00181       virtual const double * getColLower() const { return collower_; }
00182   
00184       virtual const double * getColUpper() const { return colupper_; }
00185   
00195       virtual const char * getRowSense() const { return rowsense_; }
00196   
00205       virtual const double * getRightHandSide() const { return rhs_; }
00206   
00215       virtual const double * getRowRange() const { return rowrange_; }
00216   
00218       virtual const double * getRowLower() const { return rowlower_; }
00219   
00221       virtual const double * getRowUpper() const { return rowupper_; }
00222   
00224       virtual const double * getObjCoefficients() const { return objcoeffs_; }
00225   
00227       virtual double getObjSense() const { return objsense_; }
00228   
00230       virtual bool isContinuous(int colNumber) const;
00231   
00232 #if 0
00234       virtual bool isBinary(int colNumber) const;
00235   
00240       virtual bool isInteger(int colNumber) const;
00241   
00243       virtual bool isIntegerNonBinary(int colNumber) const;
00244   
00246       virtual bool isFreeBinary(int colNumber) const;
00247 #endif
00248   
00250       virtual const CoinPackedMatrix * getMatrixByRow() const;
00251   
00253       virtual const CoinPackedMatrix * getMatrixByCol() const;
00254   
00256       virtual double getInfinity() const { return OsiTestInfinity; }
00258     
00261 
00262       virtual const double * getColSolution() const { return colsol_; }
00263   
00265       virtual const double * getRowPrice() const { return rowprice_; }
00266   
00268       virtual const double * getReducedCost() const { return rc_; }
00269   
00272       virtual const double * getRowActivity() const { return lhs_; }
00273   
00275       virtual double getObjValue() const { 
00276 #if 1
00277         // This does not pass unitTest if getObjValue is called before solve
00278         return lagrangeanCost_;
00279 #else
00280         return OsiSolverInterface::getObjValue();
00281 #endif
00282       }
00283   
00286       virtual int getIterationCount() const { return volprob_.iter(); }
00287   
00305       virtual std::vector<double*> getDualRays(int maxNumRays,
00306                                                bool fullRay = false) const;
00318       virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
00319   
00320 #if 0
00321 
00323       virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05)
00324         const;
00325 #endif
00326 
00327 
00328 
00329   //---------------------------------------------------------------------------
00330 
00333     //-------------------------------------------------------------------------
00337       virtual void setObjCoeff( int elementIndex, double elementValue ) {
00338         objcoeffs_[elementIndex] = elementValue;
00339       }
00340 
00341       using OsiSolverInterface::setColLower ;
00344       virtual void setColLower( int elementIndex, double elementValue ) {
00345         collower_[elementIndex] = elementValue;
00346       }
00347       
00348       using OsiSolverInterface::setColUpper ;
00351       virtual void setColUpper( int elementIndex, double elementValue ) {
00352         colupper_[elementIndex] = elementValue;
00353       }
00354 
00356       virtual void setColBounds( int elementIndex,
00357                                  double lower, double upper ) {
00358         collower_[elementIndex] = lower;
00359         colupper_[elementIndex] = upper;
00360       }
00361 
00370       virtual void setColSetBounds(const int* indexFirst,
00371                                    const int* indexLast,
00372                                    const double* boundList);
00373       
00376       virtual void setRowLower( int elementIndex, double elementValue ) {
00377         rowlower_[elementIndex] = elementValue;
00378         convertBoundToSense(elementValue, rowupper_[elementIndex],
00379                             rowsense_[elementIndex], rhs_[elementIndex],
00380                             rowrange_[elementIndex]);
00381       }
00382       
00385       virtual void setRowUpper( int elementIndex, double elementValue ) {
00386         rowupper_[elementIndex] = elementValue;
00387         convertBoundToSense(rowlower_[elementIndex], elementValue,
00388                             rowsense_[elementIndex], rhs_[elementIndex],
00389                             rowrange_[elementIndex]);
00390       }
00391     
00393       virtual void setRowBounds( int elementIndex,
00394                                  double lower, double upper ) {
00395         rowlower_[elementIndex] = lower;
00396         rowupper_[elementIndex] = upper;
00397         convertBoundToSense(lower, upper,
00398                             rowsense_[elementIndex], rhs_[elementIndex],
00399                             rowrange_[elementIndex]);
00400       }
00401     
00403       virtual void setRowType(int index, char sense, double rightHandSide,
00404                               double range) {
00405         rowsense_[index] = sense;
00406         rhs_[index] = rightHandSide;
00407         rowrange_[index] = range;
00408         convertSenseToBound(sense, rightHandSide, range,
00409                             rowlower_[index], rowupper_[index]);
00410       }
00411     
00420       virtual void setRowSetBounds(const int* indexFirst,
00421                                    const int* indexLast,
00422                                    const double* boundList);
00423     
00434       virtual void setRowSetTypes(const int* indexFirst,
00435                                   const int* indexLast,
00436                                   const char* senseList,
00437                                   const double* rhsList,
00438                                   const double* rangeList);
00440     
00441     //-------------------------------------------------------------------------
00445       virtual void setContinuous(int index);
00447       virtual void setInteger(int index);
00450       virtual void setContinuous(const int* indices, int len);
00453       virtual void setInteger(const int* indices, int len);
00455     
00456     //-------------------------------------------------------------------------
00458     virtual void setObjSense(double s ) { objsense_ = s < 0 ? -1.0 : 1.0; }
00459     
00470     virtual void setColSolution(const double * colsol);
00471     
00482     virtual void setRowPrice(const double * rowprice);
00483 
00484     //-------------------------------------------------------------------------
00489 
00490       using OsiSolverInterface::addCol ;
00492       virtual void addCol(const CoinPackedVectorBase& vec,
00493                              const double collb, const double colub,   
00494                              const double obj);
00495 
00496       using OsiSolverInterface::addCols ;
00498       virtual void addCols(const int numcols,
00499                            const CoinPackedVectorBase * const * cols,
00500                            const double* collb, const double* colub,   
00501                            const double* obj);
00502 #if 0
00503 
00504       virtual void addCols(const CoinPackedMatrix& matrix,
00505                            const double* collb, const double* colub,   
00506                            const double* obj);
00507 #endif
00508 
00509       virtual void deleteCols(const int num, const int * colIndices);
00510     
00511       using OsiSolverInterface::addRow ;
00513       virtual void addRow(const CoinPackedVectorBase& vec,
00514                           const double rowlb, const double rowub);
00516       virtual void addRow(const CoinPackedVectorBase& vec,
00517                           const char rowsen, const double rowrhs,   
00518                           const double rowrng);
00519 
00520       using OsiSolverInterface::addRows ;
00522       virtual void addRows(const int numrows,
00523                            const CoinPackedVectorBase * const * rows,
00524                            const double* rowlb, const double* rowub);
00526       virtual void addRows(const int numrows,
00527                            const CoinPackedVectorBase * const * rows,
00528                            const char* rowsen, const double* rowrhs,   
00529                            const double* rowrng);
00530 #if 0
00531 
00532       virtual void addRows(const CoinPackedMatrix& matrix,
00533                            const double* rowlb, const double* rowub);
00535       virtual void addRows(const CoinPackedMatrix& matrix,
00536                            const char* rowsen, const double* rowrhs,   
00537                            const double* rowrng);
00538 #endif
00539 
00540       virtual void deleteRows(const int num, const int * rowIndices);
00541     
00542       //-----------------------------------------------------------------------
00543 #if 0
00544 
00565       virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs,
00566                                             double effectivenessLb = 0.0);
00567 #endif
00568 
00569 
00570 
00571   //---------------------------------------------------------------------------
00572 
00573 protected:
00575   void initFromRlbRub(const int rownum,
00576                       const double* rowlb, const double* rowub);
00577   void initFromRhsSenseRange(const int rownum, const char* rowsen,
00578                              const double* rowrhs, const double* rowrng);
00579   void initFromClbCubObj(const int colnum, const double* collb,
00580                          const double* colub, const double* obj);
00581 public:
00582    
00596     virtual void loadProblem(const CoinPackedMatrix& matrix,
00597                              const double* collb, const double* colub,   
00598                              const double* obj,
00599                              const double* rowlb, const double* rowub);
00600                             
00608     virtual void assignProblem(CoinPackedMatrix*& matrix,
00609                              double*& collb, double*& colub, double*& obj,
00610                              double*& rowlb, double*& rowub);
00611                             
00624     virtual void loadProblem(const CoinPackedMatrix& matrix,
00625                            const double* collb, const double* colub,
00626                            const double* obj,
00627                            const char* rowsen, const double* rowrhs,   
00628                            const double* rowrng);
00629     
00637     virtual void assignProblem(CoinPackedMatrix*& matrix,
00638                              double*& collb, double*& colub, double*& obj,
00639                              char*& rowsen, double*& rowrhs,
00640                              double*& rowrng);
00641 
00644     virtual void loadProblem(const int numcols, const int numrows,
00645                              const int* start, const int* index,
00646                              const double* value,
00647                              const double* collb, const double* colub,   
00648                              const double* obj,
00649                              const double* rowlb, const double* rowub);
00650 
00653     virtual void loadProblem(const int numcols, const int numrows,
00654                              const int* start, const int* index,
00655                              const double* value,
00656                              const double* collb, const double* colub,   
00657                              const double* obj,
00658                              const char* rowsen, const double* rowrhs,   
00659                              const double* rowrng);
00660     
00661     using OsiSolverInterface::readMps ;
00663     virtual int readMps(const char *filename,
00664                          const char *extension = "mps");
00665 
00670     virtual void writeMps(const char *filename,
00671                           const char *extension = "mps",
00672                           double objSense=0.0) const;
00674 
00675   //---------------------------------------------------------------------------
00676 
00679 
00680     VOL_problem* volprob() { return &volprob_; }
00682 
00683   //---------------------------------------------------------------------------
00684 
00687 
00688     OsiTestSolverInterface ();
00689     
00691     virtual OsiSolverInterface * clone(bool copyData = true) const;
00692     
00694     OsiTestSolverInterface (const OsiTestSolverInterface &);
00695     
00697     OsiTestSolverInterface & operator=(const OsiTestSolverInterface& rhs);
00698     
00700     virtual ~OsiTestSolverInterface ();
00702 
00703   //---------------------------------------------------------------------------
00704 
00705 protected:
00707 
00708 
00709     virtual void applyRowCut(const OsiRowCut& rc);
00710 
00712     virtual void applyColCut(const OsiColCut& cc);
00714 
00715   //---------------------------------------------------------------------------
00716 
00717 private:
00720 
00721     virtual int compute_rc(const VOL_dvector& u, VOL_dvector& rc);
00723     virtual int solve_subproblem(const VOL_dvector& dual,
00724                                  const VOL_dvector& rc,
00725                                  double& lcost, VOL_dvector& x, VOL_dvector& v,
00726                                  double& pcost);
00729   virtual int heuristics(const VOL_problem& /*p*/, 
00730                          const VOL_dvector& /*x*/, double& heur_val) {
00731       heur_val = DBL_MAX;
00732       return 0;
00733     }
00735 
00736   //---------------------------------------------------------------------------
00737 
00738 private:
00742     void updateRowMatrix_() const;
00744     void updateColMatrix_() const;
00745 
00748     void checkData_() const;
00751     void compute_rc_(const double* u, double* rc) const;
00753     void gutsOfDestructor_();
00754 
00757     void rowRimAllocator_();
00760     void colRimAllocator_();
00761 
00763     void rowRimResize_(const int newSize);
00765     void colRimResize_(const int newSize);
00766 
00768     void convertBoundsToSenses_();
00770     void convertSensesToBounds_();
00771 
00773     bool test_zero_one_minusone_(const CoinPackedMatrix& m) const;
00775 
00776   //---------------------------------------------------------------------------
00777 
00778 private:
00779   
00780   //---------------------------------------------------------------------------
00784 
00785     mutable bool rowMatrixCurrent_;
00787     mutable CoinPackedMatrix rowMatrix_;
00789     mutable bool colMatrixCurrent_;
00791     mutable CoinPackedMatrix colMatrix_;
00793 
00794   //---------------------------------------------------------------------------
00797 
00798     bool isZeroOneMinusOne_;
00800     OsiVolMatrixOneMinusOne_* rowMatrixOneMinusOne_;
00802     OsiVolMatrixOneMinusOne_* colMatrixOneMinusOne_;
00804 
00805   //---------------------------------------------------------------------------
00808 
00809     double  *colupper_;
00811     double  *collower_;
00813     bool    *continuous_;
00815     double  *rowupper_;
00817     double  *rowlower_;
00819     char    *rowsense_;
00821     double  *rhs_;
00824     double  *rowrange_;
00826     double  *objcoeffs_;
00828 
00829   //---------------------------------------------------------------------------
00831   double  objsense_;
00832 
00833   //---------------------------------------------------------------------------
00836 
00837     double  *colsol_;
00839     double  *rowprice_;
00841     double  *rc_;
00843     double  *lhs_;
00845     double   lagrangeanCost_;
00847 
00848   //---------------------------------------------------------------------------
00851   double  *rowpriceHotStart_;
00852 
00854   int maxNumrows_;
00856   int maxNumcols_;
00857 
00859   VOL_problem volprob_;
00860 };
00861 
00862 //#############################################################################
00870 int
00871 OsiTestSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00872 
00873 #endif

Generated on Fri Nov 5 03:15:55 2010 by  doxygen 1.4.7