OpenMS
LPWrapper.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- 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 COINOR_SOLVER
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 
44  class OPENMS_DLLAPI LPWrapper
45  {
46 public:
50  struct SolverParam
51  {
53  message_level(3), branching_tech(4), backtrack_tech(3),
54  preprocessing_tech(2), enable_feas_pump_heuristic(true), enable_gmi_cuts(true),
55  enable_mir_cuts(true), enable_cov_cuts(true), enable_clq_cuts(true), mip_gap(0.0),
56  time_limit((std::numeric_limits<Int>::max)()), output_freq(5000), output_delay(10000), enable_presolve(true),
57  enable_binarization(true)
58  {
59  }
60 
70  double mip_gap;
76  };
77 
78  enum Type
79  {
80  UNBOUNDED = 1,
84  FIXED
85  };
86 
88  {
89  CONTINUOUS = 1,
91  BINARY
92  };
93 
94  enum Sense
95  {
96  MIN = 1,
97  MAX
98  };
99 
101  {
102  FORMAT_LP = 0,
104  FORMAT_GLPK
105  };
106 
107  enum SOLVER
108  {
109  SOLVER_GLPK = 0
110 #if COINOR_SOLVER == 1
111  , SOLVER_COINOR
112 #endif
113  };
114 
116  {
117  UNDEFINED = 1,
118  OPTIMAL = 5,
119  FEASIBLE = 2,
120  NO_FEASIBLE_SOL = 4
121  };
122 
124  virtual ~LPWrapper();
125 
126  // problem creation/manipulation
128  Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values, const String& name);
132  Int addColumn(const std::vector<Int>& column_indices, const std::vector<double>& column_values, const String& name);
133 
146  Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values,
147  const String& name, double lower_bound, double upper_bound, Type type);
148 
159  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);
160 
162  void deleteRow(Int index);
164  void setColumnName(Int index, const String& name);
170  Int getRowIndex(const String& name);
172  Int getColumnIndex(const String& name);
174  double getColumnUpperBound(Int index);
176  double getColumnLowerBound(Int index);
178  double getRowUpperBound(Int index);
180  double getRowLowerBound(Int index);
182  void setRowName(Int index, const String& name);
183 
192  void setColumnBounds(Int index, double lower_bound, double upper_bound, Type type);
193 
202  void setRowBounds(Int index, double lower_bound, double upper_bound, Type type);
203 
210  void setColumnType(Int index, VariableType type);
211 
219 
221  void setObjective(Int index, double obj_value);
223  double getObjective(Int index);
224 
232 
237 
238  void setElement(Int row_index, Int column_index, double value);
239  double getElement(Int row_index, Int column_index);
240 
241  // problem reading/writing
248  void readProblem(const String& filename, const String& format);
249 
256  void writeProblem(const String& filename, const WriteFormat format) const;
257 
268  Int solve(SolverParam& solver_param, const Size verbose_level = 0);
269 
276 
277  // solution access
279  double getColumnValue(Int index);
280 
282  void getMatrixRow(Int idx, std::vector<Int>& indexes);
283 
285  SOLVER getSolver() const;
286 
287 protected:
288 #if COINOR_SOLVER == 1
289  CoinModel * model_ = nullptr;
290  std::vector<double> solution_;
291 #else
292  glp_prob * lp_problem_ = nullptr;
293 #endif
294 
296 
297 
298  }; // class
299 
300 } // namespace
301 
Definition: LPWrapper.h:45
Int addColumn()
adds an empty column to the LP matrix, returns index
void setObjective(Int index, double obj_value)
set objective value for column with index
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
Definition: LPWrapper.h:79
@ UPPER_BOUND_ONLY
Definition: LPWrapper.h:82
@ LOWER_BOUND_ONLY
Definition: LPWrapper.h:81
@ DOUBLE_BOUNDED
Definition: LPWrapper.h:83
double getObjective(Int index)
get objective value for column with index
double getElement(Int row_index, Int column_index)
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)
gets index of the row with name
double getObjectiveValue()
Sense getObjectiveSense()
String getRowName(Int index)
sets name of the index-th row
Int getNumberOfNonZeroEntriesInRow(Int idx)
void setElement(Int row_index, Int column_index, double value)
void setRowName(Int index, const String &name)
sets name of the index-th row
void deleteRow(Int index)
delete index-th row
VariableType getColumnType(Int index)
Get column/variable type.
SOLVER getSolver() const
get currently active solver
SolverStatus
Definition: LPWrapper.h:116
String getColumnName(Int index)
gets name of the index-th 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 number of columns
double getRowUpperBound(Int index)
gets row's upper bound
void setColumnType(Int index, VariableType type)
Set column/variable type.
Int getColumnIndex(const String &name)
gets index of the column with 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, returns index
SOLVER
Definition: LPWrapper.h:108
SOLVER solver_
Definition: LPWrapper.h:295
void getMatrixRow(Int idx, std::vector< Int > &indexes)
double getRowLowerBound(Int index)
gets row's lower bound
Int getNumberOfRows()
get number of rows
Sense
Definition: LPWrapper.h:95
VariableType
Definition: LPWrapper.h:88
@ INTEGER
Definition: LPWrapper.h:90
double getColumnUpperBound(Int index)
gets column's upper bound
double getColumnLowerBound(Int index)
gets column's lower bound
Int addColumn(const std::vector< Int > &column_indices, const std::vector< double > &column_values, const String &name)
adds a column to the LP matrix, returns index
double getColumnValue(Int index)
void setColumnName(Int index, const String &name)
sets name of the index-th column
virtual ~LPWrapper()
WriteFormat
Definition: LPWrapper.h:101
@ FORMAT_MPS
Definition: LPWrapper.h:103
A more convenient string class.
Definition: String.h:34
int Int
Signed integer type.
Definition: Types.h:76
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Struct that holds the parameters of the LP solver.
Definition: LPWrapper.h:51
Int backtrack_tech
Definition: LPWrapper.h:63
double mip_gap
Definition: LPWrapper.h:70
Int branching_tech
Definition: LPWrapper.h:62
bool enable_binarization
only with presolve
Definition: LPWrapper.h:75
bool enable_cov_cuts
Definition: LPWrapper.h:68
Int time_limit
Definition: LPWrapper.h:71
bool enable_gmi_cuts
Definition: LPWrapper.h:66
Int message_level
Definition: LPWrapper.h:61
Int output_delay
Definition: LPWrapper.h:73
bool enable_presolve
Definition: LPWrapper.h:74
bool enable_mir_cuts
Definition: LPWrapper.h:67
SolverParam()
Definition: LPWrapper.h:52
Int output_freq
Definition: LPWrapper.h:72
bool enable_feas_pump_heuristic
Definition: LPWrapper.h:65
Int preprocessing_tech
Definition: LPWrapper.h:64
bool enable_clq_cuts
Definition: LPWrapper.h:69