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 the HiGHS headers here (they are only available when building OpenMS), forward declare instead
22class Highs;
23
24namespace OpenMS
25{
26
48 class OPENMS_DLLAPI LPWrapper
49 {
50public:
57 {
58 double mip_gap = 0.0;
59 Int time_limit = (std::numeric_limits<Int>::max)();
60 bool enable_presolve = true;
61 };
62
76
83 {
84 CONTINUOUS = 1,
86 BINARY
87 };
88
94 enum Sense
95 {
96 MIN = 1,
97 MAX
98 };
99
106 {
107 FORMAT_LP = 0,
108 FORMAT_MPS
109 };
110
117 {
118 SOLVER_HIGHS
119 };
120
127 {
128 UNDEFINED = 1,
129 OPTIMAL = 5,
130 FEASIBLE = 2,
131 NO_FEASIBLE_SOL = 4
132 };
133
140
146 virtual ~LPWrapper();
147
148 // problem creation/manipulation
157 Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values, const std::string& name);
158
165
174 Int addColumn(const std::vector<Int>& column_indices, const std::vector<double>& column_values, const std::string& name);
175
186 Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values,
187 const std::string& name, double lower_bound, double upper_bound, Type type);
188
199 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);
200
206 void deleteRow(Int index);
207
214 void setColumnName(Int index, const std::string& name);
215
222 std::string getColumnName(Int index);
223
230 std::string getRowName(Int index);
231
238 Int getRowIndex(const std::string& name);
239
246 Int getColumnIndex(const std::string& name);
247
255
263
270 double getRowUpperBound(Int index);
271
278 double getRowLowerBound(Int index);
279
286 void setRowName(Int index, const std::string& name);
287
296 void setColumnBounds(Int index, double lower_bound, double upper_bound, Type type);
297
306 void setRowBounds(Int index, double lower_bound, double upper_bound, Type type);
307
316 void setColumnType(Int index, VariableType type);
317
325
332 void setObjective(Int index, double obj_value);
333
340 double getObjective(Int index);
341
354
361
368
376 void setElement(Int row_index, Int column_index, double value);
377
385 double getElement(Int row_index, Int column_index);
386
387 // problem reading/writing
394 void readProblem(const std::string& filename, const std::string& format);
395
402 void writeProblem(const std::string& filename, const WriteFormat format) const;
403
417 Int solve(SolverParam& solver_param, const Size verbose_level = 0);
418
429
430 // solution access
437
444 double getColumnValue(Int index);
445
453
460 void getMatrixRow(Int idx, std::vector<Int>& indexes);
461
468
469protected:
470 std::unique_ptr<Highs> highs_;
471 std::vector<double> solution_;
474 mutable std::vector<uint8_t> staged_integrality_;
476 void flushIntegrality_() const;
477 }; // class
478
479} // namespace
480
A wrapper class for the linear programming (LP) solver.
Definition LPWrapper.h:49
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:69
@ UPPER_BOUND_ONLY
Only upper bound is specified.
Definition LPWrapper.h:72
@ LOWER_BOUND_ONLY
Only lower bound is specified.
Definition LPWrapper.h:71
@ DOUBLE_BOUNDED
Both lower and upper bounds are specified.
Definition LPWrapper.h:73
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:127
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.
void flushIntegrality_() const
Apply staged_integrality_ to the HiGHS model (const: also needed by writeProblem())
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.
std::unique_ptr< Highs > highs_
HiGHS solver instance.
Definition LPWrapper.h:470
std::vector< double > solution_
Definition LPWrapper.h:471
SOLVER
Enumeration for available LP solvers.
Definition LPWrapper.h:117
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:95
VariableType
Enumeration for variable types in the LP problem.
Definition LPWrapper.h:83
@ INTEGER
Integer variable (can only take integer values within bounds)
Definition LPWrapper.h:85
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:106
std::vector< uint8_t > staged_integrality_
Definition LPWrapper.h:474
Struct that holds the parameters of the LP solver.
Definition LPWrapper.h:57
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