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

- File List
- File Members

# /home/coin/svn-release/Alps-1.4.5/CoinUtils/src/CoinMpsIO.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinMpsIO.hpp 1419 2011-04-29 17:39:07Z stefan $ */
    00002 // Copyright (C) 2000, 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 CoinMpsIO_H
    00007 #define CoinMpsIO_H
    00008
    00009 #if defined(_MSC_VER)
    00010 // Turn off compiler warning about long names
    00011 #  pragma warning(disable:4786)
    00012 #endif
    00013
    00014 #include <vector>
    00015 #include <string>
    00016
    00017 #include "CoinUtilsConfig.h"
    00018 #include "CoinPackedMatrix.hpp"
    00019 #include "CoinMessageHandler.hpp"
    00020 #include "CoinFileIO.hpp"
    00021 class CoinModel;
    00022
    00027 typedef int COINColumnIndex;
    00028
    00030 typedef int COINRowIndex;
    00031
    00032 // We are allowing free format - but there is a limit!
    00033 // User can override by using CXXFLAGS += -DCOIN_MAX_FIELD_LENGTH=nnn
    00034 #ifndef COIN_MAX_FIELD_LENGTH
    00035 #define COIN_MAX_FIELD_LENGTH 160
    00036 #endif
    00037 #define MAX_CARD_LENGTH 5*COIN_MAX_FIELD_LENGTH+80
    00038
    00039 enum COINSectionType { COIN_NO_SECTION, COIN_NAME_SECTION, COIN_ROW_SECTION,
    00040                        COIN_COLUMN_SECTION,
    00041                        COIN_RHS_SECTION, COIN_RANGES_SECTION, COIN_BOUNDS_SECTION,
    00042                        COIN_ENDATA_SECTION, COIN_EOF_SECTION, COIN_QUADRATIC_SECTION,
    00043                        COIN_CONIC_SECTION,COIN_QUAD_SECTION,COIN_SOS_SECTION,
    00044                        COIN_BASIS_SECTION,COIN_UNKNOWN_SECTION
    00045 };
    00046
    00047 enum COINMpsType { COIN_N_ROW, COIN_E_ROW, COIN_L_ROW, COIN_G_ROW,
    00048   COIN_BLANK_COLUMN, COIN_S1_COLUMN, COIN_S2_COLUMN, COIN_S3_COLUMN,
    00049   COIN_INTORG, COIN_INTEND, COIN_SOSEND, COIN_UNSET_BOUND,
    00050   COIN_UP_BOUND, COIN_FX_BOUND, COIN_LO_BOUND, COIN_FR_BOUND,
    00051                    COIN_MI_BOUND, COIN_PL_BOUND, COIN_BV_BOUND, COIN_UI_BOUND, COIN_LI_BOUND,
    00052                    COIN_SC_BOUND, COIN_S1_BOUND, COIN_S2_BOUND,
    00053                    COIN_BS_BASIS, COIN_XL_BASIS, COIN_XU_BASIS,
    00054                    COIN_LL_BASIS, COIN_UL_BASIS, COIN_UNKNOWN_MPS_TYPE
    00055 };
    00056 class CoinMpsIO;
    00058 class CoinMpsCardReader {
    00059
    00060 public:
    00061
    00064
    00065
    00066   CoinMpsCardReader ( CoinFileInput *input, CoinMpsIO * reader );
    00067
    00069   ~CoinMpsCardReader (  );
    00071
    00072
    00075
    00076   COINSectionType readToNextSection (  );
    00078   COINSectionType nextField (  );
    00092   int nextGmsField ( int expectedType );
    00094   inline COINSectionType whichSection (  ) const {
    00095     return section_;
    00096   }
    00098   inline void setWhichSection(COINSectionType section  ) {
    00099     section_=section;
    00100   }
    00102   inline bool freeFormat() const
    00103   { return freeFormat_;}
    00105   inline void setFreeFormat(bool yesNo)
    00106   { freeFormat_=yesNo;}
    00109   inline COINMpsType mpsType (  ) const {
    00110     return mpsType_;
    00111   }
    00113   int cleanCard();
    00115   inline const char *rowName (  ) const {
    00116     return rowName_;
    00117   }
    00119   inline const char *columnName (  ) const {
    00120     return columnName_;
    00121   }
    00123   inline double value (  ) const {
    00124     return value_;
    00125   }
    00127   inline const char *valueString (  ) const {
    00128     return valueString_;
    00129   }
    00131   inline const char *card (  ) const {
    00132     return card_;
    00133   }
    00135   inline char *mutableCard (  ) {
    00136     return card_;
    00137   }
    00139   inline void setPosition(char * position)
    00140   { position_=position;}
    00142   inline char * getPosition() const
    00143   { return position_;}
    00145   inline CoinBigIndex cardNumber (  ) const {
    00146     return cardNumber_;
    00147   }
    00149   inline CoinFileInput * fileInput (  ) const {
    00150     return input_;
    00151   }
    00153   inline void setStringsAllowed()
    00154   { stringsAllowed_=true;}
    00156
    00158 protected:
    00159
    00162
    00163   double value_;
    00165   char card_[MAX_CARD_LENGTH];
    00167   char *position_;
    00169   char *eol_;
    00171   COINMpsType mpsType_;
    00173   char rowName_[COIN_MAX_FIELD_LENGTH];
    00175   char columnName_[COIN_MAX_FIELD_LENGTH];
    00177   CoinFileInput *input_;
    00179   COINSectionType section_;
    00181   CoinBigIndex cardNumber_;
    00183   bool freeFormat_;
    00185   int ieeeFormat_;
    00187   bool eightChar_;
    00189   CoinMpsIO * reader_;
    00191   CoinMessageHandler * handler_;
    00193   CoinMessages messages_;
    00195   char valueString_[COIN_MAX_FIELD_LENGTH];
    00197   bool stringsAllowed_;
    00199 public:
    00202
    00203   double osi_strtod(char * ptr, char ** output, int type);
    00205   static void strcpyAndCompress ( char *to, const char *from );
    00207   static char * nextBlankOr ( char *image );
    00209   double osi_strtod(char * ptr, char ** output);
    00211
    00212 };
    00213
    00214 //#############################################################################
    00215 #ifdef USE_SBB
    00216 class SbbObject;
    00217 class SbbModel;
    00218 #endif
    00220 class CoinSet {
    00221
    00222 public:
    00223
    00226
    00227   CoinSet ( );
    00229   CoinSet ( int numberEntries, const int * which);
    00230
    00232   CoinSet (const CoinSet &);
    00233
    00235   CoinSet & operator=(const CoinSet& rhs);
    00236
    00238   virtual ~CoinSet (  );
    00240
    00241
    00244
    00245   inline int numberEntries (  ) const
    00246   { return numberEntries_;  }
    00248   inline int setType (  ) const
    00249   { return setType_;  }
    00251   inline const int * which (  ) const
    00252   { return which_;  }
    00254   inline const double * weights (  ) const
    00255   { return weights_;  }
    00257
    00258 #ifdef USE_SBB
    00259
    00261
    00262   virtual SbbObject * sbbObject(SbbModel * model) const
    00263   { return NULL;}
    00265 #endif
    00266
    00268 protected:
    00269
    00272
    00273   int numberEntries_;
    00275   int setType_;
    00277   int * which_;
    00279   double * weights_;
    00281 };
    00282
    00283 //#############################################################################
    00285 class CoinSosSet : public CoinSet{
    00286
    00287 public:
    00288
    00291
    00292   CoinSosSet ( int numberEntries, const int * which, const double * weights, int type);
    00293
    00295   virtual ~CoinSosSet (  );
    00297
    00298
    00299 #ifdef USE_SBB
    00300
    00302
    00303   virtual SbbObject * sbbObject(SbbModel * model) const ;
    00305 #endif
    00306
    00308 protected:
    00309
    00312
    00313 };
    00314
    00315 //#############################################################################
    00316
    00328 class CoinMpsIO {
    00329    friend void CoinMpsIOUnitTest(const std::string & mpsDir);
    00330
    00331 public:
    00332
    00344
    00345     int getNumCols() const;
    00346
    00348     int getNumRows() const;
    00349
    00351     int getNumElements() const;
    00352
    00354     const double * getColLower() const;
    00355
    00357     const double * getColUpper() const;
    00358
    00368     const char * getRowSense() const;
    00369
    00381     const double * getRightHandSide() const;
    00382
    00396     const double * getRowRange() const;
    00397
    00399     const double * getRowLower() const;
    00400
    00402     const double * getRowUpper() const;
    00403
    00405     const double * getObjCoefficients() const;
    00406
    00408     const CoinPackedMatrix * getMatrixByRow() const;
    00409
    00411     const CoinPackedMatrix * getMatrixByCol() const;
    00412
    00414     bool isContinuous(int colNumber) const;
    00415
    00421     bool isInteger(int columnNumber) const;
    00422
    00428     const char * integerColumns() const;
    00429
    00434     const char * rowName(int index) const;
    00435
    00440     const char * columnName(int index) const;
    00441
    00448     int rowIndex(const char * name) const;
    00449
    00454     int columnIndex(const char * name) const;
    00455
    00460     double objectiveOffset() const;
    00462     inline void setObjectiveOffset(double value)
    00463     { objectiveOffset_=value;}
    00464
    00466     const char * getProblemName() const;
    00467
    00469     const char * getObjectiveName() const;
    00470
    00472     const char * getRhsName() const;
    00473
    00475     const char * getRangeName() const;
    00476
    00478     const char * getBoundName() const;
    00480     inline int numberStringElements() const
    00481     { return numberStringElements_;}
    00483     inline const char * stringElement(int i) const
    00484     { return stringElements_[i];}
    00486
    00487
    00493
    00495     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00496                      const double* collb, const double* colub,
    00497                      const double* obj, const char* integrality,
    00498                      const double* rowlb, const double* rowub,
    00499                      char const * const * const colnames,
    00500                      char const * const * const rownames);
    00501     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00502                      const double* collb, const double* colub,
    00503                      const double* obj, const char* integrality,
    00504                      const double* rowlb, const double* rowub,
    00505                      const std::vector<std::string> & colnames,
    00506                      const std::vector<std::string> & rownames);
    00507     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00508                      const double* collb, const double* colub,
    00509                      const double* obj, const char* integrality,
    00510                      const char* rowsen, const double* rowrhs,
    00511                      const double* rowrng,
    00512                      char const * const * const colnames,
    00513                      char const * const * const rownames);
    00514     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00515                      const double* collb, const double* colub,
    00516                      const double* obj, const char* integrality,
    00517                      const char* rowsen, const double* rowrhs,
    00518                      const double* rowrng,
    00519                      const std::vector<std::string> & colnames,
    00520                      const std::vector<std::string> & rownames);
    00521
    00527     void copyInIntegerInformation(const char * integerInformation);
    00528
    00530     void setProblemName(const char *name) ;
    00531
    00533     void setObjectiveName(const char *name) ;
    00534
    00536
    00543
    00544     void setInfinity(double value);
    00545
    00547     double getInfinity() const;
    00548
    00550     void setDefaultBound(int value);
    00551
    00553     int getDefaultBound() const;
    00555     inline int allowStringElements() const
    00556     { return allowStringElements_;}
    00558     inline void setAllowStringElements(int yesNo)
    00559     { allowStringElements_ = yesNo;}
    00562     inline double getSmallElementValue() const
    00563     { return smallElement_;}
    00564     inline void setSmallElementValue(double value)
    00565     { smallElement_=value;}
    00567
    00568
    00587
    00588     void setFileName(const char * name);
    00589
    00591     const char * getFileName() const;
    00592
    00597     int readMps(const char *filename, const char *extension = "mps");
    00598
    00604      int readMps(const char *filename, const char *extension ,
    00605         int & numberSets, CoinSet **& sets);
    00606
    00617     int readMps();
    00619     int readMps(int & numberSets, CoinSet **& sets);
    00629     int readBasis(const char *filename, const char *extension ,
    00630                   double * solution, unsigned char *rowStatus, unsigned char *columnStatus,
    00631                   const std::vector<std::string> & colnames,int numberColumns,
    00632                   const std::vector<std::string> & rownames, int numberRows);
    00633
    00639     int readGms(const char *filename, const char *extension = "gms",bool convertObjective=false);
    00640
    00646      int readGms(const char *filename, const char *extension ,
    00647         int & numberSets, CoinSet **& sets);
    00648
    00655     // Not for now int readGms();
    00657     int readGms(int & numberSets, CoinSet **& sets);
    00660     int readGMPL(const char *modelName, const char * dataName=NULL, bool keepNames=false);
    00661
    00687     int writeMps(const char *filename, int compression = 0,
    00688                  int formatType = 0, int numberAcross = 2,
    00689                  CoinPackedMatrix * quadratic = NULL,
    00690                  int numberSOS=0,const CoinSet * setInfo=NULL) const;
    00691
    00693     inline const CoinMpsCardReader * reader() const
    00694     { return cardReader_;}
    00695
    00723     int readQuadraticMps(const char * filename,
    00724                          int * &columnStart, int * &column, double * &elements,
    00725                          int checkSymmetry);
    00726
    00744     int readConicMps(const char * filename,
    00745                      int * &columnStart, int * &column, int & numberCones);
    00747     inline void setConvertObjective(bool trueFalse)
    00748     { convertObjective_=trueFalse;}
    00750   int copyStringElements(const CoinModel * model);
    00752
    00755
    00756     CoinMpsIO();
    00757
    00759     CoinMpsIO (const CoinMpsIO &);
    00760
    00762     CoinMpsIO & operator=(const CoinMpsIO& rhs);
    00763
    00765     ~CoinMpsIO ();
    00767
    00768
    00776   void passInMessageHandler(CoinMessageHandler * handler);
    00777
    00779   void newLanguage(CoinMessages::Language language);
    00780
    00782   inline void setLanguage(CoinMessages::Language language) {newLanguage(language);}
    00783
    00785   inline CoinMessageHandler * messageHandler() const {return handler_;}
    00786
    00788   inline CoinMessages messages() {return messages_;}
    00790   inline CoinMessages * messagesPointer() {return & messages_;}
    00792
    00793
    00804     void releaseRedundantInformation();
    00805
    00807     void releaseRowInformation();
    00808
    00810     void releaseColumnInformation();
    00811
    00813     void releaseIntegerInformation();
    00814
    00816     void releaseRowNames();
    00817
    00819     void releaseColumnNames();
    00820
    00822     void releaseMatrixInformation();
    00824
    00825 protected:
    00826
    00829
    00831     void
    00832     setMpsDataWithoutRowAndColNames(
    00833                       const CoinPackedMatrix& m, const double infinity,
    00834                       const double* collb, const double* colub,
    00835                       const double* obj, const char* integrality,
    00836                       const double* rowlb, const double* rowub);
    00837     void
    00838     setMpsDataColAndRowNames(
    00839                       const std::vector<std::string> & colnames,
    00840                       const std::vector<std::string> & rownames);
    00841     void
    00842     setMpsDataColAndRowNames(
    00843                       char const * const * const colnames,
    00844                       char const * const * const rownames);
    00845
    00846
    00848     void gutsOfDestructor();
    00849
    00851     void gutsOfCopy(const CoinMpsIO &);
    00852
    00854     void freeAll();
    00855
    00856
    00859     inline void
    00860     convertBoundToSense(const double lower, const double upper,
    00861                         char& sense, double& right, double& range) const;
    00864     inline void
    00865     convertSenseToBound(const char sense, const double right,
    00866                         const double range,
    00867                         double& lower, double& upper) const;
    00868
    00881   int dealWithFileName(const char * filename,  const char * extension,
    00882                        CoinFileInput * &input);
    00887   void addString(int iRow,int iColumn, const char * value);
    00889   void decodeString(int iString, int & iRow, int & iColumn, const char * & value) const;
    00891
    00892
    00893   // for hashing
    00894   typedef struct {
    00895     int index, next;
    00896   } CoinHashLink;
    00897
    00900
    00901   void startHash ( char **names, const int number , int section );
    00903   void startHash ( int section ) const;
    00905   void stopHash ( int section );
    00907   int findHash ( const char *name , int section ) const;
    00909
    00912
    00913       char * problemName_;
    00914
    00916       char * objectiveName_;
    00917
    00919       char * rhsName_;
    00920
    00922       char * rangeName_;
    00923
    00925       char * boundName_;
    00926
    00928       int numberRows_;
    00929
    00931       int numberColumns_;
    00932
    00934       CoinBigIndex numberElements_;
    00935
    00937       mutable char    *rowsense_;
    00938
    00940       mutable double  *rhs_;
    00941
    00945       mutable double  *rowrange_;
    00946
    00948       mutable CoinPackedMatrix *matrixByRow_;
    00949
    00951       CoinPackedMatrix *matrixByColumn_;
    00952
    00954       double * rowlower_;
    00955
    00957       double * rowupper_;
    00958
    00960       double * collower_;
    00961
    00963       double * colupper_;
    00964
    00966       double * objective_;
    00967
    00969       double objectiveOffset_;
    00970
    00971
    00975       char * integerType_;
    00976
    00980       char **names_[2];
    00982
    00985
    00986       char * fileName_;
    00987
    00989       int numberHash_[2];
    00990
    00992       mutable CoinHashLink *hash_[2];
    00994
    00997
    00998       int defaultBound_;
    00999
    01001       double infinity_;
    01003       double smallElement_;
    01004
    01006       CoinMessageHandler * handler_;
    01012       bool defaultHandler_;
    01014       CoinMessages messages_;
    01016       CoinMpsCardReader * cardReader_;
    01018       bool convertObjective_;
    01020       int allowStringElements_;
    01022       int maximumStringElements_;
    01024       int numberStringElements_;
    01026       char ** stringElements_;
    01028
    01029 };
    01030
    01031 //#############################################################################
    01039 void
    01040 CoinMpsIOUnitTest(const std::string & mpsDir);
    01041 // Function to return number in most efficient way
    01042 // section is 0 for columns, 1 for rhs,ranges and 2 for bounds
    01043 /* formatType is
    01044    0 - normal and 8 character names
    01045    1 - extra accuracy
    01046    2 - IEEE hex - INTEL
    01047    3 - IEEE hex - not INTEL
    01048 */
    01049 void
    01050 CoinConvertDouble(int section, int formatType, double value, char outputValue[24]);
    01051
    01052 #endif