Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

osiif.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <ogdf/lib/abacus/lp.h>
35 #include <ogdf/lib/abacus/csense.h>
37 #include <coin/CoinPackedMatrix.hpp>
38 #include <coin/CoinPackedVector.hpp>
39 #include <coin/CoinWarmStartBasis.hpp>
40 #include <coin/CoinBuild.hpp>
41 
42 namespace abacus {
43 
44 class LpMasterOsi;
45 
46 
47 class OsiIF : public virtual LP {
48 public:
49 
51 
57  OsiIF(Master *master);
58 
60 
72  OsiIF(Master *master,
74  int nRow,
75  int maxRow,
76  int nCol,
77  int maxCol,
79  Array<double> &lb,
80  Array<double> &ub,
81  Array<Row*> &rows);
82 
84  virtual ~OsiIF();
85 
87  enum SOLVERTYPE { Exact, Approx };
88 
90 
91  OsiSolverInterface* osiLP() { return osiLP_; }
92 
93 
94 private:
95 
97 
101  OsiSolverInterface *osiLP_;
102 
103  /***************************************************
104  * Helper functions to free allocated memory
105  **************************************************/
106 
107  void freeDouble(const double* &ptr) {
108  delete [] ptr;
109  ptr = nullptr;
110  }
111 
112  void freeDouble(double* &ptr) {
113  delete [] ptr;
114  ptr = nullptr;
115  }
116 
117  void freeInt(int* &ptr) {
118  delete [] ptr;
119  ptr = nullptr;
120  }
121 
122  void freeChar(char* &ptr) {
123  delete [] ptr;
124  ptr = nullptr;
125  }
126 
127  void freeChar(const char* &ptr) {
128  delete [] ptr;
129  ptr = nullptr;
130  }
131 
132  void freeStatus(CoinWarmStartBasis::Status*&);
133 
135 
146  virtual void _initialize(
147  OptSense sense,
148  int nRow, int maxRow,
149  int nCol, int maxCol,
150  Array<double> &obj,
153  Array<Row*> &rows) override;
154 
156 
162 
164 
167  virtual OptSense _sense() const override;
168 
170 
173  virtual void _sense(const OptSense &newSense) override;
174 
176 
179  virtual int _nRow() const override { return numRows_; }
180 
182 
185  virtual int _maxRow() const override {
186  return numRows_; // Size management is completely done by Osi!
187  }
188 
190 
193  virtual int _nCol() const override { return numCols_; }
194 
196 
199  virtual int _maxCol() const override {
200  return numCols_; // Size management is completely done by Osi!
201  }
202 
204 
207  virtual double _obj(int i) const override { return objcoeff_[i]; }
208 
210 
213  virtual double _lBound(int i) const override { return collower_[i]; }
214 
216 
219  virtual double _uBound(int i) const override { return colupper_[i]; }
220 
222 
225  virtual double _rhs(int i) const override { return rhs_[i]; }
226 
228 
231  virtual void _row(int i, Row &r) const override;
232 
234 
237  virtual int _nnz() const override { return osiLP_->getNumElements(); }
238 
240 
243  virtual OPTSTAT _primalSimplex() override;
244 
246 
249  virtual OPTSTAT _dualSimplex() override;
250 
252 
255  virtual OPTSTAT _barrier(bool doCrossover) override;
256 
258 
261  virtual OPTSTAT _approx() override;
262 
264 
267  virtual double _value() const override { return value_; }
268 
270 
273  virtual double _xVal(int i) const override { return xVal_[i]; }
274 
276 
279  virtual double _barXVal(int i) const override;
280 
282 
285  virtual double _reco(int i) const override { return reco_[i]; }
286 
288 
291  virtual double _slack(int i) const override;
292 
294 
297  virtual double _yVal(int i) const override { return yVal_[i]; }
298 
300 
303  virtual LPVARSTAT::STATUS _lpVarStat(int i) const override;
304 
306 
309  virtual SlackStat::STATUS _slackStat(int i) const override;
310 
312 
326  virtual int _getInfeas(int &infeasRow, int &infeasCol, double *bInvRow) const override;
327 
329 
332  virtual void _remRows(ArrayBuffer<int> &ind) override;
333 
335 
338  virtual void _addRows(ArrayBuffer<Row*> &newRows) override ;
339 
341 
344  virtual void _remCols(ArrayBuffer<int> &vars) override;
345 
347 
350  virtual void _addCols(ArrayBuffer<Column*> &newVars) override;
351 
353 
357  virtual void _changeRhs(Array<double> &newRhs) override;
358 
360 
363  virtual void _changeLBound(int i, double newLb) override;
364 
366 
369  virtual void _changeUBound(int i, double newUb) override;
370 
372 
380  virtual int _pivotSlackVariableIn(ArrayBuffer<int> &rows) override;
381 
383 
388  void getSol();
389 
391  char csense2osi(CSense *sense) const;
392 
394  CSense::SENSE osi2csense(char sense) const;
395 
397  CoinWarmStartBasis::Status lpVarStat2osi(LPVARSTAT::STATUS stat) const;
398 
400  LPVARSTAT::STATUS osi2lpVarStat(CoinWarmStartBasis::Status stat) const;
401 
403  CoinWarmStartBasis::Status slackStat2osi(SlackStat::STATUS stat) const;
404 
406  SlackStat::STATUS osi2slackStat(CoinWarmStartBasis::Status stat) const;
407 
409  OsiSolverInterface* getDefaultInterface();
410 
412  OsiSolverInterface* switchInterfaces(SOLVERTYPE newMethod);
413 
415 
418  void loadDummyRow(OsiSolverInterface* s2, const double* lbounds, const double* ubounds, const double* objectives);
419 
421 
432  void _rowRealloc(int newSize) override;
433 
435 
441  void _colRealloc(int newSize) override;
442 
444 
451  virtual int _setSimplexIterationLimit(int limit) override {
452  return(!osiLP_->setIntParam(OsiMaxNumIteration, limit));
453  }
454 
456  /***
457  * \param limit Stores the iteration limit if the return value is 0.
458  *
459  * \return 0 If the iteration limit could be retrieved, \return 1 otherwise.
460  */
461  virtual int _getSimplexIterationLimit(int &limit) const override {
462  return(!osiLP_->getIntParam(OsiMaxNumIteration, limit));
463  }
464 
466  double inf,
467  const char sense,
468  const double right,
469  const double range,
470  double& lower,
471  double& upper) const
472  {
473  switch (sense) {
474  case 'E':
475  lower = upper = right;
476  break;
477  case 'L':
478  lower = -inf;
479  upper = right;
480  break;
481  case 'G':
482  lower = right;
483  upper = inf;
484  break;
485  case 'R':
486  lower = right - range;
487  upper = right;
488  break;
489  case 'N':
490  lower = -inf;
491  upper = inf;
492  break;
493  }
494  }
495 
497 
499  double value_;
500 
502  double *xVal_;
503 
504  const double *barXVal_;
505 
507  double *reco_;
508 
510  double *yVal_;
511 
513  char *cStat_;
514 
516  int numCols_;
517 
519  int numRows_;
520 
522  char *rStat_;
523 
525  const double *rhs_;
526 
528  double *rowactivity_;
529 
531  const char *rowsense_;
532 
534  const double *colupper_;
535 
537  const double *collower_;
538 
540  const double *objcoeff_;
541 
543  CoinWarmStartBasis *ws_;
544 
547 
548  OsiIF(const OsiIF &rhs);
549  const OsiIF &operator=(const OsiIF &rhs);
550 };
551 
552 }
virtual void _initialize(OptSense sense, int nRow, int maxRow, int nCol, int maxCol, Array< double > &obj, Array< double > &lBound, Array< double > &uBound, Array< Row *> &rows) override
Loads the linear program defined by the following arguments to the solver.
SlackStat::STATUS osi2slackStat(CoinWarmStartBasis::Status stat) const
Converts the OSI slack status to ABACUS format.
STATUS
The different statuses of a slack variable.
Definition: slackstat.h:51
CSense::SENSE osi2csense(char sense) const
Converts the OSI representation of the row sense to the ABACUS representation.
char * cStat_
An array storing the statuses of the variables after the linear program has been optimized.
Definition: osiif.h:513
virtual int _nnz() const override
Returns the number of nonzero elements in the constraint matrix (not including the right hand side)...
Definition: osiif.h:237
virtual double _reco(int i) const override
Returns the reduced cost of the column i.
Definition: osiif.h:285
OptSense sense() const
Definition: lp.h:215
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
Representation of constraints in the row format.
Definition: row.h:51
The master of the optimization.
Definition: master.h:69
csense.
virtual double _lBound(int i) const override
Returns the lower bound of column i.
Definition: osiif.h:213
virtual void _remCols(ArrayBuffer< int > &vars) override
Removes the columns listed in vars.
OsiIF(Master *master)
Constructor without initialization.
virtual void _addRows(ArrayBuffer< Row *> &newRows) override
Adds the rows to the linear program.
Linear programs.
Definition: lp.h:70
virtual void _remRows(ArrayBuffer< int > &ind) override
Removes the rows listed in ind.
LpMasterOsi * lpMasterOsi_
Definition: osiif.h:496
int maxRow() const
Definition: lp.h:221
void _colRealloc(int newSize) override
Reallocates the internal memory such that newSize columns can be stored.
virtual void _changeUBound(int i, double newUb) override
Sets the upper bound of column i to newLb.
SOLVERTYPE currentSolverType() const
Definition: osiif.h:89
void freeChar(const char *&ptr)
Definition: osiif.h:127
const OsiIF & operator=(const OsiIF &rhs)
void _rowRealloc(int newSize) override
Reallocates the internal memory such that newSize rows can be stored.
int numRows_
The number of rows currently used in the LP.
Definition: osiif.h:519
virtual int _getSimplexIterationLimit(int &limit) const override
Defines a pure virtual function of the base class LP.
Definition: osiif.h:461
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
CoinWarmStartBasis * ws_
A warm start object storing information about a basis of the linear program.
Definition: osiif.h:543
virtual int _maxCol() const override
Returns the maximal number of columns of the linear program.
Definition: osiif.h:199
virtual int _getInfeas(int &infeasRow, int &infeasCol, double *bInvRow) const override
Can be called if the last linear program has been solved with the dual simplex method and is infeasib...
virtual void _changeLBound(int i, double newLb) override
Sets the lower bound of column i to newLb.
void freeDouble(double *&ptr)
Definition: osiif.h:112
LPVARSTAT::STATUS osi2lpVarStat(CoinWarmStartBasis::Status stat) const
Converts the OSI variable status to ABACUS format.
double obj(int i) const
Definition: lp.h:229
virtual int _pivotSlackVariableIn(ArrayBuffer< int > &rows) override
Pivots the slack variables stored in the buffer rows into the basis.
virtual double _value() const override
Returns the optimum value of the linear program.
Definition: osiif.h:267
status of slack variables
OPTSTAT
The optimization status of the linear program.
Definition: lp.h:74
virtual OPTSTAT _primalSimplex() override
Calls the primal simplex method.
void freeDouble(const double *&ptr)
Definition: osiif.h:107
double * rowactivity_
An array storing the row activity of the linear program.
Definition: osiif.h:528
sense of optimization.
char csense2osi(CSense *sense) const
Converts the ABACUS representation of the row sense to the Osi representation.
virtual int _nRow() const override
Returns the number of rows of the linear program.
Definition: osiif.h:179
double * reco_
An array storing the values of the reduced costs after the linear program has been optimized...
Definition: osiif.h:507
virtual SlackStat::STATUS slackStat(int i) const
Definition: lp.h:357
virtual LPVARSTAT::STATUS lpVarStat(int i) const
Definition: lp.h:350
const double * objcoeff_
An array storing the objective function coefficients of the linear program.
Definition: osiif.h:540
const double * barXVal_
Definition: osiif.h:504
virtual double _obj(int i) const override
Returns the objective function coefficient of column i.
Definition: osiif.h:207
virtual double _yVal(int i) const override
Returns the value of the dual column of the row i.
Definition: osiif.h:297
Sense of constraints.
Definition: csense.h:48
void loadDummyRow(OsiSolverInterface *s2, const double *lbounds, const double *ubounds, const double *objectives)
Initializes the problem with a dummy row.
virtual LPVARSTAT::STATUS _lpVarStat(int i) const override
Returns the status of the column i.
SOLVERTYPE currentSolverType_
The type of the current solver interface.
Definition: osiif.h:546
OsiSolverInterface * getDefaultInterface()
Allocates an Open Solver Interface of type defaultOsiSolver.
status of variables.
double * xVal_
An array storing the values of the variables after the linear program has been optimized.
Definition: osiif.h:502
OsiSolverInterface * osiLP_
Pointer to the Osi solver interface.
Definition: osiif.h:101
CoinWarmStartBasis::Status slackStat2osi(SlackStat::STATUS stat) const
Converts the ABACUS slack status to OSI format.
OsiSolverInterface * switchInterfaces(SOLVERTYPE newMethod)
Switches between exact and approximate solvers.
virtual int _setSimplexIterationLimit(int limit) override
Changes the iteration limit of the Simplex algorithm.
Definition: osiif.h:451
CoinWarmStartBasis::Status lpVarStat2osi(LPVARSTAT::STATUS stat) const
Converts the ABACUS variable status to OSI format.
const char * rowsense_
An array storing the row senses of the linear program.
Definition: osiif.h:531
virtual ~OsiIF()
The destructor.
virtual double _slack(int i) const override
Returns the value of the slack column of the row i.
virtual OPTSTAT _dualSimplex() override
Calls the dual simplex method.
virtual OPTSTAT _barrier(bool doCrossover) override
Calls the barrier method.
void freeStatus(CoinWarmStartBasis::Status *&)
virtual OptSense _sense() const override
Returns the sense of the optimization.
virtual double _uBound(int i) const override
Returns the upper bound of column i.
Definition: osiif.h:219
virtual OPTSTAT _approx() override
Calls an approximate method.
int nRow() const
Definition: lp.h:219
Sense of optimization.
Definition: optsense.h:44
double rhs(int i) const
Definition: lp.h:257
virtual void _row(int i, Row &r) const override
Stores a copy of row i in r.
STATUS
The enumeration of the statuses a variable gets from the linear program solver.
Definition: lpvarstat.h:54
void convertSenseToBound(double inf, const char sense, const double right, const double range, double &lower, double &upper) const
Definition: osiif.h:465
int nCol() const
Definition: lp.h:223
The OSI LP master.
Definition: lpmasterosi.h:43
double uBound(int i) const
Definition: lp.h:243
void getSol()
Extracts the solution.
virtual double _rhs(int i) const override
Returns the right hand side of row i.
Definition: osiif.h:225
int numCols_
The number of columns currently used in the LP.
Definition: osiif.h:516
double lBound(int i) const
Definition: lp.h:236
double value_
The value of the optimal solution.
Definition: osiif.h:499
void freeChar(char *&ptr)
Definition: osiif.h:122
const double * rhs_
An array storing the right hand sides of the linear program.
Definition: osiif.h:525
const double * collower_
An array storing the column lower bounds of the linear program.
Definition: osiif.h:537
virtual int _maxRow() const override
Returns the maximal number of rows of the linear program.
Definition: osiif.h:185
virtual int _nCol() const override
Returns the number of columns of the linear program.
Definition: osiif.h:193
SOLVERTYPE
The enumeration of possible solver types.
Definition: osiif.h:87
double * yVal_
An array storing the values of the dual variables after the linear program has been optimized...
Definition: osiif.h:510
virtual double _barXVal(int i) const override
Returns the value of the column i.
char * rStat_
An array storing the statuses of the slack variables after the linear program has been optimized...
Definition: osiif.h:522
virtual void _addCols(ArrayBuffer< Column *> &newVars) override
Adds the columns newCols to the linear program.
OsiSolverInterface * osiLP()
Definition: osiif.h:91
void freeInt(int *&ptr)
Definition: osiif.h:117
virtual SlackStat::STATUS _slackStat(int i) const override
Returns the status of the slack column i.
int maxCol() const
Definition: lp.h:225
const double * colupper_
An array storing the column upper bounds of the linear program.
Definition: osiif.h:534
virtual double _xVal(int i) const override
Returns the value of the column i.
Definition: osiif.h:273
virtual void _loadBasis(Array< LPVARSTAT::STATUS > &lpVarStat, Array< SlackStat::STATUS > &slackStat) override
Loads a basis to the solver.
linear program.
virtual void _changeRhs(Array< double > &newRhs) override
Sets the right hand side of the linear program to newRhs.