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

- File List
- File Members

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

Go to the documentation of this file.

    00001 // Copyright (C) 2000, International Business Machines
    00002 // Corporation and others.  All Rights Reserved.
    00003 #ifndef CoinMpsIO_H
    00004 #define CoinMpsIO_H
    00005
    00006 #if defined(_MSC_VER)
    00007 // Turn off compiler warning about long names
    00008 #  pragma warning(disable:4786)
    00009 #endif
    00010
    00011 #include <vector>
    00012 #include <string>
    00013
    00014 #include "CoinUtilsConfig.h"
    00015 #include "CoinPackedMatrix.hpp"
    00016 #include "CoinMessageHandler.hpp"
    00017 #include "CoinFileIO.hpp"
    00018 class CoinModel;
    00019 // Plus infinity
    00020 #ifndef COIN_DBL_MAX
    00021 #define COIN_DBL_MAX DBL_MAX
    00022 #endif
    00023
    00028 typedef int COINColumnIndex;
    00029
    00031 typedef int COINRowIndex;
    00032
    00033 // We are allowing free format - but there is a limit!
    00034 // User can override by using CXXFLAGS += -DCOIN_MAX_FIELD_LENGTH=nnn
    00035 #ifndef COIN_MAX_FIELD_LENGTH
    00036 #define COIN_MAX_FIELD_LENGTH 160
    00037 #endif
    00038 #define MAX_CARD_LENGTH 5*COIN_MAX_FIELD_LENGTH+80
    00039
    00040 enum COINSectionType { COIN_NO_SECTION, COIN_NAME_SECTION, COIN_ROW_SECTION,
    00041                        COIN_COLUMN_SECTION,
    00042                        COIN_RHS_SECTION, COIN_RANGES_SECTION, COIN_BOUNDS_SECTION,
    00043                        COIN_ENDATA_SECTION, COIN_EOF_SECTION, COIN_QUADRATIC_SECTION,
    00044                        COIN_CONIC_SECTION,COIN_QUAD_SECTION,COIN_SOS_SECTION,
    00045                        COIN_BASIS_SECTION,COIN_UNKNOWN_SECTION
    00046 };
    00047
    00048 enum COINMpsType { COIN_N_ROW, COIN_E_ROW, COIN_L_ROW, COIN_G_ROW,
    00049   COIN_BLANK_COLUMN, COIN_S1_COLUMN, COIN_S2_COLUMN, COIN_S3_COLUMN,
    00050   COIN_INTORG, COIN_INTEND, COIN_SOSEND, COIN_UNSET_BOUND,
    00051   COIN_UP_BOUND, COIN_FX_BOUND, COIN_LO_BOUND, COIN_FR_BOUND,
    00052                    COIN_MI_BOUND, COIN_PL_BOUND, COIN_BV_BOUND, COIN_UI_BOUND, COIN_LI_BOUND,
    00053                    COIN_SC_BOUND, COIN_S1_BOUND, COIN_S2_BOUND,
    00054                    COIN_BS_BASIS, COIN_XL_BASIS, COIN_XU_BASIS,
    00055                    COIN_LL_BASIS, COIN_UL_BASIS, COIN_UNKNOWN_MPS_TYPE
    00056 };
    00057 class CoinMpsIO;
    00059 class CoinMpsCardReader {
    00060
    00061 public:
    00062
    00065
    00066
    00067   CoinMpsCardReader ( CoinFileInput *input, CoinMpsIO * reader );
    00068
    00070   ~CoinMpsCardReader (  );
    00072
    00073
    00076
    00077   COINSectionType readToNextSection (  );
    00079   COINSectionType nextField (  );
    00093   int nextGmsField ( int expectedType );
    00095   inline COINSectionType whichSection (  ) const {
    00096     return section_;
    00097   }
    00099   inline void setWhichSection(COINSectionType section  ) {
    00100     section_=section;
    00101   }
    00103   inline bool freeFormat() const
    00104   { return freeFormat_;}
    00106   inline void setFreeFormat(bool yesNo)
    00107   { freeFormat_=yesNo;}
    00110   inline COINMpsType mpsType (  ) const {
    00111     return mpsType_;
    00112   }
    00114   int cleanCard();
    00116   inline const char *rowName (  ) const {
    00117     return rowName_;
    00118   }
    00120   inline const char *columnName (  ) const {
    00121     return columnName_;
    00122   }
    00124   inline double value (  ) const {
    00125     return value_;
    00126   }
    00128   inline const char *valueString (  ) const {
    00129     return valueString_;
    00130   }
    00132   inline const char *card (  ) const {
    00133     return card_;
    00134   }
    00136   inline char *mutableCard (  ) {
    00137     return card_;
    00138   }
    00140   inline void setPosition(char * position)
    00141   { position_=position;}
    00143   inline char * getPosition() const
    00144   { return position_;}
    00146   inline CoinBigIndex cardNumber (  ) const {
    00147     return cardNumber_;
    00148   }
    00150   inline CoinFileInput * fileInput (  ) const {
    00151     return input_;
    00152   }
    00154   inline void setStringsAllowed()
    00155   { stringsAllowed_=true;}
    00157
    00159 protected:
    00160
    00163
    00164   double value_;
    00166   char card_[MAX_CARD_LENGTH];
    00168   char *position_;
    00170   char *eol_;
    00172   COINMpsType mpsType_;
    00174   char rowName_[COIN_MAX_FIELD_LENGTH];
    00176   char columnName_[COIN_MAX_FIELD_LENGTH];
    00178   CoinFileInput *input_;
    00180   COINSectionType section_;
    00182   CoinBigIndex cardNumber_;
    00184   bool freeFormat_;
    00186   int ieeeFormat_;
    00188   bool eightChar_;
    00190   CoinMpsIO * reader_;
    00192   CoinMessageHandler * handler_;
    00194   CoinMessages messages_;
    00196   char valueString_[COIN_MAX_FIELD_LENGTH];
    00198   bool stringsAllowed_;
    00200 public:
    00203
    00204   double osi_strtod(char * ptr, char ** output, int type);
    00206   static void strcpyAndCompress ( char *to, const char *from );
    00208   static char * nextBlankOr ( char *image );
    00210   double osi_strtod(char * ptr, char ** output);
    00212
    00213 };
    00214
    00215 //#############################################################################
    00216 #ifdef USE_SBB
    00217 class SbbObject;
    00218 class SbbModel;
    00219 #endif
    00221 class CoinSet {
    00222
    00223 public:
    00224
    00227
    00228   CoinSet ( );
    00230   CoinSet ( int numberEntries, const int * which);
    00231
    00233   CoinSet (const CoinSet &);
    00234
    00236   CoinSet & operator=(const CoinSet& rhs);
    00237
    00239   virtual ~CoinSet (  );
    00241
    00242
    00245
    00246   inline int numberEntries (  ) const
    00247   { return numberEntries_;  }
    00249   inline int setType (  ) const
    00250   { return setType_;  }
    00252   inline const int * which (  ) const
    00253   { return which_;  }
    00255   inline const double * weights (  ) const
    00256   { return weights_;  }
    00258
    00259 #ifdef USE_SBB
    00260
    00262
    00263   virtual SbbObject * sbbObject(SbbModel * model) const
    00264   { return NULL;}
    00266 #endif
    00267
    00269 protected:
    00270
    00273
    00274   int numberEntries_;
    00276   int setType_;
    00278   int * which_;
    00280   double * weights_;
    00282 };
    00283
    00284 //#############################################################################
    00286 class CoinSosSet : public CoinSet{
    00287
    00288 public:
    00289
    00292
    00293   CoinSosSet ( int numberEntries, const int * which, const double * weights, int type);
    00294
    00296   virtual ~CoinSosSet (  );
    00298
    00299
    00300 #ifdef USE_SBB
    00301
    00303
    00304   virtual SbbObject * sbbObject(SbbModel * model) const ;
    00306 #endif
    00307
    00309 protected:
    00310
    00313
    00314 };
    00315
    00316 //#############################################################################
    00317
    00329 class CoinMpsIO {
    00330    friend void CoinMpsIOUnitTest(const std::string & mpsDir);
    00331
    00332 public:
    00333
    00345
    00346     int getNumCols() const;
    00347
    00349     int getNumRows() const;
    00350
    00352     int getNumElements() const;
    00353
    00355     const double * getColLower() const;
    00356
    00358     const double * getColUpper() const;
    00359
    00369     const char * getRowSense() const;
    00370
    00382     const double * getRightHandSide() const;
    00383
    00397     const double * getRowRange() const;
    00398
    00400     const double * getRowLower() const;
    00401
    00403     const double * getRowUpper() const;
    00404
    00406     const double * getObjCoefficients() const;
    00407
    00409     const CoinPackedMatrix * getMatrixByRow() const;
    00410
    00412     const CoinPackedMatrix * getMatrixByCol() const;
    00413
    00415     bool isContinuous(int colNumber) const;
    00416
    00422     bool isInteger(int columnNumber) const;
    00423
    00429     const char * integerColumns() const;
    00430
    00435     const char * rowName(int index) const;
    00436
    00441     const char * columnName(int index) const;
    00442
    00449     int rowIndex(const char * name) const;
    00450
    00455     int columnIndex(const char * name) const;
    00456
    00461     double objectiveOffset() const;
    00463     inline void setObjectiveOffset(double value)
    00464     { objectiveOffset_=value;}
    00465
    00467     const char * getProblemName() const;
    00468
    00470     const char * getObjectiveName() const;
    00471
    00473     const char * getRhsName() const;
    00474
    00476     const char * getRangeName() const;
    00477
    00479     const char * getBoundName() const;
    00481     inline int numberStringElements() const
    00482     { return numberStringElements_;}
    00484     inline const char * stringElement(int i) const
    00485     { return stringElements_[i];}
    00487
    00488
    00494
    00496     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00497                      const double* collb, const double* colub,
    00498                      const double* obj, const char* integrality,
    00499                      const double* rowlb, const double* rowub,
    00500                      char const * const * const colnames,
    00501                      char const * const * const rownames);
    00502     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00503                      const double* collb, const double* colub,
    00504                      const double* obj, const char* integrality,
    00505                      const double* rowlb, const double* rowub,
    00506                      const std::vector<std::string> & colnames,
    00507                      const std::vector<std::string> & rownames);
    00508     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00509                      const double* collb, const double* colub,
    00510                      const double* obj, const char* integrality,
    00511                      const char* rowsen, const double* rowrhs,
    00512                      const double* rowrng,
    00513                      char const * const * const colnames,
    00514                      char const * const * const rownames);
    00515     void setMpsData(const CoinPackedMatrix& m, const double infinity,
    00516                      const double* collb, const double* colub,
    00517                      const double* obj, const char* integrality,
    00518                      const char* rowsen, const double* rowrhs,
    00519                      const double* rowrng,
    00520                      const std::vector<std::string> & colnames,
    00521                      const std::vector<std::string> & rownames);
    00522
    00528     void copyInIntegerInformation(const char * integerInformation);
    00529
    00531     void setProblemName(const char *name) ;
    00532
    00534     void setObjectiveName(const char *name) ;
    00535
    00537
    00544
    00545     void setInfinity(double value);
    00546
    00548     double getInfinity() const;
    00549
    00551     void setDefaultBound(int value);
    00552
    00554     int getDefaultBound() const;
    00556     inline int allowStringElements() const
    00557     { return allowStringElements_;}
    00559     inline void setAllowStringElements(int yesNo)
    00560     { allowStringElements_ = yesNo;}
    00563     inline double getSmallElementValue() const
    00564     { return smallElement_;}
    00565     inline void setSmallElementValue(double value)
    00566     { smallElement_=value;}
    00568
    00569
    00588
    00589     void setFileName(const char * name);
    00590
    00592     const char * getFileName() const;
    00593
    00598     int readMps(const char *filename, const char *extension = "mps");
    00599
    00605      int readMps(const char *filename, const char *extension ,
    00606         int & numberSets, CoinSet **& sets);
    00607
    00618     int readMps();
    00620     int readMps(int & numberSets, CoinSet **& sets);
    00630     int readBasis(const char *filename, const char *extension ,
    00631                   double * solution, unsigned char *rowStatus, unsigned char *columnStatus,
    00632                   const std::vector<std::string> & colnames,int numberColumns,
    00633                   const std::vector<std::string> & rownames, int numberRows);
    00634
    00640     int readGms(const char *filename, const char *extension = "gms",bool convertObjective=false);
    00641
    00647      int readGms(const char *filename, const char *extension ,
    00648         int & numberSets, CoinSet **& sets);
    00649
    00656     // Not for now int readGms();
    00658     int readGms(int & numberSets, CoinSet **& sets);
    00661     int readGMPL(const char *modelName, const char * dataName=NULL, bool keepNames=false);
    00662
    00688     int writeMps(const char *filename, int compression = 0,
    00689                  int formatType = 0, int numberAcross = 2,
    00690                  CoinPackedMatrix * quadratic = NULL,
    00691                  int numberSOS=0,const CoinSet * setInfo=NULL) const;
    00692
    00694     inline const CoinMpsCardReader * reader() const
    00695     { return cardReader_;}
    00696
    00724     int readQuadraticMps(const char * filename,
    00725                          int * &columnStart, int * &column, double * &elements,
    00726                          int checkSymmetry);
    00727
    00745     int readConicMps(const char * filename,
    00746                      int * &columnStart, int * &column, int & numberCones);
    00748     inline void setConvertObjective(bool trueFalse)
    00749     { convertObjective_=trueFalse;}
    00751   int copyStringElements(const CoinModel * model);
    00753
    00756
    00757     CoinMpsIO();
    00758
    00760     CoinMpsIO (const CoinMpsIO &);
    00761
    00763     CoinMpsIO & operator=(const CoinMpsIO& rhs);
    00764
    00766     ~CoinMpsIO ();
    00768
    00769
    00777   void passInMessageHandler(CoinMessageHandler * handler);
    00778
    00780   void newLanguage(CoinMessages::Language language);
    00781
    00783   void setLanguage(CoinMessages::Language language) {newLanguage(language);}
    00784
    00786   CoinMessageHandler * messageHandler() const {return handler_;}
    00787
    00789   CoinMessages messages() {return messages_;}
    00791   CoinMessages * messagesPointer() {return & messages_;}
    00793
    00794
    00805     void releaseRedundantInformation();
    00806
    00808     void releaseRowInformation();
    00809
    00811     void releaseColumnInformation();
    00812
    00814     void releaseIntegerInformation();
    00815
    00817     void releaseRowNames();
    00818
    00820     void releaseColumnNames();
    00821
    00823     void releaseMatrixInformation();
    00825
    00826 protected:
    00827
    00830
    00832     void
    00833     setMpsDataWithoutRowAndColNames(
    00834                       const CoinPackedMatrix& m, const double infinity,
    00835                       const double* collb, const double* colub,
    00836                       const double* obj, const char* integrality,
    00837                       const double* rowlb, const double* rowub);
    00838     void
    00839     setMpsDataColAndRowNames(
    00840                       const std::vector<std::string> & colnames,
    00841                       const std::vector<std::string> & rownames);
    00842     void
    00843     setMpsDataColAndRowNames(
    00844                       char const * const * const colnames,
    00845                       char const * const * const rownames);
    00846
    00847
    00849     void gutsOfDestructor();
    00850
    00852     void gutsOfCopy(const CoinMpsIO &);
    00853
    00855     void freeAll();
    00856
    00857
    00860     inline void
    00861     convertBoundToSense(const double lower, const double upper,
    00862                         char& sense, double& right, double& range) const;
    00865     inline void
    00866     convertSenseToBound(const char sense, const double right,
    00867                         const double range,
    00868                         double& lower, double& upper) const;
    00869
    00882   int dealWithFileName(const char * filename,  const char * extension,
    00883                        CoinFileInput * &input);
    00888   void addString(int iRow,int iColumn, const char * value);
    00890   void decodeString(int iString, int & iRow, int & iColumn, const char * & value) const;
    00892
    00893
    00894   // for hashing
    00895   typedef struct {
    00896     int index, next;
    00897   } CoinHashLink;
    00898
    00901
    00902   void startHash ( char **names, const int number , int section );
    00904   void startHash ( int section ) const;
    00906   void stopHash ( int section );
    00908   int findHash ( const char *name , int section ) const;
    00910
    00913
    00914       char * problemName_;
    00915
    00917       char * objectiveName_;
    00918
    00920       char * rhsName_;
    00921
    00923       char * rangeName_;
    00924
    00926       char * boundName_;
    00927
    00929       int numberRows_;
    00930
    00932       int numberColumns_;
    00933
    00935       CoinBigIndex numberElements_;
    00936
    00938       mutable char    *rowsense_;
    00939
    00941       mutable double  *rhs_;
    00942
    00946       mutable double  *rowrange_;
    00947
    00949       mutable CoinPackedMatrix *matrixByRow_;
    00950
    00952       CoinPackedMatrix *matrixByColumn_;
    00953
    00955       double * rowlower_;
    00956
    00958       double * rowupper_;
    00959
    00961       double * collower_;
    00962
    00964       double * colupper_;
    00965
    00967       double * objective_;
    00968
    00970       double objectiveOffset_;
    00971
    00972
    00976       char * integerType_;
    00977
    00981       char **names_[2];
    00983
    00986
    00987       char * fileName_;
    00988
    00990       int numberHash_[2];
    00991
    00993       mutable CoinHashLink *hash_[2];
    00995
    00998
    00999       int defaultBound_;
    01000
    01002       double infinity_;
    01004       double smallElement_;
    01005
    01007       CoinMessageHandler * handler_;
    01013       bool defaultHandler_;
    01015       CoinMessages messages_;
    01017       CoinMpsCardReader * cardReader_;
    01019       bool convertObjective_;
    01021       int allowStringElements_;
    01023       int maximumStringElements_;
    01025       int numberStringElements_;
    01027       char ** stringElements_;
    01029
    01030 };
    01031
    01032 //#############################################################################
    01040 void
    01041 CoinMpsIOUnitTest(const std::string & mpsDir);
    01042 // Function to return number in most efficient way
    01043 // section is 0 for columns, 1 for rhs,ranges and 2 for bounds
    01044 /* formatType is
    01045    0 - normal and 8 character names
    01046    1 - extra accuracy
    01047    2 - IEEE hex - INTEL
    01048    3 - IEEE hex - not INTEL
    01049 */
    01050 void
    01051 CoinConvertDouble(int section, int formatType, double value, char outputValue[24]);
    01052
    01053 #endif