Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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,
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 }
abacus::OsiIF::slackStat2osi
CoinWarmStartBasis::Status slackStat2osi(SlackStat::STATUS stat) const
Converts the ABACUS slack status to OSI format.
ogdf::ArrayBuffer
An array that keeps track of the number of inserted elements; also usable as an efficient stack.
Definition: Array.h:53
abacus::OsiIF::currentSolverType_
SOLVERTYPE currentSolverType_
The type of the current solver interface.
Definition: osiif.h:546
abacus::OsiIF::_getSimplexIterationLimit
virtual int _getSimplexIterationLimit(int &limit) const override
Defines a pure virtual function of the base class LP.
Definition: osiif.h:461
lpvarstat.h
status of variables.
abacus::OsiIF::objcoeff_
const double * objcoeff_
An array storing the objective function coefficients of the linear program.
Definition: osiif.h:540
abacus::OsiIF::_rowRealloc
void _rowRealloc(int newSize) override
Reallocates the internal memory such that newSize rows can be stored.
abacus::LP::sense
OptSense sense() const
Definition: lp.h:215
abacus::OsiIF::_loadBasis
virtual void _loadBasis(Array< LPVARSTAT::STATUS > &lpVarStat, Array< SlackStat::STATUS > &slackStat) override
Loads a basis to the solver.
abacus::OsiIF::_reco
virtual double _reco(int i) const override
Returns the reduced cost of the column i.
Definition: osiif.h:285
abacus::OsiIF::osiLP
OsiSolverInterface * osiLP()
Definition: osiif.h:91
abacus::OsiIF::freeDouble
void freeDouble(double *&ptr)
Definition: osiif.h:112
abacus::OsiIF::_barrier
virtual OPTSTAT _barrier(bool doCrossover) override
Calls the barrier method.
abacus::OsiIF::rowsense_
const char * rowsense_
An array storing the row senses of the linear program.
Definition: osiif.h:531
abacus::OptSense
Sense of optimization.
Definition: optsense.h:44
abacus::OsiIF::Approx
@ Approx
Definition: osiif.h:87
abacus::OsiIF::lpVarStat2osi
CoinWarmStartBasis::Status lpVarStat2osi(LPVARSTAT::STATUS stat) const
Converts the ABACUS variable status to OSI format.
abacus::OsiIF::OsiIF
OsiIF(Master *master)
Constructor without initialization.
abacus::OsiIF::csense2osi
char csense2osi(CSense *sense) const
Converts the ABACUS representation of the row sense to the Osi representation.
abacus
Definition: ILPClusterPlanarity.h:50
abacus::OsiIF::_obj
virtual double _obj(int i) const override
Returns the objective function coefficient of column i.
Definition: osiif.h:207
abacus::OsiIF::_remCols
virtual void _remCols(ArrayBuffer< int > &vars) override
Removes the columns listed in vars.
abacus::OsiIF::_changeUBound
virtual void _changeUBound(int i, double newUb) override
Sets the upper bound of column i to newLb.
abacus::OsiIF::_nnz
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
abacus::LP::lpVarStat
virtual LPVARSTAT::STATUS lpVarStat(int i) const
Definition: lp.h:350
abacus::OsiIF::_nRow
virtual int _nRow() const override
Returns the number of rows of the linear program.
Definition: osiif.h:179
abacus::OsiIF::_rhs
virtual double _rhs(int i) const override
Returns the right hand side of row i.
Definition: osiif.h:225
abacus::OsiIF::_lBound
virtual double _lBound(int i) const override
Returns the lower bound of column i.
Definition: osiif.h:213
abacus::LPVARSTAT::STATUS
STATUS
The enumeration of the statuses a variable gets from the linear program solver.
Definition: lpvarstat.h:54
abacus::OsiIF::loadDummyRow
void loadDummyRow(OsiSolverInterface *s2, const double *lbounds, const double *ubounds, const double *objectives)
Initializes the problem with a dummy row.
abacus::OsiIF::_lpVarStat
virtual LPVARSTAT::STATUS _lpVarStat(int i) const override
Returns the status of the column i.
abacus::OsiIF::_yVal
virtual double _yVal(int i) const override
Returns the value of the dual column of the row i.
Definition: osiif.h:297
abacus::LP::obj
double obj(int i) const
Definition: lp.h:229
abacus::OsiIF::rhs_
const double * rhs_
An array storing the right hand sides of the linear program.
Definition: osiif.h:525
abacus::OsiIF::collower_
const double * collower_
An array storing the column lower bounds of the linear program.
Definition: osiif.h:537
abacus::OsiIF::freeDouble
void freeDouble(const double *&ptr)
Definition: osiif.h:107
abacus::OsiIF::lpMasterOsi_
LpMasterOsi * lpMasterOsi_
Definition: osiif.h:496
abacus::OsiIF::rowactivity_
double * rowactivity_
An array storing the row activity of the linear program.
Definition: osiif.h:528
abacus::OsiIF::freeChar
void freeChar(const char *&ptr)
Definition: osiif.h:127
abacus::SlackStat::STATUS
STATUS
The different statuses of a slack variable.
Definition: slackstat.h:51
abacus::OsiIF::operator=
const OsiIF & operator=(const OsiIF &rhs)
abacus::OsiIF::xVal_
double * xVal_
An array storing the values of the variables after the linear program has been optimized.
Definition: osiif.h:502
abacus::OsiIF::numRows_
int numRows_
The number of rows currently used in the LP.
Definition: osiif.h:519
abacus::OsiIF::_nCol
virtual int _nCol() const override
Returns the number of columns of the linear program.
Definition: osiif.h:193
abacus::OsiIF::numCols_
int numCols_
The number of columns currently used in the LP.
Definition: osiif.h:516
abacus::LP::maxRow
int maxRow() const
Definition: lp.h:221
abacus::OsiIF::_addRows
virtual void _addRows(ArrayBuffer< Row * > &newRows) override
Adds the rows to the linear program.
abacus::OsiIF::ws_
CoinWarmStartBasis * ws_
A warm start object storing information about a basis of the linear program.
Definition: osiif.h:543
abacus::OsiIF::_row
virtual void _row(int i, Row &r) const override
Stores a copy of row i in r.
abacus::LP::slackStat
virtual SlackStat::STATUS slackStat(int i) const
Definition: lp.h:357
abacus::OsiIF
Definition: osiif.h:47
r
int r[]
Definition: hierarchical-ranking.cpp:13
abacus::LP::OPTSTAT
OPTSTAT
The optimization status of the linear program.
Definition: lp.h:74
abacus::OsiIF::_initialize
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.
abacus::LpMasterOsi
The OSI LP master.
Definition: lpmasterosi.h:43
abacus::OsiIF::_primalSimplex
virtual OPTSTAT _primalSimplex() override
Calls the primal simplex method.
abacus::OsiIF::_maxCol
virtual int _maxCol() const override
Returns the maximal number of columns of the linear program.
Definition: osiif.h:199
abacus::OsiIF::_value
virtual double _value() const override
Returns the optimum value of the linear program.
Definition: osiif.h:267
abacus::OsiIF::_getInfeas
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...
lp.h
linear program.
abacus::LP::nRow
int nRow() const
Definition: lp.h:219
abacus::OsiIF::_changeLBound
virtual void _changeLBound(int i, double newLb) override
Sets the lower bound of column i to newLb.
abacus::OsiIF::_slackStat
virtual SlackStat::STATUS _slackStat(int i) const override
Returns the status of the slack column i.
abacus::OsiIF::yVal_
double * yVal_
An array storing the values of the dual variables after the linear program has been optimized.
Definition: osiif.h:510
ogdf::Array
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:219
optsense.h
sense of optimization.
abacus::Row
Representation of constraints in the row format.
Definition: row.h:51
abacus::OsiIF::_pivotSlackVariableIn
virtual int _pivotSlackVariableIn(ArrayBuffer< int > &rows) override
Pivots the slack variables stored in the buffer rows into the basis.
abacus::OsiIF::_xVal
virtual double _xVal(int i) const override
Returns the value of the column i.
Definition: osiif.h:273
abacus::OsiIF::switchInterfaces
OsiSolverInterface * switchInterfaces(SOLVERTYPE newMethod)
Switches between exact and approximate solvers.
csense.h
csense.
abacus::OsiIF::_sense
virtual OptSense _sense() const override
Returns the sense of the optimization.
abacus::OsiIF::getSol
void getSol()
Extracts the solution.
abacus::OsiIF::reco_
double * reco_
An array storing the values of the reduced costs after the linear program has been optimized.
Definition: osiif.h:507
abacus::LP::uBound
double uBound(int i) const
Definition: lp.h:243
abacus::LP
Linear programs.
Definition: lp.h:70
abacus::OsiIF::_slack
virtual double _slack(int i) const override
Returns the value of the slack column of the row i.
abacus::OsiIF::_colRealloc
void _colRealloc(int newSize) override
Reallocates the internal memory such that newSize columns can be stored.
abacus::LP::nCol
int nCol() const
Definition: lp.h:223
abacus::OsiIF::Exact
@ Exact
Definition: osiif.h:87
abacus::OsiIF::barXVal_
const double * barXVal_
Definition: osiif.h:504
abacus::OsiIF::value_
double value_
The value of the optimal solution.
Definition: osiif.h:499
abacus::OsiIF::currentSolverType
SOLVERTYPE currentSolverType() const
Definition: osiif.h:89
abacus::OsiIF::_remRows
virtual void _remRows(ArrayBuffer< int > &ind) override
Removes the rows listed in ind.
abacus::OsiIF::_maxRow
virtual int _maxRow() const override
Returns the maximal number of rows of the linear program.
Definition: osiif.h:185
abacus::OsiIF::rStat_
char * rStat_
An array storing the statuses of the slack variables after the linear program has been optimized.
Definition: osiif.h:522
abacus::OsiIF::SOLVERTYPE
SOLVERTYPE
The enumeration of possible solver types.
Definition: osiif.h:87
abacus::OsiIF::freeInt
void freeInt(int *&ptr)
Definition: osiif.h:117
abacus::OsiIF::osi2lpVarStat
LPVARSTAT::STATUS osi2lpVarStat(CoinWarmStartBasis::Status stat) const
Converts the OSI variable status to ABACUS format.
abacus::OsiIF::colupper_
const double * colupper_
An array storing the column upper bounds of the linear program.
Definition: osiif.h:534
abacus::OsiIF::_barXVal
virtual double _barXVal(int i) const override
Returns the value of the column i.
abacus::OsiIF::freeChar
void freeChar(char *&ptr)
Definition: osiif.h:122
abacus::OsiIF::_changeRhs
virtual void _changeRhs(Array< double > &newRhs) override
Sets the right hand side of the linear program to newRhs.
abacus::LP::rhs
double rhs(int i) const
Definition: lp.h:257
abacus::LP::maxCol
int maxCol() const
Definition: lp.h:225
abacus::OsiIF::_dualSimplex
virtual OPTSTAT _dualSimplex() override
Calls the dual simplex method.
abacus::OsiIF::osi2slackStat
SlackStat::STATUS osi2slackStat(CoinWarmStartBasis::Status stat) const
Converts the OSI slack status to ABACUS format.
abacus::OsiIF::~OsiIF
virtual ~OsiIF()
The destructor.
abacus::OsiIF::_setSimplexIterationLimit
virtual int _setSimplexIterationLimit(int limit) override
Changes the iteration limit of the Simplex algorithm.
Definition: osiif.h:451
abacus::OsiIF::freeStatus
void freeStatus(CoinWarmStartBasis::Status *&)
abacus::LP::lBound
double lBound(int i) const
Definition: lp.h:236
abacus::OsiIF::cStat_
char * cStat_
An array storing the statuses of the variables after the linear program has been optimized.
Definition: osiif.h:513
abacus::OsiIF::_addCols
virtual void _addCols(ArrayBuffer< Column * > &newVars) override
Adds the columns newCols to the linear program.
abacus::CSense
Sense of constraints.
Definition: csense.h:48
abacus::OsiIF::osi2csense
CSense::SENSE osi2csense(char sense) const
Converts the OSI representation of the row sense to the ABACUS representation.
abacus::OsiIF::_uBound
virtual double _uBound(int i) const override
Returns the upper bound of column i.
Definition: osiif.h:219
abacus::OsiIF::_approx
virtual OPTSTAT _approx() override
Calls an approximate method.
osiinclude.h
abacus::CSense::SENSE
SENSE
Definition: csense.h:51
abacus::OsiIF::getDefaultInterface
OsiSolverInterface * getDefaultInterface()
Allocates an Open Solver Interface of type defaultOsiSolver.
abacus::Master
The master of the optimization.
Definition: master.h:69
abacus::OsiIF::convertSenseToBound
void convertSenseToBound(double inf, const char sense, const double right, const double range, double &lower, double &upper) const
Definition: osiif.h:465
abacus::OsiIF::osiLP_
OsiSolverInterface * osiLP_
Pointer to the Osi solver interface.
Definition: osiif.h:101
slackstat.h
status of slack variables