# CoinModel.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinModel.hpp 1691 2014-03-19 12:43:56Z forrest $ */
    00002 // Copyright (C) 2005, International Business Machines
    00003 // Corporation and others.  All Rights Reserved.
    00004 // This code is licensed under the terms of the Eclipse Public License (EPL).
    00005
    00006 #ifndef CoinModel_H
    00007 #define CoinModel_H
    00008
    00009 #include "CoinModelUseful.hpp"
    00010 #include "CoinMessageHandler.hpp"
    00011 #include "CoinPackedMatrix.hpp"
    00012 #include "CoinFinite.hpp"
    00013 class CoinBaseModel {
    00014
    00015 public:
    00016
    00017
    00020
    00021   CoinBaseModel ();
    00022
    00024   CoinBaseModel ( const CoinBaseModel &rhs);
    00025
    00027   CoinBaseModel & operator=( const CoinBaseModel& rhs);
    00028
    00030   virtual CoinBaseModel * clone() const=0;
    00031
    00033   virtual ~CoinBaseModel () ;
    00035
    00038
    00039   inline int numberRows() const
    00040   { return numberRows_;}
    00042   inline int numberColumns() const
    00043   { return numberColumns_;}
    00045   virtual CoinBigIndex numberElements() const = 0;
    00049   inline double objectiveOffset() const
    00050   { return objectiveOffset_;}
    00052   inline void setObjectiveOffset(double value)
    00053   { objectiveOffset_=value;}
    00055   inline double optimizationDirection() const {
    00056     return  optimizationDirection_;
    00057   }
    00059   inline void setOptimizationDirection(double value)
    00060   { optimizationDirection_=value;}
    00062   inline int logLevel() const
    00063   { return logLevel_;}
    00065   void setLogLevel(int value);
    00067   inline const char * getProblemName() const
    00068   { return problemName_.c_str();}
    00070   void setProblemName(const char *name) ;
    00072   void setProblemName(const std::string &name) ;
    00074   inline const std::string & getRowBlock() const
    00075   { return rowBlockName_;}
    00077   inline void setRowBlock(const std::string &name)
    00078   { rowBlockName_ = name;}
    00080   inline const std::string & getColumnBlock() const
    00081   { return columnBlockName_;}
    00083   inline void setColumnBlock(const std::string &name)
    00084   { columnBlockName_ = name;}
    00086   void setMessageHandler(CoinMessageHandler * handler);
    00088
    00089 protected:
    00092
    00093   int numberRows_;
    00095   int numberColumns_;
    00097   double optimizationDirection_;
    00099   double objectiveOffset_;
    00101   std::string problemName_;
    00103   std::string rowBlockName_;
    00105   std::string columnBlockName_;
    00107   CoinMessageHandler * handler_;
    00109   CoinMessages messages_;
    00110
    00119   int logLevel_;
    00121
    00122
    00123 };
    00124
    00161 class CoinModel : public CoinBaseModel {
    00162
    00163 public:
    00167    void addRow(int numberInRow, const int * columns,
    00168                const double * elements, double rowLower=-COIN_DBL_MAX,
    00169               double rowUpper=COIN_DBL_MAX, const char * name=NULL);
    00171    void addColumn(int numberInColumn, const int * rows,
    00172                   const double * elements,
    00173                   double columnLower=0.0,
    00174                   double columnUpper=COIN_DBL_MAX, double objectiveValue=0.0,
    00175                   const char * name=NULL, bool isInteger=false);
    00177   inline void addCol(int numberInColumn, const int * rows,
    00178                      const double * elements,
    00179                      double columnLower=0.0,
    00180                      double columnUpper=COIN_DBL_MAX, double objectiveValue=0.0,
    00181                      const char * name=NULL, bool isInteger=false)
    00182   { addColumn(numberInColumn, rows, elements, columnLower, columnUpper, objectiveValue,
    00183               name,isInteger);}
    00185   inline void operator() (int i,int j,double value)
    00186   { setElement(i,j,value);}
    00188   void setElement(int i,int j,double value) ;
    00194   int getRow(int whichRow, int * column, double * element);
    00200   int getColumn(int whichColumn, int * column, double * element);
    00202   void setQuadraticElement(int i,int j,double value) ;
    00204   inline void operator() (int i,int j,const char * value)
    00205   { setElement(i,j,value);}
    00207   void setElement(int i,int j,const char * value) ;
    00209   int associateElement(const char * stringValue, double value);
    00213   void setRowLower(int whichRow,double rowLower);
    00217   void setRowUpper(int whichRow,double rowUpper);
    00221   void setRowBounds(int whichRow,double rowLower,double rowUpper);
    00225   void setRowName(int whichRow,const char * rowName);
    00229   void setColumnLower(int whichColumn,double columnLower);
    00233   void setColumnUpper(int whichColumn,double columnUpper);
    00237   void setColumnBounds(int whichColumn,double columnLower,double columnUpper);
    00241   void setColumnObjective(int whichColumn,double columnObjective);
    00245   void setColumnName(int whichColumn,const char * columnName);
    00249   void setColumnIsInteger(int whichColumn,bool columnIsInteger);
    00253   inline void setObjective(int whichColumn,double columnObjective)
    00254   { setColumnObjective( whichColumn, columnObjective);}
    00258   inline void setIsInteger(int whichColumn,bool columnIsInteger)
    00259   { setColumnIsInteger( whichColumn, columnIsInteger);}
    00263   inline void setInteger(int whichColumn)
    00264   { setColumnIsInteger( whichColumn, true);}
    00268   inline void setContinuous(int whichColumn)
    00269   { setColumnIsInteger( whichColumn, false);}
    00273   inline void setColLower(int whichColumn,double columnLower)
    00274   { setColumnLower( whichColumn, columnLower);}
    00278   inline void setColUpper(int whichColumn,double columnUpper)
    00279   { setColumnUpper( whichColumn, columnUpper);}
    00283   inline void setColBounds(int whichColumn,double columnLower,double columnUpper)
    00284   { setColumnBounds( whichColumn, columnLower, columnUpper);}
    00288   inline void setColObjective(int whichColumn,double columnObjective)
    00289   { setColumnObjective( whichColumn, columnObjective);}
    00293   inline void setColName(int whichColumn,const char * columnName)
    00294   { setColumnName( whichColumn, columnName);}
    00298   inline void setColIsInteger(int whichColumn,bool columnIsInteger)
    00299   { setColumnIsInteger( whichColumn, columnIsInteger);}
    00303   void setRowLower(int whichRow,const char * rowLower);
    00307   void setRowUpper(int whichRow,const char * rowUpper);
    00311   void setColumnLower(int whichColumn,const char * columnLower);
    00315   void setColumnUpper(int whichColumn,const char * columnUpper);
    00319   void setColumnObjective(int whichColumn,const char * columnObjective);
    00323   void setColumnIsInteger(int whichColumn,const char * columnIsInteger);
    00327   inline void setObjective(int whichColumn,const char * columnObjective)
    00328   { setColumnObjective( whichColumn, columnObjective);}
    00332   inline void setIsInteger(int whichColumn,const char * columnIsInteger)
    00333   { setColumnIsInteger( whichColumn, columnIsInteger);}
    00336   void deleteRow(int whichRow);
    00339   void deleteColumn(int whichColumn);
    00342   inline void deleteCol(int whichColumn)
    00343   { deleteColumn(whichColumn);}
    00345   int deleteElement(int row, int column);
    00347   void deleteThisElement(int row, int column,int position);
    00350   int packRows();
    00353   int packColumns();
    00356   inline int packCols()
    00357   { return packColumns();}
    00362   int pack();
    00363
    00366   void setObjective(int numberColumns,const double * objective) ;
    00369   void setColumnLower(int numberColumns,const double * columnLower);
    00372   inline void setColLower(int numberColumns,const double * columnLower)
    00373   { setColumnLower( numberColumns, columnLower);}
    00376   void setColumnUpper(int numberColumns,const double * columnUpper);
    00379   inline void setColUpper(int numberColumns,const double * columnUpper)
    00380   { setColumnUpper( numberColumns, columnUpper);}
    00383   void setRowLower(int numberRows,const double * rowLower);
    00386   void setRowUpper(int numberRows,const double * rowUpper);
    00387
    00413   int writeMps(const char *filename, int compression = 0,
    00414                int formatType = 0, int numberAcross = 2, bool keepStrings=false) ;
    00415
    00420   int differentModel(CoinModel & other, bool ignoreNames);
    00422
    00423
    00426
    00427   void passInMatrix(const CoinPackedMatrix & matrix);
    00430   int convertMatrix();
    00432   inline const CoinPackedMatrix * packedMatrix() const
    00433   { return packedMatrix_;}
    00435   inline const int * originalRows() const
    00436   { return rowType_;}
    00438   inline const int * originalColumns() const
    00439   { return columnType_;}
    00441
    00442
    00445
    00446   inline CoinBigIndex numberElements() const
    00447   { return numberElements_;}
    00449   inline const CoinModelTriple * elements() const
    00450   { return elements_;}
    00452   inline double operator() (int i,int j) const
    00453   { return getElement(i,j);}
    00455   double getElement(int i,int j) const;
    00457   inline double operator() (const char * rowName,const char * columnName) const
    00458   { return getElement(rowName,columnName);}
    00460   double getElement(const char * rowName,const char * columnName) const;
    00462   double getQuadraticElement(int i,int j) const;
    00467   const char * getElementAsString(int i,int j) const;
    00471   double * pointer (int i,int j) const;
    00475   int position (int i,int j) const;
    00476
    00477
    00481   CoinModelLink firstInRow(int whichRow) const ;
    00485   CoinModelLink lastInRow(int whichRow) const ;
    00489   CoinModelLink firstInColumn(int whichColumn) const ;
    00493   CoinModelLink lastInColumn(int whichColumn) const ;
    00498   CoinModelLink next(CoinModelLink & current) const ;
    00504   CoinModelLink previous(CoinModelLink & current) const ;
    00509   CoinModelLink firstInQuadraticColumn(int whichColumn) const ;
    00513   CoinModelLink lastInQuadraticColumn(int whichColumn) const ;
    00516   double  getRowLower(int whichRow) const ;
    00519   double  getRowUpper(int whichRow) const ;
    00522   const char * getRowName(int whichRow) const ;
    00523   inline double  rowLower(int whichRow) const
    00524   { return getRowLower(whichRow);}
    00527   inline double  rowUpper(int whichRow) const
    00528   { return getRowUpper(whichRow) ;}
    00531   inline const char * rowName(int whichRow) const
    00532   { return getRowName(whichRow);}
    00535   double  getColumnLower(int whichColumn) const ;
    00538   double  getColumnUpper(int whichColumn) const ;
    00541   double  getColumnObjective(int whichColumn) const ;
    00544   const char * getColumnName(int whichColumn) const ;
    00547   bool getColumnIsInteger(int whichColumn) const ;
    00550   inline double  columnLower(int whichColumn) const
    00551   { return getColumnLower(whichColumn);}
    00554   inline double  columnUpper(int whichColumn) const
    00555   { return getColumnUpper(whichColumn) ;}
    00558   inline double  columnObjective(int whichColumn) const
    00559   { return getColumnObjective(whichColumn);}
    00562   inline double  objective(int whichColumn) const
    00563   { return getColumnObjective(whichColumn);}
    00566   inline const char * columnName(int whichColumn) const
    00567   { return getColumnName(whichColumn);}
    00570   inline bool columnIsInteger(int whichColumn) const
    00571   { return getColumnIsInteger(whichColumn);}
    00574   inline bool isInteger(int whichColumn) const
    00575   { return getColumnIsInteger(whichColumn);}
    00578   inline double  getColLower(int whichColumn) const
    00579   { return getColumnLower(whichColumn);}
    00582   inline double  getColUpper(int whichColumn) const
    00583   { return getColumnUpper(whichColumn) ;}
    00586   inline double  getColObjective(int whichColumn) const
    00587   { return getColumnObjective(whichColumn);}
    00590   inline const char * getColName(int whichColumn) const
    00591   { return getColumnName(whichColumn);}
    00594   inline bool getColIsInteger(int whichColumn) const
    00595   { return getColumnIsInteger(whichColumn);}
    00598   const char *  getRowLowerAsString(int whichRow) const ;
    00601   const char *  getRowUpperAsString(int whichRow) const ;
    00602   inline const char *  rowLowerAsString(int whichRow) const
    00603   { return getRowLowerAsString(whichRow);}
    00606   inline const char *  rowUpperAsString(int whichRow) const
    00607   { return getRowUpperAsString(whichRow) ;}
    00610   const char *  getColumnLowerAsString(int whichColumn) const ;
    00613   const char *  getColumnUpperAsString(int whichColumn) const ;
    00616   const char *  getColumnObjectiveAsString(int whichColumn) const ;
    00619   const char * getColumnIsIntegerAsString(int whichColumn) const ;
    00622   inline const char *  columnLowerAsString(int whichColumn) const
    00623   { return getColumnLowerAsString(whichColumn);}
    00626   inline const char *  columnUpperAsString(int whichColumn) const
    00627   { return getColumnUpperAsString(whichColumn) ;}
    00630   inline const char *  columnObjectiveAsString(int whichColumn) const
    00631   { return getColumnObjectiveAsString(whichColumn);}
    00634   inline const char *  objectiveAsString(int whichColumn) const
    00635   { return getColumnObjectiveAsString(whichColumn);}
    00638   inline const char * columnIsIntegerAsString(int whichColumn) const
    00639   { return getColumnIsIntegerAsString(whichColumn);}
    00642   inline const char * isIntegerAsString(int whichColumn) const
    00643   { return getColumnIsIntegerAsString(whichColumn);}
    00645   int row(const char * rowName) const;
    00647   int column(const char * columnName) const;
    00649   inline int type() const
    00650   { return type_;}
    00652   inline double unsetValue() const
    00653   { return -1.23456787654321e-97;}
    00655   int createPackedMatrix(CoinPackedMatrix & matrix,
    00656                          const double * associated);
    00662   int countPlusMinusOne(CoinBigIndex * startPositive, CoinBigIndex * startNegative,
    00663                         const double * associated);
    00666   void createPlusMinusOne(CoinBigIndex * startPositive, CoinBigIndex * startNegative,
    00667                          int * indices,
    00668                          const double * associated);
    00670   int createArrays(double * & rowLower, double * &  rowUpper,
    00671                    double * & columnLower, double * & columnUpper,
    00672                    double * & objective, int * & integerType,
    00673                    double * & associated);
    00675   inline bool stringsExist() const
    00676   { return string_.numberItems()!=0;}
    00678   inline const CoinModelHash * stringArray() const
    00679   { return &string_;}
    00681   inline double * associatedArray() const
    00682   { return associated_;}
    00684   inline double * rowLowerArray() const
    00685   { return rowLower_;}
    00687   inline double * rowUpperArray() const
    00688   { return rowUpper_;}
    00690   inline double * columnLowerArray() const
    00691   { return columnLower_;}
    00693   inline double * columnUpperArray() const
    00694   { return columnUpper_;}
    00696   inline double * objectiveArray() const
    00697   { return objective_;}
    00699   inline int * integerTypeArray() const
    00700   { return integerType_;}
    00702   inline const CoinModelHash * rowNames() const
    00703   { return &rowName_;}
    00705   inline const CoinModelHash * columnNames() const
    00706   { return &columnName_;}
    00708   inline void zapRowNames()
    00709   { rowName_=CoinModelHash();}
    00711   inline void zapColumnNames()
    00712   { columnName_=CoinModelHash();}
    00714   inline const int * cutMarker() const
    00715   { return cut_;}
    00717   inline double optimizationDirection() const {
    00718     return  optimizationDirection_;
    00719   }
    00721   inline void setOptimizationDirection(double value)
    00722   { optimizationDirection_=value;}
    00724   inline void * moreInfo() const
    00725   { return moreInfo_;}
    00727   inline void setMoreInfo(void * info)
    00728   { moreInfo_ = info;}
    00737   int whatIsSet() const;
    00739
    00757   void loadBlock (const CoinPackedMatrix& matrix,
    00758                   const double* collb, const double* colub,
    00759                   const double* obj,
    00760                   const double* rowlb, const double* rowub) ;
    00777   void loadBlock (const CoinPackedMatrix& matrix,
    00778                   const double* collb, const double* colub,
    00779                   const double* obj,
    00780                   const char* rowsen, const double* rowrhs,
    00781                   const double* rowrng) ;
    00782
    00795   void loadBlock (const int numcols, const int numrows,
    00796                   const CoinBigIndex * start, const int* index,
    00797                   const double* value,
    00798                   const double* collb, const double* colub,
    00799                   const double* obj,
    00800                   const double* rowlb, const double* rowub) ;
    00801
    00814   void loadBlock (const int numcols, const int numrows,
    00815                   const CoinBigIndex * start, const int* index,
    00816                   const double* value,
    00817                   const double* collb, const double* colub,
    00818                   const double* obj,
    00819                   const char* rowsen, const double* rowrhs,
    00820                   const double* rowrng) ;
    00821
    00823
    00827    CoinModel();
    00829    CoinModel(int firstRows, int firstColumns, int firstElements,bool noNames=false);
    00832    CoinModel(const char *fileName, int allowStrings=0);
    00836    CoinModel( int nonLinear, const char * fileName,const void * info);
    00838   CoinModel(int numberRows, int numberColumns,
    00839             const CoinPackedMatrix * matrix,
    00840             const double * rowLower, const double * rowUpper,
    00841             const double * columnLower, const double * columnUpper,
    00842             const double * objective);
    00844   virtual CoinBaseModel * clone() const;
    00845
    00847    virtual ~CoinModel();
    00849
    00853    CoinModel(const CoinModel&);
    00855    CoinModel& operator=(const CoinModel&);
    00857
    00860
    00861   void validateLinks() const;
    00863 private:
    00865   void resize(int maximumRows, int maximumColumns, int maximumElements);
    00867   void fillRows(int which,bool forceCreation,bool fromAddRow=false);
    00869   void fillColumns(int which,bool forceCreation,bool fromAddColumn=false);
    00872   void fillList(int which, CoinModelLinkedList & list,int type) const ;
    00876   void createList(int type) const;
    00878   int addString(const char * string);
    00882   double getDoubleFromString(CoinYacc & info, const char * string);
    00884   void freeStringMemory(CoinYacc & info);
    00885 public:
    00887   int computeAssociated(double * associated);
    00893   CoinPackedMatrix * quadraticRow(int rowNumber,double * linear,
    00894                                   int & numberBad) const;
    00896   void replaceQuadraticRow(int rowNumber,const double * linear, const CoinPackedMatrix * quadraticPart);
    00901   CoinModel * reorder(const char * mark) const;
    00912   int expandKnapsack(int knapsackRow, int & numberOutput,double * buildObj, CoinBigIndex * buildStart,
    00913                      int * buildRow, double * buildElement,int reConstruct=-1) const;
    00915   void setCutMarker(int size,const int * marker);
    00917   void setPriorities(int size,const int * priorities);
    00919   inline const int * priorities() const
    00920   { return priority_;}
    00922   void setOriginalIndices(const int * row, const int * column);
    00923
    00924 private:
    00928   void gdb( int nonLinear, const char * fileName, const void * info);
    00930   int decodeBit(char * phrase, char * & nextPhrase, double & coefficient, bool ifFirst) const;
    00932   void badType() const;
    00935
    00936   int maximumRows_;
    00938   int maximumColumns_;
    00940   int numberElements_;
    00942   int maximumElements_;
    00944   int numberQuadraticElements_;
    00946   int maximumQuadraticElements_;
    00948   double * rowLower_;
    00950   double * rowUpper_;
    00952   CoinModelHash rowName_;
    00960   int * rowType_;
    00962   double * objective_;
    00964   double * columnLower_;
    00966   double * columnUpper_;
    00968   CoinModelHash columnName_;
    00970   int * integerType_;
    00972   CoinModelHash string_;
    00982   int * columnType_;
    00984   int * start_;
    00986   CoinModelTriple * elements_;
    00988   CoinPackedMatrix * packedMatrix_;
    00990   mutable CoinModelHash2 hashElements_;
    00992   mutable CoinModelLinkedList rowList_;
    00994   mutable CoinModelLinkedList columnList_;
    00996   CoinModelTriple * quadraticElements_;
    00998   mutable CoinModelHash2 hashQuadraticElements_;
    01000   int * sortIndices_;
    01002   double * sortElements_;
    01004   int sortSize_;
    01006   mutable CoinModelLinkedList quadraticRowList_;
    01008   mutable CoinModelLinkedList quadraticColumnList_;
    01010   int sizeAssociated_;
    01012   double * associated_;
    01014   int numberSOS_;
    01016   int * startSOS_;
    01018   int * memberSOS_;
    01020   int * typeSOS_;
    01022   int * prioritySOS_;
    01024   double * referenceSOS_;
    01026   int * priority_;
    01028   int * cut_;
    01030   void * moreInfo_;
    01038   mutable int type_;
    01040   bool noNames_;
    01047   mutable int links_;
    01049 };
    01051 double getFunctionValueFromString(const char * string, const char * x, double xValue);
    01053 double getDoubleFromString(CoinYacc & info, const char * string, const char * x, double xValue);
    01054 #endif

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines