Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
GlpkLpSolver.cpp
Go to the documentation of this file.
2
3#include <cmath>
4
18
19namespace storm {
20namespace solver {
21
22#ifdef STORM_HAVE_GLPK
23template<typename ValueType, bool RawMode>
24GlpkLpSolver<ValueType, RawMode>::GlpkLpSolver(storm::GlpkSolverEnvironment const& glpkSettings, bool debug, std::string const& name,
25 OptimizationDirection const& optDir)
26 : LpSolver<ValueType, RawMode>(optDir),
27 lp(nullptr),
28 variableToIndexMap(),
29 modelContainsIntegerVariables(false),
30 integerTolerance(glpkSettings.getIntegerTolerance()),
31 milpPresolverEnabled(glpkSettings.isMILPPresolverEnabled()),
32 isInfeasibleFlag(false),
33 isUnboundedFlag(false) {
34 // Create the LP problem for glpk.
35 lp = glp_create_prob();
36
37 // Set its name and model sense.
38 glp_set_prob_name(lp, name.c_str());
39
40 // Set whether the glpk output shall be printed to the command line.
41 glp_term_out(debug || glpkSettings.isOutputSet() ? GLP_ON : GLP_OFF);
42
43 // Set the maximal allowed MILP gap to its default value
44 glp_iocp* defaultParameters = new glp_iocp();
45 glp_init_iocp(defaultParameters);
46 this->maxMILPGap = defaultParameters->mip_gap;
47 this->maxMILPGapRelative = true;
48}
49
50#else
51
52template<typename ValueType, bool RawMode>
54 // Throw nothing in a constructor.
55}
56#endif
57
58template<typename ValueType, bool RawMode>
59GlpkLpSolver<ValueType, RawMode>::GlpkLpSolver(storm::GlpkSolverEnvironment const& glpkSettings, bool debug, std::string const& name)
60 : GlpkLpSolver(glpkSettings, debug, name, OptimizationDirection::Minimize) {
61 // Intentionally left empty.
62}
63
64template<typename ValueType, bool RawMode>
66 : GlpkLpSolver(glpkSettings, debug, "", OptimizationDirection::Minimize) {
67 // Intentionally left empty.
68}
69
70template<typename ValueType, bool RawMode>
72 : GlpkLpSolver(glpkSettings, debug, "", optDir) {
73 // Intentionally left empty.
74}
75
76template<typename ValueType, bool RawMode>
78#ifdef STORM_HAVE_GLPK
79 // Dispose of all objects allocated dynamically by glpk.
80 glp_delete_prob(this->lp);
81 glp_free_env();
82#endif
83}
84
85template<typename ValueType, bool RawMode>
87#ifdef STORM_HAVE_GLPK
88 switch (type) {
90 return GLP_CV;
92 return GLP_IV;
94 return GLP_BV;
95 }
96 STORM_LOG_ASSERT(false, "Unexpected variable type.");
97 return -1;
98#else
99 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
102#endif
103}
104
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
111 Variable resultVar;
112 if constexpr (RawMode) {
113 resultVar = variableToIndexMap.size();
114 } else {
115 resultVar = this->declareOrGetExpressionVariable(name, type);
116 // Assert whether the variable does not exist yet.
117 // Due to incremental usage (push(), pop()), a variable might be declared in the manager but not in the lp model.
118 STORM_LOG_ASSERT(variableToIndexMap.count(resultVar) == 0, "Variable " << resultVar.getName() << " exists already in the model.");
119 }
120
121 int boundType;
122 if (lowerBound.has_value()) {
123 boundType = upperBound.has_value() ? GLP_DB : GLP_LO;
124 } else {
125 boundType = upperBound.has_value() ? GLP_UP : GLP_FR;
126 }
127
128 if (type == VariableType::Integer || type == VariableType::Binary) {
129 this->modelContainsIntegerVariables = true;
130 }
131
132 // Create the variable in glpk.
133 int variableIndex = glp_add_cols(this->lp, 1);
134 glp_set_col_name(this->lp, variableIndex, name.c_str());
135 glp_set_col_bnds(lp, variableIndex, boundType, lowerBound.has_value() ? storm::utility::convertNumber<double>(*lowerBound) : 0.0,
136 upperBound.has_value() ? storm::utility::convertNumber<double>(*upperBound) : 0.0);
137 glp_set_col_kind(this->lp, variableIndex, getGlpkType<ValueType, RawMode>(type));
138 glp_set_obj_coef(this->lp, variableIndex, storm::utility::convertNumber<double>(objectiveFunctionCoefficient));
139
140 if constexpr (RawMode) {
141 this->variableToIndexMap.push_back(variableIndex);
142 } else {
143 this->variableToIndexMap.emplace(resultVar, variableIndex);
144 if (!incrementalData.empty()) {
145 incrementalData.back().variables.push_back(resultVar);
146 }
147 }
148
149 return resultVar;
150#else
151 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
154#endif
155}
156
157template<typename ValueType, bool RawMode>
159 // Intentionally left empty.
160}
161
162template<typename ValueType, bool RawMode>
163void GlpkLpSolver<ValueType, RawMode>::addConstraint(std::string const& name, Constraint const& constraint) {
164#ifdef STORM_HAVE_GLPK
165 // Add the row that will represent this constraint.
166 int constraintIndex = glp_add_rows(this->lp, 1);
167 glp_set_row_name(this->lp, constraintIndex, name.c_str());
168
169 // Extract constraint data
170 double rhs;
172 // glpk uses 1-based indexing (wtf!?)...
173 std::vector<int> variableIndices(1, -1);
174 std::vector<double> coefficients(1, 0.0);
175 if constexpr (RawMode) {
176 rhs = storm::utility::convertNumber<double>(constraint.rhs);
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));
181 }
182 coefficients.reserve(constraint.lhsCoefficients.size() + 1);
183 for (auto const& coef : constraint.lhsCoefficients) {
184 coefficients.push_back(storm::utility::convertNumber<double>(coef));
185 }
186 } else {
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.");
190
195 leftCoefficients.separateVariablesFromConstantPart(rightCoefficients);
196 rhs = rightCoefficients.getConstantPart();
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);
205 }
206 }
207
208 // Determine the type of the constraint and add it properly.
209 switch (relationType) {
211 glp_set_row_bnds(this->lp, constraintIndex, GLP_UP, 0, rhs - this->integerTolerance);
212 break;
214 glp_set_row_bnds(this->lp, constraintIndex, GLP_UP, 0, rhs);
215 break;
217 glp_set_row_bnds(this->lp, constraintIndex, GLP_LO, rhs + this->integerTolerance, 0);
218 break;
220 glp_set_row_bnds(this->lp, constraintIndex, GLP_LO, rhs, 0);
221 break;
223 glp_set_row_bnds(this->lp, constraintIndex, GLP_FX, rhs, rhs);
224 break;
225 default:
226 STORM_LOG_ASSERT(false, "Illegal operator in LP solver constraint.");
227 }
228
229 // Add the constraints
230 glp_set_mat_row(this->lp, constraintIndex, variableIndices.size() - 1, variableIndices.data(), coefficients.data());
231
232 this->currentModelHasBeenOptimized = false;
233#else
234 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
237#endif
238}
239
240template<typename ValueType, bool RawMode>
242 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException,
243 "Indicator constraints are not supported by GLPK. Use an LP solver that supports them (e.g. via --lpsolver highs or --lpsolver gurobi).");
244}
245
246#ifdef STORM_HAVE_GLPK
247// Method used within the MIP solver to terminate early
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);
251 double factor = storm::utility::one<double>();
252 if (!mipgap.second) {
253 // Compute absolute gap
254 factor = storm::utility::abs(glp_mip_obj_val(glp_ios_get_prob(t))) + DBL_EPSILON;
255 STORM_LOG_ASSERT(factor >= 0.0, "Expected non-negative factor.");
256 }
257 if (actualRelativeGap * factor <= mipgap.first) {
258 // Terminate early
259 mipgap.first = actualRelativeGap;
260 mipgap.second = true; // The gap is relative.
261 glp_ios_terminate(t);
262 }
263}
264#endif
265
266template<typename ValueType, bool RawMode>
268#ifdef STORM_HAVE_GLPK
269 // First, reset the flags.
270 this->isInfeasibleFlag = false;
271 this->isUnboundedFlag = false;
272
273 // Start by setting the model sense.
274 glp_set_obj_dir(this->lp, this->getOptimizationDirection() == OptimizationDirection::Minimize ? GLP_MIN : GLP_MAX);
275
276 int error = 0;
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;
284 } else {
285 // Without presolving, we solve the relaxed model first. This is required because
286 // glp_intopt requires that either presolving is enabled or an optimal initial basis is provided.
287 error = glp_simplex(this->lp, nullptr);
288 STORM_LOG_THROW(error == 0, storm::exceptions::InvalidStateException, "Unable to optimize relaxed glpk model (" << error << ").");
289 // If the relaxed model is already not feasible, we don't have to solve the actual model.
290 if (glp_get_status(this->lp) == GLP_INFEAS || glp_get_status(this->lp) == GLP_NOFEAS) {
291 this->isInfeasibleFlag = true;
292 }
293 // If the relaxed model is unbounded, there could still be no feasible integer solution.
294 // However, since we can not provide an optimal initial basis, we will need to enable presolving
295 if (glp_get_status(this->lp) == GLP_UNBND) {
296 parameters->presolve = GLP_ON;
297 } else {
298 parameters->presolve = GLP_OFF;
299 }
300 }
301 if (!this->isInfeasibleFlag) {
302 // Check whether we allow sub-optimal solutions via a non-zero MIP gap.
303 // parameters->mip_gap = this->maxMILPGap; (only works for relative values. Also, we need to obtain the actual gap anyway.
304 std::pair<double, bool> mipgap(this->maxMILPGap, this->maxMILPGapRelative);
305 if (!storm::utility::isZero(this->maxMILPGap)) {
306 parameters->cb_func = &callback;
307 parameters->cb_info = &mipgap;
308 }
309
310 // Invoke mip solving
311 error = glp_intopt(this->lp, parameters);
312 int status = glp_mip_status(this->lp);
313 delete parameters;
314
315 // mipgap.first has been set to the achieved mipgap (either within the callback function or because it has been set to this->maxMILPGap)
316 this->actualRelativeMILPGap = mipgap.first;
317
318 // In case the error is caused by an infeasible problem, we do not want to view this as an error and
319 // reset the error code.
320 if (error == GLP_ENOPFS || status == GLP_NOFEAS) {
321 this->isInfeasibleFlag = true;
322 error = 0;
323 } else if (error == GLP_ENODFS) {
324 this->isUnboundedFlag = true;
325 error = 0;
326 } else if (error == GLP_ESTOP) {
327 // Early termination due to achieved MIP Gap. That's fine.
328 error = 0;
329 } else if (error == GLP_EBOUND) {
330 STORM_LOG_THROW(false, storm::exceptions::InvalidStateException,
331 "The bounds of some variables are illegal. Note that glpk only accepts integer bounds for integer variables.");
332 }
333 }
334 } else {
335 error = glp_simplex(this->lp, nullptr);
336 }
337
338 STORM_LOG_THROW(error == 0, storm::exceptions::InvalidStateException, "Unable to optimize glpk model (" << error << ").");
339 this->currentModelHasBeenOptimized = true;
340#else
341 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
344#endif
345}
346
347template<typename ValueType, bool RawMode>
349#ifdef STORM_HAVE_GLPK
350 STORM_LOG_THROW(this->currentModelHasBeenOptimized, storm::exceptions::InvalidStateException,
351 "Illegal call to GlpkLpSolver::isInfeasible: model has not been optimized.");
352
353 if (this->modelContainsIntegerVariables) {
354 return isInfeasibleFlag;
355 } else {
356 return glp_get_status(this->lp) == GLP_INFEAS || glp_get_status(this->lp) == GLP_NOFEAS;
357 }
358#else
359 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
362#endif
363}
364
365template<typename ValueType, bool RawMode>
367#ifdef STORM_HAVE_GLPK
368 STORM_LOG_THROW(this->currentModelHasBeenOptimized, storm::exceptions::InvalidStateException,
369 "Illegal call to GlpkLpSolver::isUnbounded: model has not been optimized.");
370
371 if (this->modelContainsIntegerVariables) {
372 return isUnboundedFlag;
373 } else {
374 return glp_get_status(this->lp) == GLP_UNBND;
375 }
376#else
377 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
380#endif
381}
382
383template<typename ValueType, bool RawMode>
385 if (!this->currentModelHasBeenOptimized) {
386 return false;
387 }
388
389 return !isInfeasible() && !isUnbounded();
390}
391
392template<typename ValueType, bool RawMode>
394#ifdef STORM_HAVE_GLPK
395 if (!this->isOptimal()) {
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.");
399 }
400
401 int variableIndex = variableToIndexMap.at(variable);
402
403 double value = 0;
404 if (this->modelContainsIntegerVariables) {
405 value = glp_mip_col_val(this->lp, static_cast<int>(variableIndex));
406 } else {
407 value = glp_get_col_prim(this->lp, static_cast<int>(variableIndex));
408 }
410#else
411 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
414#endif
415}
416
417template<typename ValueType, bool RawMode>
419#ifdef STORM_HAVE_GLPK
420 if (!this->isOptimal()) {
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.");
424 }
425
426 int variableIndex = variableToIndexMap.at(variable);
427
428 double value = 0;
429 if (this->modelContainsIntegerVariables) {
430 value = glp_mip_col_val(this->lp, variableIndex);
431 } else {
432 value = glp_get_col_prim(this->lp, variableIndex);
433 }
434
435 double roundedValue = std::round(value);
436 double diff = std::abs(roundedValue - value);
437 STORM_LOG_ERROR_COND(diff <= this->integerTolerance,
438 "Illegal value for integer variable in GLPK solution (" << value << "). Difference to nearest int is " << diff);
439 return static_cast<int_fast64_t>(roundedValue);
440#else
441 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
444#endif
445}
446
447template<typename ValueType, bool RawMode>
449#ifdef STORM_HAVE_GLPK
450 if (!this->isOptimal()) {
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.");
454 }
455
456 int variableIndex = variableToIndexMap.at(variable);
457
458 double value = 0;
459 if (this->modelContainsIntegerVariables) {
460 value = glp_mip_col_val(this->lp, variableIndex);
461 } else {
462 value = glp_get_col_prim(this->lp, variableIndex);
463 }
464
465 if (value > 0.5) {
466 STORM_LOG_ERROR_COND(std::abs(value - 1.0) <= this->integerTolerance, "Illegal value for binary variable in GLPK solution (" << value << ").");
467 return true;
468 } else {
469 STORM_LOG_ERROR_COND(std::abs(value) <= this->integerTolerance, "Illegal value for binary variable in GLPK solution (" << value << ").");
470 return false;
471 }
472
473 return static_cast<bool>(value);
474#else
475 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
478#endif
479}
480
481template<typename ValueType, bool RawMode>
483#ifdef STORM_HAVE_GLPK
484 if (!this->isOptimal()) {
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.");
488 }
489
490 double value = 0;
491 if (this->modelContainsIntegerVariables) {
492 value = glp_mip_obj_val(this->lp);
493 } else {
494 value = glp_get_obj_val(this->lp);
495 }
496
498#else
499 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
502#endif
503}
504
505template<typename ValueType, bool RawMode>
506void GlpkLpSolver<ValueType, RawMode>::writeModelToFile(std::string const& filename) const {
507#ifdef STORM_HAVE_GLPK
508 glp_write_lp(this->lp, nullptr, filename.c_str());
509#else
510 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
513#endif
514}
515
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.");
521 } else {
522 IncrementalLevel lvl;
523 lvl.firstConstraintIndex = glp_get_num_rows(this->lp) + 1;
524 incrementalData.push_back(lvl);
525 }
526#else
527 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
530#endif
531}
532
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.");
538 } else {
539 if (incrementalData.empty()) {
540 STORM_LOG_ERROR("Tried to pop from a solver without pushing before.");
541 } else {
542 IncrementalLevel const& lvl = incrementalData.back();
543 // Since glpk uses 1-based indexing, we need to prepend an additional index
544 std::vector<int> indicesToBeRemoved = storm::utility::vector::buildVectorForRange(lvl.firstConstraintIndex - 1, glp_get_num_rows(this->lp) + 1);
545 if (indicesToBeRemoved.size() > 1) {
546 glp_del_rows(this->lp, indicesToBeRemoved.size() - 1, indicesToBeRemoved.data());
547 }
548 indicesToBeRemoved.clear();
549
550 if (!lvl.variables.empty()) {
551 int firstIndex = -1;
552 bool first = true;
553 for (auto const& var : lvl.variables) {
554 if (first) {
555 auto it = variableToIndexMap.find(var);
556 firstIndex = it->second;
557 variableToIndexMap.erase(it);
558 first = false;
559 } else {
560 variableToIndexMap.erase(var);
561 }
562 }
563 // Since glpk uses 1-based indexing, we need to prepend an additional index
564 std::vector<int> indicesToBeRemoved = storm::utility::vector::buildVectorForRange(firstIndex - 1, glp_get_num_cols(this->lp) + 1);
565 glp_del_cols(this->lp, indicesToBeRemoved.size() - 1, indicesToBeRemoved.data());
566 }
567 incrementalData.pop_back();
568 update();
569 // Check whether we need to adapt the current basis (i.e. the number of basic variables does not equal the number of constraints)
570 int n = glp_get_num_rows(lp);
571 int m = glp_get_num_cols(lp);
572 int nb(0), mb(0);
573 for (int i = 1; i <= n; ++i) {
574 if (glp_get_row_stat(lp, i) == GLP_BS) {
575 ++nb;
576 }
577 }
578 for (int j = 1; j <= m; ++j) {
579 if (glp_get_col_stat(lp, j) == GLP_BS) {
580 ++mb;
581 }
582 }
583 if (n != (nb + mb)) {
584 glp_std_basis(this->lp);
585 }
586 }
587 }
588#else
589 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException,
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.");
592#endif
593}
594
595template<typename ValueType, bool RawMode>
596void GlpkLpSolver<ValueType, RawMode>::setMaximalMILPGap(ValueType const& gap, bool relative) {
597 this->maxMILPGap = storm::utility::convertNumber<double>(gap);
598 this->maxMILPGapRelative = relative;
599}
600
601template<typename ValueType, bool RawMode>
602ValueType GlpkLpSolver<ValueType, RawMode>::getMILPGap(bool relative) const {
603 STORM_LOG_ASSERT(this->isOptimal(), "Asked for the MILP gap although there is no (bounded) solution.");
604 if (relative) {
605 return storm::utility::convertNumber<ValueType>(this->actualRelativeMILPGap);
606 } else {
608 }
609}
610
611template class GlpkLpSolver<double, true>;
612template class GlpkLpSolver<double, false>;
615
616} // namespace solver
617} // 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...
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.
Definition LpSolver.h:50
OptimizationDirection getOptimizationDirection() const
Definition LpSolver.cpp:125
storm::expressions::Variable declareOrGetExpressionVariable(std::string const &name, VariableType const &type)
Definition LpSolver.cpp:136
#define STORM_LOG_ERROR(message)
Definition logging.h:36
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_ERROR_COND(cond, message)
Definition macros.h:68
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
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].
Definition vector.h:130
bool isZero(ValueType const &a)
Definition constants.cpp:44
ValueType abs(ValueType const &number)
ValueType one()
Definition constants.cpp:21
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...