CoinUtils  2.9.15
CoinLpIO.hpp
Go to the documentation of this file.
1 /* $Id: CoinLpIO.hpp 1727 2014-08-05 16:30:20Z tkr $ */
2 // Last edit: 11/5/08
3 //
4 // Name: CoinLpIO.hpp; Support for Lp files
5 // Author: Francois Margot
6 // Tepper School of Business
7 // Carnegie Mellon University, Pittsburgh, PA 15213
8 // email: fmargot@andrew.cmu.edu
9 // Date: 12/28/03
10 //-----------------------------------------------------------------------------
11 // Copyright (C) 2003, Francois Margot, International Business Machines
12 // Corporation and others. All Rights Reserved.
13 // This code is licensed under the terms of the Eclipse Public License (EPL).
14 
15 #ifndef CoinLpIO_H
16 #define CoinLpIO_H
17 
18 #include <cstdio>
19 
20 #include "CoinPackedMatrix.hpp"
21 #include "CoinMessage.hpp"
22 
23 const int MAX_OBJECTIVES = 2;
24 
25 typedef int COINColumnIndex;
26 
101 class CoinLpIO {
102  friend void CoinLpIOUnitTest(const std::string & lpDir);
103 public:
104 
107  CoinLpIO();
109 
111  void gutsOfDestructor();
112 
114  void gutsOfCopy(const CoinLpIO &);
115 
117  CoinLpIO & operator = (const CoinLpIO& rhs) ;
118 
120  CoinLpIO (const CoinLpIO &);
121 
123  ~CoinLpIO();
124 
130  void freePreviousNames(const int section);
131 
133  void freeAll();
135 
138  inline void
139  convertBoundToSense(const double lower, const double upper,
140  char& sense, double& right, double& range) const;
141 
144 
146  const char * getProblemName() const;
147 
149  void setProblemName(const char *name);
150 
152  int getNumCols() const;
153 
155  int getNumRows() const;
156 
158  int getNumElements() const;
159 
161  const double * getColLower() const;
162 
164  const double * getColUpper() const;
165 
167  const double * getRowLower() const;
168 
170  const double * getRowUpper() const;
180  const char * getRowSense() const;
181 
193  const double * getRightHandSide() const;
194 
208  const double * getRowRange() const;
209 
211  const int getNumObjectives() const;
212 
214  const double * getObjCoefficients() const;
215 
217  const double * getObjCoefficients(int j) const;
218 
220  const CoinPackedMatrix * getMatrixByRow() const;
221 
223  const CoinPackedMatrix * getMatrixByCol() const;
224 
226  const char * getObjName() const;
227 
229  const char * getObjName(int j) const;
230 
236  void getPreviousRowNames(char const * const * prev,
237  int *card_prev) const;
238 
243  void getPreviousColNames(char const * const * prev,
244  int *card_prev) const;
245 
248  char const * const * getRowNames() const;
249 
251  char const * const *getColNames() const;
252 
256  const char * rowName(int index) const;
257 
261  const char * columnName(int index) const;
262 
266  int rowIndex(const char * name) const;
267 
270  int columnIndex(const char * name) const;
271 
273  double objectiveOffset() const;
274 
276  double objectiveOffset(int j) const;
277 
279  inline void setObjectiveOffset(double value)
280  { objectiveOffset_[0] = value;}
281 
283  inline void setObjectiveOffset(double value, int j)
284  { objectiveOffset_[j] = value;}
285 
288  bool isInteger(int columnNumber) const;
289 
291  const char * integerColumns() const;
293 
296  double getInfinity() const;
298 
301  void setInfinity(const double);
302 
304  double getEpsilon() const;
305 
308  void setEpsilon(const double);
309 
311  int getNumberAcross() const;
312 
315  void setNumberAcross(const int);
316 
318  int getDecimals() const;
319 
322  void setDecimals(const int);
324 
338  const CoinPackedMatrix& m,
339  const double* collb, const double* colub,
340  const double* obj_coeff,
341  const char* integrality,
342  const double* rowlb, const double* rowub);
343 
345  const CoinPackedMatrix& m,
346  const double* collb, const double* colub,
347  const double* obj_coeff[MAX_OBJECTIVES],
348  int num_objectives,
349  const char* integrality,
350  const double* rowlb, const double* rowub);
351 
364  int is_invalid_name(const char *buff, const bool ranged) const;
365 
382  int are_invalid_names(char const * const *vnames,
383  const int card_vnames,
384  const bool check_ranged) const;
385 
388  void setDefaultRowNames();
389 
391  void setDefaultColNames();
392 
412  void setLpDataRowAndColNames(char const * const * const rownames,
413  char const * const * const colnames);
414 
429  int writeLp(const char *filename,
430  const double epsilon,
431  const int numberAcross,
432  const int decimals,
433  const bool useRowNames = true);
434 
449  int writeLp(FILE *fp,
450  const double epsilon,
451  const int numberAcross,
452  const int decimals,
453  const bool useRowNames = true);
454 
457  int writeLp(const char *filename, const bool useRowNames = true);
458 
461  int writeLp(FILE *fp, const bool useRowNames = true);
462 
467  void readLp(const char *filename, const double epsilon);
468 
473  void readLp(const char *filename);
474 
480  void readLp(FILE *fp, const double epsilon);
481 
486  void readLp(FILE *fp);
487 
489  void print() const;
491 
499 
501  void newLanguage(CoinMessages::Language language);
502 
504  inline void setLanguage(CoinMessages::Language language) {newLanguage(language);}
505 
507  inline CoinMessageHandler * messageHandler() const {return handler_;}
508 
510  inline CoinMessages messages() {return messages_;}
512  inline CoinMessages * messagesPointer() {return & messages_;}
514 
515 protected:
517  char * problemName_;
518 
529 
532 
535 
538 
541 
544 
546  double * rowlower_;
547 
549  double * rowupper_;
550 
552  double * collower_;
553 
555  double * colupper_;
556 
558  mutable double * rhs_;
559 
563  mutable double *rowrange_;
564 
566  mutable char * rowsense_;
567 
570 
573 
576 
579  char * integerType_;
580 
582  char * fileName_;
583 
585  double infinity_;
586 
588  double epsilon_;
589 
592 
595 
598 
605  char **previous_names_[2];
606 
612 
617  char **names_[2];
618 
619  typedef struct {
620  int index, next;
621  } CoinHashLink;
622 
626  int maxHash_[2];
627 
631  int numberHash_[2];
632 
636  mutable CoinHashLink *hash_[2];
637 
643  void startHash(char const * const * const names,
644  const COINColumnIndex number,
645  int section);
646 
650  void stopHash(int section);
651 
656  COINColumnIndex findHash(const char *name, int section) const;
657 
662  void insertHash(const char *thisName, int section);
663 
666  void out_coeff(FILE *fp, double v, int print_1) const;
667 
671  int find_obj(FILE *fp) const;
672 
678  int is_subject_to(const char *buff) const;
679 
682  int first_is_number(const char *buff) const;
683 
686  int is_comment(const char *buff) const;
687 
689  void skip_comment(char *buff, FILE *fp) const;
690 
692  void scan_next(char *buff, FILE *fp) const;
693 
696  int is_free(const char *buff) const;
697 
700  int is_inf(const char *buff) const;
701 
707  int is_sense(const char *buff) const;
708 
718  int is_keyword(const char *buff) const;
719 
722  int read_monom_obj(FILE *fp, double *coeff, char **name, int *cnt,
723  char **obj_name, int *num_objectives, int *obj_starts);
724 
729  int read_monom_row(FILE *fp, char *start_str, double *coeff, char **name,
730  int cnt_coeff) const;
731 
733  void realloc_coeff(double **coeff, char ***colNames, int *maxcoeff) const;
734 
736  void realloc_row(char ***rowNames, int **start, double **rhs,
737  double **rowlow, double **rowup, int *maxrow) const;
738 
740  void realloc_col(double **collow, double **colup, char **is_int,
741  int *maxcol) const;
742 
744  void read_row(FILE *fp, char *buff, double **pcoeff, char ***pcolNames,
745  int *cnt_coeff, int *maxcoeff,
746  double *rhs, double *rowlow, double *rowup,
747  int *cnt_row, double inf) const;
748 
762  void checkRowNames();
763 
772  void checkColNames();
773 
774 };
775 
776 void
777 CoinLpIOUnitTest(const std::string& lpDir);
778 
779 
780 #endif
int is_keyword(const char *buff) const
Return an integer indicating if one of the keywords "Bounds", "Integers", "Generals", "Binaries", "End", or one of their variants has been read.
double * rowrange_
Pointer to dense vector of slack variable upper bounds for ranged constraints (undefined for non-rang...
Definition: CoinLpIO.hpp:563
const char * integerColumns() const
Get characteristic vector of integer variables.
char * rowsense_
Pointer to dense vector of row senses.
Definition: CoinLpIO.hpp:566
double * colupper_
Pointer to dense vector of column upper bounds.
Definition: CoinLpIO.hpp:555
int first_is_number(const char *buff) const
Return 1 if the first character of buff is a number.
double * rowlower_
Pointer to dense vector of row lower bounds.
Definition: CoinLpIO.hpp:546
void setEpsilon(const double)
Set epsilon.
const char * getProblemName() const
Get the problem name.
void setProblemName(const char *name)
Set problem name.
void getPreviousColNames(char const *const *prev, int *card_prev) const
Get pointer to array[*card_prev] of previous column names.
void setInfinity(const double)
Set infinity.
CoinLpIO & operator=(const CoinLpIO &rhs)
assignment operator
int are_invalid_names(char const *const *vnames, const int card_vnames, const bool check_ranged) const
Return 0 if each of the card_vnames entries of vnames is a valid name, return a positive number other...
char ** previous_names_[2]
Row names (including objective function name) and column names when stopHash() for the corresponding ...
Definition: CoinLpIO.hpp:605
void getPreviousRowNames(char const *const *prev, int *card_prev) const
Get pointer to array[*card_prev] of previous row names.
double objectiveOffset() const
Returns the (constant) objective offset.
void out_coeff(FILE *fp, double v, int print_1) const
Write a coefficient.
void stopHash(int section)
Delete hash storage.
int is_subject_to(const char *buff) const
Return an integer indicating if the keyword "subject to" or one of its variants has been read...
int read_monom_obj(FILE *fp, double *coeff, char **name, int *cnt, char **obj_name, int *num_objectives, int *obj_starts)
Read a monomial of the objective function.
int num_objectives_
Number of objectives.
Definition: CoinLpIO.hpp:572
int is_inf(const char *buff) const
Return 1 if buff is the keyword "inf" or one of its variants.
int read_monom_row(FILE *fp, char *start_str, double *coeff, char **name, int cnt_coeff) const
Read a monomial of a constraint.
const double * getColUpper() const
Get pointer to array[getNumCols()] of column upper bounds.
int find_obj(FILE *fp) const
Locate the objective function.
int columnIndex(const char *name) const
Return the index for the specified column name.
CoinMessages * messagesPointer()
Return the messages pointer.
Definition: CoinLpIO.hpp:512
const double * getRowUpper() const
Get pointer to array[getNumRows()] of row upper bounds.
const char * rowName(int index) const
Return the row name for the specified index.
char const *const * getColNames() const
Get pointer to array[getNumCols()] of column names.
CoinPackedMatrix * matrixByRow_
Pointer to row-wise copy of problem matrix coefficients.
Definition: CoinLpIO.hpp:543
CoinLpIO()
Default Constructor.
~CoinLpIO()
Destructor.
int decimals_
Number of decimals printed for coefficients.
Definition: CoinLpIO.hpp:594
int numberHash_[2]
Number of entries in a hash table section.
Definition: CoinLpIO.hpp:631
const double * getRowLower() const
Get pointer to array[getNumRows()] of row lower bounds.
int numberColumns_
Number of columns.
Definition: CoinLpIO.hpp:534
void print() const
Dump the data. Low level method for debugging.
Base class for message handling.
void setDefaultRowNames()
Set objective function name to the default "obj" and row names to the default "cons0", "cons1", ...
CoinHashLink * hash_[2]
Hash tables with two sections.
Definition: CoinLpIO.hpp:636
CoinMessageHandler * handler_
Message handler.
Definition: CoinLpIO.hpp:520
char * problemName_
Problem name.
Definition: CoinLpIO.hpp:517
char ** names_[2]
Row names (including objective function name) and column names (linked to Hash tables).
Definition: CoinLpIO.hpp:617
CoinMessageHandler * messageHandler() const
Return the message handler.
Definition: CoinLpIO.hpp:507
int getNumCols() const
Get number of columns.
double epsilon_
Value to use for epsilon.
Definition: CoinLpIO.hpp:588
void setLpDataRowAndColNames(char const *const *const rownames, char const *const *const colnames)
Set the row and column names.
double infinity_
Value to use for infinity.
Definition: CoinLpIO.hpp:585
const CoinPackedMatrix * getMatrixByRow() const
Get pointer to row-wise copy of the coefficient matrix.
int numberElements_
Number of elements.
Definition: CoinLpIO.hpp:537
void read_row(FILE *fp, char *buff, double **pcoeff, char ***pcolNames, int *cnt_coeff, int *maxcoeff, double *rhs, double *rowlow, double *rowup, int *cnt_row, double inf) const
Read a constraint.
void checkRowNames()
Check that current objective name and all row names are distinct including row names obtained by addi...
const int MAX_OBJECTIVES
Definition: CoinLpIO.hpp:23
char const *const * getRowNames() const
Get pointer to array[getNumRows()+1] of row names, including objective function name as last entry...
void setLanguage(CoinMessages::Language language)
Set the language for messages.
Definition: CoinLpIO.hpp:504
int getDecimals() const
Get decimals, the number of digits to write after the decimal point.
void readLp(const char *filename, const double epsilon)
Read the data in Lp format from the file with name filename, using the given value for epsilon...
void realloc_col(double **collow, double **colup, char **is_int, int *maxcol) const
Reallocate vectors related to columns.
int is_free(const char *buff) const
Return 1 if buff is the keyword "free" or one of its variants.
const CoinPackedMatrix * getMatrixByCol() const
Get pointer to column-wise copy of the coefficient matrix.
char * objName_[MAX_OBJECTIVES]
Objective function name.
Definition: CoinLpIO.hpp:597
char * integerType_
Pointer to dense vector specifying if a variable is continuous (0) or integer (1).
Definition: CoinLpIO.hpp:579
const char * getObjName() const
Get objective function name.
void startHash(char const *const *const names, const COINColumnIndex number, int section)
Build the hash table for the given names.
int card_previous_names_[2]
card_previous_names_[section] holds the number of entries in the vector previous_names_[section].
Definition: CoinLpIO.hpp:611
char * fileName_
Current file name.
Definition: CoinLpIO.hpp:582
CoinPackedMatrix * matrixByColumn_
Pointer to column-wise copy of problem matrix coefficients.
Definition: CoinLpIO.hpp:540
void newLanguage(CoinMessages::Language language)
Set the language for messages.
void setLpDataWithoutRowAndColNames(const CoinPackedMatrix &m, const double *collb, const double *colub, const double *obj_coeff, const char *integrality, const double *rowlb, const double *rowub)
Set the data of the object.
int writeLp(const char *filename, const double epsilon, const int numberAcross, const int decimals, const bool useRowNames=true)
Write the data in Lp format in the file with name filename.
int numberAcross_
Number of monomials printed in a row.
Definition: CoinLpIO.hpp:591
void skip_comment(char *buff, FILE *fp) const
Read the file fp until buff contains an end of line.
void gutsOfCopy(const CoinLpIO &)
Does the heavy lifting for copy and assignment.
CoinMessages messages_
Messages.
Definition: CoinLpIO.hpp:528
int getNumberAcross() const
Get numberAcross, the number of monomials to be printed per line.
int is_sense(const char *buff) const
Return an integer indicating the inequality sense read.
double objectiveOffset_[MAX_OBJECTIVES]
Constant offset for objective value.
Definition: CoinLpIO.hpp:575
const double * getRowRange() const
Get pointer to array[getNumRows()] of row ranges.
double * objective_[MAX_OBJECTIVES]
Pointer to dense vector of objective coefficients.
Definition: CoinLpIO.hpp:569
const double * getRightHandSide() const
Get pointer to array[getNumRows()] of constraint right-hand sides.
int rowIndex(const char *name) const
Return the index for the specified row name.
Sparse Matrix Base Class.
const double * getObjCoefficients() const
Get pointer to array[getNumCols()] of objective function coefficients.
void insertHash(const char *thisName, int section)
Insert thisName in the hash table if not present yet; does nothing if the name is already in...
int COINColumnIndex
Definition: CoinLpIO.hpp:25
double getInfinity() const
Get infinity.
Language
Supported languages.
void setObjectiveOffset(double value)
Set objective offset.
Definition: CoinLpIO.hpp:279
void setObjectiveOffset(double value, int j)
Set objective offset.
Definition: CoinLpIO.hpp:283
double * rhs_
Pointer to dense vector of row rhs.
Definition: CoinLpIO.hpp:558
int is_comment(const char *buff) const
Return 1 if the first character of buff is &#39;/&#39; or &#39;\&#39;.
const double * getColLower() const
Get pointer to array[getNumCols()] of column lower bounds.
void realloc_row(char ***rowNames, int **start, double **rhs, double **rowlow, double **rowup, int *maxrow) const
Reallocate vectors related to rows.
int getNumElements() const
Get number of nonzero elements.
const char * columnName(int index) const
Return the column name for the specified index.
void setDefaultColNames()
Set column names to the default "x0", "x1", ...
This file contains the enum for the standard set of Coin messages and a class definition whose sole p...
void scan_next(char *buff, FILE *fp) const
Put in buff the next string that is not part of a comment.
int getNumRows() const
Get number of rows.
double * collower_
Pointer to dense vector of column lower bounds.
Definition: CoinLpIO.hpp:552
int maxHash_[2]
Maximum number of entries in a hash table section.
Definition: CoinLpIO.hpp:626
int numberRows_
Number of rows.
Definition: CoinLpIO.hpp:531
Class to read and write Lp files.
Definition: CoinLpIO.hpp:101
void gutsOfDestructor()
Does the heavy lifting for destruct and assignment.
CoinMessages messages()
Return the messages.
Definition: CoinLpIO.hpp:510
void checkColNames()
Check that current column names are distinct.
void setDecimals(const int)
Set decimals.
void freeAll()
Free all memory (except memory related to hash tables and objName_).
const int getNumObjectives() const
Get pointer to array[getNumCols()] of objective function coefficients.
bool isInteger(int columnNumber) const
Return true if a column is an integer (binary or general integer) variable.
double * rowupper_
Pointer to dense vector of row upper bounds.
Definition: CoinLpIO.hpp:549
friend void CoinLpIOUnitTest(const std::string &lpDir)
void passInMessageHandler(CoinMessageHandler *handler)
Pass in Message handler.
int is_invalid_name(const char *buff, const bool ranged) const
Return 0 if buff is a valid name for a row, a column or objective function, return a positive number ...
void convertBoundToSense(const double lower, const double upper, char &sense, double &right, double &range) const
A quick inlined function to convert from lb/ub style constraint definition to sense/rhs/range style...
double getEpsilon() const
Get epsilon.
Class to hold and manipulate an array of massaged messages.
void setNumberAcross(const int)
Set numberAcross.
void realloc_coeff(double **coeff, char ***colNames, int *maxcoeff) const
Reallocate vectors related to number of coefficients.
void freePreviousNames(const int section)
Free the vector previous_names_[section] and set card_previous_names_[section] to 0...
bool defaultHandler_
Flag to say if the message handler is the default handler.
Definition: CoinLpIO.hpp:526
const char * getRowSense() const
Get pointer to array[getNumRows()] of constraint senses.
COINColumnIndex findHash(const char *name, int section) const
Return the index of the given name, return -1 if the name is not found.