CoinUtils  trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
CoinLpIO.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 // Last edit: 11/5/08
00003 //
00004 // Name:     CoinLpIO.hpp; Support for Lp files
00005 // Author:   Francois Margot
00006 //           Tepper School of Business
00007 //           Carnegie Mellon University, Pittsburgh, PA 15213
00008 //           email: fmargot@andrew.cmu.edu
00009 // Date:     12/28/03
00010 //-----------------------------------------------------------------------------
00011 // Copyright (C) 2003, Francois Margot, International Business Machines
00012 // Corporation and others.  All Rights Reserved.
00013 // This code is licensed under the terms of the Eclipse Public License (EPL).
00014 
00015 #ifndef CoinLpIO_H
00016 #define CoinLpIO_H
00017 
00018 #include <cstdio>
00019 
00020 #include "CoinPackedMatrix.hpp"
00021 #include "CoinMessage.hpp"
00022 
00023 typedef int COINColumnIndex;
00024 
00099 class CoinLpIO {
00100       friend void CoinLpIOUnitTest(const std::string & lpDir); 
00101 public:
00102 
00105 
00106   CoinLpIO(); 
00107   
00109   void gutsOfDestructor(); 
00110  
00112   void gutsOfCopy(const CoinLpIO &); 
00113  
00115   CoinLpIO & operator = (const CoinLpIO& rhs) ; 
00116  
00118   CoinLpIO (const CoinLpIO &); 
00119 
00121   ~CoinLpIO();
00122 
00128   void freePreviousNames(const int section);
00129 
00131   void freeAll();
00133 
00136     inline void
00137     convertBoundToSense(const double lower, const double upper,
00138                         char& sense, double& right, double& range) const;
00139 
00142 
00144   const char * getProblemName() const;
00145 
00147   void setProblemName(const char *name);
00148 
00150   int getNumCols() const;
00151 
00153   int getNumRows() const;
00154 
00156   int getNumElements() const;
00157   
00159   const double * getColLower() const;
00160 
00162   const double * getColUpper() const;
00163 
00165   const double * getRowLower() const;
00166   
00168   const double * getRowUpper() const;
00178   const char * getRowSense() const;
00179   
00191   const double * getRightHandSide() const;
00192   
00206   const double * getRowRange() const;
00207 
00209   const double * getObjCoefficients() const;
00210   
00212   const CoinPackedMatrix * getMatrixByRow() const;
00213 
00215   const CoinPackedMatrix * getMatrixByCol() const;
00216 
00218   const char * getObjName() const;
00219   
00225   void getPreviousRowNames(char const * const * prev, 
00226                            int *card_prev) const;
00227 
00232   void getPreviousColNames(char const * const * prev, 
00233                            int *card_prev) const;
00234 
00237   char const * const * getRowNames() const;
00238   
00240   char const * const *getColNames() const;
00241   
00245   const char * rowName(int index) const;
00246 
00250   const char * columnName(int index) const;
00251 
00255   int rowIndex(const char * name) const;
00256 
00259   int columnIndex(const char * name) const;
00260 
00262   double objectiveOffset() const;
00263   
00265   inline void setObjectiveOffset(double value)
00266   { objectiveOffset_ = value;}
00267   
00270   bool isInteger(int columnNumber) const;
00271   
00273   const char * integerColumns() const;
00275   
00278 
00279   double getInfinity() const;
00280 
00283   void setInfinity(const double);
00284 
00286   double getEpsilon() const;
00287 
00290   void setEpsilon(const double);
00291 
00293   int getNumberAcross() const;
00294 
00297   void setNumberAcross(const int);
00298 
00300   int getDecimals() const;
00301 
00304   void setDecimals(const int);
00306 
00318   void setLpDataWithoutRowAndColNames(
00319                               const CoinPackedMatrix& m,
00320                               const double* collb, const double* colub,
00321                               const double* obj_coeff, 
00322                               const char* integrality,
00323                               const double* rowlb, const double* rowub);
00324 
00337   int is_invalid_name(const char *buff, const bool ranged) const;
00338   
00355   int are_invalid_names(char const * const *vnames, 
00356                                   const int card_vnames,
00357                                   const bool check_ranged) const;
00358   
00361   void setDefaultRowNames();
00362 
00364   void setDefaultColNames();
00365 
00385   void setLpDataRowAndColNames(char const * const * const rownames,
00386                                char const * const * const colnames);
00387 
00402   int writeLp(const char *filename, 
00403               const double epsilon, 
00404               const int numberAcross,
00405               const int decimals,
00406               const bool useRowNames = true);
00407 
00422   int writeLp(FILE *fp, 
00423               const double epsilon, 
00424               const int numberAcross,
00425               const int decimals,
00426               const bool useRowNames = true);
00427 
00430   int writeLp(const char *filename, const bool useRowNames = true);
00431 
00434   int writeLp(FILE *fp, const bool useRowNames = true);
00435 
00440   void readLp(const char *filename, const double epsilon);
00441 
00446   void readLp(const char *filename);
00447 
00453   void readLp(FILE *fp, const double epsilon);
00454 
00459   void readLp(FILE *fp);
00460 
00462   void print() const;
00464 
00471   void passInMessageHandler(CoinMessageHandler * handler);
00472 
00474   void newLanguage(CoinMessages::Language language);
00475 
00477   inline void setLanguage(CoinMessages::Language language) {newLanguage(language);}
00478 
00480   inline CoinMessageHandler * messageHandler() const {return handler_;}
00481 
00483   inline CoinMessages messages() {return messages_;}
00485   inline CoinMessages * messagesPointer() {return & messages_;}
00487 
00488 protected:
00490   char * problemName_;
00491 
00493   CoinMessageHandler * handler_;
00499   bool defaultHandler_;
00501   CoinMessages messages_;
00502 
00504   int numberRows_;
00505   
00507   int numberColumns_;
00508   
00510   int numberElements_;
00511   
00513   mutable CoinPackedMatrix *matrixByColumn_;  
00514   
00516   CoinPackedMatrix *matrixByRow_;  
00517   
00519   double * rowlower_;
00520   
00522   double * rowupper_;
00523   
00525   double * collower_;
00526   
00528   double * colupper_;
00529   
00531   mutable double * rhs_;
00532   
00536   mutable double  *rowrange_;
00537 
00539   mutable char * rowsense_;
00540   
00542   double * objective_;
00543   
00545   double objectiveOffset_;
00546   
00549   char * integerType_;
00550   
00552   char * fileName_;
00553   
00555   double infinity_;
00556 
00558   double epsilon_;
00559 
00561   int numberAcross_;
00562 
00564   int decimals_;
00565 
00567   char *objName_;
00568 
00575   char **previous_names_[2];
00576 
00581   int card_previous_names_[2];
00582 
00587   char **names_[2];
00588 
00589   typedef struct {
00590     int index, next;
00591   } CoinHashLink;
00592 
00596   int maxHash_[2];
00597 
00601   int numberHash_[2];
00602 
00606   mutable CoinHashLink *hash_[2];
00607 
00613   void startHash(char const * const * const names, 
00614                  const COINColumnIndex number, 
00615                  int section);
00616 
00620   void stopHash(int section);
00621 
00626   COINColumnIndex findHash(const char *name, int section) const;
00627 
00632   void insertHash(const char *thisName, int section);
00633 
00636   void out_coeff(FILE *fp, double v, int print_1) const;
00637 
00641   int find_obj(FILE *fp) const;
00642 
00648   int is_subject_to(const char *buff) const;
00649 
00652   int first_is_number(const char *buff) const;
00653 
00656   int is_comment(const char *buff) const;
00657 
00659   void skip_comment(char *buff, FILE *fp) const;
00660 
00662   void scan_next(char *buff, FILE *fp) const;
00663 
00666   int is_free(const char *buff) const;
00667   
00670   int is_inf(const char *buff) const;
00671   
00677   int is_sense(const char *buff) const;
00678 
00688   int is_keyword(const char *buff) const;
00689 
00692   int read_monom_obj(FILE *fp, double *coeff, char **name, int *cnt, 
00693                      char **obj_name);
00694 
00699   int read_monom_row(FILE *fp, char *start_str, double *coeff, char **name, 
00700                      int cnt_coeff) const;
00701 
00703   void realloc_coeff(double **coeff, char ***colNames, int *maxcoeff) const;
00704 
00706   void realloc_row(char ***rowNames, int **start, double **rhs, 
00707                    double **rowlow, double **rowup, int *maxrow) const;
00708     
00710   void realloc_col(double **collow, double **colup, char **is_int,
00711                    int *maxcol) const;
00712 
00714   void read_row(FILE *fp, char *buff, double **pcoeff, char ***pcolNames, 
00715                 int *cnt_coeff, int *maxcoeff,
00716                      double *rhs, double *rowlow, double *rowup, 
00717                      int *cnt_row, double inf) const;
00718 
00732   void checkRowNames();
00733 
00742   void checkColNames();
00743 
00744 };
00745 
00746 void
00747 CoinLpIOUnitTest(const std::string& lpDir);
00748 
00749 
00750 #endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines