22#ifdef STORM_HAVE_SOPLEX
24using namespace soplex;
26soplex::Rational to_soplex_rational(storm::RationalNumber
const& in) {
30storm::RationalNumber from_soplex_rational(soplex::Rational
const& r) {
34template<
typename ValueType,
bool RawMode>
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);
44 solver.setIntParam(SoPlex::VERBOSITY, 0);
47template<
typename ValueType,
bool RawMode>
52template<
typename ValueType,
bool RawMode>
53SoplexLpSolver<ValueType, RawMode>::SoplexLpSolver(OptimizationDirection
const& optDir) : SoplexLpSolver(
"", optDir) {
57template<
typename ValueType,
bool RawMode>
58SoplexLpSolver<ValueType, RawMode>::~SoplexLpSolver() {}
60template<
typename ValueType,
bool RawMode>
61void SoplexLpSolver<ValueType, RawMode>::update()
const {
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.");
72 if constexpr (RawMode) {
73 resultVar = nextVariableIndex;
75 resultVar = this->declareOrGetExpressionVariable(name, type);
78 STORM_LOG_ASSERT(variableToIndexMap.count(resultVar) == 0,
"Variable " << resultVar.getName() <<
" exists already in the model.");
79 this->variableToIndexMap.emplace(resultVar, nextVariableIndex);
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));
89 solver.addColReal(soplex::LPColReal(objectiveFunctionCoefficient, variables, upperBound.has_value() ? *upperBound : soplex::infinity,
90 lowerBound.has_value() ? *lowerBound : -soplex::infinity));
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);
101 using SoplexValueType = std::conditional_t<std::is_same_v<ValueType, storm::RationalNumber>, soplex::Rational, soplex::Real>;
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);
110 rhs = constraint.rhs;
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));
121 row.add(*varIt, *coefIt);
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.");
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));
145 SoplexValueType l, r;
146 switch (relationType) {
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.");
153 l = -soplex::infinity;
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.");
163 r = soplex::infinity;
172 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
173 solver.addRowRational(LPRowRational(l, row, r));
175 solver.addRowReal(LPRow(l, row, r));
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.");
184template<
typename ValueType,
bool RawMode>
185void SoplexLpSolver<ValueType, RawMode>::optimize()
const {
189 if (this->getOptimizationDirection() == storm::solver::OptimizationDirection::Minimize) {
190 solver.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MINIMIZE);
192 solver.setIntParam(SoPlex::OBJSENSE, SoPlex::OBJSENSE_MAXIMIZE);
195 status = solver.optimize();
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.");
202 this->currentModelHasBeenOptimized =
true;
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.");
210 return (status == soplex::SPxSolver::INFEASIBLE);
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.");
218 return (status == soplex::SPxSolver::UNBOUNDED);
221template<
typename ValueType,
bool RawMode>
222bool SoplexLpSolver<ValueType, RawMode>::isOptimal()
const {
223 if (!this->currentModelHasBeenOptimized) {
226 return (status == soplex::SPxSolver::OPTIMAL);
229template<
typename ValueType,
bool RawMode>
230ValueType SoplexLpSolver<ValueType, RawMode>::getContinuousValue(Variable
const& variable)
const {
234 if constexpr (RawMode) {
237 STORM_LOG_THROW(variableToIndexMap.count(variable) != 0, storm::exceptions::InvalidAccessException,
238 "Accessing value of unknown variable '" << variable.getName() <<
"'.");
239 varIndex = variableToIndexMap.at(variable);
241 STORM_LOG_ASSERT(varIndex < nextVariableIndex,
"Variable Index exceeds highest value.");
243 if (primalSolution.dim() == 0) {
244 primalSolution = TypedDVector(nextVariableIndex);
245 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
246 solver.getPrimalRational(primalSolution);
248 solver.getPrimal(primalSolution);
251 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
254 return primalSolution[varIndex];
259double SoplexLpSolver<double>::getObjectiveValue()
const {
261 return solver.objValueReal();
264template<
typename ValueType,
bool RawMode>
265ValueType SoplexLpSolver<ValueType, RawMode>::getObjectiveValue()
const {
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.");
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);
284 solver.writeFileReal(filename.c_str(),
nullptr,
nullptr,
nullptr);
288template<
typename ValueType,
bool RawMode>
289void SoplexLpSolver<ValueType, RawMode>::push() {
290 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Push/Pop not supported on SoPlex.");
293template<
typename ValueType,
bool RawMode>
294void SoplexLpSolver<ValueType, RawMode>::pop() {
295 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Push/Pop not supported on SoPlex.");
299template<
typename ValueType,
bool RawMode>
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.");
306template<
typename ValueType,
bool RawMode>
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.");
313template<
typename ValueType,
bool RawMode>
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.");
320template<
typename ValueType,
bool RawMode>
323template<
typename ValueType,
bool RawMode>
325 std::optional<ValueType>
const&,
326 std::optional<ValueType>
const&, ValueType) {
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.");
332template<
typename ValueType,
bool RawMode>
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.");
339template<
typename ValueType,
bool RawMode>
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.");
346template<
typename ValueType,
bool RawMode>
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.");
353template<
typename ValueType,
bool RawMode>
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.");
360template<
typename ValueType,
bool RawMode>
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.");
367template<
typename ValueType,
bool RawMode>
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.");
374template<
typename ValueType,
bool RawMode>
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.");
381template<
typename ValueType,
bool RawMode>
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.");
388template<
typename ValueType,
bool RawMode>
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.");
395template<
typename ValueType,
bool RawMode>
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.");
402template<
typename ValueType,
bool RawMode>
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.");
409template<
typename ValueType,
bool RawMode>
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.");
417template<
typename ValueType,
bool RawMode>
419 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"SoPlex does not support integer variables.");
422template<
typename ValueType,
bool RawMode>
424 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"SoPlex does not support binary variables.");
427template<
typename ValueType,
bool RawMode>
429 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"SoPlex does not support integer variables.");
432template<
typename ValueType,
bool RawMode>
434 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"SoPlex does not support integer variables.");
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.
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)
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
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
ValueType const & getConstantPart() const
void separateVariablesFromConstantPart(VariableCoefficients &rhs)
Brings all variables of the right-hand side coefficients to the left-hand side by negating them and m...