- Main Page
- Namespaces
- Classes
- Files
- Directories

- Search for |

- File List
- File Members

# CoinModel.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinModel.hpp 1448 2011-06-19 15:34:41Z stefan $ */
    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 "CoinPackedMatrix.hpp"
    00011 #include "CoinFinite.hpp"
    00012 class CoinBaseModel {
    00013
    00014 public:
    00015
    00016
    00019
    00020   CoinBaseModel ();
    00021
    00023   CoinBaseModel ( const CoinBaseModel &rhs);
    00024
    00026   CoinBaseModel & operator=( const CoinBaseModel& rhs);
    00027
    00029   virtual CoinBaseModel * clone() const=0;
    00030
    00032   virtual ~CoinBaseModel () ;
    00034
    00037
    00038   inline int numberRows() const
    00039   { return numberRows_;}
    00041   inline int numberColumns() const
    00042   { return numberColumns_;}
    00044   virtual CoinBigIndex numberElements() const = 0;
    00048   inline double objectiveOffset() const
    00049   { return objectiveOffset_;}
    00051   inline void setObjectiveOffset(double value)
    00052   { objectiveOffset_=value;}
    00054   inline double optimizationDirection() const {
    00055     return  optimizationDirection_;
    00056   }
    00058   inline void setOptimizationDirection(double value)
    00059   { optimizationDirection_=value;}
    00061   inline int logLevel() const
    00062   { return logLevel_;}
    00064   void setLogLevel(int value);
    00066   inline const char * getProblemName() const
    00067   { return problemName_.c_str();}
    00069   void setProblemName(const char *name) ;
    00071   void setProblemName(const std::string &name) ;
    00073   inline const std::string & getRowBlock() const
    00074   { return rowBlockName_;}
    00076   inline void setRowBlock(const std::string &name)
    00077   { rowBlockName_ = name;}
    00079   inline const std::string & getColumnBlock() const
    00080   { return columnBlockName_;}
    00082   inline void setColumnBlock(const std::string &name)
    00083   { columnBlockName_ = name;}
    00085
    00086 protected:
    00089
    00090   int numberRows_;
    00092   int numberColumns_;
    00094   double optimizationDirection_;
    00096   double objectiveOffset_;
    00098   std::string problemName_;
    00100   std::string rowBlockName_;
    00102   std::string columnBlockName_;
    00110   int logLevel_;
    00112
    00113
    00114 };
    00115
    00152 class CoinModel : public CoinBaseModel {
    00153
    00154 public:
    00158    void addRow(int numberInRow, const int * columns,
    00159                const double * elements, double rowLower=-COIN_DBL_MAX,
    00160               double rowUpper=COIN_DBL_MAX, const char * name=NULL);
    00162    void addColumn(int numberInColumn, const int * rows,
    00163                   const double * elements,
    00164                   double columnLower=0.0,
    00165                   double columnUpper=COIN_DBL_MAX, double objectiveValue=0.0,
    00166                   const char * name=NULL, bool isInteger=false);
    00168   inline void addCol(int numberInColumn, const int * rows,
    00169                      const double * elements,
    00170                      double columnLower=0.0,
    00171                      double columnUpper=COIN_DBL_MAX, double objectiveValue=0.0,
    00172                      const char * name=NULL, bool isInteger=false)
    00173   { addColumn(numberInColumn, rows, elements, columnLower, columnUpper, objectiveValue,
    00174               name,isInteger);}
    00176   inline void operator() (int i,int j,double value)
    00177   { setElement(i,j,value);}
    00179   void setElement(int i,int j,double value) ;
    00185   int getRow(int whichRow, int * column, double * element);
    00191   int getColumn(int whichColumn, int * column, double * element);
    00193   void setQuadraticElement(int i,int j,double value) ;
    00195   inline void operator() (int i,int j,const char * value)
    00196   { setElement(i,j,value);}
    00198   void setElement(int i,int j,const char * value) ;
    00200   int associateElement(const char * stringValue, double value);
    00204   void setRowLower(int whichRow,double rowLower);
    00208   void setRowUpper(int whichRow,double rowUpper);
    00212   void setRowBounds(int whichRow,double rowLower,double rowUpper);
    00216   void setRowName(int whichRow,const char * rowName);
    00220   void setColumnLower(int whichColumn,double columnLower);
    00224   void setColumnUpper(int whichColumn,double columnUpper);
    00228   void setColumnBounds(int whichColumn,double columnLower,double columnUpper);
    00232   void setColumnObjective(int whichColumn,double columnObjective);
    00236   void setColumnName(int whichColumn,const char * columnName);
    00240   void setColumnIsInteger(int whichColumn,bool columnIsInteger);
    00244   inline void setObjective(int whichColumn,double columnObjective)
    00245   { setColumnObjective( whichColumn, columnObjective);}
    00249   inline void setIsInteger(int whichColumn,bool columnIsInteger)
    00250   { setColumnIsInteger( whichColumn, columnIsInteger);}
    00254   inline void setInteger(int whichColumn)
    00255   { setColumnIsInteger( whichColumn, true);}
    00259   inline void setContinuous(int whichColumn)
    00260   { setColumnIsInteger( whichColumn, false);}
    00264   inline void setColLower(int whichColumn,double columnLower)
    00265   { setColumnLower( whichColumn, columnLower);}
    00269   inline void setColUpper(int whichColumn,double columnUpper)
    00270   { setColumnUpper( whichColumn, columnUpper);}
    00274   inline void setColBounds(int whichColumn,double columnLower,double columnUpper)
    00275   { setColumnBounds( whichColumn, columnLower, columnUpper);}
    00279   inline void setColObjective(int whichColumn,double columnObjective)
    00280   { setColumnObjective( whichColumn, columnObjective);}
    00284   inline void setColName(int whichColumn,const char * columnName)
    00285   { setColumnName( whichColumn, columnName);}
    00289   inline void setColIsInteger(int whichColumn,bool columnIsInteger)
    00290   { setColumnIsInteger( whichColumn, columnIsInteger);}
    00294   void setRowLower(int whichRow,const char * rowLower);
    00298   void setRowUpper(int whichRow,const char * rowUpper);
    00302   void setColumnLower(int whichColumn,const char * columnLower);
    00306   void setColumnUpper(int whichColumn,const char * columnUpper);
    00310   void setColumnObjective(int whichColumn,const char * columnObjective);
    00314   void setColumnIsInteger(int whichColumn,const char * columnIsInteger);
    00318   inline void setObjective(int whichColumn,const char * columnObjective)
    00319   { setColumnObjective( whichColumn, columnObjective);}
    00323   inline void setIsInteger(int whichColumn,const char * columnIsInteger)
    00324   { setColumnIsInteger( whichColumn, columnIsInteger);}
    00327   void deleteRow(int whichRow);
    00330   void deleteColumn(int whichColumn);
    00333   inline void deleteCol(int whichColumn)
    00334   { deleteColumn(whichColumn);}
    00336   int deleteElement(int row, int column);
    00338   void deleteThisElement(int row, int column,int position);
    00341   int packRows();
    00344   int packColumns();
    00347   inline int packCols()
    00348   { return packColumns();}
    00353   int pack();
    00354
    00357   void setObjective(int numberColumns,const double * objective) ;
    00360   void setColumnLower(int numberColumns,const double * columnLower);
    00363   inline void setColLower(int numberColumns,const double * columnLower)
    00364   { setColumnLower( numberColumns, columnLower);}
    00367   void setColumnUpper(int numberColumns,const double * columnUpper);
    00370   inline void setColUpper(int numberColumns,const double * columnUpper)
    00371   { setColumnUpper( numberColumns, columnUpper);}
    00374   void setRowLower(int numberRows,const double * rowLower);
    00377   void setRowUpper(int numberRows,const double * rowUpper);
    00378
    00404   int writeMps(const char *filename, int compression = 0,
    00405                int formatType = 0, int numberAcross = 2, bool keepStrings=false) ;
    00406
    00411   int differentModel(CoinModel & other, bool ignoreNames);
    00413
    00414
    00417
    00418   void passInMatrix(const CoinPackedMatrix & matrix);
    00421   int convertMatrix();
    00423   inline const CoinPackedMatrix * packedMatrix() const
    00424   { return packedMatrix_;}
    00426   inline const int * originalRows() const
    00427   { return rowType_;}
    00429   inline const int * originalColumns() const
    00430   { return columnType_;}
    00432
    00433
    00436
    00437   inline CoinBigIndex numberElements() const
    00438   { return numberElements_;}
    00440   inline const CoinModelTriple * elements() const
    00441   { return elements_;}
    00443   inline double operator() (int i,int j) const
    00444   { return getElement(i,j);}
    00446   double getElement(int i,int j) const;
    00448   inline double operator() (const char * rowName,const char * columnName) const
    00449   { return getElement(rowName,columnName);}
    00451   double getElement(const char * rowName,const char * columnName) const;
    00453   double getQuadraticElement(int i,int j) const;
    00458   const char * getElementAsString(int i,int j) const;
    00462   double * pointer (int i,int j) const;
    00466   int position (int i,int j) const;
    00467
    00468
    00472   CoinModelLink firstInRow(int whichRow) const ;
    00476   CoinModelLink lastInRow(int whichRow) const ;
    00480   CoinModelLink firstInColumn(int whichColumn) const ;
    00484   CoinModelLink lastInColumn(int whichColumn) const ;
    00489   CoinModelLink next(CoinModelLink & current) const ;
    00495   CoinModelLink previous(CoinModelLink & current) const ;
    00500   CoinModelLink firstInQuadraticColumn(int whichColumn) const ;
    00504   CoinModelLink lastInQuadraticColumn(int whichColumn) const ;
    00507   double  getRowLower(int whichRow) const ;
    00510   double  getRowUpper(int whichRow) const ;
    00513   const char * getRowName(int whichRow) const ;
    00514   inline double  rowLower(int whichRow) const
    00515   { return getRowLower(whichRow);}
    00518   inline double  rowUpper(int whichRow) const
    00519   { return getRowUpper(whichRow) ;}
    00522   inline const char * rowName(int whichRow) const
    00523   { return getRowName(whichRow);}
    00526   double  getColumnLower(int whichColumn) const ;
    00529   double  getColumnUpper(int whichColumn) const ;
    00532   double  getColumnObjective(int whichColumn) const ;
    00535   const char * getColumnName(int whichColumn) const ;
    00538   bool getColumnIsInteger(int whichColumn) const ;
    00541   inline double  columnLower(int whichColumn) const
    00542   { return getColumnLower(whichColumn);}
    00545   inline double  columnUpper(int whichColumn) const
    00546   { return getColumnUpper(whichColumn) ;}
    00549   inline double  columnObjective(int whichColumn) const
    00550   { return getColumnObjective(whichColumn);}
    00553   inline double  objective(int whichColumn) const
    00554   { return getColumnObjective(whichColumn);}
    00557   inline const char * columnName(int whichColumn) const
    00558   { return getColumnName(whichColumn);}
    00561   inline bool columnIsInteger(int whichColumn) const
    00562   { return getColumnIsInteger(whichColumn);}
    00565   inline bool isInteger(int whichColumn) const
    00566   { return getColumnIsInteger(whichColumn);}
    00569   inline double  getColLower(int whichColumn) const
    00570   { return getColumnLower(whichColumn);}
    00573   inline double  getColUpper(int whichColumn) const
    00574   { return getColumnUpper(whichColumn) ;}
    00577   inline double  getColObjective(int whichColumn) const
    00578   { return getColumnObjective(whichColumn);}
    00581   inline const char * getColName(int whichColumn) const
    00582   { return getColumnName(whichColumn);}
    00585   inline bool getColIsInteger(int whichColumn) const
    00586   { return getColumnIsInteger(whichColumn);}
    00589   const char *  getRowLowerAsString(int whichRow) const ;
    00592   const char *  getRowUpperAsString(int whichRow) const ;
    00593   inline const char *  rowLowerAsString(int whichRow) const
    00594   { return getRowLowerAsString(whichRow);}
    00597   inline const char *  rowUpperAsString(int whichRow) const
    00598   { return getRowUpperAsString(whichRow) ;}
    00601   const char *  getColumnLowerAsString(int whichColumn) const ;
    00604   const char *  getColumnUpperAsString(int whichColumn) const ;
    00607   const char *  getColumnObjectiveAsString(int whichColumn) const ;
    00610   const char * getColumnIsIntegerAsString(int whichColumn) const ;
    00613   inline const char *  columnLowerAsString(int whichColumn) const
    00614   { return getColumnLowerAsString(whichColumn);}
    00617   inline const char *  columnUpperAsString(int whichColumn) const
    00618   { return getColumnUpperAsString(whichColumn) ;}
    00621   inline const char *  columnObjectiveAsString(int whichColumn) const
    00622   { return getColumnObjectiveAsString(whichColumn);}
    00625   inline const char *  objectiveAsString(int whichColumn) const
    00626   { return getColumnObjectiveAsString(whichColumn);}
    00629   inline const char * columnIsIntegerAsString(int whichColumn) const
    00630   { return getColumnIsIntegerAsString(whichColumn);}
    00633   inline const char * isIntegerAsString(int whichColumn) const
    00634   { return getColumnIsIntegerAsString(whichColumn);}
    00636   int row(const char * rowName) const;
    00638   int column(const char * columnName) const;
    00640   inline int type() const
    00641   { return type_;}
    00643   inline double unsetValue() const
    00644   { return -1.23456787654321e-97;}
    00646   int createPackedMatrix(CoinPackedMatrix & matrix,
    00647                          const double * associated);
    00653   int countPlusMinusOne(CoinBigIndex * startPositive, CoinBigIndex * startNegative,
    00654                         const double * associated);
    00657   void createPlusMinusOne(CoinBigIndex * startPositive, CoinBigIndex * startNegative,
    00658                          int * indices,
    00659                          const double * associated);
    00661   int createArrays(double * & rowLower, double * &  rowUpper,
    00662                    double * & columnLower, double * & columnUpper,
    00663                    double * & objective, int * & integerType,
    00664                    double * & associated);
    00666   inline bool stringsExist() const
    00667   { return string_.numberItems()!=0;}
    00669   inline const CoinModelHash * stringArray() const
    00670   { return &string_;}
    00672   inline double * associatedArray() const
    00673   { return associated_;}
    00675   inline double * rowLowerArray() const
    00676   { return rowLower_;}
    00678   inline double * rowUpperArray() const
    00679   { return rowUpper_;}
    00681   inline double * columnLowerArray() const
    00682   { return columnLower_;}
    00684   inline double * columnUpperArray() const
    00685   { return columnUpper_;}
    00687   inline double * objectiveArray() const
    00688   { return objective_;}
    00690   inline int * integerTypeArray() const
    00691   { return integerType_;}
    00693   inline const CoinModelHash * rowNames() const
    00694   { return &rowName_;}
    00696   inline const CoinModelHash * columnNames() const
    00697   { return &columnName_;}
    00699   inline const int * cutMarker() const
    00700   { return cut_;}
    00702   inline double optimizationDirection() const {
    00703     return  optimizationDirection_;
    00704   }
    00706   inline void setOptimizationDirection(double value)
    00707   { optimizationDirection_=value;}
    00709   inline void * moreInfo() const
    00710   { return moreInfo_;}
    00712   inline void setMoreInfo(void * info)
    00713   { moreInfo_ = info;}
    00722   int whatIsSet() const;
    00724
    00742   void loadBlock (const CoinPackedMatrix& matrix,
    00743                   const double* collb, const double* colub,
    00744                   const double* obj,
    00745                   const double* rowlb, const double* rowub) ;
    00762   void loadBlock (const CoinPackedMatrix& matrix,
    00763                   const double* collb, const double* colub,
    00764                   const double* obj,
    00765                   const char* rowsen, const double* rowrhs,
    00766                   const double* rowrng) ;
    00767
    00780   void loadBlock (const int numcols, const int numrows,
    00781                   const CoinBigIndex * start, const int* index,
    00782                   const double* value,
    00783                   const double* collb, const double* colub,
    00784                   const double* obj,
    00785                   const double* rowlb, const double* rowub) ;
    00786
    00799   void loadBlock (const int numcols, const int numrows,
    00800                   const CoinBigIndex * start, const int* index,
    00801                   const double* value,
    00802                   const double* collb, const double* colub,
    00803                   const double* obj,
    00804                   const char* rowsen, const double* rowrhs,
    00805                   const double* rowrng) ;
    00806
    00808
    00812    CoinModel();
    00815     CoinModel(const char *fileName, int allowStrings=0);
    00819     CoinModel( int nonLinear, const char * fileName,const void * info);
    00821   CoinModel(int numberRows, int numberColumns,
    00822             const CoinPackedMatrix * matrix,
    00823             const double * rowLower, const double * rowUpper,
    00824             const double * columnLower, const double * columnUpper,
    00825             const double * objective);
    00827   virtual CoinBaseModel * clone() const;
    00828
    00830    virtual ~CoinModel();
    00832
    00836    CoinModel(const CoinModel&);
    00838    CoinModel& operator=(const CoinModel&);
    00840
    00843
    00844   void validateLinks() const;
    00846 private:
    00848   void resize(int maximumRows, int maximumColumns, int maximumElements);
    00850   void fillRows(int which,bool forceCreation,bool fromAddRow=false);
    00852   void fillColumns(int which,bool forceCreation,bool fromAddColumn=false);
    00855   void fillList(int which, CoinModelLinkedList & list,int type) const ;
    00859   void createList(int type) const;
    00861   int addString(const char * string);
    00865   double getDoubleFromString(CoinYacc & info, const char * string);
    00867   void freeStringMemory(CoinYacc & info);
    00868 public:
    00870   int computeAssociated(double * associated);
    00876   CoinPackedMatrix * quadraticRow(int rowNumber,double * linear,
    00877                                   int & numberBad) const;
    00879   void replaceQuadraticRow(int rowNumber,const double * linear, const CoinPackedMatrix * quadraticPart);
    00884   CoinModel * reorder(const char * mark) const;
    00895   int expandKnapsack(int knapsackRow, int & numberOutput,double * buildObj, CoinBigIndex * buildStart,
    00896                      int * buildRow, double * buildElement,int reConstruct=-1) const;
    00898   void setCutMarker(int size,const int * marker);
    00900   void setPriorities(int size,const int * priorities);
    00902   inline const int * priorities() const
    00903   { return priority_;}
    00905   void setOriginalIndices(const int * row, const int * column);
    00906
    00907 private:
    00911   void gdb( int nonLinear, const char * fileName, const void * info);
    00913   int decodeBit(char * phrase, char * & nextPhrase, double & coefficient, bool ifFirst) const;
    00915   void badType() const;
    00918
    00919   int maximumRows_;
    00921   int maximumColumns_;
    00923   int numberElements_;
    00925   int maximumElements_;
    00927   int numberQuadraticElements_;
    00929   int maximumQuadraticElements_;
    00931   double * rowLower_;
    00933   double * rowUpper_;
    00935   CoinModelHash rowName_;
    00943   int * rowType_;
    00945   double * objective_;
    00947   double * columnLower_;
    00949   double * columnUpper_;
    00951   CoinModelHash columnName_;
    00953   int * integerType_;
    00955   CoinModelHash string_;
    00965   int * columnType_;
    00967   int * start_;
    00969   CoinModelTriple * elements_;
    00971   CoinPackedMatrix * packedMatrix_;
    00973   mutable CoinModelHash2 hashElements_;
    00975   mutable CoinModelLinkedList rowList_;
    00977   mutable CoinModelLinkedList columnList_;
    00979   CoinModelTriple * quadraticElements_;
    00981   mutable CoinModelHash2 hashQuadraticElements_;
    00983   int * sortIndices_;
    00985   double * sortElements_;
    00987   int sortSize_;
    00989   mutable CoinModelLinkedList quadraticRowList_;
    00991   mutable CoinModelLinkedList quadraticColumnList_;
    00993   int sizeAssociated_;
    00995   double * associated_;
    00997   int numberSOS_;
    00999   int * startSOS_;
    01001   int * memberSOS_;
    01003   int * typeSOS_;
    01005   int * prioritySOS_;
    01007   double * referenceSOS_;
    01009   int * priority_;
    01011   int * cut_;
    01013   void * moreInfo_;
    01021   mutable int type_;
    01028   mutable int links_;
    01030 };
    01032 double getFunctionValueFromString(const char * string, const char * x, double xValue);
    01034 double getDoubleFromString(CoinYacc & info, const char * string, const char * x, double xValue);
    01035 #endif