Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
GlpkLpSolver.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
6
7#include "storm-config.h"
8
9#ifdef STORM_HAVE_GLPK
10#include <glpk.h>
11#endif
12
13namespace storm {
14
16
17namespace solver {
21template<typename ValueType, bool RawMode = false>
22class GlpkLpSolver : public LpSolver<ValueType, RawMode> {
23 public:
28
36 GlpkLpSolver(storm::GlpkSolverEnvironment const& glpkSettings, bool debug, std::string const& name, OptimizationDirection const& optDir);
37
44 GlpkLpSolver(storm::GlpkSolverEnvironment const& glpkSettings, bool debug, std::string const& name);
45
52 GlpkLpSolver(storm::GlpkSolverEnvironment const& glpkSettings, bool debug, OptimizationDirection const& optDir);
53
58 GlpkLpSolver(storm::GlpkSolverEnvironment const& glpkSettings, bool debug);
59
63 virtual ~GlpkLpSolver();
64
65 virtual Variable addVariable(std::string const& name, VariableType const& type, std::optional<ValueType> const& lowerBound = std::nullopt,
66 std::optional<ValueType> const& upperBound = std::nullopt, ValueType objectiveFunctionCoefficient = 0) override;
67
68 // Methods to incorporate recent changes.
69 virtual void update() const override;
70
71 // Methods to add constraints
72 virtual void addConstraint(std::string const& name, Constraint const& constraint) override;
73 virtual void addIndicatorConstraint(std::string const& name, Variable indicatorVariable, bool indicatorValue, Constraint const& constraint) override;
74
75 // Methods to optimize and retrieve optimality status.
76 virtual void optimize() const override;
77 virtual bool isInfeasible() const override;
78 virtual bool isUnbounded() const override;
79 virtual bool isOptimal() const override;
80
81 // Methods to retrieve values of variables and the objective function in the optimal solutions.
82 virtual ValueType getContinuousValue(Variable const& name) const override;
83 virtual int_fast64_t getIntegerValue(Variable const& name) const override;
84 virtual bool getBinaryValue(Variable const& name) const override;
85 virtual ValueType getObjectiveValue() const override;
86
87 // Methods to print the LP problem to a file.
88 virtual void writeModelToFile(std::string const& filename) const override;
89
90 virtual void push() override;
91 virtual void pop() override;
92
93 virtual void setMaximalMILPGap(ValueType const& gap, bool relative) override;
94 virtual ValueType getMILPGap(bool relative) const override;
95
96 private:
97#ifdef STORM_HAVE_GLPK
98 // The glpk LP problem.
99 glp_prob* lp;
100#endif
101
102 // A mapping from variables to their indices.
103 std::conditional_t<RawMode, std::vector<int>, std::map<storm::expressions::Variable, int>> variableToIndexMap;
104
105 // A flag storing whether the model is an LP or an MILP.
106 bool modelContainsIntegerVariables;
107
108 // The tolerance used when checking whether integer/binary variables have (near-)integral values.
109 double integerTolerance;
110
111 // Whether glpk's built-in MILP presolver should be used.
112 bool milpPresolverEnabled;
113
114 // Flags that store whether the MILP was found to be infeasible or unbounded.
115 mutable bool isInfeasibleFlag;
116 mutable bool isUnboundedFlag;
117
118 mutable double maxMILPGap;
119 mutable bool maxMILPGapRelative;
120 mutable double actualRelativeMILPGap;
121
122 struct IncrementalLevel {
123 std::vector<Variable> variables;
124 int firstConstraintIndex;
125 };
126 std::vector<IncrementalLevel> incrementalData;
127};
128
129} // namespace solver
130} // namespace storm
virtual bool getBinaryValue(Variable const &name) const override
Retrieves the value of the binary variable with the given name.
virtual void setMaximalMILPGap(ValueType const &gap, bool relative) override
Specifies the maximum difference between lower- and upper objective bounds that triggers termination.
virtual void push() override
Pushes a backtracking point on the solver's stack.
virtual ValueType getContinuousValue(Variable const &name) const override
Retrieves the value of the continuous variable with the given name.
virtual int_fast64_t getIntegerValue(Variable const &name) const override
Retrieves the value of the integer variable with the given name.
virtual Variable addVariable(std::string const &name, VariableType const &type, std::optional< ValueType > const &lowerBound=std::nullopt, std::optional< ValueType > const &upperBound=std::nullopt, ValueType objectiveFunctionCoefficient=0) override
virtual bool isOptimal() const override
Retrieves whether the model was found to be optimal, i.e.
virtual void writeModelToFile(std::string const &filename) const override
Writes the current LP problem to the given file.
virtual bool isInfeasible() const override
Retrieves whether the model was found to be infeasible.
virtual void pop() override
Pops a backtracking point from the solver's stack.
typename LpSolver< ValueType, RawMode >::Constraint Constraint
virtual ValueType getObjectiveValue() const override
Retrieves the value of the objective function.
virtual void addIndicatorConstraint(std::string const &name, Variable indicatorVariable, bool indicatorValue, Constraint const &constraint) override
Adds the given indicator constraint to the LP problem: "If indicatorVariable == indicatorValue,...
virtual void addConstraint(std::string const &name, Constraint const &constraint) override
Adds a the given constraint to the LP problem.
virtual void update() const override
Updates the model to make the variables that have been declared since the last call to update usable.
virtual ValueType getMILPGap(bool relative) const override
Returns the obtained gap after a call to optimize().
virtual ~GlpkLpSolver()
Destructs a solver by freeing the pointers to glpk's structures.
typename LpSolver< ValueType, RawMode >::VariableType VariableType
typename LpSolver< ValueType, RawMode >::Constant Constant
virtual bool isUnbounded() const override
Retrieves whether the model was found to be infeasible.
virtual void optimize() const override
Optimizes the LP problem previously constructed.
GlpkLpSolver(storm::GlpkSolverEnvironment const &glpkSettings, bool debug, std::string const &name, OptimizationDirection const &optDir)
Constructs a solver with the given name and model sense.
typename LpSolver< ValueType, RawMode >::Variable Variable
std::conditional_t< RawMode, RawLpConstraint< ValueType >, storm::expressions::Expression > Constraint
Definition LpSolver.h:54
std::conditional_t< RawMode, ValueType, storm::expressions::Expression > Constant
Definition LpSolver.h:53
std::conditional_t< RawMode, typename RawLpConstraint< ValueType >::VariableIndexType, storm::expressions::Variable > Variable
Definition LpSolver.h:52
VariableType
Enumerates the different types of variables.
Definition LpSolver.h:59