coin-Bcp
CoinLpIO.hpp
Go to the documentation of this file.
1 /* $Id: CoinLpIO.hpp 2088 2019-01-22 09:15:46Z 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 // 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 #include "CoinFileIO.hpp"
23 class CoinSet;
24 
25 const int MAX_OBJECTIVES = 2;
26 
27 typedef int COINColumnIndex;
28 
105 class CoinLpIO {
106  friend void CoinLpIOUnitTest(const std::string &lpDir);
107 
108 public:
111  CoinLpIO();
113 
115  void gutsOfDestructor();
116 
118  void gutsOfCopy(const CoinLpIO &);
119 
121  CoinLpIO &operator=(const CoinLpIO &rhs);
122 
124  CoinLpIO(const CoinLpIO &);
125 
127  ~CoinLpIO();
128 
134  void freePreviousNames(const int section);
135 
137  void freeAll();
139 
142  inline void
143  convertBoundToSense(const double lower, const double upper,
144  char &sense, double &right, double &range) const;
145 
148 
150  const char *getProblemName() const;
151 
153  void setProblemName(const char *name);
154 
156  int getNumCols() const;
157 
159  int getNumRows() const;
160 
163 
165  const double *getColLower() const;
166 
168  const double *getColUpper() const;
169 
171  const double *getRowLower() const;
172 
174  const double *getRowUpper() const;
184  const char *getRowSense() const;
185 
197  const double *getRightHandSide() const;
198 
212  const double *getRowRange() const;
213 
215  const int getNumObjectives() const;
216 
218  const double *getObjCoefficients() const;
219 
221  const double *getObjCoefficients(int j) const;
222 
224  const CoinPackedMatrix *getMatrixByRow() const;
225 
227  const CoinPackedMatrix *getMatrixByCol() const;
228 
230  const char *getObjName() const;
231 
233  const char *getObjName(int j) const;
234 
240  void getPreviousRowNames(char const *const *prev,
241  int *card_prev) const;
242 
247  void getPreviousColNames(char const *const *prev,
248  int *card_prev) const;
249 
252  char const *const *getRowNames() const;
253 
255  char const *const *getColNames() const;
256 
260  const char *rowName(int index) const;
261 
265  const char *columnName(int index) const;
266 
270  int rowIndex(const char *name) const;
271 
274  int columnIndex(const char *name) const;
275 
277  double objectiveOffset() const;
278 
280  double objectiveOffset(int j) const;
281 
283  inline void setObjectiveOffset(double value)
284  {
285  objectiveOffset_[0] = value;
286  }
287 
289  inline void setObjectiveOffset(double value, int j)
290  {
291  objectiveOffset_[j] = value;
292  }
293 
296  bool isInteger(int columnNumber) const;
297 
299  const char *integerColumns() const;
301 
304  double getInfinity() const;
306 
309  void setInfinity(const double);
310 
312  double getEpsilon() const;
313 
316  void setEpsilon(const double);
317 
319  int getNumberAcross() const;
320 
323  void setNumberAcross(const int);
324 
326  int getDecimals() const;
327 
330  void setDecimals(const int);
332 
346  const CoinPackedMatrix &m,
347  const double *collb, const double *colub,
348  const double *obj_coeff,
349  const char *integrality,
350  const double *rowlb, const double *rowub);
351 
353  const CoinPackedMatrix &m,
354  const double *collb, const double *colub,
355  const double *obj_coeff[MAX_OBJECTIVES],
356  int num_objectives,
357  const char *integrality,
358  const double *rowlb, const double *rowub);
359 
372  int is_invalid_name(const char *buff, const bool ranged) const;
373 
390  int are_invalid_names(char const *const *vnames,
391  const int card_vnames,
392  const bool check_ranged) const;
393 
396  void setDefaultRowNames();
397 
399  void setDefaultColNames();
400 
420  void setLpDataRowAndColNames(char const *const *const rownames,
421  char const *const *const colnames);
422 
437  int writeLp(const char *filename,
438  const double epsilon,
439  const int numberAcross,
440  const int decimals,
441  const bool useRowNames = true);
442 
457  int writeLp(FILE *fp,
458  const double epsilon,
459  const int numberAcross,
460  const int decimals,
461  const bool useRowNames = true);
462 
465  int writeLp(const char *filename, const bool useRowNames = true);
466 
469  int writeLp(FILE *fp, const bool useRowNames = true);
470 
475  void readLp(const char *filename, const double epsilon);
476 
481  void readLp(const char *filename);
482 
488  void readLp(FILE *fp, const double epsilon);
489 
494  void readLp(FILE *fp);
495 
497  void readLp();
499  void print() const;
500 
502  void loadSOS(int numberSets, const CoinSet *sets);
503 
505  void loadSOS(int numberSets, const CoinSet **sets);
506 
508  inline int numberSets() const
509  {
510  return numberSets_;
511  }
512 
514  inline CoinSet **setInformation() const
515  {
516  return set_;
517  }
519 
527 
529  void newLanguage(CoinMessages::Language language);
530 
532  inline void setLanguage(CoinMessages::Language language) { newLanguage(language); }
533 
535  inline CoinMessageHandler *messageHandler() const { return handler_; }
536 
538  inline CoinMessages messages() { return messages_; }
540  inline CoinMessages *messagesPointer() { return &messages_; }
542 
543 protected:
546 
557 
560 
563 
566 
569 
572 
574  double *rowlower_;
575 
577  double *rowupper_;
578 
580  double *collower_;
581 
583  double *colupper_;
584 
586  mutable double *rhs_;
587 
591  mutable double *rowrange_;
592 
594  mutable char *rowsense_;
595 
598 
601 
604 
608 
611 
614 
616  char *fileName_;
617 
619  double infinity_;
620 
622  double epsilon_;
623 
626 
629 
632 
639  char **previous_names_[2];
640 
646 
651  char **names_[2];
652 
653  typedef struct {
654  int index, next;
655  } CoinHashLink;
656 
660  int maxHash_[2];
661 
665  int numberHash_[2];
666 
670  mutable CoinHashLink *hash_[2];
671 
673  mutable char inputBuffer_[1028];
675  mutable int bufferLength_;
677  mutable int bufferPosition_;
681  mutable bool eofFound_;
683  int fscanfLpIO(char *buff) const;
685  int newCardLpIO() const;
686 
692  void startHash(char const *const *const names,
693  const COINColumnIndex number,
694  int section);
695 
699  void stopHash(int section);
700 
705  COINColumnIndex findHash(const char *name, int section) const;
706 
711  void insertHash(const char *thisName, int section);
712 
715  void out_coeff(FILE *fp, double v, int print_1) const;
716 
720  int find_obj() const;
721 
727  int is_subject_to(const char *buff) const;
728 
731  int first_is_number(const char *buff) const;
732 
735  int is_comment(const char *buff) const;
736 
738  void skip_comment(char *buff) const;
739 
742  int is_free(const char *buff) const;
743 
746  int is_inf(const char *buff) const;
747 
753  int is_sense(const char *buff) const;
754 
766  int is_keyword(const char *buff) const;
767 
770  int read_monom_obj(double *coeff, char **name, int *cnt,
771  char **obj_name, int *num_objectives, int *obj_starts);
772 
777  int read_monom_row(char *start_str, double *coeff, char **name,
778  int cnt_coeff) const;
779 
781  void realloc_coeff(double **coeff, char ***colNames, int *maxcoeff) const;
782 
784  void realloc_row(char ***rowNames, CoinBigIndex **start, double **rhs,
785  double **rowlow, double **rowup, int *maxrow) const;
786 
788  void realloc_col(double **collow, double **colup, char **is_int,
789  int *maxcol) const;
790 
792  void read_row(char *buff, double **pcoeff, char ***pcolNames,
793  int *cnt_coeff, int *maxcoeff,
794  double *rhs, double *rowlow, double *rowup,
795  int *cnt_row, double inf) const;
796 
810  void checkRowNames();
811 
820  void checkColNames();
821 };
822 
823 void CoinLpIOUnitTest(const std::string &lpDir);
824 
825 #endif
826 
827 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
828 */
CoinMessages * messagesPointer()
Return the messages pointer.
Definition: CoinLpIO.hpp:540
char ** previous_names_[2]
Row names (including objective function name) and column names when stopHash() for the corresponding ...
Definition: CoinLpIO.hpp:639
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...
char * objName_[MAX_OBJECTIVES]
Objective function name.
Definition: CoinLpIO.hpp:631
int CoinBigIndex
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.
int numberSets_
Number of sets.
Definition: CoinLpIO.hpp:613
int find_obj() const
Locate the objective function.
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 readLp()
Does work of readLp.
CoinLpIO()
Default Constructor.
double * objective_[MAX_OBJECTIVES]
Pointer to dense vector of objective coefficients.
Definition: CoinLpIO.hpp:597
void loadSOS(int numberSets, const CoinSet *sets)
Load in SOS stuff.
Base class for message handling.
const int MAX_OBJECTIVES
Definition: CoinLpIO.hpp:25
const char * rowName(int index) const
Return the row name for the specified index.
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 &#39;/&#39; or &#39;\&#39;.
int numberColumns_
Number of columns.
Definition: CoinLpIO.hpp:562
Class to read and write Lp files.
Definition: CoinLpIO.hpp:105
int numberSets() const
Number of SOS sets.
Definition: CoinLpIO.hpp:508
void CoinLpIOUnitTest(const std::string &lpDir)
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.
CoinSet ** setInformation() const
Set information.
Definition: CoinLpIO.hpp:514
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:651
double * rowlower_
Pointer to dense vector of row lower bounds.
Definition: CoinLpIO.hpp:574
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:545
int getNumCols() const
Get number of columns.
void setDefaultRowNames()
Set objective function name to the default &quot;obj&quot; and row names to the default &quot;cons0&quot;, &quot;cons1&quot;, ...
CoinMessages messages_
Messages.
Definition: CoinLpIO.hpp:556
void passInMessageHandler(CoinMessageHandler *handler)
Pass in Message handler.
char * integerType_
Pointer to dense vector specifying if a variable is continuous (0) or integer (1).
Definition: CoinLpIO.hpp:607
void out_coeff(FILE *fp, double v, int print_1) const
Write a coefficient.
void setObjectiveOffset(double value, int j)
Set objective offset.
Definition: CoinLpIO.hpp:289
double * rowrange_
Pointer to dense vector of slack variable upper bounds for ranged constraints (undefined for non-rang...
Definition: CoinLpIO.hpp:591
const double * getRowRange() const
Get pointer to array[getNumRows()] of row ranges.
void realloc_row(char ***rowNames, CoinBigIndex **start, double **rhs, double **rowlow, double **rowup, int *maxrow) const
Reallocate vectors related to rows.
char * rowsense_
Pointer to dense vector of row senses.
Definition: CoinLpIO.hpp:594
int getNumberAcross() const
Get numberAcross, the number of monomials to be printed per line.
Sparse Matrix Base Class.
void gutsOfCopy(const CoinLpIO &)
Does the heavy lifting for copy and assignment.
CoinMessageHandler * messageHandler() const
Return the message handler.
Definition: CoinLpIO.hpp:535
CoinFileInput * input_
File handler.
Definition: CoinLpIO.hpp:679
int numberRows_
Number of rows.
Definition: CoinLpIO.hpp:559
char * fileName_
Current file name.
Definition: CoinLpIO.hpp:616
int maxHash_[2]
Maximum number of entries in a hash table section.
Definition: CoinLpIO.hpp:660
double * rhs_
Pointer to dense vector of row rhs.
Definition: CoinLpIO.hpp:586
CoinMessages messages()
Return the messages.
Definition: CoinLpIO.hpp:538
double objectiveOffset_[MAX_OBJECTIVES]
Constant offset for objective value.
Definition: CoinLpIO.hpp:603
CoinBigIndex numberElements_
Number of elements.
Definition: CoinLpIO.hpp:565
int rowIndex(const char *name) const
Return the index for the specified row name.
double infinity_
Value to use for infinity.
Definition: CoinLpIO.hpp:619
CoinPackedMatrix * matrixByColumn_
Pointer to column-wise copy of problem matrix coefficients.
Definition: CoinLpIO.hpp:568
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.
friend void CoinLpIOUnitTest(const std::string &lpDir)
bool isInteger(int columnNumber) const
Return true if a column is an integer (binary or general integer) variable.
CoinBigIndex getNumElements() const
Get number of nonzero elements.
const double * getObjCoefficients() const
Get pointer to array[getNumCols()] of objective function coefficients.
int num_objectives_
Number of objectives.
Definition: CoinLpIO.hpp:600
int read_monom_obj(double *coeff, char **name, int *cnt, char **obj_name, int *num_objectives, int *obj_starts)
Read a monomial of the objective function.
int bufferPosition_
Current buffer position.
Definition: CoinLpIO.hpp:677
void newLanguage(CoinMessages::Language language)
Set the language for messages.
CoinHashLink * hash_[2]
Hash tables with two sections.
Definition: CoinLpIO.hpp:670
int bufferLength_
Current buffer length (negative if not got eol)
Definition: CoinLpIO.hpp:675
int newCardLpIO() const
Get next line into inputBuffer_ (returns number in)
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.
Very simple class for containing data on set.
Definition: CoinMpsIO.hpp:269
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:625
void getPreviousColNames(char const *const *prev, int *card_prev) const
Get pointer to array[*card_prev] of previous column names.
void setLanguage(CoinMessages::Language language)
Set the language for messages.
Definition: CoinLpIO.hpp:532
void read_row(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.
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 &quot;free&quot; or one of its variants.
Abstract base class for file input classes.
Definition: CoinFileIO.hpp:40
~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:665
void stopHash(int section)
Delete hash storage.
bool eofFound_
If already inserted one End.
Definition: CoinLpIO.hpp:681
double * rowupper_
Pointer to dense vector of row upper bounds.
Definition: CoinLpIO.hpp:577
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.
int read_monom_row(char *start_str, double *coeff, char **name, int cnt_coeff) const
Read a monomial of a constraint.
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 &quot;subject to&quot; or one of its variants has been read...
double getInfinity() const
Get infinity.
Class to hold and manipulate an array of massaged messages.
double * collower_
Pointer to dense vector of column lower bounds.
Definition: CoinLpIO.hpp:580
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...
void skip_comment(char *buff) const
Read the file fp until buff contains an end of line.
bool defaultHandler_
Flag to say if the message handler is the default handler.
Definition: CoinLpIO.hpp:554
const int getNumObjectives() const
Get pointer to array[getNumCols()] of objective function coefficients.
void gutsOfDestructor()
Does the heavy lifting for destruct and assignment.
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 &quot;inf&quot; or one of its variants.
CoinPackedMatrix * matrixByRow_
Pointer to row-wise copy of problem matrix coefficients.
Definition: CoinLpIO.hpp:571
void setObjectiveOffset(double value)
Set objective offset.
Definition: CoinLpIO.hpp:283
CoinMessageHandler * handler_
Message handler.
Definition: CoinLpIO.hpp:548
double * colupper_
Pointer to dense vector of column upper bounds.
Definition: CoinLpIO.hpp:583
const char * getObjName() const
Get objective function name.
int decimals_
Number of decimals printed for coefficients.
Definition: CoinLpIO.hpp:628
CoinLpIO & operator=(const CoinLpIO &rhs)
assignment operator
int COINColumnIndex
Definition: CoinLpIO.hpp:27
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:622
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 &quot;Bounds&quot;, &quot;Integers&quot;, &quot;Generals&quot;, &quot;Binaries&quot;, &quot;Semi-continuous&quot;, &quot;Sos&quot;, &quot;End&quot;, 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.
Language
Supported languages.
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:645
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.
int fscanfLpIO(char *buff) const
Get next string (returns number in)
char inputBuffer_[1028]
Current buffer (needed so can get rid of blanks with :
Definition: CoinLpIO.hpp:673
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 &quot;x0&quot;, &quot;x1&quot;, ...
CoinSet ** set_
Pointer to sets.
Definition: CoinLpIO.hpp:610
const double * getRightHandSide() const
Get pointer to array[getNumRows()] of constraint right-hand sides.