CoinLpIO.hpp
Go to the documentation of this file.
1 /* $Id: CoinLpIO.hpp 1215 2009-11-05 11:03:04Z forrest $ */
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 
14 #include <cstdio>
15 
17 
18 typedef int COINColumnIndex;
19 
91 class CoinLpIO {
92 
93 public:
94 
97  CoinLpIO();
99 
101  ~CoinLpIO();
102 
108  void freePreviousNames(const int section);
109 
111  void freeAll();
113 
116  inline void
117  convertBoundToSense(const double lower, const double upper,
118  char& sense, double& right, double& range) const;
119 
122 
124  const char * getProblemName() const;
125 
127  void setProblemName(const char *name);
128 
130  int getNumCols() const;
131 
133  int getNumRows() const;
134 
136  int getNumElements() const;
137 
139  const double * getColLower() const;
140 
142  const double * getColUpper() const;
143 
145  const double * getRowLower() const;
146 
148  const double * getRowUpper() const;
158  const char * getRowSense() const;
159 
171  const double * getRightHandSide() const;
172 
186  const double * getRowRange() const;
187 
189  const double * getObjCoefficients() const;
190 
192  const CoinPackedMatrix * getMatrixByRow() const;
193 
195  const CoinPackedMatrix * getMatrixByCol() const;
196 
198  const char * getObjName() const;
199 
205  void getPreviousRowNames(char const * const * prev,
206  int *card_prev) const;
207 
212  void getPreviousColNames(char const * const * prev,
213  int *card_prev) const;
214 
217  char const * const * getRowNames() const;
218 
220  char const * const *getColNames() const;
221 
225  const char * rowName(int index) const;
226 
230  const char * columnName(int index) const;
231 
235  int rowIndex(const char * name) const;
236 
239  int columnIndex(const char * name) const;
240 
242  double objectiveOffset() const;
243 
245  inline void setObjectiveOffset(double value)
246  { objectiveOffset_ = value;}
247 
250  bool isInteger(int columnNumber) const;
251 
253  const char * integerColumns() const;
255 
258  double getInfinity() const;
260 
263  void setInfinity(const double);
264 
266  double getEpsilon() const;
267 
270  void setEpsilon(const double);
271 
273  int getNumberAcross() const;
274 
277  void setNumberAcross(const int);
278 
280  int getDecimals() const;
281 
284  void setDecimals(const int);
286 
299  const CoinPackedMatrix& m,
300  const double* collb, const double* colub,
301  const double* obj_coeff,
302  const char* integrality,
303  const double* rowlb, const double* rowub);
304 
317  int is_invalid_name(const char *buff, const bool ranged) const;
318 
335  int are_invalid_names(char const * const *vnames,
336  const int card_vnames,
337  const bool check_ranged) const;
338 
341  void setDefaultRowNames();
342 
344  void setDefaultColNames();
345 
365  void setLpDataRowAndColNames(char const * const * const rownames,
366  char const * const * const colnames);
367 
382  int writeLp(const char *filename,
383  const double epsilon,
384  const int numberAcross,
385  const int decimals,
386  const bool useRowNames = true);
387 
402  int writeLp(FILE *fp,
403  const double epsilon,
404  const int numberAcross,
405  const int decimals,
406  const bool useRowNames = true);
407 
410  int writeLp(const char *filename, const bool useRowNames = true);
411 
414  int writeLp(FILE *fp, const bool useRowNames = true);
415 
420  void readLp(const char *filename, const double epsilon);
421 
426  void readLp(const char *filename);
427 
433  void readLp(FILE *fp, const double epsilon);
434 
439  void readLp(FILE *fp);
440 
442  void print() const;
444 
445 protected:
447  char * problemName_;
448 
451 
454 
457 
460 
463 
465  double * rowlower_;
466 
468  double * rowupper_;
469 
471  double * collower_;
472 
474  double * colupper_;
475 
477  mutable double * rhs_;
478 
482  mutable double *rowrange_;
483 
485  mutable char * rowsense_;
486 
488  double * objective_;
489 
492 
495  char * integerType_;
496 
498  char * fileName_;
499 
501  double infinity_;
502 
504  double epsilon_;
505 
508 
511 
513  char *objName_;
514 
521  char **previous_names_[2];
522 
528 
533  char **names_[2];
534 
535  typedef struct {
536  int index, next;
537  } CoinHashLink;
538 
542  int maxHash_[2];
543 
547  int numberHash_[2];
548 
552  mutable CoinHashLink *hash_[2];
553 
559  void startHash(char const * const * const names,
560  const COINColumnIndex number,
561  int section);
562 
566  void stopHash(int section);
567 
572  COINColumnIndex findHash(const char *name, int section) const;
573 
578  void insertHash(const char *thisName, int section);
579 
582  void out_coeff(FILE *fp, double v, int print_1) const;
583 
587  int find_obj(FILE *fp) const;
588 
594  int is_subject_to(const char *buff) const;
595 
598  int first_is_number(const char *buff) const;
599 
602  int is_comment(const char *buff) const;
603 
605  void skip_comment(char *buff, FILE *fp) const;
606 
608  void scan_next(char *buff, FILE *fp) const;
609 
612  int is_free(const char *buff) const;
613 
616  int is_inf(const char *buff) const;
617 
623  int is_sense(const char *buff) const;
624 
634  int is_keyword(const char *buff) const;
635 
638  int read_monom_obj(FILE *fp, double *coeff, char **name, int *cnt,
639  char **obj_name);
640 
645  int read_monom_row(FILE *fp, char *start_str, double *coeff, char **name,
646  int cnt_coeff) const;
647 
649  void realloc_coeff(double **coeff, char ***colNames, int *maxcoeff) const;
650 
652  void realloc_row(char ***rowNames, int **start, double **rhs,
653  double **rowlow, double **rowup, int *maxrow) const;
654 
656  void realloc_col(double **collow, double **colup, char **is_int,
657  int *maxcol) const;
658 
660  void read_row(FILE *fp, char *buff, double **pcoeff, char ***pcolNames,
661  int *cnt_coeff, int *maxcoeff,
662  double *rhs, double *rowlow, double *rowup,
663  int *cnt_row, double inf) const;
664 
678  void checkRowNames();
679 
688  void checkColNames();
689 
690 };
691 
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.
char ** previous_names_[2]
Row names (including objective function name) and column names when stopHash() for the corresponding ...
Definition: CoinLpIO.hpp:521
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...
const char * integerColumns() const
Get characteristic vector of integer variables.
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.
void setDecimals(const int)
Set decimals.
int first_is_number(const char *buff) const
Return 1 if the first character of buff is a number.
double objectiveOffset() const
Returns the (constant) objective offset.
void skip_comment(char *buff, FILE *fp) const
Read the file fp until buff contains an end of line.
CoinLpIO()
Default Constructor.
const char * rowName(int index) const
Return the row name for the specified index.
void scan_next(char *buff, FILE *fp) const
Put in buff the next string that is not part of a comment.
COINColumnIndex findHash(const char *name, int section) const
Return the index of the given name, return -1 if the name is not found.
int is_comment(const char *buff) const
Return 1 if the first character of buff is '/' or '\'.
int numberColumns_
Number of columns.
Definition: CoinLpIO.hpp:453
Class to read and write Lp files.
Definition: CoinLpIO.hpp:91
int numberElements_
Number of elements.
Definition: CoinLpIO.hpp:456
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.
void setEpsilon(const double)
Set epsilon.
char ** names_[2]
Row names (including objective function name) and column names (linked to Hash tables).
Definition: CoinLpIO.hpp:533
double * rowlower_
Pointer to dense vector of row lower bounds.
Definition: CoinLpIO.hpp:465
void getPreviousRowNames(char const *const *prev, int *card_prev) const
Get pointer to array[*card_prev] of previous row names.
char * problemName_
Problem name.
Definition: CoinLpIO.hpp:447
int getNumCols() const
Get number of columns.
void setDefaultRowNames()
Set objective function name to the default "obj" and row names to the default "cons0", "cons1", ...
char * integerType_
Pointer to dense vector specifying if a variable is continuous (0) or integer (1).
Definition: CoinLpIO.hpp:495
void out_coeff(FILE *fp, double v, int print_1) const
Write a coefficient.
double * rowrange_
Pointer to dense vector of slack variable upper bounds for ranged constraints (undefined for non-rang...
Definition: CoinLpIO.hpp:482
const double * getRowRange() const
Get pointer to array[getNumRows()] of row ranges.
int read_monom_row(FILE *fp, char *start_str, double *coeff, char **name, int cnt_coeff) const
Read a monomial of a constraint.
char * rowsense_
Pointer to dense vector of row senses.
Definition: CoinLpIO.hpp:485
int getNumberAcross() const
Get numberAcross, the number of monomials to be printed per line.
Sparse Matrix Base Class.
int numberRows_
Number of rows.
Definition: CoinLpIO.hpp:450
char * fileName_
Current file name.
Definition: CoinLpIO.hpp:498
int maxHash_[2]
Maximum number of entries in a hash table section.
Definition: CoinLpIO.hpp:542
double * rhs_
Pointer to dense vector of row rhs.
Definition: CoinLpIO.hpp:477
int rowIndex(const char *name) const
Return the index for the specified row name.
double infinity_
Value to use for infinity.
Definition: CoinLpIO.hpp:501
CoinPackedMatrix * matrixByColumn_
Pointer to column-wise copy of problem matrix coefficients.
Definition: CoinLpIO.hpp:459
void setLpDataRowAndColNames(char const *const *const rownames, char const *const *const colnames)
Set the row and column names.
char const *const * getColNames() const
Get pointer to array[getNumCols()] of column names.
bool isInteger(int columnNumber) const
Return true if a column is an integer (binary or general integer) variable.
const double * getObjCoefficients() const
Get pointer to array[getNumCols()] of objective function coefficients.
CoinHashLink * hash_[2]
Hash tables with two sections.
Definition: CoinLpIO.hpp:552
const double * getColUpper() const
Get pointer to array[getNumCols()] of column upper bounds.
void startHash(char const *const *const names, const COINColumnIndex number, int section)
Build the hash table for the given names.
void checkColNames()
Check that current column names are distinct.
const char * getProblemName() const
Get the problem name.
int numberAcross_
Number of monomials printed in a row.
Definition: CoinLpIO.hpp:507
void getPreviousColNames(char const *const *prev, int *card_prev) const
Get pointer to array[*card_prev] of previous column names.
int find_obj(FILE *fp) const
Locate the objective function.
char * objName_
Objective function name.
Definition: CoinLpIO.hpp:513
int columnIndex(const char *name) const
Return the index for the specified column name.
int is_free(const char *buff) const
Return 1 if buff is the keyword "free" or one of its variants.
~CoinLpIO()
Destructor.
int getDecimals() const
Get decimals, the number of digits to write after the decimal point.
int numberHash_[2]
Number of entries in a hash table section.
Definition: CoinLpIO.hpp:547
void stopHash(int section)
Delete hash storage.
double * rowupper_
Pointer to dense vector of row upper bounds.
Definition: CoinLpIO.hpp:468
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...
const double * getRowLower() const
Get pointer to array[getNumRows()] of row lower bounds.
void setNumberAcross(const int)
Set numberAcross.
const double * getRowUpper() const
Get pointer to array[getNumRows()] of row upper bounds.
void freePreviousNames(const int section)
Free the vector previous_names_[section] and set card_previous_names_[section] to 0...
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...
double getInfinity() const
Get infinity.
double * collower_
Pointer to dense vector of column lower bounds.
Definition: CoinLpIO.hpp:471
const CoinPackedMatrix * getMatrixByRow() const
Get pointer to row-wise copy of the coefficient matrix.
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 read_monom_obj(FILE *fp, double *coeff, char **name, int *cnt, char **obj_name)
Read a monomial of the objective function.
double objectiveOffset_
Constant offset for objective value.
Definition: CoinLpIO.hpp:491
void realloc_col(double **collow, double **colup, char **is_int, int *maxcol) const
Reallocate vectors related to columns.
char const *const * getRowNames() const
Get pointer to array[getNumRows()+1] of row names, including objective function name as last entry...
double getEpsilon() const
Get epsilon.
int is_inf(const char *buff) const
Return 1 if buff is the keyword "inf" or one of its variants.
CoinPackedMatrix * matrixByRow_
Pointer to row-wise copy of problem matrix coefficients.
Definition: CoinLpIO.hpp:462
void setObjectiveOffset(double value)
Set objective offset.
Definition: CoinLpIO.hpp:245
void realloc_row(char ***rowNames, int **start, double **rhs, double **rowlow, double **rowup, int *maxrow) const
Reallocate vectors related to rows.
double * colupper_
Pointer to dense vector of column upper bounds.
Definition: CoinLpIO.hpp:474
int getNumElements() const
Get number of nonzero elements.
const char * getObjName() const
Get objective function name.
int decimals_
Number of decimals printed for coefficients.
Definition: CoinLpIO.hpp:510
int COINColumnIndex
Definition: CoinLpIO.hpp:16
int is_sense(const char *buff) const
Return an integer indicating the inequality sense read.
double epsilon_
Value to use for epsilon.
Definition: CoinLpIO.hpp:504
const char * columnName(int index) const
Return the column name for the specified index.
void freeAll()
Free all memory (except memory related to hash tables and objName_).
void realloc_coeff(double **coeff, char ***colNames, int *maxcoeff) const
Reallocate vectors related to number of coefficients.
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.
void setInfinity(const double)
Set infinity.
void checkRowNames()
Check that current objective name and all row names are distinct including row names obtained by addi...
const double * getColLower() const
Get pointer to array[getNumCols()] of column lower bounds.
const char * getRowSense() const
Get pointer to array[getNumRows()] of constraint senses.
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...
void setProblemName(const char *name)
Set problem name.
int card_previous_names_[2]
card_previous_names_[section] holds the number of entries in the vector previous_names_[section].
Definition: CoinLpIO.hpp:527
const CoinPackedMatrix * getMatrixByCol() const
Get pointer to column-wise copy of the coefficient matrix.
int getNumRows() const
Get number of rows.
void print() const
Dump the data. Low level method for debugging.
double * objective_
Pointer to dense vector of objective coefficients.
Definition: CoinLpIO.hpp:488
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 setDefaultColNames()
Set column names to the default "x0", "x1", ...
const double * getRightHandSide() const
Get pointer to array[getNumRows()] of constraint right-hand sides.