OpenMS
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
LPWrapper.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Alexandra Zerck $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/DATASTRUCTURES/String.h> // for String
12 
13 #include <OpenMS/CONCEPT/Types.h>
14 #include <OpenMS/OpenMSConfig.h>
15 #include <OpenMS/config.h>
16 
17 #include <limits>
18 
19 // do NOT include glpk and CoinOr headers here, as they define bad stuff, which ripples through OpenMS then...
20 // include them in LPWrapper.cpp where they do not harm
21 // only declare them here
22 class CoinModel;
23 
24 // if GLPK was found:
25 #ifndef OPENMS_HAS_COINOR
26  #ifndef GLP_PROB_DEFINED
27  #define GLP_PROB_DEFINED
28  // depending on the glpk version
29  // define glp_prob as forward or struct
30  #if OPENMS_GLPK_VERSION_MAJOR == 4 && OPENMS_GLPK_VERSION_MINOR < 48
31  typedef struct
32  {
33  double _opaque_prob[100];
34  } glp_prob;
35  #else
36  class glp_prob;
37  #endif
38  #endif
39 #endif
40 
41 namespace OpenMS
42 {
43 
65  class OPENMS_DLLAPI LPWrapper
66  {
67 public:
77  struct SolverParam
78  {
83  message_level(3), branching_tech(4), backtrack_tech(3),
84  preprocessing_tech(2), enable_feas_pump_heuristic(true), enable_gmi_cuts(true),
85  enable_mir_cuts(true), enable_cov_cuts(true), enable_clq_cuts(true), mip_gap(0.0),
86  time_limit((std::numeric_limits<Int>::max)()), output_freq(5000), output_delay(10000), enable_presolve(true),
87  enable_binarization(true)
88  {
89  }
90 
100  double mip_gap;
106  };
107 
113  enum Type
114  {
115  UNBOUNDED = 1,
119  FIXED
120  };
121 
128  {
129  CONTINUOUS = 1,
131  BINARY
132  };
133 
139  enum Sense
140  {
141  MIN = 1,
142  MAX
143  };
144 
151  {
152  FORMAT_LP = 0,
154  FORMAT_GLPK
155  };
156 
162  enum SOLVER
163  {
164  SOLVER_GLPK = 0
165 #ifdef OPENMS_HAS_COINOR
166  , SOLVER_COINOR
167 #endif
168  };
169 
176  {
177  UNDEFINED = 1,
178  OPTIMAL = 5,
179  FEASIBLE = 2,
180  NO_FEASIBLE_SOL = 4
181  };
182 
189 
195  virtual ~LPWrapper();
196 
197  // problem creation/manipulation
206  Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values, const String& name);
207 
214 
223  Int addColumn(const std::vector<Int>& column_indices, const std::vector<double>& column_values, const String& name);
224 
237  Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values,
238  const String& name, double lower_bound, double upper_bound, Type type);
239 
250  Int addColumn(const std::vector<Int>& column_indices, const std::vector<double>& column_values, const String& name, double lower_bound, double upper_bound, Type type);
251 
257  void deleteRow(Int index);
258 
265  void setColumnName(Int index, const String& name);
266 
274 
282 
289  Int getRowIndex(const String& name);
290 
297  Int getColumnIndex(const String& name);
298 
305  double getColumnUpperBound(Int index);
306 
313  double getColumnLowerBound(Int index);
314 
321  double getRowUpperBound(Int index);
322 
329  double getRowLowerBound(Int index);
330 
337  void setRowName(Int index, const String& name);
338 
347  void setColumnBounds(Int index, double lower_bound, double upper_bound, Type type);
348 
357  void setRowBounds(Int index, double lower_bound, double upper_bound, Type type);
358 
365  void setColumnType(Int index, VariableType type);
366 
374 
381  void setObjective(Int index, double obj_value);
382 
389  double getObjective(Int index);
390 
403 
410 
417 
425  void setElement(Int row_index, Int column_index, double value);
426 
434  double getElement(Int row_index, Int column_index);
435 
436  // problem reading/writing
443  void readProblem(const String& filename, const String& format);
444 
451  void writeProblem(const String& filename, const WriteFormat format) const;
452 
463  Int solve(SolverParam& solver_param, const Size verbose_level = 0);
464 
471 
472  // solution access
479 
486  double getColumnValue(Int index);
487 
495 
502  void getMatrixRow(Int idx, std::vector<Int>& indexes);
503 
509  SOLVER getSolver() const;
510 
511 protected:
512 #ifdef OPENMS_HAS_COINOR
513  CoinModel * model_ = nullptr;
514  std::vector<double> solution_;
515 #else
516  glp_prob * lp_problem_ = nullptr;
517 #endif
518 
520 
521 
522  }; // class
523 
524 } // namespace
525 
A wrapper class for linear programming (LP) solvers.
Definition: LPWrapper.h:66
Int addColumn()
Adds an empty column to the LP matrix.
void setObjective(Int index, double obj_value)
Set the objective coefficient for a column/variable.
void setColumnBounds(Int index, double lower_bound, double upper_bound, Type type)
Set column bounds.
Int solve(SolverParam &solver_param, const Size verbose_level=0)
solve problems, parameters like enabled heuristics can be given via solver_param
Type
Enumeration for variable/constraint bound types.
Definition: LPWrapper.h:114
@ UPPER_BOUND_ONLY
Only upper bound is specified.
Definition: LPWrapper.h:117
@ LOWER_BOUND_ONLY
Only lower bound is specified.
Definition: LPWrapper.h:116
@ DOUBLE_BOUNDED
Both lower and upper bounds are specified.
Definition: LPWrapper.h:118
double getObjective(Int index)
Get the objective coefficient for a column/variable.
double getElement(Int row_index, Int column_index)
Get the value of a matrix element at the specified position.
void setObjectiveSense(Sense sense)
Set objective direction.
void readProblem(const String &filename, const String &format)
Read LP from file.
void writeProblem(const String &filename, const WriteFormat format) const
Write LP formulation to a file.
Int getRowIndex(const String &name)
Find the index of a row by its name.
double getObjectiveValue()
Get the objective function value of the solution.
Sense getObjectiveSense()
Get the current objective direction.
String getRowName(Int index)
Get the name of a row.
Int getNumberOfNonZeroEntriesInRow(Int idx)
Get the number of non-zero entries in a specific row.
void setElement(Int row_index, Int column_index, double value)
Set the value of a matrix element at the specified position.
void setRowName(Int index, const String &name)
Set the name of a row.
void deleteRow(Int index)
Delete the row at the specified index.
VariableType getColumnType(Int index)
Get column/variable type.
SOLVER getSolver() const
Get the currently active solver backend.
SolverStatus
Enumeration for solver status after solving an LP problem.
Definition: LPWrapper.h:176
String getColumnName(Int index)
Get the name of a column.
void setRowBounds(Int index, double lower_bound, double upper_bound, Type type)
Set row bounds.
Int addColumn(const std::vector< Int > &column_indices, const std::vector< double > &column_values, const String &name, double lower_bound, double upper_bound, Type type)
Adds a column with boundaries to the LP matrix, returns index.
Int addRow(const std::vector< Int > &row_indices, const std::vector< double > &row_values, const String &name, double lower_bound, double upper_bound, Type type)
Adds a row with boundaries to the LP matrix, returns index.
Int getNumberOfColumns()
Get the number of columns/variables in the LP problem.
double getRowUpperBound(Int index)
Get the upper bound of a row.
LPWrapper()
Default constructor.
void setColumnType(Int index, VariableType type)
Set column/variable type.
Int getColumnIndex(const String &name)
Find the index of a column by its name.
SolverStatus getStatus()
Get solution status.
Int addRow(const std::vector< Int > &row_indices, const std::vector< double > &row_values, const String &name)
Adds a row to the LP matrix.
SOLVER
Enumeration for available LP solvers.
Definition: LPWrapper.h:163
SOLVER solver_
Currently active solver backend.
Definition: LPWrapper.h:519
void getMatrixRow(Int idx, std::vector< Int > &indexes)
Get the indices of non-zero entries in a specific row.
double getRowLowerBound(Int index)
Get the lower bound of a row.
Int getNumberOfRows()
Get the number of rows/constraints in the LP problem.
Sense
Enumeration for optimization direction.
Definition: LPWrapper.h:140
VariableType
Enumeration for variable types in the LP problem.
Definition: LPWrapper.h:128
@ INTEGER
Integer variable (can only take integer values within bounds)
Definition: LPWrapper.h:130
double getColumnUpperBound(Int index)
Get the upper bound of a column.
double getColumnLowerBound(Int index)
Get the lower bound of a column.
Int addColumn(const std::vector< Int > &column_indices, const std::vector< double > &column_values, const String &name)
Adds a column to the LP matrix.
double getColumnValue(Int index)
Get the value of a variable in the solution.
void setColumnName(Int index, const String &name)
Set the name of a column.
virtual ~LPWrapper()
Virtual destructor.
WriteFormat
Enumeration for LP problem file formats.
Definition: LPWrapper.h:151
@ FORMAT_MPS
MPS format (industry standard)
Definition: LPWrapper.h:153
A more convenient string class.
Definition: String.h:34
int Int
Signed integer type.
Definition: Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Struct that holds the parameters of the LP solver.
Definition: LPWrapper.h:78
Int backtrack_tech
Backtracking technique for MIP problems.
Definition: LPWrapper.h:93
double mip_gap
Relative gap tolerance for MIP problems.
Definition: LPWrapper.h:100
Int branching_tech
Branching technique for MIP problems.
Definition: LPWrapper.h:92
bool enable_binarization
Enable binarization (only with presolve)
Definition: LPWrapper.h:105
bool enable_cov_cuts
Enable cover cuts.
Definition: LPWrapper.h:98
Int time_limit
Time limit in milliseconds.
Definition: LPWrapper.h:101
bool enable_gmi_cuts
Enable Gomory mixed-integer cuts.
Definition: LPWrapper.h:96
Int message_level
Controls verbosity of solver output (0-3)
Definition: LPWrapper.h:91
Int output_delay
Output delay in milliseconds.
Definition: LPWrapper.h:103
bool enable_presolve
Enable presolve techniques.
Definition: LPWrapper.h:104
bool enable_mir_cuts
Enable mixed-integer rounding cuts.
Definition: LPWrapper.h:97
SolverParam()
Default constructor that initializes all parameters with reasonable defaults.
Definition: LPWrapper.h:82
Int output_freq
Output frequency in milliseconds.
Definition: LPWrapper.h:102
bool enable_feas_pump_heuristic
Enable feasibility pump heuristic.
Definition: LPWrapper.h:95
Int preprocessing_tech
Preprocessing technique.
Definition: LPWrapper.h:94
bool enable_clq_cuts
Enable clique cuts.
Definition: LPWrapper.h:99