/home/coin/SVN-release/Clp-1.6.0/Clp/src/ClpModel.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2002, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #ifndef ClpModel_H
00004 #define ClpModel_H
00005 
00006 #include "ClpConfig.h"
00007 
00008 #include <iostream>
00009 #include <cassert>
00010 #include <cmath>
00011 #include <vector>
00012 #include <string>
00013 //#ifndef COIN_USE_CLP
00014 //#define COIN_USE_CLP
00015 //#endif
00016 #include "ClpPackedMatrix.hpp"
00017 #include "CoinMessageHandler.hpp"
00018 #include "ClpParameters.hpp"
00019 #include "ClpObjective.hpp"
00020 class ClpEventHandler;
00021 
00022 // Plus infinity
00023 #ifndef COIN_DBL_MAX
00024 #define COIN_DBL_MAX DBL_MAX
00025 #endif
00026 
00036 class CoinBuild;
00037 class CoinModel;
00038 class ClpModel {
00039 
00040 public:
00041 
00047 
00048     ClpModel (bool emptyMessages=false  );
00049 
00054     ClpModel(const ClpModel & rhs, int scalingMode=-1);
00056     ClpModel & operator=(const ClpModel & rhs);
00061     ClpModel (const ClpModel * wholeModel,
00062       int numberRows, const int * whichRows,
00063       int numberColumns, const int * whichColumns,
00064               bool dropNames=true, bool dropIntegers=true);
00066     ~ClpModel (  );
00068 
00082   void loadProblem (  const ClpMatrixBase& matrix,
00083                      const double* collb, const double* colub,   
00084                      const double* obj,
00085                      const double* rowlb, const double* rowub,
00086                       const double * rowObjective=NULL);
00087   void loadProblem (  const CoinPackedMatrix& matrix,
00088                      const double* collb, const double* colub,   
00089                      const double* obj,
00090                      const double* rowlb, const double* rowub,
00091                       const double * rowObjective=NULL);
00092 
00095   void loadProblem (  const int numcols, const int numrows,
00096                      const CoinBigIndex* start, const int* index,
00097                      const double* value,
00098                      const double* collb, const double* colub,   
00099                      const double* obj,
00100                       const double* rowlb, const double* rowub,
00101                       const double * rowObjective=NULL);
00107   int loadProblem (  CoinModel & modelObject,bool tryPlusMinusOne=false);
00109   void loadProblem (  const int numcols, const int numrows,
00110                      const CoinBigIndex* start, const int* index,
00111                       const double* value,const int * length,
00112                      const double* collb, const double* colub,   
00113                      const double* obj,
00114                       const double* rowlb, const double* rowub,
00115                       const double * rowObjective=NULL);
00117   void loadQuadraticObjective(const int numberColumns, 
00118                               const CoinBigIndex * start,
00119                               const int * column, const double * element);
00120   void loadQuadraticObjective (  const CoinPackedMatrix& matrix);
00122   void deleteQuadraticObjective();
00124   void setRowObjective(const double * rowObjective);
00126   int readMps(const char *filename,
00127               bool keepNames=false,
00128               bool ignoreErrors = false);
00130   int readGMPL(const char *filename,const char * dataName,
00131                bool keepNames=false);
00133   void copyInIntegerInformation(const char * information);
00135   void deleteIntegerInformation();
00137   void setContinuous(int index);
00139   void setInteger(int index);
00141   bool isInteger(int index) const;
00143   void resize (int newNumberRows, int newNumberColumns);
00145   void deleteRows(int number, const int * which);
00147   void addRow(int numberInRow, const int * columns,
00148                const double * elements, double rowLower=-COIN_DBL_MAX, 
00149               double rowUpper=COIN_DBL_MAX);
00151   void addRows(int number, const double * rowLower, 
00152                const double * rowUpper,
00153                const CoinBigIndex * rowStarts, const int * columns,
00154                const double * elements);
00156   void addRows(int number, const double * rowLower, 
00157                const double * rowUpper,
00158                const CoinBigIndex * rowStarts, const int * rowLengths,
00159                const int * columns,
00160                const double * elements);
00161 #ifndef CLP_NO_VECTOR
00162   void addRows(int number, const double * rowLower, 
00163                const double * rowUpper,
00164                const CoinPackedVectorBase * const * rows);
00165 #endif
00166 
00171   int addRows(const CoinBuild & buildObject,bool tryPlusMinusOne=false,
00172                bool checkDuplicates=true);
00181   int addRows(CoinModel & modelObject,bool tryPlusMinusOne=false,
00182               bool checkDuplicates=true);
00183 
00185   void deleteColumns(int number, const int * which);
00187   void addColumn(int numberInColumn,
00188                  const int * rows,
00189                  const double * elements,
00190                  double columnLower=0.0, 
00191                  double  columnUpper=COIN_DBL_MAX,
00192                  double  objective=0.0);
00194   void addColumns(int number, const double * columnLower, 
00195                   const double * columnUpper,
00196                   const double * objective,
00197                   const CoinBigIndex * columnStarts, const int * rows,
00198                   const double * elements);
00199   void addColumns(int number, const double * columnLower, 
00200                   const double * columnUpper,
00201                   const double * objective,
00202                   const CoinBigIndex * columnStarts, const int * columnLengths,
00203                   const int * rows,
00204                   const double * elements);
00205 #ifndef CLP_NO_VECTOR
00206   void addColumns(int number, const double * columnLower, 
00207                const double * columnUpper,
00208                   const double * objective,
00209                const CoinPackedVectorBase * const * columns);
00210 #endif
00211 
00216   int addColumns(const CoinBuild & buildObject,bool tryPlusMinusOne=false,
00217                   bool checkDuplicates=true);
00225   int addColumns(CoinModel & modelObject,bool tryPlusMinusOne=false,
00226                  bool checkDuplicates=true);
00228   inline void modifyCoefficient(int row, int column, double newElement,
00229                            bool keepZero=false)
00230         {matrix_->modifyCoefficient(row,column,newElement,keepZero);}
00232   void chgRowLower(const double * rowLower);
00234   void chgRowUpper(const double * rowUpper);
00236   void chgColumnLower(const double * columnLower);
00238   void chgColumnUpper(const double * columnUpper);
00240   void chgObjCoefficients(const double * objIn); 
00244   void borrowModel(ClpModel & otherModel);
00247   void returnModel(ClpModel & otherModel);
00248 
00250   void createEmptyMatrix();
00258   int cleanMatrix(double threshold=1.0e-20);
00259 #ifndef CLP_NO_STD
00261   void dropNames();
00263   void copyNames(std::vector<std::string> & rowNames,
00264                  std::vector<std::string> & columnNames);
00266   void copyRowNames(const std::vector<std::string> & rowNames,int first, int last);
00268   void copyColumnNames(const std::vector<std::string> & columnNames, int first, int last);
00270   void copyRowNames(const char * const * rowNames,int first, int last);
00272   void copyColumnNames(const char * const * columnNames, int first, int last);
00274   void setRowName(int rowIndex, std::string & name) ;
00276   void setColumnName(int colIndex, std::string & name) ;
00277 #endif
00278 
00285   int findNetwork(char * rotate, double fractionNeeded=0.75);
00288   CoinModel * createCoinModel() const;
00289 
00302     int writeMps(const char *filename, 
00303                   int formatType=0,int numberAcross=2,
00304                  double objSense=0.0) const ;
00306 
00308 
00309    inline int numberRows() const {
00310       return numberRows_;
00311    }
00312    inline int getNumRows() const {
00313       return numberRows_;
00314    }
00316    inline int getNumCols() const {
00317       return numberColumns_;
00318    }
00319    inline int numberColumns() const {
00320       return numberColumns_;
00321    }
00323    inline double primalTolerance() const {
00324       return dblParam_[ClpPrimalTolerance];
00325    }
00326    void setPrimalTolerance( double value) ;
00328    inline double dualTolerance() const  { return dblParam_[ClpDualTolerance]; }
00329    void setDualTolerance( double value) ;
00331   inline double primalObjectiveLimit() const { return dblParam_[ClpPrimalObjectiveLimit];}
00332   void setPrimalObjectiveLimit(double value);
00334   inline double dualObjectiveLimit() const { return dblParam_[ClpDualObjectiveLimit];}
00335   void setDualObjectiveLimit(double value);
00337   inline double objectiveOffset() const { return dblParam_[ClpObjOffset];}
00338   void setObjectiveOffset(double value);
00339 #ifndef CLP_NO_STD
00340   inline std::string problemName() const { return strParam_[ClpProbName]; }
00341 #endif
00343    inline int numberIterations() const  { return numberIterations_; }
00344    inline int getIterationCount() const { return numberIterations_; }
00345   inline void setNumberIterations(int numberIterations)
00346   { numberIterations_ = numberIterations;}
00348   inline int solveType() const
00349   { return solveType_;}
00350   inline void setSolveType(int type)
00351   { solveType_=type;}
00353    inline int maximumIterations() const { return intParam_[ClpMaxNumIteration]; }
00354    void setMaximumIterations(int value);
00356    inline double maximumSeconds() const { return dblParam_[ClpMaxSeconds]; }
00357    void setMaximumSeconds(double value);
00359   bool hitMaximumIterations() const;
00369    inline int status() const            { return problemStatus_; }
00370    inline int problemStatus() const            { return problemStatus_; }
00372   inline void setProblemStatus(int problemStatus)
00373   { problemStatus_ = problemStatus;}
00388    inline int secondaryStatus() const            { return secondaryStatus_; }
00389   inline void setSecondaryStatus(int status)
00390   { secondaryStatus_ = status;}
00392    inline bool isAbandoned() const             { return problemStatus_==4; }
00394    inline bool isProvenOptimal() const         { return problemStatus_==0; }
00396    inline bool isProvenPrimalInfeasible() const { return problemStatus_==1; }
00398    inline bool isProvenDualInfeasible() const  { return problemStatus_==2; }
00400    bool isPrimalObjectiveLimitReached() const ;
00402    bool isDualObjectiveLimitReached() const ;
00404    inline bool isIterationLimitReached() const { return problemStatus_==3; }
00406    inline double optimizationDirection() const {
00407       return  optimizationDirection_;
00408    }
00409    inline double getObjSense() const    { return optimizationDirection_; }
00410    void setOptimizationDirection(double value);
00412    inline double * primalRowSolution() const    { return rowActivity_; }
00413    inline const double * getRowActivity() const { return rowActivity_; }
00415    inline double * primalColumnSolution() const { return columnActivity_; }
00416    inline const double * getColSolution() const { return columnActivity_; }
00417    inline void setColSolution(const double * input)
00418    { memcpy(columnActivity_,input,numberColumns_*sizeof(double));}
00420    inline double * dualRowSolution() const      { return dual_; }
00421    inline const double * getRowPrice() const    { return dual_; }
00423    inline double * dualColumnSolution() const   { return reducedCost_; }
00424    inline const double * getReducedCost() const { return reducedCost_; }
00426    inline double* rowLower() const              { return rowLower_; }
00427    inline const double* getRowLower() const     { return rowLower_; }
00429    inline double* rowUpper() const              { return rowUpper_; }
00430    inline const double* getRowUpper() const     { return rowUpper_; }
00431     //-------------------------------------------------------------------------
00435        void setObjectiveCoefficient( int elementIndex, double elementValue );
00437        inline void setObjCoeff( int elementIndex, double elementValue )
00438        { setObjectiveCoefficient( elementIndex, elementValue);}
00439 
00442        void setColumnLower( int elementIndex, double elementValue );
00443       
00446        void setColumnUpper( int elementIndex, double elementValue );
00447 
00449       void setColumnBounds( int elementIndex,
00450         double lower, double upper );
00451 
00460       void setColumnSetBounds(const int* indexFirst,
00461                                    const int* indexLast,
00462                                    const double* boundList);
00463       
00466        inline void setColLower( int elementIndex, double elementValue )
00467        { setColumnLower(elementIndex, elementValue);}
00470        inline void setColUpper( int elementIndex, double elementValue )
00471        { setColumnUpper(elementIndex, elementValue);}
00472 
00474       inline void setColBounds( int elementIndex,
00475         double lower, double upper )
00476        { setColumnBounds(elementIndex, lower, upper);}
00477 
00484       inline void setColSetBounds(const int* indexFirst,
00485                                    const int* indexLast,
00486                                    const double* boundList)
00487       { setColumnSetBounds(indexFirst, indexLast, boundList);}
00488       
00491       void setRowLower( int elementIndex, double elementValue );
00492       
00495       void setRowUpper( int elementIndex, double elementValue ) ;
00496     
00498       void setRowBounds( int elementIndex,
00499                                  double lower, double upper ) ;
00500     
00507       void setRowSetBounds(const int* indexFirst,
00508                                    const int* indexLast,
00509                                    const double* boundList);
00510     
00512 
00513    inline const double * rowScale() const {return rowScale_;}
00514    inline const double * columnScale() const {return columnScale_;}
00515    inline void setRowScale(double * scale) { delete [] (double *) rowScale_; rowScale_ = scale;}
00516    inline void setColumnScale(double * scale) { delete [] (double *) columnScale_; columnScale_ = scale;}
00518   inline double objectiveScale() const 
00519           { return objectiveScale_;} 
00520   inline void setObjectiveScale(double value)
00521           { objectiveScale_ = value;} 
00523   inline double rhsScale() const 
00524           { return rhsScale_;} 
00525   inline void setRhsScale(double value)
00526           { rhsScale_ = value;} 
00528    void scaling(int mode=1);
00531   void unscale();
00533    inline int scalingFlag() const {return scalingFlag_;}
00535    inline double * objective() const            
00536   {
00537     if (objective_) {
00538       double offset; 
00539       return objective_->gradient(NULL,NULL,offset,false);
00540     } else {
00541       return NULL;
00542     }
00543   }
00544    inline double * objective(const double * solution, double & offset,bool refresh=true) const            
00545   {
00546     offset=0.0;
00547     if (objective_) {
00548       return objective_->gradient(NULL,solution,offset,refresh);
00549     } else {
00550       return NULL;
00551     }
00552   }
00553    inline const double * getObjCoefficients() const 
00554   { 
00555     if (objective_) {
00556       double offset; 
00557       return objective_->gradient(NULL,NULL,offset,false);
00558     } else {
00559       return NULL;
00560     }
00561   }
00563    inline double * rowObjective() const         { return rowObjective_; }
00564    inline const double * getRowObjCoefficients() const {
00565       return rowObjective_;
00566    }
00568    inline double * columnLower() const          { return columnLower_; }
00569    inline const double * getColLower() const    { return columnLower_; }
00571    inline double * columnUpper() const          { return columnUpper_; }
00572    inline const double * getColUpper() const    { return columnUpper_; }
00574    inline CoinPackedMatrix * matrix() const {
00575      if ( matrix_ == NULL ) return NULL;
00576      else return matrix_->getPackedMatrix();
00577    }
00579    inline int getNumElements() const 
00580      { return matrix_->getNumElements();}
00583    inline double getSmallElementValue() const
00584   { return smallElement_;}
00585   inline void setSmallElementValue(double value)
00586   { smallElement_=value;} 
00588    inline ClpMatrixBase * rowCopy() const       { return rowCopy_; }
00590    inline ClpMatrixBase * clpMatrix() const     { return matrix_; }
00596    void replaceMatrix(ClpMatrixBase * matrix,bool deleteCurrent=false);
00602    inline void replaceMatrix(CoinPackedMatrix * matrix,
00603                              bool deleteCurrent=false)
00604   { replaceMatrix(new ClpPackedMatrix(matrix),deleteCurrent);}
00606    inline double objectiveValue() const {
00607       return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00608    }
00609   inline void setObjectiveValue(double value) {
00610     objectiveValue_ = (value+ dblParam_[ClpObjOffset])/optimizationDirection_;
00611    }
00612    inline double getObjValue() const {
00613       return objectiveValue_*optimizationDirection_ - dblParam_[ClpObjOffset];
00614    }
00616    inline char * integerInformation() const     { return integerType_; }
00619    double * infeasibilityRay() const;
00620    double * unboundedRay() const;
00622   inline bool statusExists() const
00623   { return (status_!=NULL);}
00625   inline unsigned char *  statusArray() const
00626   { return status_;}
00629   unsigned char *  statusCopy() const;
00631   void copyinStatus(const unsigned char * statusArray);
00632 
00634   inline void setUserPointer (void * pointer)
00635   { userPointer_=pointer;}
00636   inline void * getUserPointer () const
00637   { return userPointer_;}
00639   inline int whatsChanged() const 
00640           { return whatsChanged_;} 
00641   inline void setWhatsChanged(int value)
00642           { whatsChanged_ = value;} 
00644   inline int numberThreads() const 
00645           { return numberThreads_;} 
00646   inline void setNumberThreads(int value)
00647           { numberThreads_ = value;} 
00649 
00651 
00652    void passInMessageHandler(CoinMessageHandler * handler);
00654   CoinMessageHandler * pushMessageHandler(CoinMessageHandler * handler,
00655                                           bool & oldDefault);
00657   void popMessageHandler(CoinMessageHandler * oldHandler,bool oldDefault);
00659    void newLanguage(CoinMessages::Language language);
00660    inline void setLanguage(CoinMessages::Language language) { newLanguage(language); }
00662    inline CoinMessageHandler * messageHandler() const       { return handler_; }
00664    inline CoinMessages messages() const                     { return messages_; }
00666    inline CoinMessages * messagesPointer()                  { return & messages_; }
00668    inline CoinMessages coinMessages() const                  { return coinMessages_; }
00670    inline CoinMessages * coinMessagesPointer()                  { return & coinMessages_; }
00679    inline void setLogLevel(int value)    { handler_->setLogLevel(value); }
00680    inline int logLevel() const           { return handler_->logLevel(); }
00682    inline bool defaultHandler() const
00683    { return defaultHandler_;}
00685    void passInEventHandler(const ClpEventHandler * eventHandler);
00687    inline ClpEventHandler * eventHandler() const
00688   { return eventHandler_;}
00690    inline int lengthNames() const { return lengthNames_; }
00691 #ifndef CLP_NO_STD
00693    inline void setLengthNames(int value) { lengthNames_=value; }
00695    inline const std::vector<std::string> * rowNames() const {
00696       return &rowNames_;
00697    }
00698    inline const std::string& rowName(int iRow) const {
00699       return rowNames_[iRow];
00700    }
00702    std::string getRowName(int iRow) const;
00704    inline const std::vector<std::string> * columnNames() const {
00705       return &columnNames_;
00706    }
00707    inline const std::string& columnName(int iColumn) const {
00708       return columnNames_[iColumn];
00709    }
00711    std::string getColumnName(int iColumn) const;
00712 #endif
00714   inline ClpObjective * objectiveAsObject() const
00715   { return objective_;}
00716   void setObjective(ClpObjective * objective);
00717   inline void setObjectivePointer(ClpObjective * objective)
00718   { objective_ = objective;}
00721   int emptyProblem(int * infeasNumber=NULL, double * infeasSum=NULL,bool printMessage=true);
00722   
00724 
00733    void times(double scalar,
00734                        const double * x, double * y) const;
00738     void transposeTimes(double scalar,
00739                                 const double * x, double * y) const ;
00741 
00742 
00743   //---------------------------------------------------------------------------
00761 
00762     bool setIntParam(ClpIntParam key, int value) ;
00764     bool setDblParam(ClpDblParam key, double value) ;
00765 #ifndef CLP_NO_STD
00767     bool setStrParam(ClpStrParam key, const std::string & value);
00768 #endif
00769     // Get an integer parameter
00770     inline bool getIntParam(ClpIntParam key, int& value) const {
00771       if (key<ClpLastIntParam) {
00772         value = intParam_[key];
00773         return true;
00774       } else {
00775         return false;
00776       }
00777     }
00778     // Get an double parameter
00779     inline bool getDblParam(ClpDblParam key, double& value) const {
00780       if (key<ClpLastDblParam) {
00781         value = dblParam_[key];
00782         return true;
00783       } else {
00784         return false;
00785       }
00786     }
00787 #ifndef CLP_NO_STD
00788     // Get a string parameter
00789     inline bool getStrParam(ClpStrParam key, std::string& value) const {
00790       if (key<ClpLastStrParam) {
00791         value = strParam_[key];
00792         return true;
00793       } else {
00794         return false;
00795       }
00796     }
00797 #endif
00799     void generateCpp( FILE * fp);
00800 
00829 #define COIN_CBC_USING_CLP 0x01000000
00830   inline unsigned int specialOptions() const
00831   { return specialOptions_;}
00832   void setSpecialOptions(unsigned int value);
00834 
00837 protected:
00839   void gutsOfDelete();
00842   void gutsOfCopy(const ClpModel & rhs, bool trueCopy=true);
00844   void getRowBound(int iRow, double& lower, double& upper) const;
00846   void gutsOfLoadModel ( int numberRows, int numberColumns,
00847                      const double* collb, const double* colub,   
00848                      const double* obj,
00849                      const double* rowlb, const double* rowub,
00850                       const double * rowObjective=NULL);
00852   void gutsOfScaling();
00854    inline double rawObjectiveValue() const {
00855       return objectiveValue_;
00856    }
00858   const char * const * rowNamesAsChar() const;
00860   const char * const * columnNamesAsChar() const;
00862   void deleteNamesAsChar(const char * const * names,int number) const;
00864   void onStopped();
00866 
00867 
00869 protected:
00870 
00873 
00874   double optimizationDirection_;
00876   double dblParam_[ClpLastDblParam];
00878   double objectiveValue_;
00880   double smallElement_;
00882   double objectiveScale_;
00884   double rhsScale_;
00886   int numberRows_;
00888   int numberColumns_;
00890   double * rowActivity_;
00892   double * columnActivity_;
00894   double * dual_;
00896   double * reducedCost_;
00898   double* rowLower_;
00900   double* rowUpper_;
00902   ClpObjective * objective_;
00904   double * rowObjective_;
00906   double * columnLower_;
00908   double * columnUpper_;
00910   ClpMatrixBase * matrix_;
00912   ClpMatrixBase * rowCopy_;
00914   double * ray_;
00916   double * rowScale_;
00918   double * columnScale_;
00920   int scalingFlag_;
00928   unsigned char * status_;
00930   char * integerType_;
00932   void * userPointer_;
00934   int intParam_[ClpLastIntParam];
00936   int numberIterations_;
00938   int solveType_;
00954   unsigned int whatsChanged_;
00956   int problemStatus_;
00958   int secondaryStatus_;
00960   int lengthNames_;
00962   int numberThreads_;
00966   unsigned int specialOptions_;
00968   CoinMessageHandler * handler_;
00970   bool defaultHandler_;
00972   ClpEventHandler * eventHandler_;
00973 #ifndef CLP_NO_STD
00975   std::vector<std::string> rowNames_;
00977   std::vector<std::string> columnNames_;
00978 #endif
00980   CoinMessages messages_;
00982   CoinMessages coinMessages_;
00983 #ifndef CLP_NO_STD
00985   std::string strParam_[ClpLastStrParam];
00986 #endif
00987 
00988 };
00991 class ClpDataSave {
00992 
00993 public:
00997 
00998     ClpDataSave (  );
00999 
01001     ClpDataSave(const ClpDataSave &);
01003     ClpDataSave & operator=(const ClpDataSave & rhs);
01005     ~ClpDataSave (  );
01006 
01008 
01010 public:
01011 
01014   double dualBound_;
01015   double infeasibilityCost_;
01016   double pivotTolerance_;
01017   double acceptablePivot_;
01018   double objectiveScale_;
01019   int sparseThreshold_;
01020   int perturbation_;
01021   int forceFactorization_;
01022   int scalingFlag_;
01024 };
01025 
01026 #endif

Generated on Mon Apr 28 23:49:14 2008 by  doxygen 1.4.7