/home/coin/SVN-release/Cbc-1.1.1/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 
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 *card (  ) const {
00129     return card_;
00130   };
00132   inline char *mutableCard (  ) {
00133     return card_;
00134   };
00136   inline void setPosition(char * position)
00137   { position_=position;};
00139   inline char * getPosition() const
00140   { return position_;};
00142   inline CoinBigIndex cardNumber (  ) const {
00143     return cardNumber_;
00144   };
00146   inline CoinFileInput * fileInput (  ) const {
00147     return input_;
00148   };
00150 
00152 protected:
00153 
00156 
00157   double value_;
00159   char card_[MAX_CARD_LENGTH];
00161   char *position_;
00163   char *eol_;
00165   COINMpsType mpsType_;
00167   char rowName_[COIN_MAX_FIELD_LENGTH];
00169   char columnName_[COIN_MAX_FIELD_LENGTH];
00171   CoinFileInput *input_;
00173   COINSectionType section_;
00175   CoinBigIndex cardNumber_;
00177   bool freeFormat_;
00179   int ieeeFormat_;
00181   bool eightChar_;
00183   CoinMpsIO * reader_;
00185   CoinMessageHandler * handler_;
00187   CoinMessages messages_;
00189 public:
00192 
00193   static double osi_strtod(char * ptr, char ** output, int type);
00195   static void strcpyAndCompress ( char *to, const char *from );
00197   static char * nextBlankOr ( char *image );
00199 
00200 };
00201 
00202 //#############################################################################
00203 #ifdef USE_SBB
00204 class SbbObject;
00205 class SbbModel;
00206 #endif
00208 class CoinSet {
00209 
00210 public:
00211 
00214 
00215   CoinSet ( int numberEntries, const int * which);
00216 
00218   virtual ~CoinSet (  );
00220 
00221 
00224 
00225   inline int numberEntries (  ) const 
00226   { return numberEntries_;  };
00228   inline int setType (  ) const 
00229   { return setType_;  };
00231   inline const int * which (  ) const 
00232   { return which_;  };
00234 
00235 #ifdef USE_SBB
00236 
00238 
00239   virtual SbbObject * sbbObject(SbbModel * model) const 
00240   { return NULL;};
00242 #endif
00243 
00245 protected:
00246 
00249 
00250   int numberEntries_;
00252   int setType_;
00254   int * which_;
00256 };
00257 
00258 //#############################################################################
00260 class CoinSosSet : public CoinSet{
00261 
00262 public:
00263 
00266 
00267   CoinSosSet ( int numberEntries, const int * which, const double * weights, int type);
00268 
00270   virtual ~CoinSosSet (  );
00272 
00273 
00276 
00277   inline const double * weights (  ) const 
00278   { return weights_;  };
00280 #ifdef USE_SBB
00281 
00283 
00284   virtual SbbObject * sbbObject(SbbModel * model) const ;
00286 #endif
00287 
00289 protected:
00290 
00293 
00294   double * weights_;
00296 };
00297 
00298 //#############################################################################
00299 
00311 class CoinMpsIO {
00312    friend void CoinMpsIOUnitTest(const std::string & mpsDir);
00313 
00314 public:
00315 
00327 
00328     int getNumCols() const;
00329 
00331     int getNumRows() const;
00332 
00334     int getNumElements() const;
00335 
00337     const double * getColLower() const;
00338 
00340     const double * getColUpper() const;
00341 
00351     const char * getRowSense() const;
00352 
00364     const double * getRightHandSide() const;
00365 
00379     const double * getRowRange() const;
00380 
00382     const double * getRowLower() const;
00383 
00385     const double * getRowUpper() const;
00386 
00388     const double * getObjCoefficients() const;
00389 
00391     const CoinPackedMatrix * getMatrixByRow() const;
00392 
00394     const CoinPackedMatrix * getMatrixByCol() const;
00395 
00397     bool isContinuous(int colNumber) const;
00398 
00404     bool isInteger(int columnNumber) const;
00405   
00411     const char * integerColumns() const;
00412 
00417     const char * rowName(int index) const;
00418 
00423     const char * columnName(int index) const;
00424 
00431     int rowIndex(const char * name) const;
00432 
00437     int columnIndex(const char * name) const;
00438 
00443     double objectiveOffset() const;
00445     inline void setObjectiveOffset(double value)
00446     { objectiveOffset_=value;};
00447 
00449     const char * getProblemName() const;
00450 
00452     const char * getObjectiveName() const;
00453 
00455     const char * getRhsName() const;
00456 
00458     const char * getRangeName() const;
00459 
00461     const char * getBoundName() const;
00463 
00464 
00470   
00472     void setMpsData(const CoinPackedMatrix& m, const double infinity,
00473                      const double* collb, const double* colub,
00474                      const double* obj, const char* integrality,
00475                      const double* rowlb, const double* rowub,
00476                      char const * const * const colnames,
00477                      char const * const * const rownames);
00478     void setMpsData(const CoinPackedMatrix& m, const double infinity,
00479                      const double* collb, const double* colub,
00480                      const double* obj, const char* integrality,
00481                      const double* rowlb, const double* rowub,
00482                      const std::vector<std::string> & colnames,
00483                      const std::vector<std::string> & rownames);
00484     void setMpsData(const CoinPackedMatrix& m, const double infinity,
00485                      const double* collb, const double* colub,
00486                      const double* obj, const char* integrality,
00487                      const char* rowsen, const double* rowrhs,
00488                      const double* rowrng,
00489                      char const * const * const colnames,
00490                      char const * const * const rownames);
00491     void setMpsData(const CoinPackedMatrix& m, const double infinity,
00492                      const double* collb, const double* colub,
00493                      const double* obj, const char* integrality,
00494                      const char* rowsen, const double* rowrhs,
00495                      const double* rowrng,
00496                      const std::vector<std::string> & colnames,
00497                      const std::vector<std::string> & rownames);
00498 
00504     void copyInIntegerInformation(const char * integerInformation);
00505 
00507     void setProblemName(const char *name) ;
00508 
00510 
00517 
00518     void setInfinity(double value);
00519 
00521     double getInfinity() const;
00522 
00524     void setDefaultBound(int value);
00525 
00527     int getDefaultBound() const;
00529 
00530 
00549 
00550     void setFileName(const char * name);
00551 
00553     const char * getFileName() const;
00554 
00559     int readMps(const char *filename, const char *extension = "mps");
00560 
00566      int readMps(const char *filename, const char *extension ,
00567         int & numberSets, CoinSet **& sets);
00568 
00579     int readMps();
00581     int readMps(int & numberSets, CoinSet **& sets);
00591     int readBasis(const char *filename, const char *extension ,
00592                   double * solution, unsigned char *rowStatus, unsigned char *columnStatus,
00593                   const std::vector<std::string> & colnames,int numberColumns,
00594                   const std::vector<std::string> & rownames, int numberRows);
00595 
00601     int readGms(const char *filename, const char *extension = "gms",bool convertObjective=false);
00602 
00608      int readGms(const char *filename, const char *extension ,
00609         int & numberSets, CoinSet **& sets);
00610 
00617     // Not for now int readGms();
00619     int readGms(int & numberSets, CoinSet **& sets);
00622     int readGMPL(const char *modelName, const char * dataName=NULL, bool keepNames=false);
00623 
00649     int writeMps(const char *filename, int compression = 0,
00650                  int formatType = 0, int numberAcross = 2,
00651                  CoinPackedMatrix * quadratic = NULL) const;
00652 
00654     inline const CoinMpsCardReader * reader() const
00655     { return cardReader_;};
00656   
00684     int readQuadraticMps(const char * filename,
00685                          int * &columnStart, int * &column, double * &elements,
00686                          int checkSymmetry);
00687 
00705     int readConicMps(const char * filename,
00706                      int * &columnStart, int * &column, int & numberCones);
00708     inline void setConvertObjective(bool trueFalse)
00709     { convertObjective_=trueFalse;};
00711 
00714 
00715     CoinMpsIO(); 
00716       
00718     CoinMpsIO (const CoinMpsIO &);
00719   
00721     CoinMpsIO & operator=(const CoinMpsIO& rhs);
00722   
00724     ~CoinMpsIO ();
00726 
00727 
00735   void passInMessageHandler(CoinMessageHandler * handler);
00736 
00738   void newLanguage(CoinMessages::Language language);
00739 
00741   void setLanguage(CoinMessages::Language language) {newLanguage(language);};
00742 
00744   CoinMessageHandler * messageHandler() const {return handler_;};
00745 
00747   CoinMessages messages() {return messages_;};
00749   CoinMessages * messagesPointer() {return & messages_;};
00751 
00752 
00763     void releaseRedundantInformation();
00764 
00766     void releaseRowInformation();
00767 
00769     void releaseColumnInformation();
00770 
00772     void releaseIntegerInformation();
00773 
00775     void releaseRowNames();
00776 
00778     void releaseColumnNames();
00779 
00781     void releaseMatrixInformation();
00783 
00784 protected:
00785   
00788 
00790     void
00791     setMpsDataWithoutRowAndColNames(
00792                       const CoinPackedMatrix& m, const double infinity,
00793                       const double* collb, const double* colub,
00794                       const double* obj, const char* integrality,
00795                       const double* rowlb, const double* rowub);
00796     void
00797     setMpsDataColAndRowNames(
00798                       const std::vector<std::string> & colnames,
00799                       const std::vector<std::string> & rownames);
00800     void
00801     setMpsDataColAndRowNames(
00802                       char const * const * const colnames,
00803                       char const * const * const rownames);
00804 
00805   
00807     void gutsOfDestructor();
00808 
00810     void gutsOfCopy(const CoinMpsIO &);
00811   
00813     void freeAll();
00814 
00815 
00818     inline void
00819     convertBoundToSense(const double lower, const double upper,
00820                         char& sense, double& right, double& range) const;
00823     inline void
00824     convertSenseToBound(const char sense, const double right,
00825                         const double range,
00826                         double& lower, double& upper) const;
00827 
00840   int dealWithFileName(const char * filename,  const char * extension,
00841                        CoinFileInput * &input); 
00843 
00844   
00845   // for hashing
00846   typedef struct {
00847     int index, next;
00848   } CoinHashLink;
00849 
00852 
00853   void startHash ( char **names, const int number , int section );
00855   void startHash ( int section ) const;
00857   void stopHash ( int section );
00859   int findHash ( const char *name , int section ) const;
00861 
00864 
00865       char * problemName_;
00866 
00868       char * objectiveName_;
00869 
00871       char * rhsName_;
00872 
00874       char * rangeName_;
00875 
00877       char * boundName_;
00878 
00880       int numberRows_;
00881 
00883       int numberColumns_;
00884 
00886       CoinBigIndex numberElements_;
00887 
00889       mutable char    *rowsense_;
00890   
00892       mutable double  *rhs_;
00893   
00897       mutable double  *rowrange_;
00898    
00900       mutable CoinPackedMatrix *matrixByRow_;  
00901 
00903       CoinPackedMatrix *matrixByColumn_;  
00904 
00906       double * rowlower_;
00907 
00909       double * rowupper_;
00910 
00912       double * collower_;
00913 
00915       double * colupper_;
00916 
00918       double * objective_;
00919 
00921       double objectiveOffset_;
00922 
00923 
00927       char * integerType_;
00928 
00932       char **names_[2];
00934 
00937 
00938       char * fileName_;
00939 
00941       int numberHash_[2];
00942 
00944       mutable CoinHashLink *hash_[2];
00946 
00949 
00950       int defaultBound_; 
00951 
00953       double infinity_;
00954 
00956       CoinMessageHandler * handler_;
00962       bool defaultHandler_;
00964       CoinMessages messages_;
00966       CoinMpsCardReader * cardReader_;
00968       bool convertObjective_;
00970 
00971 };
00972 
00973 //#############################################################################
00981 void
00982 CoinMpsIOUnitTest(const std::string & mpsDir);
00983 // Function to return number in most efficient way
00984 // section is 0 for columns, 1 for rhs,ranges and 2 for bounds
00985 /* formatType is
00986    0 - normal and 8 character names
00987    1 - extra accuracy
00988    2 - IEEE hex - INTEL
00989    3 - IEEE hex - not INTEL
00990 */
00991 void
00992 CoinConvertDouble(int section, int formatType, double value, char outputValue[20]);
00993 
00994 #endif

Generated on Thu May 15 21:59:05 2008 by  doxygen 1.4.7