/home/coin/SVN-release/Clp-1.9.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;}
00562 
00563 
00582 
00583     void setFileName(const char * name);
00584 
00586     const char * getFileName() const;
00587 
00592     int readMps(const char *filename, const char *extension = "mps");
00593 
00599      int readMps(const char *filename, const char *extension ,
00600         int & numberSets, CoinSet **& sets);
00601 
00612     int readMps();
00614     int readMps(int & numberSets, CoinSet **& sets);
00624     int readBasis(const char *filename, const char *extension ,
00625                   double * solution, unsigned char *rowStatus, unsigned char *columnStatus,
00626                   const std::vector<std::string> & colnames,int numberColumns,
00627                   const std::vector<std::string> & rownames, int numberRows);
00628 
00634     int readGms(const char *filename, const char *extension = "gms",bool convertObjective=false);
00635 
00641      int readGms(const char *filename, const char *extension ,
00642         int & numberSets, CoinSet **& sets);
00643 
00650     // Not for now int readGms();
00652     int readGms(int & numberSets, CoinSet **& sets);
00655     int readGMPL(const char *modelName, const char * dataName=NULL, bool keepNames=false);
00656 
00682     int writeMps(const char *filename, int compression = 0,
00683                  int formatType = 0, int numberAcross = 2,
00684                  CoinPackedMatrix * quadratic = NULL,
00685                  int numberSOS=0,const CoinSet * setInfo=NULL) const;
00686 
00688     inline const CoinMpsCardReader * reader() const
00689     { return cardReader_;}
00690   
00718     int readQuadraticMps(const char * filename,
00719                          int * &columnStart, int * &column, double * &elements,
00720                          int checkSymmetry);
00721 
00739     int readConicMps(const char * filename,
00740                      int * &columnStart, int * &column, int & numberCones);
00742     inline void setConvertObjective(bool trueFalse)
00743     { convertObjective_=trueFalse;}
00745   int copyStringElements(const CoinModel * model);
00747 
00750 
00751     CoinMpsIO(); 
00752       
00754     CoinMpsIO (const CoinMpsIO &);
00755   
00757     CoinMpsIO & operator=(const CoinMpsIO& rhs);
00758   
00760     ~CoinMpsIO ();
00762 
00763 
00771   void passInMessageHandler(CoinMessageHandler * handler);
00772 
00774   void newLanguage(CoinMessages::Language language);
00775 
00777   void setLanguage(CoinMessages::Language language) {newLanguage(language);}
00778 
00780   CoinMessageHandler * messageHandler() const {return handler_;}
00781 
00783   CoinMessages messages() {return messages_;}
00785   CoinMessages * messagesPointer() {return & messages_;}
00787 
00788 
00799     void releaseRedundantInformation();
00800 
00802     void releaseRowInformation();
00803 
00805     void releaseColumnInformation();
00806 
00808     void releaseIntegerInformation();
00809 
00811     void releaseRowNames();
00812 
00814     void releaseColumnNames();
00815 
00817     void releaseMatrixInformation();
00819 
00820 protected:
00821   
00824 
00826     void
00827     setMpsDataWithoutRowAndColNames(
00828                       const CoinPackedMatrix& m, const double infinity,
00829                       const double* collb, const double* colub,
00830                       const double* obj, const char* integrality,
00831                       const double* rowlb, const double* rowub);
00832     void
00833     setMpsDataColAndRowNames(
00834                       const std::vector<std::string> & colnames,
00835                       const std::vector<std::string> & rownames);
00836     void
00837     setMpsDataColAndRowNames(
00838                       char const * const * const colnames,
00839                       char const * const * const rownames);
00840 
00841   
00843     void gutsOfDestructor();
00844 
00846     void gutsOfCopy(const CoinMpsIO &);
00847   
00849     void freeAll();
00850 
00851 
00854     inline void
00855     convertBoundToSense(const double lower, const double upper,
00856                         char& sense, double& right, double& range) const;
00859     inline void
00860     convertSenseToBound(const char sense, const double right,
00861                         const double range,
00862                         double& lower, double& upper) const;
00863 
00876   int dealWithFileName(const char * filename,  const char * extension,
00877                        CoinFileInput * &input); 
00882   void addString(int iRow,int iColumn, const char * value);
00884   void decodeString(int iString, int & iRow, int & iColumn, const char * & value) const;
00886 
00887   
00888   // for hashing
00889   typedef struct {
00890     int index, next;
00891   } CoinHashLink;
00892 
00895 
00896   void startHash ( char **names, const int number , int section );
00898   void startHash ( int section ) const;
00900   void stopHash ( int section );
00902   int findHash ( const char *name , int section ) const;
00904 
00907 
00908       char * problemName_;
00909 
00911       char * objectiveName_;
00912 
00914       char * rhsName_;
00915 
00917       char * rangeName_;
00918 
00920       char * boundName_;
00921 
00923       int numberRows_;
00924 
00926       int numberColumns_;
00927 
00929       CoinBigIndex numberElements_;
00930 
00932       mutable char    *rowsense_;
00933   
00935       mutable double  *rhs_;
00936   
00940       mutable double  *rowrange_;
00941    
00943       mutable CoinPackedMatrix *matrixByRow_;  
00944 
00946       CoinPackedMatrix *matrixByColumn_;  
00947 
00949       double * rowlower_;
00950 
00952       double * rowupper_;
00953 
00955       double * collower_;
00956 
00958       double * colupper_;
00959 
00961       double * objective_;
00962 
00964       double objectiveOffset_;
00965 
00966 
00970       char * integerType_;
00971 
00975       char **names_[2];
00977 
00980 
00981       char * fileName_;
00982 
00984       int numberHash_[2];
00985 
00987       mutable CoinHashLink *hash_[2];
00989 
00992 
00993       int defaultBound_; 
00994 
00996       double infinity_;
00997 
00999       CoinMessageHandler * handler_;
01005       bool defaultHandler_;
01007       CoinMessages messages_;
01009       CoinMpsCardReader * cardReader_;
01011       bool convertObjective_;
01013       int allowStringElements_;
01015       int maximumStringElements_;
01017       int numberStringElements_;
01019       char ** stringElements_;
01021 
01022 };
01023 
01024 //#############################################################################
01032 void
01033 CoinMpsIOUnitTest(const std::string & mpsDir);
01034 // Function to return number in most efficient way
01035 // section is 0 for columns, 1 for rhs,ranges and 2 for bounds
01036 /* formatType is
01037    0 - normal and 8 character names
01038    1 - extra accuracy
01039    2 - IEEE hex - INTEL
01040    3 - IEEE hex - not INTEL
01041 */
01042 void
01043 CoinConvertDouble(int section, int formatType, double value, char outputValue[24]);
01044 
01045 #endif

Generated on Sat Feb 7 03:01:51 2009 by  doxygen 1.4.7