23template<
typename ValueType,
bool RawMode>
29 modelContainsIntegerVariables(false),
30 integerTolerance(glpkSettings.getIntegerTolerance()),
31 milpPresolverEnabled(glpkSettings.isMILPPresolverEnabled()),
32 isInfeasibleFlag(false),
33 isUnboundedFlag(false) {
35 lp = glp_create_prob();
38 glp_set_prob_name(lp, name.c_str());
41 glp_term_out(debug || glpkSettings.
isOutputSet() ? GLP_ON : GLP_OFF);
44 glp_iocp* defaultParameters =
new glp_iocp();
45 glp_init_iocp(defaultParameters);
46 this->maxMILPGap = defaultParameters->mip_gap;
47 this->maxMILPGapRelative =
true;
52template<
typename ValueType,
bool RawMode>
58template<
typename ValueType,
bool RawMode>
64template<
typename ValueType,
bool RawMode>
70template<
typename ValueType,
bool RawMode>
76template<
typename ValueType,
bool RawMode>
80 glp_delete_prob(this->lp);
85template<
typename ValueType,
bool RawMode>
100 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
101 "version with GLPK support.");
105template<
typename ValueType,
bool RawMode>
107 std::optional<ValueType>
const& lowerBound,
108 std::optional<ValueType>
const& upperBound,
109 ValueType objectiveFunctionCoefficient) {
110#ifdef STORM_HAVE_GLPK
112 if constexpr (RawMode) {
113 resultVar = variableToIndexMap.size();
118 STORM_LOG_ASSERT(variableToIndexMap.count(resultVar) == 0,
"Variable " << resultVar.getName() <<
" exists already in the model.");
122 if (lowerBound.has_value()) {
123 boundType = upperBound.has_value() ? GLP_DB : GLP_LO;
125 boundType = upperBound.has_value() ? GLP_UP : GLP_FR;
128 if (type == VariableType::Integer || type == VariableType::Binary) {
129 this->modelContainsIntegerVariables =
true;
133 int variableIndex = glp_add_cols(this->lp, 1);
134 glp_set_col_name(this->lp, variableIndex, name.c_str());
140 if constexpr (RawMode) {
141 this->variableToIndexMap.push_back(variableIndex);
143 this->variableToIndexMap.emplace(resultVar, variableIndex);
144 if (!incrementalData.empty()) {
145 incrementalData.back().variables.push_back(resultVar);
152 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
153 "version with GLPK support.");
157template<
typename ValueType,
bool RawMode>
162template<
typename ValueType,
bool RawMode>
164#ifdef STORM_HAVE_GLPK
166 int constraintIndex = glp_add_rows(this->lp, 1);
167 glp_set_row_name(this->lp, constraintIndex, name.c_str());
173 std::vector<int> variableIndices(1, -1);
174 std::vector<double> coefficients(1, 0.0);
175 if constexpr (RawMode) {
177 relationType = constraint.relationType;
178 variableIndices.reserve(constraint.lhsVariableIndices.size() + 1);
179 for (
auto const& var : constraint.lhsVariableIndices) {
180 variableIndices.push_back(this->variableToIndexMap.at(var));
182 coefficients.reserve(constraint.lhsCoefficients.size() + 1);
183 for (
auto const& coef : constraint.lhsCoefficients) {
187 STORM_LOG_THROW(constraint.getManager() == this->getManager(), storm::exceptions::InvalidArgumentException,
188 "Constraint was not built over the proper variables.");
189 STORM_LOG_THROW(constraint.isRelationalExpression(), storm::exceptions::InvalidArgumentException,
"Illegal constraint is not a relational expression.");
197 relationType = constraint.getBaseExpression().asBinaryRelationExpression().getRelationType();
198 int len = std::distance(leftCoefficients.
begin(), leftCoefficients.
end());
199 variableIndices.reserve(len + 1);
200 coefficients.reserve(len + 1);
201 for (
auto const& variableCoefficientPair : leftCoefficients) {
202 auto variableIndexPair = this->variableToIndexMap.find(variableCoefficientPair.first);
203 variableIndices.push_back(variableIndexPair->second);
204 coefficients.push_back(variableCoefficientPair.second);
209 switch (relationType) {
211 glp_set_row_bnds(this->lp, constraintIndex, GLP_UP, 0, rhs - this->integerTolerance);
214 glp_set_row_bnds(this->lp, constraintIndex, GLP_UP, 0, rhs);
217 glp_set_row_bnds(this->lp, constraintIndex, GLP_LO, rhs + this->integerTolerance, 0);
220 glp_set_row_bnds(this->lp, constraintIndex, GLP_LO, rhs, 0);
223 glp_set_row_bnds(this->lp, constraintIndex, GLP_FX, rhs, rhs);
230 glp_set_mat_row(this->lp, constraintIndex, variableIndices.size() - 1, variableIndices.data(), coefficients.data());
235 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
236 "version with GLPK support.");
240template<
typename ValueType,
bool RawMode>
243 "Indicator constraints are not supported by GLPK. Use an LP solver that supports them (e.g. via --lpsolver highs or --lpsolver gurobi).");
246#ifdef STORM_HAVE_GLPK
248void callback(glp_tree* t,
void* info) {
249 auto& mipgap = *
static_cast<std::pair<double, bool>*
>(info);
250 double actualRelativeGap = glp_ios_mip_gap(t);
252 if (!mipgap.second) {
257 if (actualRelativeGap * factor <= mipgap.first) {
259 mipgap.first = actualRelativeGap;
260 mipgap.second =
true;
261 glp_ios_terminate(t);
266template<
typename ValueType,
bool RawMode>
268#ifdef STORM_HAVE_GLPK
270 this->isInfeasibleFlag =
false;
271 this->isUnboundedFlag =
false;
277 if (this->modelContainsIntegerVariables) {
278 glp_iocp* parameters =
new glp_iocp();
279 glp_init_iocp(parameters);
280 parameters->tol_int = this->integerTolerance;
281 this->isInfeasibleFlag =
false;
282 if (this->milpPresolverEnabled) {
283 parameters->presolve = GLP_ON;
287 error = glp_simplex(this->lp,
nullptr);
288 STORM_LOG_THROW(error == 0, storm::exceptions::InvalidStateException,
"Unable to optimize relaxed glpk model (" << error <<
").");
290 if (glp_get_status(this->lp) == GLP_INFEAS || glp_get_status(this->lp) == GLP_NOFEAS) {
291 this->isInfeasibleFlag =
true;
295 if (glp_get_status(this->lp) == GLP_UNBND) {
296 parameters->presolve = GLP_ON;
298 parameters->presolve = GLP_OFF;
301 if (!this->isInfeasibleFlag) {
304 std::pair<double, bool> mipgap(this->maxMILPGap, this->maxMILPGapRelative);
306 parameters->cb_func = &callback;
307 parameters->cb_info = &mipgap;
311 error = glp_intopt(this->lp, parameters);
312 int status = glp_mip_status(this->lp);
316 this->actualRelativeMILPGap = mipgap.first;
320 if (error == GLP_ENOPFS || status == GLP_NOFEAS) {
321 this->isInfeasibleFlag =
true;
323 }
else if (error == GLP_ENODFS) {
324 this->isUnboundedFlag =
true;
326 }
else if (error == GLP_ESTOP) {
329 }
else if (error == GLP_EBOUND) {
331 "The bounds of some variables are illegal. Note that glpk only accepts integer bounds for integer variables.");
335 error = glp_simplex(this->lp,
nullptr);
338 STORM_LOG_THROW(error == 0, storm::exceptions::InvalidStateException,
"Unable to optimize glpk model (" << error <<
").");
342 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
343 "version with GLPK support.");
347template<
typename ValueType,
bool RawMode>
349#ifdef STORM_HAVE_GLPK
351 "Illegal call to GlpkLpSolver::isInfeasible: model has not been optimized.");
353 if (this->modelContainsIntegerVariables) {
354 return isInfeasibleFlag;
356 return glp_get_status(this->lp) == GLP_INFEAS || glp_get_status(this->lp) == GLP_NOFEAS;
360 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
361 "version with GLPK support.");
365template<
typename ValueType,
bool RawMode>
367#ifdef STORM_HAVE_GLPK
369 "Illegal call to GlpkLpSolver::isUnbounded: model has not been optimized.");
371 if (this->modelContainsIntegerVariables) {
372 return isUnboundedFlag;
374 return glp_get_status(this->lp) == GLP_UNBND;
378 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
379 "version with GLPK support.");
383template<
typename ValueType,
bool RawMode>
392template<
typename ValueType,
bool RawMode>
394#ifdef STORM_HAVE_GLPK
396 STORM_LOG_THROW(!this->
isInfeasible(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from infeasible model.");
397 STORM_LOG_THROW(!this->
isUnbounded(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unbounded model.");
398 STORM_LOG_THROW(
false, storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unoptimized model.");
401 int variableIndex = variableToIndexMap.at(variable);
404 if (this->modelContainsIntegerVariables) {
405 value = glp_mip_col_val(this->lp,
static_cast<int>(variableIndex));
407 value = glp_get_col_prim(this->lp,
static_cast<int>(variableIndex));
412 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
413 "version with GLPK support.");
417template<
typename ValueType,
bool RawMode>
419#ifdef STORM_HAVE_GLPK
421 STORM_LOG_THROW(!this->
isInfeasible(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from infeasible model.");
422 STORM_LOG_THROW(!this->
isUnbounded(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unbounded model.");
423 STORM_LOG_THROW(
false, storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unoptimized model.");
426 int variableIndex = variableToIndexMap.at(variable);
429 if (this->modelContainsIntegerVariables) {
430 value = glp_mip_col_val(this->lp, variableIndex);
432 value = glp_get_col_prim(this->lp, variableIndex);
435 double roundedValue = std::round(value);
436 double diff = std::abs(roundedValue - value);
438 "Illegal value for integer variable in GLPK solution (" << value <<
"). Difference to nearest int is " << diff);
439 return static_cast<int_fast64_t
>(roundedValue);
442 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
443 "version with GLPK support.");
447template<
typename ValueType,
bool RawMode>
449#ifdef STORM_HAVE_GLPK
451 STORM_LOG_THROW(!this->
isInfeasible(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from infeasible model.");
452 STORM_LOG_THROW(!this->
isUnbounded(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unbounded model.");
453 STORM_LOG_THROW(
false, storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unoptimized model.");
456 int variableIndex = variableToIndexMap.at(variable);
459 if (this->modelContainsIntegerVariables) {
460 value = glp_mip_col_val(this->lp, variableIndex);
462 value = glp_get_col_prim(this->lp, variableIndex);
466 STORM_LOG_ERROR_COND(std::abs(value - 1.0) <= this->integerTolerance,
"Illegal value for binary variable in GLPK solution (" << value <<
").");
469 STORM_LOG_ERROR_COND(std::abs(value) <= this->integerTolerance,
"Illegal value for binary variable in GLPK solution (" << value <<
").");
473 return static_cast<bool>(value);
476 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
477 "version with GLPK support.");
481template<
typename ValueType,
bool RawMode>
483#ifdef STORM_HAVE_GLPK
485 STORM_LOG_THROW(!this->
isInfeasible(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from infeasible model.");
486 STORM_LOG_THROW(!this->
isUnbounded(), storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unbounded model.");
487 STORM_LOG_THROW(
false, storm::exceptions::InvalidAccessException,
"Unable to get glpk solution from unoptimized model.");
491 if (this->modelContainsIntegerVariables) {
492 value = glp_mip_obj_val(this->lp);
494 value = glp_get_obj_val(this->lp);
500 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
501 "version with GLPK support.");
505template<
typename ValueType,
bool RawMode>
507#ifdef STORM_HAVE_GLPK
508 glp_write_lp(this->lp,
nullptr, filename.c_str());
511 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
512 "version with GLPK support.");
516template<
typename ValueType,
bool RawMode>
518#ifdef STORM_HAVE_GLPK
519 if constexpr (RawMode) {
520 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Incremental LP solving not supported in raw mode.");
522 IncrementalLevel lvl;
523 lvl.firstConstraintIndex = glp_get_num_rows(this->lp) + 1;
524 incrementalData.push_back(lvl);
528 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
529 "version with GLPK support.");
533template<
typename ValueType,
bool RawMode>
535#ifdef STORM_HAVE_GLPK
536 if constexpr (RawMode) {
537 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Incremental LP solving not supported in raw mode.");
539 if (incrementalData.empty()) {
542 IncrementalLevel
const& lvl = incrementalData.back();
545 if (indicesToBeRemoved.size() > 1) {
546 glp_del_rows(this->lp, indicesToBeRemoved.size() - 1, indicesToBeRemoved.data());
548 indicesToBeRemoved.clear();
550 if (!lvl.variables.empty()) {
553 for (
auto const& var : lvl.variables) {
555 auto it = variableToIndexMap.find(var);
556 firstIndex = it->second;
557 variableToIndexMap.erase(it);
560 variableToIndexMap.erase(var);
565 glp_del_cols(this->lp, indicesToBeRemoved.size() - 1, indicesToBeRemoved.data());
567 incrementalData.pop_back();
570 int n = glp_get_num_rows(lp);
571 int m = glp_get_num_cols(lp);
573 for (
int i = 1; i <= n; ++i) {
574 if (glp_get_row_stat(lp, i) == GLP_BS) {
578 for (
int j = 1; j <= m; ++j) {
579 if (glp_get_col_stat(lp, j) == GLP_BS) {
583 if (n != (nb + mb)) {
584 glp_std_basis(this->lp);
590 "This version of storm was compiled without support for GLPK. Yet, a method was called that requires this support. Please choose a "
591 "version with GLPK support.");
595template<
typename ValueType,
bool RawMode>
598 this->maxMILPGapRelative = relative;
601template<
typename ValueType,
bool RawMode>
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...
A class that implements the LpSolver interface using glpk as the background solver.
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
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
An interface that captures the functionality of an LP solver.
bool currentModelHasBeenOptimized
OptimizationDirection getOptimizationDirection() const
storm::expressions::Variable declareOrGetExpressionVariable(std::string const &name, VariableType const &type)
#define STORM_LOG_ERROR(message)
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_ERROR_COND(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
SFTBDDChecker::ValueType ValueType
RelationType
An enum type specifying the different relations applicable.
int getGlpkType(typename GlpkLpSolver< ValueType, RawMode >::VariableType const &type)
std::vector< T > buildVectorForRange(T min, T max)
Constructs a vector [min, min+1, ...., max-1].
bool isZero(ValueType const &a)
ValueType abs(ValueType const &number)
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...