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

- File List
- File Members

# /home/coin/SVN-release/CoinUtils-2.6.1/CoinUtils/src/CoinMpsIO.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinMpsIO.hpp 1215 2009-11-05 11:03:04Z forrest $ */
    00002 // Copyright (C) 2000, International Business Machines
    00003 // Corporation and others.  All Rights Reserved.
    00004 #ifndef CoinMpsIO_H
    00005 #define CoinMpsIO_H
    00006
    00007 #if defined(_MSC_VER)
    00008 // Turn off compiler warning about long names
    00009 #  pragma warning(disable:4786)
    00010 #endif
    00011
    00012 #include <vector>
    00013 #include <string>
    00014
    00015 #include "CoinUtilsConfig.h"
    00016 #include "CoinPackedMatrix.hpp"
    00017 #include "CoinMessageHandler.hpp"
    00018 #include "CoinFileIO.hpp"
    00019 class CoinModel;
    00020 // Plus infinity
    00021 #ifndef COIN_DBL_MAX
    00022 #define COIN_DBL_MAX DBL_MAX
    00023 #endif
    00024
    00029 typedef int COINColumnIndex;
    00030
    00032 typedef int COINRowIndex;
    00033
    00034 // We are allowing free format - but there is a limit!
    00035 // User can override by using CXXFLAGS += -DCOIN_MAX_FIELD_LENGTH=nnn
    00036 #ifndef COIN_MAX_FIELD_LENGTH
    00037 #define COIN_MAX_FIELD_LENGTH 160
    00038 #endif
    00039 #define MAX_CARD_LENGTH 5*COIN_MAX_FIELD_LENGTH+80
    00040
    00041 enum COINSectionType { COIN_NO_SECTION, COIN_NAME_SECTION, COIN_ROW_SECTION,
    00042                        COIN_COLUMN_SECTION,
    00043                        COIN_RHS_SECTION, COIN_RANGES_SECTION, COIN_BOUNDS_SECTION,
    00044                        COIN_ENDATA_SECTION, COIN_EOF_SECTION, COIN_QUADRATIC_SECTION,
    00045                        COIN_CONIC_SECTION,COIN_QUAD_SECTION,COIN_SOS_SECTION,
    00046                        COIN_BASIS_SECTION,COIN_UNKNOWN_SECTION
    00047 };
    00048
    00049 enum COINMpsType { COIN_N_ROW, COIN_E_ROW, COIN_L_ROW, COIN_G_ROW,
    00050   COIN_BLANK_COLUMN, COIN_S1_COLUMN, COIN_S2_COLUMN, COIN_S3_COLUMN,
    00051   COIN_INTORG, COIN_INTEND, COIN_SOSEND, COIN_UNSET_BOUND,
    00052   COIN_UP_BOUND, COIN_FX_BOUND, COIN_LO_BOUND, COIN_FR_BOUND,
    00053                    COIN_MI_BOUND, COIN_PL_BOUND, COIN_BV_BOUND, COIN_UI_BOUND, COIN_LI_BOUND,
    00054                    COIN_SC_BOUND, COIN_S1_BOUND, COIN_S2_BOUND,
    00055                    COIN_BS_BASIS, COIN_XL_BASIS, COIN_XU_BASIS,
    00056                    COIN_LL_BASIS, COIN_UL_BASIS, COIN_UNKNOWN_MPS_TYPE
    00057 };
    00058 class CoinMpsIO;
    00060 class CoinMpsCardReader {
    00061
    00062 public:
    00063
    00066
    00067
    00068   CoinMpsCardReader ( CoinFileInput *input, CoinMpsIO * reader );
    00069
    00071   ~CoinMpsCardReader (  );
    00073
    00074
    00077
    00078   COINSectionType readToNextSection (  );
    00080   COINSectionType nextField (  );
    00094   int nextGmsField ( int expectedType );
    00096   inline COINSectionType whichSection (  ) const {
    00097     return section_;
    00098   }
    00100   inline void setWhichSection(COINSectionType section  ) {
    00101     section_=section;
    00102   }
    00104   inline bool freeFormat() const
    00105   { return freeFormat_;}
    00107   inline void setFreeFormat(bool yesNo)
    00108   { freeFormat_=yesNo;}
    00111   inline COINMpsType mpsType (  ) const {
    00112     return mpsType_;
    00113   }
    00115   int cleanCard();
    00117   inline const char *rowName (  ) const {
    00118     return rowName_;
    00119   }
    00121   inline const char *columnName (  ) const {
    00122     return columnName_;
    00123   }
    00125   inline double value (  ) const {
    00126     return value_;
    00127   }
    00129   inline const char *valueString (  ) const {
    00130     return valueString_;
    00131   }
    00133   inline const char *card (  ) const {
    00134     return card_;
    00135   }
    00137   inline char *mutableCard (  ) {
    00138     return card_;
    00139   }
    00141   inline void setPosition(char * position)
    00142   { position_=position;}
    00144   inline char * getPosition() const
    00145   { return position_;}
    00147   inline CoinBigIndex cardNumber (  ) const {
    00148     return cardNumber_;
    00149   }
    00151   inline CoinFileInput * fileInput (  ) const {
    00152     return input_;
    00153   }
    00155   inline void setStringsAllowed()
    00156   { stringsAllowed_=true;}
    00158
    00160 protected:
    00161
    00164
    00165   double value_;
    00167   char card_[MAX_CARD_LENGTH];
    00169   char *position_;
    00171   char *eol_;
    00173   COINMpsType mpsType_;
    00175   char rowName_[COIN_MAX_FIELD_LENGTH];
    00177   char columnName_[COIN_MAX_FIELD_LENGTH];
    00179   CoinFileInput *input_;
    00181   COINSectionType section_;
    00183   CoinBigIndex cardNumber_;
    00185   bool freeFormat_;
    00187   int ieeeFormat_;
    00189   bool eightChar_;
    00191   CoinMpsIO * reader_;
    00193   CoinMessageHandler * handler_;
    00195   CoinMessages messages_;
    00197   char valueString_[COIN_MAX_FIELD_LENGTH];
    00199   bool stringsAllowed_;
    00201 public:
    00204
    00205   double osi_strtod(char * ptr, char ** output, int type);
    00207   static void strcpyAndCompress ( char *to, const char *from );
    00209   static char * nextBlankOr ( char *image );
    00211   double osi_strtod(char * ptr, char ** output);
    00213
    00214 };
    00215
    00216 //#############################################################################
    00217 #ifdef USE_SBB
    00218 class SbbObject;
    00219 class SbbModel;
    00220 #endif
    00222 class CoinSet {
    00223
    00224 public:
    00225
    00228
    00229   CoinSet ( );
    00231   CoinSet ( int numberEntries, const int * which);
    00232
    00234   CoinSet (const CoinSet &);
    00235
    00237   CoinSet & operator=(const CoinSet& rhs);
    00238
    00240   virtual ~CoinSet (  );
    00242
    00243
    00246
    00247   inline int numberEntries (  ) const
    00248   { return numberEntries_;  }
    00250   inline int setType (  ) const
    00251   { return setType_;  }
    00253   inline const int * which (  ) const
    00254   { return which_;  }
    00256   inline const double * weights (  ) const
    00257   { return weights_;  }
    00259
    00260 #ifdef USE_SBB
    00261
    00263
    00264   virtual SbbObject * sbbObject(SbbModel * model) const
    00265   { return NULL;}
    00267 #endif
    00268
    00270 protected:
    00271
    00274
    00275   int numberEntries_;
    00277   int setType_;
    00279   int * which_;
    00281   double * weights_;
    00283 };
    00284
    00285 //#############################################################################
    00287 class CoinSosSet : public CoinSet{
    00288
    00289 public:
    00290
    00293
    00294   CoinSosSet ( int numberEntries, const int * which, const double * weights, int type);
    00295
    00297   virtual ~CoinSosSet (  );
    00299
    00300
    00301 #ifdef USE_SBB
    00302
    00304
    00305   virtual SbbObject * sbbObject(SbbModel * model) const ;
    00307 #endif
    00308
    00310 protected:
    00311
    00314
    00315 };
    00316
    00317 //#############################################################################
    00318
    00330 class CoinMpsIO {
    00331    friend void CoinMpsIOUnitTest(const std::string & mpsDir);
    00332
    00333 public:
    00334
    00346
    00347     int getNumCols() const;
    00348
    00350     int getNumRows() const;
    00351
    00353     int getNumElements() const;
    00354
    00356     const double * getColLower() const;
    00357
    00359     const double * getColUpper() const;
    00360
    00370     const char * getRowSense() const;
    00371
    00383     const double * getRightHandSide() const;
    00384
    00398     const double * getRowRange() const;
    00399
    00401     const double * getRowLower() const;
    00402
    00404     const double * getRowUpper() const;
    00405
    00407     const double * getObjCoefficients() const;
    00408
    00410     const CoinPackedMatrix * getMatrixByRow() const;
    00411
    00413     const CoinPackedMatrix * getMatrixByCol() const;
    00414
    00416     bool isContinuous(int colNumber) const;
    00417
    00423     bool isInteger(int columnNumber) const;
    00424
    00430     const char * integerColumns() const;
    00431
    00436     const char * rowName(int index) const;
    00437
    00442     const char * columnName(int index) const;
    00443
    00450     int rowIndex(const char * name) const;
    00451
    00456     int columnIndex(const char * name) const;
    00457
    00462     double objectiveOffset() const;
    00464     inline void setObjectiveOffset(double value)
    00465     { objectiveOffset_=value;}
    00466
    00468     const char * getProblemName() const;
    00469
    00471     const char * getObjectiveName() const;
    00472
    00474     const char * getRhsName() const;
    00475
    00477     const char * getRangeName() const;
    00478
    00480     const char * getBoundName() const;
    00482     inline int numberStringElements() const
    00483     { return numberStringElements_;}
    00485     inline const char * stringElement(int i) const
    00486     { return stringElements_[i];}
    00488
    00489
    00495
    00497     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00498                      const double* collb, const double* colub,
    00499                      const double* obj, const char* integrality,
    00500                      const double* rowlb, const double* rowub,
    00501                      char const * const * const colnames,
    00502                      char const * const * const rownames);
    00503     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00504                      const double* collb, const double* colub,
    00505                      const double* obj, const char* integrality,
    00506                      const double* rowlb, const double* rowub,
    00507                      const std::vector<std::string> & colnames,
    00508                      const std::vector<std::string> & rownames);
    00509     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00510                      const double* collb, const double* colub,
    00511                      const double* obj, const char* integrality,
    00512                      const char* rowsen, const double* rowrhs,
    00513                      const double* rowrng,
    00514                      char const * const * const colnames,
    00515                      char const * const * const rownames);
    00516     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00517                      const double* collb, const double* colub,
    00518                      const double* obj, const char* integrality,
    00519                      const char* rowsen, const double* rowrhs,
    00520                      const double* rowrng,
    00521                      const std::vector<std::string> & colnames,
    00522                      const std::vector<std::string> & rownames);
    00523
    00529     void copyInIntegerInformation(const char * integerInformation);
    00530
    00532     void setProblemName(const char *name) ;
    00533
    00535     void setObjectiveName(const char *name) ;
    00536
    00538
    00545
    00546     void setInfinity(double value);
    00547
    00549     double getInfinity() const;
    00550
    00552     void setDefaultBound(int value);
    00553
    00555     int getDefaultBound() const;
    00557     inline int allowStringElements() const
    00558     { return allowStringElements_;}
    00560     inline void setAllowStringElements(int yesNo)
    00561     { allowStringElements_ = yesNo;}
    00564     inline double getSmallElementValue() const
    00565     { return smallElement_;}
    00566     inline void setSmallElementValue(double value)
    00567     { smallElement_=value;}
    00569
    00570
    00589
    00590     void setFileName(const char * name);
    00591
    00593     const char * getFileName() const;
    00594
    00599     int readMps(const char *filename, const char *extension = "mps");
    00600
    00606      int readMps(const char *filename, const char *extension ,
    00607         int & numberSets, CoinSet **& sets);
    00608
    00619     int readMps();
    00621     int readMps(int & numberSets, CoinSet **& sets);
    00631     int readBasis(const char *filename, const char *extension ,
    00632                   double * solution, unsigned char *rowStatus, unsigned char *columnStatus,
    00633                   const std::vector<std::string> & colnames,int numberColumns,
    00634                   const std::vector<std::string> & rownames, int numberRows);
    00635
    00641     int readGms(const char *filename, const char *extension = "gms",bool convertObjective=false);
    00642
    00648      int readGms(const char *filename, const char *extension ,
    00649         int & numberSets, CoinSet **& sets);
    00650
    00657     // Not for now int readGms();
    00659     int readGms(int & numberSets, CoinSet **& sets);
    00662     int readGMPL(const char *modelName, const char * dataName=NULL, bool keepNames=false);
    00663
    00689     int writeMps(const char *filename, int compression = 0,
    00690                  int formatType = 0, int numberAcross = 2,
    00691                  CoinPackedMatrix * quadratic = NULL,
    00692                  int numberSOS=0,const CoinSet * setInfo=NULL) const;
    00693
    00695     inline const CoinMpsCardReader * reader() const
    00696     { return cardReader_;}
    00697
    00725     int readQuadraticMps(const char * filename,
    00726                          int * &columnStart, int * &column, double * &elements,
    00727                          int checkSymmetry);
    00728
    00746     int readConicMps(const char * filename,
    00747                      int * &columnStart, int * &column, int & numberCones);
    00749     inline void setConvertObjective(bool trueFalse)
    00750     { convertObjective_=trueFalse;}
    00752   int copyStringElements(const CoinModel * model);
    00754
    00757
    00758     CoinMpsIO();
    00759
    00761     CoinMpsIO (const CoinMpsIO &);
    00762
    00764     CoinMpsIO & operator=(const CoinMpsIO& rhs);
    00765
    00767     ~CoinMpsIO ();
    00769
    00770
    00778   void passInMessageHandler(CoinMessageHandler * handler);
    00779
    00781   void newLanguage(CoinMessages::Language language);
    00782
    00784   void setLanguage(CoinMessages::Language language) {newLanguage(language);}
    00785
    00787   CoinMessageHandler * messageHandler() const {return handler_;}
    00788
    00790   CoinMessages messages() {return messages_;}
    00792   CoinMessages * messagesPointer() {return & messages_;}
    00794
    00795
    00806     void releaseRedundantInformation();
    00807
    00809     void releaseRowInformation();
    00810
    00812     void releaseColumnInformation();
    00813
    00815     void releaseIntegerInformation();
    00816
    00818     void releaseRowNames();
    00819
    00821     void releaseColumnNames();
    00822
    00824     void releaseMatrixInformation();
    00826
    00827 protected:
    00828
    00831
    00833     void
    00834     setMpsDataWithoutRowAndColNames(
    00835                       const CoinPackedMatrix& m, const double infinity,
    00836                       const double* collb, const double* colub,
    00837                       const double* obj, const char* integrality,
    00838                       const double* rowlb, const double* rowub);
    00839     void
    00840     setMpsDataColAndRowNames(
    00841                       const std::vector<std::string> & colnames,
    00842                       const std::vector<std::string> & rownames);
    00843     void
    00844     setMpsDataColAndRowNames(
    00845                       char const * const * const colnames,
    00846                       char const * const * const rownames);
    00847
    00848
    00850     void gutsOfDestructor();
    00851
    00853     void gutsOfCopy(const CoinMpsIO &);
    00854
    00856     void freeAll();
    00857
    00858
    00861     inline void
    00862     convertBoundToSense(const double lower, const double upper,
    00863                         char& sense, double& right, double& range) const;
    00866     inline void
    00867     convertSenseToBound(const char sense, const double right,
    00868                         const double range,
    00869                         double& lower, double& upper) const;
    00870
    00883   int dealWithFileName(const char * filename,  const char * extension,
    00884                        CoinFileInput * &input);
    00889   void addString(int iRow,int iColumn, const char * value);
    00891   void decodeString(int iString, int & iRow, int & iColumn, const char * & value) const;
    00893
    00894
    00895   // for hashing
    00896   typedef struct {
    00897     int index, next;
    00898   } CoinHashLink;
    00899
    00902
    00903   void startHash ( char **names, const int number , int section );
    00905   void startHash ( int section ) const;
    00907   void stopHash ( int section );
    00909   int findHash ( const char *name , int section ) const;
    00911
    00914
    00915       char * problemName_;
    00916
    00918       char * objectiveName_;
    00919
    00921       char * rhsName_;
    00922
    00924       char * rangeName_;
    00925
    00927       char * boundName_;
    00928
    00930       int numberRows_;
    00931
    00933       int numberColumns_;
    00934
    00936       CoinBigIndex numberElements_;
    00937
    00939       mutable char    *rowsense_;
    00940
    00942       mutable double  *rhs_;
    00943
    00947       mutable double  *rowrange_;
    00948
    00950       mutable CoinPackedMatrix *matrixByRow_;
    00951
    00953       CoinPackedMatrix *matrixByColumn_;
    00954
    00956       double * rowlower_;
    00957
    00959       double * rowupper_;
    00960
    00962       double * collower_;
    00963
    00965       double * colupper_;
    00966
    00968       double * objective_;
    00969
    00971       double objectiveOffset_;
    00972
    00973
    00977       char * integerType_;
    00978
    00982       char **names_[2];
    00984
    00987
    00988       char * fileName_;
    00989
    00991       int numberHash_[2];
    00992
    00994       mutable CoinHashLink *hash_[2];
    00996
    00999
    01000       int defaultBound_;
    01001
    01003       double infinity_;
    01005       double smallElement_;
    01006
    01008       CoinMessageHandler * handler_;
    01014       bool defaultHandler_;
    01016       CoinMessages messages_;
    01018       CoinMpsCardReader * cardReader_;
    01020       bool convertObjective_;
    01022       int allowStringElements_;
    01024       int maximumStringElements_;
    01026       int numberStringElements_;
    01028       char ** stringElements_;
    01030
    01031 };
    01032
    01033 //#############################################################################
    01041 void
    01042 CoinMpsIOUnitTest(const std::string & mpsDir);
    01043 // Function to return number in most efficient way
    01044 // section is 0 for columns, 1 for rhs,ranges and 2 for bounds
    01045 /* formatType is
    01046    0 - normal and 8 character names
    01047    1 - extra accuracy
    01048    2 - IEEE hex - INTEL
    01049    3 - IEEE hex - not INTEL
    01050 */
    01051 void
    01052 CoinConvertDouble(int section, int formatType, double value, char outputValue[24]);
    01053
    01054 #endif