Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SoplexLpSolver.cpp
Go to the documentation of this file.
2
3#include <numeric>
4
18
19namespace storm {
20namespace solver {
21
22#ifdef STORM_HAVE_SOPLEX
23
24using namespace soplex;
25
26soplex::Rational to_soplex_rational(storm::RationalNumber const& in) {
27 return soplex::Rational(storm::utility::convertNumber<GmpRationalNumber>(in).get_mpq_t());
28}
29
30storm::RationalNumber from_soplex_rational(soplex::Rational const& r) {
31 return storm::utility::convertNumber<storm::RationalNumber>(GmpRationalNumber(r.backend().data()));
32}
33
34template<typename ValueType, bool RawMode>
35SoplexLpSolver<ValueType, RawMode>::SoplexLpSolver(std::string const& name, OptimizationDirection const& optDir) : LpSolver<ValueType, RawMode>(optDir) {
36 if (std::is_same_v<ValueType, storm::RationalNumber>) {
37 solver.setIntParam(SoPlex::READMODE, SoPlex::READMODE_RATIONAL);
38 solver.setIntParam(SoPlex::SOLVEMODE, SoPlex::SOLVEMODE_RATIONAL);
39 solver.setIntParam(SoPlex::CHECKMODE, SoPlex::CHECKMODE_RATIONAL);
40 solver.setIntParam(SoPlex::SYNCMODE, SoPlex::SYNCMODE_AUTO);
41 solver.setRealParam(SoPlex::FEASTOL, 0.0);
42 solver.setRealParam(SoPlex::OPTTOL, 0.0);
43 }
44 solver.setIntParam(SoPlex::VERBOSITY, 0);
45}
46
47template<typename ValueType, bool RawMode>
48SoplexLpSolver<ValueType, RawMode>::SoplexLpSolver(std::string const& name) : SoplexLpSolver(name, OptimizationDirection::Minimize) {
49 // Intentionally left empty.
50}
51
52template<typename ValueType, bool RawMode>
53SoplexLpSolver<ValueType, RawMode>::SoplexLpSolver(OptimizationDirection const& optDir) : SoplexLpSolver("", optDir) {
54 // Intentionally left empty.
55}
56
57template<typename ValueType, bool RawMode>
58SoplexLpSolver<ValueType, RawMode>::~SoplexLpSolver() {}
59
60template<typename ValueType, bool RawMode>
61void SoplexLpSolver<ValueType, RawMode>::update() const {
62 // Nothing to be done.
63}
64
65template<typename ValueType, bool RawMode>
66typename SoplexLpSolver<ValueType, RawMode>::Variable SoplexLpSolver<ValueType, RawMode>::addVariable(std::string const& name, VariableType const& type,
67 std::optional<ValueType> const& lowerBound,
68 std::optional<ValueType> const& upperBound,
69 ValueType objectiveFunctionCoefficient) {
70 STORM_LOG_THROW(type == VariableType::Continuous, storm::exceptions::NotSupportedException, "Soplex LP Solver only supports variables of continuous type.");
71 Variable resultVar;
72 if constexpr (RawMode) {
73 resultVar = nextVariableIndex;
74 } else {
75 resultVar = this->declareOrGetExpressionVariable(name, type);
76 // Assert whether the variable does not exist yet.
77 // Due to incremental usage (push(), pop()), a variable might be declared in the manager but not in the lp model.
78 STORM_LOG_ASSERT(variableToIndexMap.count(resultVar) == 0, "Variable " << resultVar.getName() << " exists already in the model.");
79 this->variableToIndexMap.emplace(resultVar, nextVariableIndex);
80 }
81 ++nextVariableIndex;
82
83 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
84 soplex::Rational const rat_inf(soplex::infinity);
85 solver.addColRational(soplex::LPColRational(to_soplex_rational(objectiveFunctionCoefficient), variables,
86 upperBound.has_value() ? to_soplex_rational(*upperBound) : rat_inf,
87 lowerBound.has_value() ? to_soplex_rational(*lowerBound) : -rat_inf));
88 } else {
89 solver.addColReal(soplex::LPColReal(objectiveFunctionCoefficient, variables, upperBound.has_value() ? *upperBound : soplex::infinity,
90 lowerBound.has_value() ? *lowerBound : -soplex::infinity));
91 }
92 return resultVar;
93}
94
95template<typename ValueType, bool RawMode>
96void SoplexLpSolver<ValueType, RawMode>::addConstraint(std::string const& name, Constraint const& constraint) {
97 if constexpr (!RawMode) {
98 STORM_LOG_TRACE("Adding constraint " << (name == "" ? std::to_string(nextConstraintIndex) : name) << " to SoplexLpSolver:\n"
99 << "\t" << constraint);
100 }
101 using SoplexValueType = std::conditional_t<std::is_same_v<ValueType, storm::RationalNumber>, soplex::Rational, soplex::Real>;
102 // Extract constraint data
103 SoplexValueType rhs;
105 TypedDSVector row(0);
106 if constexpr (RawMode) {
107 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
108 rhs = to_soplex_rational(constraint.rhs);
109 } else {
110 rhs = constraint.rhs;
111 }
112 relationType = constraint.relationType;
113 row = TypedDSVector(constraint.lhsVariableIndices.size());
114 auto varIt = constraint.lhsVariableIndices.cbegin();
115 auto varItEnd = constraint.lhsVariableIndices.cend();
116 auto coefIt = constraint.lhsCoefficients.cbegin();
117 for (; varIt != varItEnd; ++varIt, ++coefIt) {
118 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
119 row.add(*varIt, to_soplex_rational(*coefIt));
120 } else {
121 row.add(*varIt, *coefIt);
122 }
123 }
124 } else {
125 STORM_LOG_THROW(constraint.getManager() == this->getManager(), storm::exceptions::InvalidArgumentException,
126 "Constraint was not built over the proper variables.");
127 STORM_LOG_THROW(constraint.isRelationalExpression(), storm::exceptions::InvalidArgumentException, "Illegal constraint is not a relational expression.");
128
129 // FIXME: We're potentially losing precision as the coefficients and the rhs are converted to double. The LinearCoefficientVisitor needs a ValueType!
134 leftCoefficients.separateVariablesFromConstantPart(rightCoefficients);
135 rhs = rightCoefficients.getConstantPart();
136 relationType = constraint.getBaseExpression().asBinaryRelationExpression().getRelationType();
137 row = TypedDSVector(std::distance(leftCoefficients.begin(), leftCoefficients.end()));
138 for (auto const& variableCoefficientPair : leftCoefficients) {
139 auto variableIndexPair = this->variableToIndexMap.find(variableCoefficientPair.first);
140 row.add(variableIndexPair->second, leftCoefficients.getCoefficient(variableIndexPair->first));
141 }
142 }
143
144 // Determine the type of the constraint and add it properly.
145 SoplexValueType l, r;
146 switch (relationType) {
148 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException,
149 "Strict inequalities are not supported by SoPlex. Use an LP solver that supports them (e.g. via --lpsolver highs, --lpsolver glpk "
150 "or --lpsolver gurobi), or use nonstrict inequalities.");
151 break;
153 l = -soplex::infinity;
154 r = rhs;
155 break;
157 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException,
158 "Strict inequalities are not supported by SoPlex. Use an LP solver that supports them (e.g. via --lpsolver highs, --lpsolver glpk "
159 "or --lpsolver gurobi), or use nonstrict inequalities.");
160 break;
162 l = rhs;
163 r = soplex::infinity;
164 break;
166 l = rhs;
167 r = rhs;
168 break;
169 default:
170 STORM_LOG_ASSERT(false, "Illegal operator in LP solver constraint.");
171 }
172 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
173 solver.addRowRational(LPRowRational(l, row, r));
174 } else {
175 solver.addRowReal(LPRow(l, row, r));
176 }
177}
178
179template<typename ValueType, bool RawMode>
180void SoplexLpSolver<ValueType, RawMode>::addIndicatorConstraint(std::string const&, Variable, bool, Constraint const&) {
181 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Indicator Constraints not supported for SoPlex.");
182}
183
184template<typename ValueType, bool RawMode>
185void SoplexLpSolver<ValueType, RawMode>::optimize() const {
186 // First incorporate all recent changes.
187 this->update();
188 //
189 if (this->getOptimizationDirection() == storm::solver::OptimizationDirection::Minimize) {
190 solver.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MINIMIZE);
191 } else {
192 solver.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MAXIMIZE);
193 }
194
195 status = solver.optimize();
196 STORM_LOG_TRACE("soplex status " << status);
197 if (status == soplex::SPxSolver::ERROR) {
198 STORM_LOG_THROW(false, storm::exceptions::InternalException, "Soplex failed.");
199 } else if (status == soplex::SPxSolver::UNKNOWN) {
200 STORM_LOG_THROW(false, storm::exceptions::InternalException, "Soplex gives up on this problem.");
201 }
202 this->currentModelHasBeenOptimized = true;
203}
204
205template<typename ValueType, bool RawMode>
206bool SoplexLpSolver<ValueType, RawMode>::isInfeasible() const {
207 STORM_LOG_THROW(this->currentModelHasBeenOptimized, storm::exceptions::InvalidStateException,
208 "Illegal call to SoplexLpSolver<ValueType, RawMode>::isInfeasible: model has not been optimized.");
209
210 return (status == soplex::SPxSolver::INFEASIBLE);
211}
212
213template<typename ValueType, bool RawMode>
214bool SoplexLpSolver<ValueType, RawMode>::isUnbounded() const {
215 STORM_LOG_THROW(this->currentModelHasBeenOptimized, storm::exceptions::InvalidStateException,
216 "Illegal call to SoplexLpSolver<ValueType, RawMode>::isUnbounded: model has not been optimized.");
217
218 return (status == soplex::SPxSolver::UNBOUNDED);
219}
220
221template<typename ValueType, bool RawMode>
222bool SoplexLpSolver<ValueType, RawMode>::isOptimal() const {
223 if (!this->currentModelHasBeenOptimized) {
224 return false;
225 }
226 return (status == soplex::SPxSolver::OPTIMAL);
227}
228
229template<typename ValueType, bool RawMode>
230ValueType SoplexLpSolver<ValueType, RawMode>::getContinuousValue(Variable const& variable) const {
231 ensureSolved();
232
233 uint64_t varIndex;
234 if constexpr (RawMode) {
235 varIndex = variable;
236 } else {
237 STORM_LOG_THROW(variableToIndexMap.count(variable) != 0, storm::exceptions::InvalidAccessException,
238 "Accessing value of unknown variable '" << variable.getName() << "'.");
239 varIndex = variableToIndexMap.at(variable);
240 }
241 STORM_LOG_ASSERT(varIndex < nextVariableIndex, "Variable Index exceeds highest value.");
242
243 if (primalSolution.dim() == 0) {
244 primalSolution = TypedDVector(nextVariableIndex);
245 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
246 solver.getPrimalRational(primalSolution);
247 } else {
248 solver.getPrimal(primalSolution);
249 }
250 }
251 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
252 return storm::utility::convertNumber<ValueType>(from_soplex_rational(primalSolution[varIndex]));
253 } else {
254 return primalSolution[varIndex];
255 }
256}
257
258template<>
259double SoplexLpSolver<double>::getObjectiveValue() const {
260 ensureSolved();
261 return solver.objValueReal();
262}
263
264template<typename ValueType, bool RawMode>
265ValueType SoplexLpSolver<ValueType, RawMode>::getObjectiveValue() const {
266 ensureSolved();
267 return storm::utility::convertNumber<ValueType>(from_soplex_rational(solver.objValueRational()));
268}
269
270template<typename ValueType, bool RawMode>
271void SoplexLpSolver<ValueType, RawMode>::ensureSolved() const {
272 if (!this->isOptimal()) {
273 STORM_LOG_THROW(!this->isInfeasible(), storm::exceptions::InvalidAccessException, "Unable to get Soplex solution from infeasible model.");
274 STORM_LOG_THROW(!this->isUnbounded(), storm::exceptions::InvalidAccessException, "Unable to get Soplex solution from unbounded model.");
275 STORM_LOG_THROW(false, storm::exceptions::InvalidAccessException, "Unable to get Soplex solution from unoptimized model.");
276 }
277}
278
279template<typename ValueType, bool RawMode>
280void SoplexLpSolver<ValueType, RawMode>::writeModelToFile(std::string const& filename) const {
281 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
282 solver.writeFileRational(filename.c_str(), nullptr, nullptr, nullptr);
283 } else {
284 solver.writeFileReal(filename.c_str(), nullptr, nullptr, nullptr);
285 }
286}
287
288template<typename ValueType, bool RawMode>
289void SoplexLpSolver<ValueType, RawMode>::push() {
290 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Push/Pop not supported on SoPlex.");
291}
292
293template<typename ValueType, bool RawMode>
294void SoplexLpSolver<ValueType, RawMode>::pop() {
295 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Push/Pop not supported on SoPlex.");
296}
297
298#else
299template<typename ValueType, bool RawMode>
301 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
302 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
303 "version of support with Soplex support.");
304}
305
306template<typename ValueType, bool RawMode>
308 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
309 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
310 "version of support with Soplex support.");
311}
312
313template<typename ValueType, bool RawMode>
315 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
316 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
317 "version of support with Soplex support.");
318}
319
320template<typename ValueType, bool RawMode>
322
323template<typename ValueType, bool RawMode>
325 std::optional<ValueType> const&,
326 std::optional<ValueType> const&, ValueType) {
327 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
328 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
329 "version of support with Soplex support.");
330}
331
332template<typename ValueType, bool RawMode>
334 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
335 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
336 "version of support with Soplex support.");
337}
338
339template<typename ValueType, bool RawMode>
341 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
342 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
343 "version of support with Soplex support.");
344}
345
346template<typename ValueType, bool RawMode>
348 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
349 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
350 "version of support with Soplex support.");
351}
352
353template<typename ValueType, bool RawMode>
355 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
356 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
357 "version of support with Soplex support.");
358}
359
360template<typename ValueType, bool RawMode>
362 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
363 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
364 "version of support with Soplex support.");
365}
366
367template<typename ValueType, bool RawMode>
369 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
370 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
371 "version of support with Soplex support.");
372}
373
374template<typename ValueType, bool RawMode>
376 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
377 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
378 "version of support with Soplex support.");
379}
380
381template<typename ValueType, bool RawMode>
383 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
384 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
385 "version of support with Soplex support.");
386}
387
388template<typename ValueType, bool RawMode>
390 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
391 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
392 "version of support with Soplex support.");
393}
394
395template<typename ValueType, bool RawMode>
397 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
398 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
399 "version of support with Soplex support.");
400}
401
402template<typename ValueType, bool RawMode>
404 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
405 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
406 "version of support with Soplex support.");
407}
408
409template<typename ValueType, bool RawMode>
411 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
412 "This version of storm was compiled without support for Soplex. Yet, a method was called that requires this support. Please choose a "
413 "version of support with Soplex support.");
414}
415#endif
416
417template<typename ValueType, bool RawMode>
419 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "SoPlex does not support integer variables.");
420}
421
422template<typename ValueType, bool RawMode>
424 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "SoPlex does not support binary variables.");
425}
426
427template<typename ValueType, bool RawMode>
429 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "SoPlex does not support integer variables.");
430}
431
432template<typename ValueType, bool RawMode>
434 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "SoPlex does not support integer variables.");
435}
436
437template class SoplexLpSolver<double, false>;
439template class SoplexLpSolver<double, true>;
441} // namespace solver
442} // namespace storm
Computes the coefficients of a linear expression.
VariableCoefficients getLinearCoefficients(Expression const &expression)
Computes the coefficients of all identifiers appearing in the expression if the expression was rewrit...
An interface that captures the functionality of an LP solver.
Definition LpSolver.h:50
typename LpSolver< ValueType, RawMode >::Variable Variable
virtual void pop() override
Pops a backtracking point from the solver's stack.
virtual ValueType getMILPGap(bool relative) const override
Returns the obtained gap after a call to optimize().
virtual ValueType getObjectiveValue() const override
Retrieves the value of the objective function.
virtual int_fast64_t getIntegerValue(Variable const &name) const override
Retrieves the value of the integer variable with the given name.
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 bool isInfeasible() const override
Retrieves whether the model was found to be infeasible.
SoplexLpSolver(std::string const &name, OptimizationDirection const &optDir)
Constructs a solver with the given name and model sense.
typename LpSolver< ValueType, RawMode >::VariableType VariableType
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 isUnbounded() const override
Retrieves whether the model was found to be infeasible.
virtual void push() override
Pushes a backtracking point on the solver's stack.
virtual ~SoplexLpSolver()
Destructs a solver by freeing the pointers to Gurobi's structures.
virtual ValueType getContinuousValue(Variable const &name) const override
Retrieves the value of the continuous 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 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 void optimize() const override
Optimizes the LP problem previously constructed.
virtual void setMaximalMILPGap(ValueType const &gap, bool relative) override
Specifies the maximum difference between lower- and upper objective bounds that triggers termination.
typename LpSolver< ValueType, RawMode >::Constraint Constraint
virtual bool getBinaryValue(Variable const &name) const override
Retrieves the value of the binary variable with the given name.
#define STORM_LOG_TRACE(message)
Definition logging.h:22
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
SFTBDDChecker::ValueType ValueType
RelationType
An enum type specifying the different relations applicable.
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.
std::map< storm::expressions::Variable, ValueType >::const_iterator begin() const
std::map< storm::expressions::Variable, ValueType >::const_iterator end() const
void separateVariablesFromConstantPart(VariableCoefficients &rhs)
Brings all variables of the right-hand side coefficients to the left-hand side by negating them and m...