Osi trunk
OsiMskSolverInterface.hpp
Go to the documentation of this file.
00001 // Osi interface for Mosek ver. 5.0
00002 // Lower versions are not supported
00003 //-----------------------------------------------------------------------------
00004 // name:     OSI Interface for MOSEK
00005 // author:   Bo Jensen
00006 // email:    support@MOSEK.com
00007 //-----------------------------------------------------------------------------
00008 
00009 // This code is licensed under the terms of the Eclipse Public License (EPL).
00010 
00011 #ifndef OsiMskSolverInterface_H
00012 #define OsiMskSolverInterface_H
00013 
00014 #include "OsiSolverInterface.hpp"
00015 
00016 typedef void* MSKtask_t;
00017 typedef void* MSKenv_t;
00018 
00019 /* MOSEK Solver Interface
00020    Instantiation of OsiMskSolverInterface for MOSEK
00021 */
00022 
00023 class OsiMskSolverInterface : 
00024   virtual public OsiSolverInterface {
00025   friend void OsiMskSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00026 public:
00027   
00028   //---------------------------------------------------------------------------
00031 
00032   virtual void initialSolve();
00033   
00035   virtual void resolve();
00036   
00038   virtual void branchAndBound();
00040   
00041   //---------------------------------------------------------------------------
00057     // Set an integer parameter
00058     bool setIntParam(OsiIntParam key, int value);
00059     // Set an double parameter
00060     bool setDblParam(OsiDblParam key, double value);
00061     // Set a string parameter
00062     bool setStrParam(OsiStrParam key, const std::string & value);
00063     // Get an integer parameter
00064     bool getIntParam(OsiIntParam key, int& value) const;
00065     // Get an double parameter
00066     bool getDblParam(OsiDblParam key, double& value) const;
00067     // Get a string parameter
00068     bool getStrParam(OsiStrParam key, std::string& value) const;
00070 
00071   //---------------------------------------------------------------------------
00073 
00074 
00075     virtual bool isAbandoned() const;
00077     virtual bool isProvenOptimal() const;
00079     virtual bool isProvenPrimalInfeasible() const;
00081     virtual bool isProvenDualInfeasible() const;
00083     virtual bool isPrimalObjectiveLimitReached() const;
00085     virtual bool isDualObjectiveLimitReached() const;
00087     virtual bool isIterationLimitReached() const;
00089 
00090   //---------------------------------------------------------------------------
00099   CoinWarmStart* getEmptyWarmStart () const;
00100 
00102 
00103     virtual CoinWarmStart* getWarmStart() const;
00106     virtual bool setWarmStart(const CoinWarmStart* warmstart);
00108 
00109   //---------------------------------------------------------------------------
00116 
00117     virtual void markHotStart();
00119     virtual void solveFromHotStart();
00121     virtual void unmarkHotStart();
00123 
00124   //---------------------------------------------------------------------------
00139 
00140       virtual int getNumCols() const;
00141   
00143       virtual int getNumRows() const;
00144   
00146       virtual int getNumElements() const;
00147   
00149       virtual const double * getColLower() const;
00150   
00152       virtual const double * getColUpper() const;
00153   
00163       virtual const char * getRowSense() const;
00164   
00173       virtual const double * getRightHandSide() const;
00174   
00183       virtual const double * getRowRange() const;
00184   
00186       virtual const double * getRowLower() const;
00187   
00189       virtual const double * getRowUpper() const;
00190   
00192       virtual const double * getObjCoefficients() const;
00193   
00195       virtual double getObjSense() const;
00196 
00198       virtual bool isContinuous(int colNumber) const;
00199 
00200 #if 0
00201 
00202       virtual bool isBinary(int columnNumber) const;
00203   
00208       virtual bool isInteger(int columnNumber) const;
00209   
00211       virtual bool isIntegerNonBinary(int columnNumber) const;
00212   
00214       virtual bool isFreeBinary(int columnNumber) const;
00215 #endif
00216   
00218       virtual const CoinPackedMatrix * getMatrixByRow() const;
00219   
00221       virtual const CoinPackedMatrix * getMatrixByCol() const;
00222   
00224       virtual double getInfinity() const;
00226     
00229 
00230       virtual const double * getColSolution() const;
00231   
00233       virtual const double * getRowPrice() const;
00234   
00236       virtual const double * getReducedCost() const;
00237   
00240       virtual const double * getRowActivity() const;
00241   
00243       virtual double getObjValue() const;
00244   
00247       virtual int getIterationCount() const;
00248   
00266       virtual std::vector<double*> getDualRays(int maxNumRays,
00267                                                bool fullRay=false) const;
00279       virtual std::vector<double*> getPrimalRays(int maxNumRays) const;
00280   
00281 #if 0
00282 
00284       virtual OsiVectorInt getFractionalIndices(const double etol=1.e-05)
00285         const;
00286 #endif
00287 
00288 
00289   
00290   //---------------------------------------------------------------------------
00291 
00294     //-------------------------------------------------------------------------
00298       virtual void setObjCoeff( int elementIndex, double elementValue );
00299 
00301       virtual void setObjCoeffSet(const int* indexFirst,
00302                                   const int* indexLast,
00303                                   const double* coeffList);
00304 
00307       virtual void setColLower( int elementIndex, double elementValue );
00308       
00311       virtual void setColUpper( int elementIndex, double elementValue );
00312       
00316       virtual void setColBounds( int elementIndex,
00317                                  double lower, double upper );
00318     
00326       virtual void setColSetBounds(const int* indexFirst,
00327                                    const int* indexLast,
00328                                    const double* boundList);
00329       
00332       virtual void setRowLower( int elementIndex, double elementValue );
00333       
00336       virtual void setRowUpper( int elementIndex, double elementValue );
00337     
00341       virtual void setRowBounds( int elementIndex,
00342                                  double lower, double upper );
00343     
00345       virtual void setRowType(int index, char sense, double rightHandSide,
00346                               double range);
00347     
00355       virtual void setRowSetBounds(const int* indexFirst,
00356                                    const int* indexLast,
00357                                    const double* boundList);
00358     
00368       virtual void setRowSetTypes(const int* indexFirst,
00369                                   const int* indexLast,
00370                                   const char* senseList,
00371                                   const double* rhsList,
00372                                   const double* rangeList);
00374     
00375     //-------------------------------------------------------------------------
00379       virtual void setContinuous(int index);
00381       virtual void setInteger(int index);
00384       virtual void setContinuous(const int* indices, int len);
00387       virtual void setInteger(const int* indices, int len);
00389     
00390     //-------------------------------------------------------------------------
00392     virtual void setObjSense(double s);
00393     
00404     virtual void setColSolution(const double * colsol);
00405     
00416     virtual void setRowPrice(const double * rowprice);
00417     
00418     //-------------------------------------------------------------------------
00424       virtual void addCol(const CoinPackedVectorBase& vec,
00425                           const double collb, const double colub,   
00426                           const double obj);
00428       virtual void addCols(const int numcols,
00429                            const CoinPackedVectorBase * const * cols,
00430                            const double* collb, const double* colub,   
00431                            const double* obj);
00433       virtual void deleteCols(const int num, const int * colIndices);
00434     
00436       virtual void addRow(const CoinPackedVectorBase& vec,
00437                           const double rowlb, const double rowub);
00439       virtual void addRow(const CoinPackedVectorBase& vec,
00440                           const char rowsen, const double rowrhs,   
00441                           const double rowrng);
00443       virtual void addRows(const int numrows,
00444                            const CoinPackedVectorBase * const * rows,
00445                            const double* rowlb, const double* rowub);
00447       virtual void addRows(const int numrows,
00448                            const CoinPackedVectorBase * const * rows,
00449                            const char* rowsen, const double* rowrhs,   
00450                            const double* rowrng);
00452       virtual void deleteRows(const int num, const int * rowIndices);
00453     
00454 #if 0
00455   // ??? implemented in OsiSolverInterface
00456       //-----------------------------------------------------------------------
00478       virtual ApplyCutsReturnCode applyCuts(const OsiCuts & cs,
00479                                             double effectivenessLb = 0.0);
00480 #endif
00481 
00482 
00483 
00484   //---------------------------------------------------------------------------
00485 
00499     virtual void loadProblem(const CoinPackedMatrix& matrix,
00500                              const double* collb, const double* colub,   
00501                              const double* obj,
00502                              const double* rowlb, const double* rowub);
00503                             
00511     virtual void assignProblem(CoinPackedMatrix*& matrix,
00512                                double*& collb, double*& colub, double*& obj,
00513                                double*& rowlb, double*& rowub);
00514 
00527     virtual void loadProblem(const CoinPackedMatrix& matrix,
00528                              const double* collb, const double* colub,
00529                              const double* obj,
00530                              const char* rowsen, const double* rowrhs,   
00531                              const double* rowrng);
00532 
00540     virtual void assignProblem(CoinPackedMatrix*& matrix,
00541                                double*& collb, double*& colub, double*& obj,
00542                                char*& rowsen, double*& rowrhs,
00543                                double*& rowrng);
00544 
00547     virtual void loadProblem(const int numcols, const int numrows,
00548                              const int* start, const int* index,
00549                              const double* value,
00550                              const double* collb, const double* colub,   
00551                              const double* obj,
00552                              const double* rowlb, const double* rowub);
00553 
00556     virtual void loadProblem(const int numcols, const int numrows,
00557                              const int* start, const int* index,
00558                              const double* value,
00559                              const double* collb, const double* colub,   
00560                              const double* obj,
00561                              const char* rowsen, const double* rowrhs,   
00562                              const double* rowrng);
00563 
00565     virtual int readMps(const char *filename,
00566                          const char *extension = "mps");
00567 
00572     virtual void writeMps(const char *filename,
00573                           const char *extension = "mps",
00574                           double objSense=0.0) const;
00576 
00584     void passInMessageHandler(CoinMessageHandler * handler);
00586 
00587   //---------------------------------------------------------------------------
00588 
00594   enum keepCachedFlag
00595   {
00597     KEEPCACHED_NONE    = 0,
00599     KEEPCACHED_COLUMN  = 1,
00601     KEEPCACHED_ROW     = 2,
00603     KEEPCACHED_MATRIX  = 4,
00605     KEEPCACHED_RESULTS = 8,
00607     KEEPCACHED_PROBLEM = KEEPCACHED_COLUMN | KEEPCACHED_ROW | KEEPCACHED_MATRIX,
00609     KEEPCACHED_ALL     = KEEPCACHED_PROBLEM | KEEPCACHED_RESULTS,
00611     FREECACHED_COLUMN  = KEEPCACHED_PROBLEM & !KEEPCACHED_COLUMN,
00613     FREECACHED_ROW     = KEEPCACHED_PROBLEM & !KEEPCACHED_ROW,
00615     FREECACHED_MATRIX  = KEEPCACHED_PROBLEM & !KEEPCACHED_MATRIX,
00617     FREECACHED_RESULTS = KEEPCACHED_ALL & !KEEPCACHED_RESULTS
00618   };
00619 
00620   MSKtask_t getLpPtr( int keepCached = KEEPCACHED_NONE );
00621   
00623 
00624   MSKenv_t getEnvironmentPtr();
00626 
00628   const char* getCtype() const;
00629   
00639   static void incrementInstanceCounter(); 
00640   
00649   static void decrementInstanceCounter();
00650   
00652   static unsigned int getNumInstances();
00654 
00655   
00658 
00659 
00660 
00661   OsiMskSolverInterface(MSKenv_t mskenv = NULL); 
00662   
00664   virtual OsiSolverInterface * clone(bool copyData = true) const;
00665   
00667   OsiMskSolverInterface( const OsiMskSolverInterface& );
00668   
00670   OsiMskSolverInterface& operator=( const OsiMskSolverInterface& rhs );
00671   
00673   virtual ~OsiMskSolverInterface();
00675 
00676 protected:
00677   
00680 
00681   virtual void applyRowCut( const OsiRowCut & rc );
00682   
00686   virtual void applyColCut( const OsiColCut & cc );
00688   
00689 private:
00692 
00693   void switchToLP();
00694 
00696   void switchToMIP();
00697 
00699   void resizeColType( int minsize );
00700 
00702   void freeColType();
00703 
00704   bool definedSolution(int solution) const;
00705 
00706   int solverUsed() const;
00707 
00709 
00712 
00713   static MSKenv_t env_ ;
00714   
00716   //static unsigned int numInstances_;
00718   static unsigned int numInstances_;
00719   
00722   
00723   int Mskerr;
00724   int MSKsolverused_;
00725   double ObjOffset_;
00726 
00727   int InitialSolver;
00728 
00730   public: MSKtask_t getMutableLpPtr() const;
00731   
00733   void gutsOfCopy( const OsiMskSolverInterface & source );
00734   
00736   void gutsOfConstructor();
00737   
00739   void gutsOfDestructor();
00740 
00742   void freeCachedColRim();
00743 
00745   void freeCachedRowRim();
00746 
00748   void freeCachedResults();
00749   
00751   void freeCachedMatrix();
00752 
00754   void freeCachedData( int keepCached = KEEPCACHED_NONE );
00755 
00757   void freeAllMemory();
00758 
00760   void printBounds(); 
00763 
00764   mutable MSKtask_t task_;
00765 
00767   int *hotStartCStat_;
00768   int hotStartCStatSize_;
00769   int *hotStartRStat_;
00770   int hotStartRStatSize_;
00771   int hotStartMaxIteration_;
00772 
00775 
00776   mutable double  *obj_;
00777   
00779   mutable double  *collower_;
00780   
00782   mutable double  *colupper_;
00783   
00785   mutable char    *rowsense_;
00786   
00788   mutable double  *rhs_;
00789   
00791   mutable double  *rowrange_;
00792   
00794   mutable double  *rowlower_;
00795   
00797   mutable double  *rowupper_;
00798   
00800   mutable double  *colsol_;
00801   
00803   mutable double  *rowsol_;
00804 
00806   mutable double  *redcost_;
00807 
00809   mutable double  *rowact_;
00810 
00812   mutable CoinPackedMatrix *matrixByRow_;  
00813   
00815   mutable CoinPackedMatrix *matrixByCol_;  
00817 
00820 
00821   char            *coltype_;
00822 
00824   int             coltypesize_;
00825   
00827   mutable bool    probtypemip_;
00828 
00830 };
00831 
00832 //#############################################################################
00835 void OsiMskSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir);
00836 
00837 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines