OpenMS
LPWrapper.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Alexandra Zerck $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/DATASTRUCTURES/String.h> // for String
38 
39 #include <OpenMS/CONCEPT/Types.h>
40 #include <OpenMS/OpenMSConfig.h>
41 #include <OpenMS/config.h>
42 
43 #include <limits>
44 
45 // do NOT include glpk and CoinOr headers here, as they define bad stuff, which ripples through OpenMS then...
46 // include them in LPWrapper.cpp where they do not harm
47 // only declare them here
48 class CoinModel;
49 
50 // if GLPK was found:
51 #ifndef COINOR_SOLVER
52  #ifndef GLP_PROB_DEFINED
53  #define GLP_PROB_DEFINED
54  // depending on the glpk version
55  // define glp_prob as forward or struct
56  #if OPENMS_GLPK_VERSION_MAJOR == 4 && OPENMS_GLPK_VERSION_MINOR < 48
57  typedef struct
58  {
59  double _opaque_prob[100];
60  } glp_prob;
61  #else
62  class glp_prob;
63  #endif
64  #endif
65 #endif
66 
67 namespace OpenMS
68 {
69 
70  class OPENMS_DLLAPI LPWrapper
71  {
72 public:
76  struct SolverParam
77  {
79  message_level(3), branching_tech(4), backtrack_tech(3),
80  preprocessing_tech(2), enable_feas_pump_heuristic(true), enable_gmi_cuts(true),
81  enable_mir_cuts(true), enable_cov_cuts(true), enable_clq_cuts(true), mip_gap(0.0),
82  time_limit((std::numeric_limits<Int>::max)()), output_freq(5000), output_delay(10000), enable_presolve(true),
83  enable_binarization(true)
84  {
85  }
86 
96  double mip_gap;
102  };
103 
104  enum Type
105  {
106  UNBOUNDED = 1,
110  FIXED
111  };
112 
114  {
115  CONTINUOUS = 1,
117  BINARY
118  };
119 
120  enum Sense
121  {
122  MIN = 1,
123  MAX
124  };
125 
127  {
128  FORMAT_LP = 0,
130  FORMAT_GLPK
131  };
132 
133  enum SOLVER
134  {
135  SOLVER_GLPK = 0
136 #if COINOR_SOLVER == 1
137  , SOLVER_COINOR
138 #endif
139  };
140 
142  {
143  UNDEFINED = 1,
144  OPTIMAL = 5,
145  FEASIBLE = 2,
146  NO_FEASIBLE_SOL = 4
147  };
148 
150  virtual ~LPWrapper();
151 
152  // problem creation/manipulation
154  Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values, const String& name);
158  Int addColumn(const std::vector<Int>& column_indices, const std::vector<double>& column_values, const String& name);
159 
172  Int addRow(const std::vector<Int>& row_indices, const std::vector<double>& row_values,
173  const String& name, double lower_bound, double upper_bound, Type type);
174 
185  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);
186 
188  void deleteRow(Int index);
190  void setColumnName(Int index, const String& name);
196  Int getRowIndex(const String& name);
198  Int getColumnIndex(const String& name);
200  double getColumnUpperBound(Int index);
202  double getColumnLowerBound(Int index);
204  double getRowUpperBound(Int index);
206  double getRowLowerBound(Int index);
208  void setRowName(Int index, const String& name);
209 
218  void setColumnBounds(Int index, double lower_bound, double upper_bound, Type type);
219 
228  void setRowBounds(Int index, double lower_bound, double upper_bound, Type type);
229 
236  void setColumnType(Int index, VariableType type);
237 
245 
247  void setObjective(Int index, double obj_value);
249  double getObjective(Int index);
250 
258 
263 
264  void setElement(Int row_index, Int column_index, double value);
265  double getElement(Int row_index, Int column_index);
266 
267  // problem reading/writing
274  void readProblem(const String& filename, const String& format);
275 
282  void writeProblem(const String& filename, const WriteFormat format) const;
283 
294  Int solve(SolverParam& solver_param, const Size verbose_level = 0);
295 
302 
303  // solution access
305  double getColumnValue(Int index);
306 
308  void getMatrixRow(Int idx, std::vector<Int>& indexes);
309 
311  SOLVER getSolver() const;
312 
313 protected:
314 #if COINOR_SOLVER == 1
315  CoinModel * model_ = nullptr;
316  std::vector<double> solution_;
317 #else
318  glp_prob * lp_problem_ = nullptr;
319 #endif
320 
322 
323 
324  }; // class
325 
326 } // namespace
327 
Definition: LPWrapper.h:71
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:105
@ UPPER_BOUND_ONLY
Definition: LPWrapper.h:108
@ LOWER_BOUND_ONLY
Definition: LPWrapper.h:107
@ DOUBLE_BOUNDED
Definition: LPWrapper.h:109
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:142
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:134
SOLVER solver_
Definition: LPWrapper.h:321
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:121
VariableType
Definition: LPWrapper.h:114
@ INTEGER
Definition: LPWrapper.h:116
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:127
@ FORMAT_MPS
Definition: LPWrapper.h:129
A more convenient string class.
Definition: String.h:60
int Int
Signed integer type.
Definition: Types.h:102
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Struct that holds the parameters of the LP solver.
Definition: LPWrapper.h:77
Int backtrack_tech
Definition: LPWrapper.h:89
double mip_gap
Definition: LPWrapper.h:96
Int branching_tech
Definition: LPWrapper.h:88
bool enable_binarization
only with presolve
Definition: LPWrapper.h:101
bool enable_cov_cuts
Definition: LPWrapper.h:94
Int time_limit
Definition: LPWrapper.h:97
bool enable_gmi_cuts
Definition: LPWrapper.h:92
Int message_level
Definition: LPWrapper.h:87
Int output_delay
Definition: LPWrapper.h:99
bool enable_presolve
Definition: LPWrapper.h:100
bool enable_mir_cuts
Definition: LPWrapper.h:93
SolverParam()
Definition: LPWrapper.h:78
Int output_freq
Definition: LPWrapper.h:98
bool enable_feas_pump_heuristic
Definition: LPWrapper.h:91
Int preprocessing_tech
Definition: LPWrapper.h:90
bool enable_clq_cuts
Definition: LPWrapper.h:95