/home/coin/SVN-release/CoinAll-1.1.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 
00541 
00542     void setInfinity(double value);
00543 
00545     double getInfinity() const;
00546 
00548     void setDefaultBound(int value);
00549 
00551     int getDefaultBound() const;
00553     inline int allowStringElements() const
00554     { return allowStringElements_;}
00556     inline void setAllowStringElements(int yesNo)
00557     { allowStringElements_ = yesNo;}
00559 
00560 
00579 
00580     void setFileName(const char * name);
00581 
00583     const char * getFileName() const;
00584 
00589     int readMps(const char *filename, const char *extension = "mps");
00590 
00596      int readMps(const char *filename, const char *extension ,
00597         int & numberSets, CoinSet **& sets);
00598 
00609     int readMps();
00611     int readMps(int & numberSets, CoinSet **& sets);
00621     int readBasis(const char *filename, const char *extension ,
00622                   double * solution, unsigned char *rowStatus, unsigned char *columnStatus,
00623                   const std::vector<std::string> & colnames,int numberColumns,
00624                   const std::vector<std::string> & rownames, int numberRows);
00625 
00631     int readGms(const char *filename, const char *extension = "gms",bool convertObjective=false);
00632 
00638      int readGms(const char *filename, const char *extension ,
00639         int & numberSets, CoinSet **& sets);
00640 
00647     // Not for now int readGms();
00649     int readGms(int & numberSets, CoinSet **& sets);
00652     int readGMPL(const char *modelName, const char * dataName=NULL, bool keepNames=false);
00653 
00679     int writeMps(const char *filename, int compression = 0,
00680                  int formatType = 0, int numberAcross = 2,
00681                  CoinPackedMatrix * quadratic = NULL,
00682                  int numberSOS=0,const CoinSet * setInfo=NULL) const;
00683 
00685     inline const CoinMpsCardReader * reader() const
00686     { return cardReader_;}
00687   
00715     int readQuadraticMps(const char * filename,
00716                          int * &columnStart, int * &column, double * &elements,
00717                          int checkSymmetry);
00718 
00736     int readConicMps(const char * filename,
00737                      int * &columnStart, int * &column, int & numberCones);
00739     inline void setConvertObjective(bool trueFalse)
00740     { convertObjective_=trueFalse;}
00742   int copyStringElements(const CoinModel * model);
00744 
00747 
00748     CoinMpsIO(); 
00749       
00751     CoinMpsIO (const CoinMpsIO &);
00752   
00754     CoinMpsIO & operator=(const CoinMpsIO& rhs);
00755   
00757     ~CoinMpsIO ();
00759 
00760 
00768   void passInMessageHandler(CoinMessageHandler * handler);
00769 
00771   void newLanguage(CoinMessages::Language language);
00772 
00774   void setLanguage(CoinMessages::Language language) {newLanguage(language);}
00775 
00777   CoinMessageHandler * messageHandler() const {return handler_;}
00778 
00780   CoinMessages messages() {return messages_;}
00782   CoinMessages * messagesPointer() {return & messages_;}
00784 
00785 
00796     void releaseRedundantInformation();
00797 
00799     void releaseRowInformation();
00800 
00802     void releaseColumnInformation();
00803 
00805     void releaseIntegerInformation();
00806 
00808     void releaseRowNames();
00809 
00811     void releaseColumnNames();
00812 
00814     void releaseMatrixInformation();
00816 
00817 protected:
00818   
00821 
00823     void
00824     setMpsDataWithoutRowAndColNames(
00825                       const CoinPackedMatrix& m, const double infinity,
00826                       const double* collb, const double* colub,
00827                       const double* obj, const char* integrality,
00828                       const double* rowlb, const double* rowub);
00829     void
00830     setMpsDataColAndRowNames(
00831                       const std::vector<std::string> & colnames,
00832                       const std::vector<std::string> & rownames);
00833     void
00834     setMpsDataColAndRowNames(
00835                       char const * const * const colnames,
00836                       char const * const * const rownames);
00837 
00838   
00840     void gutsOfDestructor();
00841 
00843     void gutsOfCopy(const CoinMpsIO &);
00844   
00846     void freeAll();
00847 
00848 
00851     inline void
00852     convertBoundToSense(const double lower, const double upper,
00853                         char& sense, double& right, double& range) const;
00856     inline void
00857     convertSenseToBound(const char sense, const double right,
00858                         const double range,
00859                         double& lower, double& upper) const;
00860 
00873   int dealWithFileName(const char * filename,  const char * extension,
00874                        CoinFileInput * &input); 
00879   void addString(int iRow,int iColumn, const char * value);
00881   void decodeString(int iString, int & iRow, int & iColumn, const char * & value) const;
00883 
00884   
00885   // for hashing
00886   typedef struct {
00887     int index, next;
00888   } CoinHashLink;
00889 
00892 
00893   void startHash ( char **names, const int number , int section );
00895   void startHash ( int section ) const;
00897   void stopHash ( int section );
00899   int findHash ( const char *name , int section ) const;
00901 
00904 
00905       char * problemName_;
00906 
00908       char * objectiveName_;
00909 
00911       char * rhsName_;
00912 
00914       char * rangeName_;
00915 
00917       char * boundName_;
00918 
00920       int numberRows_;
00921 
00923       int numberColumns_;
00924 
00926       CoinBigIndex numberElements_;
00927 
00929       mutable char    *rowsense_;
00930   
00932       mutable double  *rhs_;
00933   
00937       mutable double  *rowrange_;
00938    
00940       mutable CoinPackedMatrix *matrixByRow_;  
00941 
00943       CoinPackedMatrix *matrixByColumn_;  
00944 
00946       double * rowlower_;
00947 
00949       double * rowupper_;
00950 
00952       double * collower_;
00953 
00955       double * colupper_;
00956 
00958       double * objective_;
00959 
00961       double objectiveOffset_;
00962 
00963 
00967       char * integerType_;
00968 
00972       char **names_[2];
00974 
00977 
00978       char * fileName_;
00979 
00981       int numberHash_[2];
00982 
00984       mutable CoinHashLink *hash_[2];
00986 
00989 
00990       int defaultBound_; 
00991 
00993       double infinity_;
00994 
00996       CoinMessageHandler * handler_;
01002       bool defaultHandler_;
01004       CoinMessages messages_;
01006       CoinMpsCardReader * cardReader_;
01008       bool convertObjective_;
01010       int allowStringElements_;
01012       int maximumStringElements_;
01014       int numberStringElements_;
01016       char ** stringElements_;
01018 
01019 };
01020 
01021 //#############################################################################
01029 void
01030 CoinMpsIOUnitTest(const std::string & mpsDir);
01031 // Function to return number in most efficient way
01032 // section is 0 for columns, 1 for rhs,ranges and 2 for bounds
01033 /* formatType is
01034    0 - normal and 8 character names
01035    1 - extra accuracy
01036    2 - IEEE hex - INTEL
01037    3 - IEEE hex - not INTEL
01038 */
01039 void
01040 CoinConvertDouble(int section, int formatType, double value, char outputValue[24]);
01041 
01042 #endif

Generated on Sun Nov 14 14:06:32 2010 for Coin-All by  doxygen 1.4.7