Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
GlpkLpSolver.h
Go to the documentation of this file.
1#ifndef STORM_SOLVER_GLPKLPSOLVER_H_
2#define STORM_SOLVER_GLPKLPSOLVER_H_
3
4#include <map>
7
8// To detect whether the usage of glpk is possible, this include is neccessary.
9#include "storm-config.h"
10
11#ifdef STORM_HAVE_GLPK
12#include <glpk.h>
13#endif
14
15namespace storm {
16namespace solver {
17#ifdef STORM_HAVE_GLPK
21template<typename ValueType, bool RawMode = false>
22class GlpkLpSolver : public LpSolver<ValueType, RawMode> {
23 public:
28
36 GlpkLpSolver(std::string const& name, OptimizationDirection const& optDir);
37
44 GlpkLpSolver(std::string const& name);
45
53
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 // The glpk LP problem.
98 glp_prob* lp;
99
100 // A mapping from variables to their indices.
101 std::conditional_t<RawMode, std::vector<int>, std::map<storm::expressions::Variable, int>> variableToIndexMap;
102
103 // A flag storing whether the model is an LP or an MILP.
104 bool modelContainsIntegerVariables;
105
106 // Flags that store whether the MILP was found to be infeasible or unbounded.
107 mutable bool isInfeasibleFlag;
108 mutable bool isUnboundedFlag;
109
110 mutable double maxMILPGap;
111 mutable bool maxMILPGapRelative;
112 mutable double actualRelativeMILPGap;
113
114 struct IncrementalLevel {
115 std::vector<Variable> variables;
116 int firstConstraintIndex;
117 };
118 std::vector<IncrementalLevel> incrementalData;
119};
120#else
121// If glpk is not available, we provide a stub implementation that emits an error if any of its methods is called.
122class GlpkLpSolver : public LpSolver {
123 public:
124 GlpkLpSolver(std::string const& name, OptimizationDirection const& modelSense) {
125 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
126 "requires this support. Please choose a version of support with glpk support.";
127 }
128
129 GlpkLpSolver(std::string const& name) : LpSolver(MINIMIZE) {
130 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
131 "requires this support. Please choose a version of support with glpk support.";
132 }
133
134 GlpkLpSolver(OptimizationDirection const& modelSense) : LpSolver(modelSense) {
135 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
136 "requires this support. Please choose a version of support with glpk support.";
137 }
138
139 GlpkLpSolver() : LpSolver(MINIMIZE) {
140 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
141 "requires this support. Please choose a version of support with glpk support.";
142 }
143
144 virtual ~GlpkLpSolver() {
145 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
146 "requires this support. Please choose a version of support with glpk support.";
147 }
148
149 virtual void update() const override {
150 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
151 "requires this support. Please choose a version of support with glpk support.";
152 }
153
154 virtual void addConstraint(std::string const& name, storm::expressions::Expression const& constraint) override {
155 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
156 "requires this support. Please choose a version of support with glpk support.";
157 }
158
159 virtual void optimize() const override {
160 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
161 "requires this support. Please choose a version of support with glpk support.";
162 }
163
164 virtual bool isInfeasible() const override {
165 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
166 "requires this support. Please choose a version of support with glpk support.";
167 }
168
169 virtual bool isUnbounded() const override {
170 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
171 "requires this support. Please choose a version of support with glpk support.";
172 }
173
174 virtual bool isOptimal() const override {
175 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
176 "requires this support. Please choose a version of support with glpk support.";
177 }
178
179 virtual ValueType getContinuousValue(storm::expressions::Variable const& variable) const override {
180 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
181 "requires this support. Please choose a version of support with glpk support.";
182 }
183
184 virtual int_fast64_t getIntegerValue(storm::expressions::Variable const& variable) const override {
185 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
186 "requires this support. Please choose a version of support with glpk support.";
187 }
188
189 virtual bool getBinaryValue(storm::expressions::Variable const& variable) const override {
190 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
191 "requires this support. Please choose a version of support with glpk support.";
192 }
193
194 virtual ValueType getObjectiveValue() const override {
195 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
196 "requires this support. Please choose a version of support with glpk support.";
197 }
198
199 virtual void writeModelToFile(std::string const& filename) const override {
200 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
201 "requires this support. Please choose a version of support with glpk support.";
202 }
203
204 virtual void push() override {
205 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
206 "requires this support. Please choose a version of support with glpk support.";
207 }
208
209 virtual void pop() override {
210 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
211 "requires this support. Please choose a version of support with glpk support.";
212 }
213
214 virtual ValueType getMILPGap(bool relative) const override {
215 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
216 "requires this support. Please choose a version of support with glpk support.";
217 }
218
219 virtual ValueType getMILPGap(bool relative) const override {
220 throw storm::exceptions::NotImplementedException() << "This version of storm was compiled without support for glpk. Yet, a method was called that "
221 "requires this support. Please choose a version of support with glpk support.";
222 }
223};
224#endif
225} // namespace solver
226} // namespace storm
227
228#endif /* STORM_SOLVER_GLPKLPSOLVER_H_ */
GlpkLpSolver(OptimizationDirection const &modelSense)
virtual void push() override
Pushes a backtracking point on the solver's stack.
virtual void pop() override
Pops a backtracking point from the solver's stack.
virtual ValueType getContinuousValue(storm::expressions::Variable const &variable) const override
virtual bool getBinaryValue(storm::expressions::Variable const &variable) const override
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 ValueType getMILPGap(bool relative) const override
Returns the obtained gap after a call to optimize()
virtual void addConstraint(std::string const &name, storm::expressions::Expression const &constraint) override
virtual bool isUnbounded() const override
Retrieves whether the model was found to be infeasible.
virtual bool isOptimal() const override
Retrieves whether the model was found to be optimal, i.e.
GlpkLpSolver(std::string const &name)
virtual void optimize() const override
Optimizes the LP problem previously constructed.
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 getObjectiveValue() const override
Retrieves the value of the objective function.
GlpkLpSolver(std::string const &name, OptimizationDirection const &modelSense)
virtual int_fast64_t getIntegerValue(storm::expressions::Variable const &variable) const override
An interface that captures the functionality of an LP solver.
Definition LpSolver.h:51
virtual void setMaximalMILPGap(ValueType const &gap, bool relative)=0
Specifies the maximum difference between lower- and upper objective bounds that triggers termination.
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)=0
Registers a variable of the given type.
std::conditional_t< RawMode, RawLpConstraint< ValueType >, storm::expressions::Expression > Constraint
Definition LpSolver.h:55
std::conditional_t< RawMode, ValueType, storm::expressions::Expression > Constant
Definition LpSolver.h:54
std::conditional_t< RawMode, typename RawLpConstraint< ValueType >::VariableIndexType, storm::expressions::Variable > Variable
Definition LpSolver.h:53
virtual void addIndicatorConstraint(std::string const &name, Variable indicatorVariable, bool indicatorValue, Constraint const &constraint)=0
Adds the given indicator constraint to the LP problem: "If indicatorVariable == indicatorValue,...
VariableType
Enumerates the different types of variables.
Definition LpSolver.h:60
SFTBDDChecker::ValueType ValueType
LabParser.cpp.
Definition cli.cpp:18