- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# /home/coin/svn-release/Alps-1.4.6/CoinUtils/src/CoinModel.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinModel.hpp 1581 2013-04-06 12:48:50Z 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 void zapRowNames()
    00700   { rowName_=CoinModelHash();}
    00702   inline void zapColumnNames()
    00703   { columnName_=CoinModelHash();}
    00705   inline const int * cutMarker() const
    00706   { return cut_;}
    00708   inline double optimizationDirection() const {
    00709     return  optimizationDirection_;
    00710   }
    00712   inline void setOptimizationDirection(double value)
    00713   { optimizationDirection_=value;}
    00715   inline void * moreInfo() const
    00716   { return moreInfo_;}
    00718   inline void setMoreInfo(void * info)
    00719   { moreInfo_ = info;}
    00728   int whatIsSet() const;
    00730
    00748   void loadBlock (const CoinPackedMatrix& matrix,
    00749                   const double* collb, const double* colub,
    00750                   const double* obj,
    00751                   const double* rowlb, const double* rowub) ;
    00768   void loadBlock (const CoinPackedMatrix& matrix,
    00769                   const double* collb, const double* colub,
    00770                   const double* obj,
    00771                   const char* rowsen, const double* rowrhs,
    00772                   const double* rowrng) ;
    00773
    00786   void loadBlock (const int numcols, const int numrows,
    00787                   const CoinBigIndex * start, const int* index,
    00788                   const double* value,
    00789                   const double* collb, const double* colub,
    00790                   const double* obj,
    00791                   const double* rowlb, const double* rowub) ;
    00792
    00805   void loadBlock (const int numcols, const int numrows,
    00806                   const CoinBigIndex * start, const int* index,
    00807                   const double* value,
    00808                   const double* collb, const double* colub,
    00809                   const double* obj,
    00810                   const char* rowsen, const double* rowrhs,
    00811                   const double* rowrng) ;
    00812
    00814
    00818    CoinModel();
    00820    CoinModel(int firstRows, int firstColumns, int firstElements,bool noNames=false);
    00823    CoinModel(const char *fileName, int allowStrings=0);
    00827    CoinModel( int nonLinear, const char * fileName,const void * info);
    00829   CoinModel(int numberRows, int numberColumns,
    00830             const CoinPackedMatrix * matrix,
    00831             const double * rowLower, const double * rowUpper,
    00832             const double * columnLower, const double * columnUpper,
    00833             const double * objective);
    00835   virtual CoinBaseModel * clone() const;
    00836
    00838    virtual ~CoinModel();
    00840
    00844    CoinModel(const CoinModel&);
    00846    CoinModel& operator=(const CoinModel&);
    00848
    00851
    00852   void validateLinks() const;
    00854 private:
    00856   void resize(int maximumRows, int maximumColumns, int maximumElements);
    00858   void fillRows(int which,bool forceCreation,bool fromAddRow=false);
    00860   void fillColumns(int which,bool forceCreation,bool fromAddColumn=false);
    00863   void fillList(int which, CoinModelLinkedList & list,int type) const ;
    00867   void createList(int type) const;
    00869   int addString(const char * string);
    00873   double getDoubleFromString(CoinYacc & info, const char * string);
    00875   void freeStringMemory(CoinYacc & info);
    00876 public:
    00878   int computeAssociated(double * associated);
    00884   CoinPackedMatrix * quadraticRow(int rowNumber,double * linear,
    00885                                   int & numberBad) const;
    00887   void replaceQuadraticRow(int rowNumber,const double * linear, const CoinPackedMatrix * quadraticPart);
    00892   CoinModel * reorder(const char * mark) const;
    00903   int expandKnapsack(int knapsackRow, int & numberOutput,double * buildObj, CoinBigIndex * buildStart,
    00904                      int * buildRow, double * buildElement,int reConstruct=-1) const;
    00906   void setCutMarker(int size,const int * marker);
    00908   void setPriorities(int size,const int * priorities);
    00910   inline const int * priorities() const
    00911   { return priority_;}
    00913   void setOriginalIndices(const int * row, const int * column);
    00914
    00915 private:
    00919   void gdb( int nonLinear, const char * fileName, const void * info);
    00921   int decodeBit(char * phrase, char * & nextPhrase, double & coefficient, bool ifFirst) const;
    00923   void badType() const;
    00926
    00927   int maximumRows_;
    00929   int maximumColumns_;
    00931   int numberElements_;
    00933   int maximumElements_;
    00935   int numberQuadraticElements_;
    00937   int maximumQuadraticElements_;
    00939   double * rowLower_;
    00941   double * rowUpper_;
    00943   CoinModelHash rowName_;
    00951   int * rowType_;
    00953   double * objective_;
    00955   double * columnLower_;
    00957   double * columnUpper_;
    00959   CoinModelHash columnName_;
    00961   int * integerType_;
    00963   CoinModelHash string_;
    00973   int * columnType_;
    00975   int * start_;
    00977   CoinModelTriple * elements_;
    00979   CoinPackedMatrix * packedMatrix_;
    00981   mutable CoinModelHash2 hashElements_;
    00983   mutable CoinModelLinkedList rowList_;
    00985   mutable CoinModelLinkedList columnList_;
    00987   CoinModelTriple * quadraticElements_;
    00989   mutable CoinModelHash2 hashQuadraticElements_;
    00991   int * sortIndices_;
    00993   double * sortElements_;
    00995   int sortSize_;
    00997   mutable CoinModelLinkedList quadraticRowList_;
    00999   mutable CoinModelLinkedList quadraticColumnList_;
    01001   int sizeAssociated_;
    01003   double * associated_;
    01005   int numberSOS_;
    01007   int * startSOS_;
    01009   int * memberSOS_;
    01011   int * typeSOS_;
    01013   int * prioritySOS_;
    01015   double * referenceSOS_;
    01017   int * priority_;
    01019   int * cut_;
    01021   void * moreInfo_;
    01029   mutable int type_;
    01031   bool noNames_;
    01038   mutable int links_;
    01040 };
    01042 double getFunctionValueFromString(const char * string, const char * x, double xValue);
    01044 double getDoubleFromString(CoinYacc & info, const char * string, const char * x, double xValue);
    01045 #endif