OpenMS
Loading...
Searching...
No Matches
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/StringUtils.h> // for String
12
14#include <OpenMS/OpenMSConfig.h>
15#include <OpenMS/config.h>
16
17#include <cstdint>
18#include <limits>
19#include <memory>
20
21// do NOT include glpk and CoinOr headers here, as they define bad stuff, which ripples through OpenMS then...
22// include them in LPWrapper.cpp where they do not harm
23// only declare them here
24class CoinModel;
25
26// Forward declare HiGHS
27class Highs;
28
29// if GLPK was found:
30#if !defined(OPENMS_HAS_COINOR) && !defined(OPENMS_HAS_HIGHS)
31 #ifndef GLP_PROB_DEFINED
32 #define GLP_PROB_DEFINED
33 // depending on the glpk version
34 // define glp_prob as forward or struct
35 #if OPENMS_GLPK_VERSION_MAJOR == 4 && OPENMS_GLPK_VERSION_MINOR < 48
36 typedef struct
37 {
38 double _opaque_prob[100];
39 } glp_prob;
40 #else
41 class glp_prob;
42 #endif
43 #endif
44#endif
45
46namespace OpenMS
47{
48
71 class OPENMS_DLLAPI LPWrapper
72 {
73public:
84 {
89 message_level(3), branching_tech(4), backtrack_tech(3),
90 preprocessing_tech(2), enable_feas_pump_heuristic(true), enable_gmi_cuts(true),
91 enable_mir_cuts(true), enable_cov_cuts(true), enable_clq_cuts(true), mip_gap(0.0),
92 time_limit((std::numeric_limits<Int>::max)()), output_freq(5000), output_delay(10000), enable_presolve(true),
93 enable_binarization(true)
94 {
95 }
96
106 double mip_gap;
112 };
113
127
134 {
135 CONTINUOUS = 1,
137 BINARY
138 };
139
145 enum Sense
146 {
147 MIN = 1,
148 MAX
149 };
150
157 {
158 FORMAT_LP = 0,
160 FORMAT_GLPK
161 };
162
169 {
170 SOLVER_GLPK = 0
171#ifdef OPENMS_HAS_COINOR
172 , SOLVER_COINOR
173#endif
174#ifdef OPENMS_HAS_HIGHS
175 , SOLVER_HIGHS
176#endif
177 };
178
185 {
186 UNDEFINED = 1,
187 OPTIMAL = 5,
188 FEASIBLE = 2,
189 NO_FEASIBLE_SOL = 4
190 };
191
199
205 virtual ~LPWrapper();
206
207 // problem creation/manipulation
216 Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values, const std::string& name);
217
224
233 Int addColumn(const std::vector<Int>& column_indices, const std::vector<double>& column_values, const std::string& name);
234
247 Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values,
248 const std::string& name, double lower_bound, double upper_bound, Type type);
249
260 Int addColumn(const std::vector<Int>& column_indices, const std::vector<double>& column_values, const std::string& name, double lower_bound, double upper_bound, Type type);
261
267 void deleteRow(Int index);
268
275 void setColumnName(Int index, const std::string& name);
276
283 std::string getColumnName(Int index);
284
291 std::string getRowName(Int index);
292
299 Int getRowIndex(const std::string& name);
300
307 Int getColumnIndex(const std::string& name);
308
316
324
331 double getRowUpperBound(Int index);
332
339 double getRowLowerBound(Int index);
340
347 void setRowName(Int index, const std::string& name);
348
357 void setColumnBounds(Int index, double lower_bound, double upper_bound, Type type);
358
367 void setRowBounds(Int index, double lower_bound, double upper_bound, Type type);
368
377 void setColumnType(Int index, VariableType type);
378
386
393 void setObjective(Int index, double obj_value);
394
401 double getObjective(Int index);
402
415
422
429
437 void setElement(Int row_index, Int column_index, double value);
438
446 double getElement(Int row_index, Int column_index);
447
448 // problem reading/writing
455 void readProblem(const std::string& filename, const std::string& format);
456
463 void writeProblem(const std::string& filename, const WriteFormat format) const;
464
481 Int solve(SolverParam& solver_param, const Size verbose_level = 0);
482
493
494 // solution access
501
508 double getColumnValue(Int index);
509
517
524 void getMatrixRow(Int idx, std::vector<Int>& indexes);
525
532
533protected:
534#ifdef OPENMS_HAS_COINOR
535 CoinModel * model_ = nullptr;
536 std::vector<double> solution_;
537 SolverStatus solver_status_ = UNDEFINED;
538#elif defined(OPENMS_HAS_HIGHS)
539 std::unique_ptr<Highs> highs_;
540 std::vector<double> solution_;
543 mutable std::vector<uint8_t> staged_integrality_;
545 void flushIntegrality_() const;
546#else
547 glp_prob * lp_problem_ = nullptr;
548#endif
549
551
552
553 }; // class
554
555} // namespace
556
A wrapper class for linear programming (LP) solvers.
Definition LPWrapper.h:72
Int addColumn()
Adds an empty column to the LP matrix.
Int addRow(const std::vector< Int > &row_indices, const std::vector< double > &row_values, const std::string &name, double lower_bound, double upper_bound, Type type)
Adds a row with boundaries to the LP matrix, returns index.
Int addColumn(const std::vector< Int > &column_indices, const std::vector< double > &column_values, const std::string &name, double lower_bound, double upper_bound, Type type)
Adds a column with boundaries to the LP matrix, returns index.
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.
std::string getColumnName(Int index)
Get the name of a column.
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:120
@ UPPER_BOUND_ONLY
Only upper bound is specified.
Definition LPWrapper.h:123
@ LOWER_BOUND_ONLY
Only lower bound is specified.
Definition LPWrapper.h:122
@ DOUBLE_BOUNDED
Both lower and upper bounds are specified.
Definition LPWrapper.h:124
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 writeProblem(const std::string &filename, const WriteFormat format) const
Write LP formulation to a file.
void setObjectiveSense(Sense sense)
Set objective direction.
double getObjectiveValue()
Get the objective function value of the solution.
Int getColumnIndex(const std::string &name)
Find the index of a column by its name.
Sense getObjectiveSense()
Get the current objective direction.
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 deleteRow(Int index)
Delete the row at the specified index.
std::string getRowName(Int index)
Get the name of a row.
void setRowName(Int index, const std::string &name)
Set the name of a row.
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:185
Int addRow(const std::vector< Int > &row_indices, const std::vector< double > &row_values, const std::string &name)
Adds a row to the LP matrix.
void setRowBounds(Int index, double lower_bound, double upper_bound, Type type)
Set row bounds.
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.
void setColumnName(Int index, const std::string &name)
Set the name of a column.
SolverStatus getStatus()
Get solution status.
SOLVER
Enumeration for available LP solvers.
Definition LPWrapper.h:169
SOLVER solver_
Currently active solver backend.
Definition LPWrapper.h:550
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.
Int getRowIndex(const std::string &name)
Find the index of a row by its name.
Sense
Enumeration for optimization direction.
Definition LPWrapper.h:146
VariableType
Enumeration for variable types in the LP problem.
Definition LPWrapper.h:134
@ INTEGER
Integer variable (can only take integer values within bounds)
Definition LPWrapper.h:136
double getColumnUpperBound(Int index)
Get the upper bound of a column.
void readProblem(const std::string &filename, const std::string &format)
Read LP from file.
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 std::string &name)
Adds a column to the LP matrix.
double getColumnValue(Int index)
Get the value of a variable in the solution.
virtual ~LPWrapper()
Virtual destructor.
WriteFormat
Enumeration for LP problem file formats.
Definition LPWrapper.h:157
@ FORMAT_MPS
MPS format (industry standard)
Definition LPWrapper.h:159
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
STL namespace.
Struct that holds the parameters of the LP solver.
Definition LPWrapper.h:84
Int backtrack_tech
Backtracking technique for MIP problems.
Definition LPWrapper.h:99
double mip_gap
Relative gap tolerance for MIP problems.
Definition LPWrapper.h:106
Int branching_tech
Branching technique for MIP problems.
Definition LPWrapper.h:98
bool enable_binarization
Enable binarization (only with presolve)
Definition LPWrapper.h:111
bool enable_cov_cuts
Enable cover cuts.
Definition LPWrapper.h:104
Int time_limit
Time limit in milliseconds.
Definition LPWrapper.h:107
bool enable_gmi_cuts
Enable Gomory mixed-integer cuts.
Definition LPWrapper.h:102
Int message_level
Controls verbosity of solver output (0-3)
Definition LPWrapper.h:97
Int output_delay
Output delay in milliseconds.
Definition LPWrapper.h:109
bool enable_presolve
Enable presolve techniques.
Definition LPWrapper.h:110
bool enable_mir_cuts
Enable mixed-integer rounding cuts.
Definition LPWrapper.h:103
SolverParam()
Default constructor that initializes all parameters with reasonable defaults.
Definition LPWrapper.h:88
Int output_freq
Output frequency in milliseconds.
Definition LPWrapper.h:108
bool enable_feas_pump_heuristic
Enable feasibility pump heuristic.
Definition LPWrapper.h:101
Int preprocessing_tech
Preprocessing technique.
Definition LPWrapper.h:100
bool enable_clq_cuts
Enable clique cuts.
Definition LPWrapper.h:105