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 #pragma GCC visibility push(default)
43 namespace abacus {
44 
45 class LpMasterOsi;
46 
47 
48 class OsiIF : public virtual LP {
49 public:
50 
52 
58  OsiIF(Master *master);
59 
61 
73  OsiIF(Master *master,
75  int nRow,
76  int maxRow,
77  int nCol,
78  int maxCol,
80  Array<double> &lb,
81  Array<double> &ub,
82  Array<Row*> &rows);
83 
85  virtual ~OsiIF();
86 
88  enum SOLVERTYPE { Exact, Approx };
89 
91 
92  OsiSolverInterface* osiLP() { return osiLP_; }
93 
94 
95 private:
96 
98 
102  OsiSolverInterface *osiLP_;
103 
104  /***************************************************
105  * Helper functions to free allocated memory
106  **************************************************/
107 
108  void freeDouble(const double* &ptr) {
109  delete [] ptr;
110  ptr = nullptr;
111  }
112 
113  void freeDouble(double* &ptr) {
114  delete [] ptr;
115  ptr = nullptr;
116  }
117 
118  void freeInt(int* &ptr) {
119  delete [] ptr;
120  ptr = nullptr;
121  }
122 
123  void freeChar(char* &ptr) {
124  delete [] ptr;
125  ptr = nullptr;
126  }
127 
128  void freeChar(const char* &ptr) {
129  delete [] ptr;
130  ptr = nullptr;
131  }
132 
133  void freeStatus(CoinWarmStartBasis::Status*&);
134 
136 
147  virtual void _initialize(
148  OptSense sense,
149  int nRow, int maxRow,
150  int nCol, int maxCol,
154  Array<Row*> &rows) override;
155 
157 
163 
165 
168  virtual OptSense _sense() const override;
169 
171 
174  virtual void _sense(const OptSense &newSense) override;
175 
177 
180  virtual int _nRow() const override { return numRows_; }
181 
183 
186  virtual int _maxRow() const override {
187  return numRows_; // Size management is completely done by Osi!
188  }
189 
191 
194  virtual int _nCol() const override { return numCols_; }
195 
197 
200  virtual int _maxCol() const override {
201  return numCols_; // Size management is completely done by Osi!
202  }
203 
205 
208  virtual double _obj(int i) const override { return objcoeff_[i]; }
209 
211 
214  virtual double _lBound(int i) const override { return collower_[i]; }
215 
217 
220  virtual double _uBound(int i) const override { return colupper_[i]; }
221 
223 
226  virtual double _rhs(int i) const override { return rhs_[i]; }
227 
229 
232  virtual void _row(int i, Row &r) const override;
233 
235 
238  virtual int _nnz() const override { return osiLP_->getNumElements(); }
239 
241 
244  virtual OPTSTAT _primalSimplex() override;
245 
247 
250  virtual OPTSTAT _dualSimplex() override;
251 
253 
256  virtual OPTSTAT _barrier(bool doCrossover) override;
257 
259 
262  virtual OPTSTAT _approx() override;
263 
265 
268  virtual double _value() const override { return value_; }
269 
271 
274  virtual double _xVal(int i) const override { return xVal_[i]; }
275 
277 
280  virtual double _barXVal(int i) const override;
281 
283 
286  virtual double _reco(int i) const override { return reco_[i]; }
287 
289 
292  virtual double _slack(int i) const override;
293 
295 
298  virtual double _yVal(int i) const override { return yVal_[i]; }
299 
301 
304  virtual LPVARSTAT::STATUS _lpVarStat(int i) const override;
305 
307 
310  virtual SlackStat::STATUS _slackStat(int i) const override;
311 
313 
327  virtual int _getInfeas(int &infeasRow, int &infeasCol, double *bInvRow) const override;
328 
330 
333  virtual void _remRows(ArrayBuffer<int> &ind) override;
334 
336 
339  virtual void _addRows(ArrayBuffer<Row*> &newRows) override ;
340 
342 
345  virtual void _remCols(ArrayBuffer<int> &vars) override;
346 
348 
351  virtual void _addCols(ArrayBuffer<Column*> &newVars) override;
352 
354 
358  virtual void _changeRhs(Array<double> &newRhs) override;
359 
361 
364  virtual void _changeLBound(int i, double newLb) override;
365 
367 
370  virtual void _changeUBound(int i, double newUb) override;
371 
373 
381  virtual int _pivotSlackVariableIn(ArrayBuffer<int> &rows) override;
382 
384 
389  void getSol();
390 
392  char csense2osi(CSense *sense) const;
393 
395  CSense::SENSE osi2csense(char sense) const;
396 
398  CoinWarmStartBasis::Status lpVarStat2osi(LPVARSTAT::STATUS stat) const;
399 
401  LPVARSTAT::STATUS osi2lpVarStat(CoinWarmStartBasis::Status stat) const;
402 
404  CoinWarmStartBasis::Status slackStat2osi(SlackStat::STATUS stat) const;
405 
407  SlackStat::STATUS osi2slackStat(CoinWarmStartBasis::Status stat) const;
408 
410  OsiSolverInterface* getDefaultInterface();
411 
413  OsiSolverInterface* switchInterfaces(SOLVERTYPE newMethod);
414 
416 
419  void loadDummyRow(OsiSolverInterface* s2, const double* lbounds, const double* ubounds, const double* objectives);
420 
422 
433  void _rowRealloc(int newSize) override;
434 
436 
442  void _colRealloc(int newSize) override;
443 
445 
452  virtual int _setSimplexIterationLimit(int limit) override {
453  return(!osiLP_->setIntParam(OsiMaxNumIteration, limit));
454  }
455 
457  /***
458  * \param limit Stores the iteration limit if the return value is 0.
459  *
460  * \return 0 If the iteration limit could be retrieved, \return 1 otherwise.
461  */
462  virtual int _getSimplexIterationLimit(int &limit) const override {
463  return(!osiLP_->getIntParam(OsiMaxNumIteration, limit));
464  }
465 
467  double inf,
468  const char sense,
469  const double right,
470  const double range,
471  double& lower,
472  double& upper) const
473  {
474  switch (sense) {
475  case 'E':
476  lower = upper = right;
477  break;
478  case 'L':
479  lower = -inf;
480  upper = right;
481  break;
482  case 'G':
483  lower = right;
484  upper = inf;
485  break;
486  case 'R':
487  lower = right - range;
488  upper = right;
489  break;
490  case 'N':
491  lower = -inf;
492  upper = inf;
493  break;
494  }
495  }
496 
498 
500  double value_;
501 
503  double *xVal_;
504 
505  const double *barXVal_;
506 
508  double *reco_;
509 
511  double *yVal_;
512 
514  char *cStat_;
515 
517  int numCols_;
518 
520  int numRows_;
521 
523  char *rStat_;
524 
526  const double *rhs_;
527 
529  double *rowactivity_;
530 
532  const char *rowsense_;
533 
535  const double *colupper_;
536 
538  const double *collower_;
539 
541  const double *objcoeff_;
542 
544  CoinWarmStartBasis *ws_;
545 
548 
549  OsiIF(const OsiIF &rhs);
550  const OsiIF &operator=(const OsiIF &rhs);
551 };
552 
553 }
554 #pragma GCC visibility pop
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:547
abacus::OsiIF::_getSimplexIterationLimit
virtual int _getSimplexIterationLimit(int &limit) const override
Defines a pure virtual function of the base class LP.
Definition: osiif.h:462
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:541
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:216
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:286
abacus::OsiIF::osiLP
OsiSolverInterface * osiLP()
Definition: osiif.h:92
abacus::OsiIF::freeDouble
void freeDouble(double *&ptr)
Definition: osiif.h:113
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:532
abacus::OptSense
Sense of optimization.
Definition: optsense.h:45
abacus::OsiIF::Approx
@ Approx
Definition: osiif.h:88
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:208
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:238
abacus::LP::lpVarStat
virtual LPVARSTAT::STATUS lpVarStat(int i) const
Definition: lp.h:351
abacus::OsiIF::_nRow
virtual int _nRow() const override
Returns the number of rows of the linear program.
Definition: osiif.h:180
abacus::OsiIF::_rhs
virtual double _rhs(int i) const override
Returns the right hand side of row i.
Definition: osiif.h:226
abacus::OsiIF::_lBound
virtual double _lBound(int i) const override
Returns the lower bound of column i.
Definition: osiif.h:214
abacus::LPVARSTAT::STATUS
STATUS
The enumeration of the statuses a variable gets from the linear program solver.
Definition: lpvarstat.h:55
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:298
abacus::LP::obj
double obj(int i) const
Definition: lp.h:230
abacus::OsiIF::rhs_
const double * rhs_
An array storing the right hand sides of the linear program.
Definition: osiif.h:526
abacus::OsiIF::collower_
const double * collower_
An array storing the column lower bounds of the linear program.
Definition: osiif.h:538
abacus::OsiIF::freeDouble
void freeDouble(const double *&ptr)
Definition: osiif.h:108
abacus::OsiIF::lpMasterOsi_
LpMasterOsi * lpMasterOsi_
Definition: osiif.h:497
abacus::OsiIF::rowactivity_
double * rowactivity_
An array storing the row activity of the linear program.
Definition: osiif.h:529
abacus::OsiIF::freeChar
void freeChar(const char *&ptr)
Definition: osiif.h:128
abacus::SlackStat::STATUS
STATUS
The different statuses of a slack variable.
Definition: slackstat.h:52
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:503
abacus::OsiIF::numRows_
int numRows_
The number of rows currently used in the LP.
Definition: osiif.h:520
abacus::OsiIF::_nCol
virtual int _nCol() const override
Returns the number of columns of the linear program.
Definition: osiif.h:194
abacus::OsiIF::numCols_
int numCols_
The number of columns currently used in the LP.
Definition: osiif.h:517
abacus::LP::maxRow
int maxRow() const
Definition: lp.h:222
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:544
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:358
abacus::OsiIF
Definition: osiif.h:48
r
int r[]
Definition: hierarchical-ranking.cpp:13
abacus::LP::OPTSTAT
OPTSTAT
The optimization status of the linear program.
Definition: lp.h:75
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:44
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:200
abacus::OsiIF::_value
virtual double _value() const override
Returns the optimum value of the linear program.
Definition: osiif.h:268
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:220
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:511
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:52
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:274
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:508
abacus::LP::uBound
double uBound(int i) const
Definition: lp.h:244
abacus::LP
Linear programs.
Definition: lp.h:71
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:224
abacus::OsiIF::Exact
@ Exact
Definition: osiif.h:88
abacus::OsiIF::barXVal_
const double * barXVal_
Definition: osiif.h:505
abacus::OsiIF::value_
double value_
The value of the optimal solution.
Definition: osiif.h:500
abacus::OsiIF::currentSolverType
SOLVERTYPE currentSolverType() const
Definition: osiif.h:90
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:186
abacus::OsiIF::rStat_
char * rStat_
An array storing the statuses of the slack variables after the linear program has been optimized.
Definition: osiif.h:523
abacus::OsiIF::SOLVERTYPE
SOLVERTYPE
The enumeration of possible solver types.
Definition: osiif.h:88
abacus::OsiIF::freeInt
void freeInt(int *&ptr)
Definition: osiif.h:118
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:535
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:123
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:258
abacus::LP::maxCol
int maxCol() const
Definition: lp.h:226
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:452
abacus::OsiIF::freeStatus
void freeStatus(CoinWarmStartBasis::Status *&)
abacus::LP::lBound
double lBound(int i) const
Definition: lp.h:237
abacus::OsiIF::cStat_
char * cStat_
An array storing the statuses of the variables after the linear program has been optimized.
Definition: osiif.h:514
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:49
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:220
abacus::OsiIF::_approx
virtual OPTSTAT _approx() override
Calls an approximate method.
osiinclude.h
abacus::CSense::SENSE
SENSE
Definition: csense.h:52
abacus::OsiIF::getDefaultInterface
OsiSolverInterface * getDefaultInterface()
Allocates an Open Solver Interface of type defaultOsiSolver.
abacus::Master
The master of the optimization.
Definition: master.h:70
abacus::OsiIF::convertSenseToBound
void convertSenseToBound(double inf, const char sense, const double right, const double range, double &lower, double &upper) const
Definition: osiif.h:466
abacus::OsiIF::osiLP_
OsiSolverInterface * osiLP_
Pointer to the Osi solver interface.
Definition: osiif.h:102
slackstat.h
status of slack variables