CoinUtils
trunk
|
00001 /* $Id$ */ 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, 00052 COIN_UI_BOUND, COIN_LI_BOUND, COIN_BOTH_BOUNDS_SET, 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 00220 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 00747 int readConicMps(const char * filename, 00748 int * &columnStart, int * &column, int * &coneType, int & numberCones); 00750 inline void setConvertObjective(bool trueFalse) 00751 { convertObjective_=trueFalse;} 00753 int copyStringElements(const CoinModel * model); 00755 00758 00759 CoinMpsIO(); 00760 00762 CoinMpsIO (const CoinMpsIO &); 00763 00765 CoinMpsIO & operator=(const CoinMpsIO& rhs); 00766 00768 ~CoinMpsIO (); 00770 00771 00779 void passInMessageHandler(CoinMessageHandler * handler); 00780 00782 void newLanguage(CoinMessages::Language language); 00783 00785 inline void setLanguage(CoinMessages::Language language) {newLanguage(language);} 00786 00788 inline CoinMessageHandler * messageHandler() const {return handler_;} 00789 00791 inline CoinMessages messages() {return messages_;} 00793 inline CoinMessages * messagesPointer() {return & messages_;} 00795 00796 00807 void releaseRedundantInformation(); 00808 00810 void releaseRowInformation(); 00811 00813 void releaseColumnInformation(); 00814 00816 void releaseIntegerInformation(); 00817 00819 void releaseRowNames(); 00820 00822 void releaseColumnNames(); 00823 00825 void releaseMatrixInformation(); 00827 00828 protected: 00829 00832 00834 void 00835 setMpsDataWithoutRowAndColNames( 00836 const CoinPackedMatrix& m, const double infinity, 00837 const double* collb, const double* colub, 00838 const double* obj, const char* integrality, 00839 const double* rowlb, const double* rowub); 00840 void 00841 setMpsDataColAndRowNames( 00842 const std::vector<std::string> & colnames, 00843 const std::vector<std::string> & rownames); 00844 void 00845 setMpsDataColAndRowNames( 00846 char const * const * const colnames, 00847 char const * const * const rownames); 00848 00849 00851 void gutsOfDestructor(); 00852 00854 void gutsOfCopy(const CoinMpsIO &); 00855 00857 void freeAll(); 00858 00859 00862 inline void 00863 convertBoundToSense(const double lower, const double upper, 00864 char& sense, double& right, double& range) const; 00867 inline void 00868 convertSenseToBound(const char sense, const double right, 00869 const double range, 00870 double& lower, double& upper) const; 00871 00884 int dealWithFileName(const char * filename, const char * extension, 00885 CoinFileInput * &input); 00890 void addString(int iRow,int iColumn, const char * value); 00892 void decodeString(int iString, int & iRow, int & iColumn, const char * & value) const; 00894 00895 00896 // for hashing 00897 typedef struct { 00898 int index, next; 00899 } CoinHashLink; 00900 00903 00904 void startHash ( char **names, const int number , int section ); 00906 void startHash ( int section ) const; 00908 void stopHash ( int section ); 00910 int findHash ( const char *name , int section ) const; 00912 00915 00916 char * problemName_; 00917 00919 char * objectiveName_; 00920 00922 char * rhsName_; 00923 00925 char * rangeName_; 00926 00928 char * boundName_; 00929 00931 int numberRows_; 00932 00934 int numberColumns_; 00935 00937 CoinBigIndex numberElements_; 00938 00940 mutable char *rowsense_; 00941 00943 mutable double *rhs_; 00944 00948 mutable double *rowrange_; 00949 00951 mutable CoinPackedMatrix *matrixByRow_; 00952 00954 CoinPackedMatrix *matrixByColumn_; 00955 00957 double * rowlower_; 00958 00960 double * rowupper_; 00961 00963 double * collower_; 00964 00966 double * colupper_; 00967 00969 double * objective_; 00970 00972 double objectiveOffset_; 00973 00974 00978 char * integerType_; 00979 00983 char **names_[2]; 00985 00988 00989 char * fileName_; 00990 00992 int numberHash_[2]; 00993 00995 mutable CoinHashLink *hash_[2]; 00997 01000 01001 int defaultBound_; 01002 01004 double infinity_; 01006 double smallElement_; 01007 01009 CoinMessageHandler * handler_; 01015 bool defaultHandler_; 01017 CoinMessages messages_; 01019 CoinMpsCardReader * cardReader_; 01021 bool convertObjective_; 01023 int allowStringElements_; 01025 int maximumStringElements_; 01027 int numberStringElements_; 01029 char ** stringElements_; 01031 01032 }; 01033 01034 //############################################################################# 01042 void 01043 CoinMpsIOUnitTest(const std::string & mpsDir); 01044 // Function to return number in most efficient way 01045 // section is 0 for columns, 1 for rhs,ranges and 2 for bounds 01046 /* formatType is 01047 0 - normal and 8 character names 01048 1 - extra accuracy 01049 2 - IEEE hex - INTEL 01050 3 - IEEE hex - not INTEL 01051 */ 01052 void 01053 CoinConvertDouble(int section, int formatType, double value, char outputValue[24]); 01054 01055 #endif 01056