22#ifdef STORM_HAVE_HIGHS
26constexpr double highInfinity = std::numeric_limits<double>::infinity();
29constexpr double strictInequalityMargin = 1e-06;
31struct HighsConstraintData {
32 std::vector<HighsInt> variableIndices;
33 std::vector<double> coefficients;
38template<
typename ValueType,
bool RawMode>
40 std::map<storm::expressions::Variable, uint64_t>
const& variableToIndexMap) {
41 HighsConstraintData result;
42 if constexpr (RawMode) {
44 result.relationType = constraint.relationType;
45 result.variableIndices.reserve(constraint.lhsVariableIndices.size());
46 result.coefficients.reserve(constraint.lhsCoefficients.size());
47 for (
auto const& variable : constraint.lhsVariableIndices) {
48 result.variableIndices.push_back(
static_cast<HighsInt
>(variable));
50 for (
auto const& coefficient : constraint.lhsCoefficients) {
54 STORM_LOG_THROW(constraint.isRelationalExpression(), storm::exceptions::InvalidArgumentException,
"Illegal constraint is not a relational expression.");
55 storm::expressions::LinearCoefficientVisitor<double>::VariableCoefficients leftCoefficients =
56 storm::expressions::LinearCoefficientVisitor<double>().getLinearCoefficients(constraint.getOperand(0));
57 storm::expressions::LinearCoefficientVisitor<double>::VariableCoefficients rightCoefficients =
58 storm::expressions::LinearCoefficientVisitor<double>().getLinearCoefficients(constraint.getOperand(1));
61 result.relationType = constraint.getBaseExpression().asBinaryRelationExpression().getRelationType();
62 result.variableIndices.reserve(leftCoefficients.
size());
63 result.coefficients.reserve(leftCoefficients.
size());
64 for (
auto const& variableCoefficientPair : leftCoefficients) {
65 auto variableIndexPair = variableToIndexMap.find(variableCoefficientPair.first);
66 result.variableIndices.push_back(
static_cast<HighsInt
>(variableIndexPair->second));
72 result.rhs -= strictInequalityMargin;
75 result.rhs += strictInequalityMargin;
82template<
typename ValueType,
bool RawMode>
86 highs.setOptionValue(
"output_flag",
false);
88 highs.setOptionValue(
"mip_feasibility_tolerance", strictInequalityMargin / 10);
91template<
typename ValueType,
bool RawMode>
94template<
typename ValueType,
bool RawMode>
95HighsLpSolver<ValueType, RawMode>::HighsLpSolver(OptimizationDirection
const& optDir) : HighsLpSolver(
"", optDir) {}
97template<
typename ValueType,
bool RawMode>
100template<
typename ValueType,
bool RawMode>
101HighsLpSolver<ValueType, RawMode>::~HighsLpSolver() {}
103template<
typename ValueType,
bool RawMode>
104double HighsLpSolver<ValueType, RawMode>::toHighsBound(
double value)
const {
105 if (!std::isfinite(value)) {
106 return value > 0 ? highs.getInfinity() : -highs.getInfinity();
111template<
typename ValueType,
bool RawMode>
112typename HighsLpSolver<ValueType, RawMode>::Variable HighsLpSolver<ValueType, RawMode>::addVariable(std::string
const& name, VariableType
const& type,
113 std::optional<ValueType>
const& lowerBound,
114 std::optional<ValueType>
const& upperBound,
115 ValueType objectiveFunctionCoefficient) {
117 if constexpr (RawMode) {
118 resultVar = nextVariableIndex;
120 resultVar = this->declareOrGetExpressionVariable(name, type);
121 STORM_LOG_ASSERT(variableToIndexMap.count(resultVar) == 0,
"Variable " << resultVar.getName() <<
" exists already in the model.");
122 this->variableToIndexMap.emplace(resultVar, nextVariableIndex);
127 if (type == VariableType::Binary) {
132 HighsStatus addColStatus =
134 STORM_LOG_THROW(addColStatus != HighsStatus::kError, storm::exceptions::InvalidStateException,
"Unable to add variable to HiGHS model.");
135 HighsInt column =
static_cast<HighsInt
>(nextVariableIndex);
137 if (type != VariableType::Continuous) {
138 HighsStatus integralityStatus = highs.changeColIntegrality(column, HighsVarType::kInteger);
139 STORM_LOG_THROW(integralityStatus != HighsStatus::kError, storm::exceptions::InvalidStateException,
"Unable to set integrality of HiGHS variable.");
142 highs.passColName(column, name);
145 variableBounds.emplace_back(lower, upper);
150template<
typename ValueType,
bool RawMode>
151void HighsLpSolver<ValueType, RawMode>::update()
const {
155template<
typename ValueType,
bool RawMode>
156void HighsLpSolver<ValueType, RawMode>::addConstraint(std::string
const&, Constraint
const& constraint) {
157 if constexpr (!RawMode) {
158 STORM_LOG_ASSERT(constraint.getManager() == this->getManager(),
"Constraint was not built over the proper variables.");
161 auto constraintData = createConstraintData<ValueType, RawMode>(constraint, this->variableToIndexMap);
164 switch (constraintData.relationType) {
166 lower = -highInfinity;
167 upper = constraintData.rhs;
170 lower = constraintData.rhs;
171 upper = highInfinity;
174 lower = constraintData.rhs;
175 upper = constraintData.rhs;
180 highs.addRow(toHighsBound(lower), toHighsBound(upper), constraintData.variableIndices.size(), constraintData.variableIndices.data(),
181 constraintData.coefficients.data());
184template<
typename ValueType,
bool RawMode>
185void HighsLpSolver<ValueType, RawMode>::addIndicatorConstraint(std::string
const&, Variable indicatorVariable,
bool indicatorValue,
186 Constraint
const& constraint) {
187 if constexpr (RawMode) {
188 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Indicator constraints not implemented in RawMode.");
190 STORM_LOG_ASSERT(this->variableToIndexMap.count(indicatorVariable) > 0,
"Indicator Variable " << indicatorVariable.getName() <<
" unknown to solver.");
191 STORM_LOG_ASSERT(indicatorVariable.hasIntegerType(),
"Indicator Variable " << indicatorVariable.getName() <<
" has unexpected type.");
192 STORM_LOG_ASSERT(constraint.getManager() == this->getManager(),
"Constraint was not built over the proper variables.");
194 auto constraintData = createConstraintData<ValueType, RawMode>(constraint, this->variableToIndexMap);
198 double minActivity = 0.0;
199 double maxActivity = 0.0;
200 for (std::size_t i = 0;
i < constraintData.variableIndices.size(); ++
i) {
201 double coefficient = constraintData.coefficients[
i];
202 auto const& bounds = variableBounds[constraintData.variableIndices[
i]];
203 double lowerBound = bounds.first;
204 double upperBound = bounds.second;
205 if (coefficient > 0) {
206 maxActivity += coefficient * (std::isfinite(upperBound) ? upperBound : highInfinity);
207 minActivity += coefficient * (std::isfinite(lowerBound) ? lowerBound : -highInfinity);
208 }
else if (coefficient < 0) {
209 maxActivity += coefficient * (std::isfinite(lowerBound) ? lowerBound : -highInfinity);
210 minActivity += coefficient * (std::isfinite(upperBound) ? upperBound : highInfinity);
214 HighsInt indicatorIndex =
static_cast<HighsInt
>(this->variableToIndexMap.at(indicatorVariable));
216 auto addIndicatorRow = [
this, &constraintData, indicatorIndex](
double mCoefficient,
double newRhs,
bool isLessEqual) {
217 std::vector<HighsInt> variableIndices = constraintData.variableIndices;
218 std::vector<double> coefficients = constraintData.coefficients;
219 variableIndices.push_back(indicatorIndex);
220 coefficients.push_back(mCoefficient);
221 double lower = -highInfinity;
222 double upper = highInfinity;
228 highs.addRow(toHighsBound(lower), toHighsBound(upper), variableIndices.size(), variableIndices.data(), coefficients.data());
231 auto addSingleIndicatorConstraint = [&](
bool isLessEqual,
bool indicatorValue) {
234 m = maxActivity - constraintData.rhs;
236 m = constraintData.rhs - minActivity;
238 STORM_LOG_THROW(std::isfinite(m), storm::exceptions::NotSupportedException,
239 "Indicator constraints over unbounded variables are not supported by the HiGHS solver.");
240 double mCoefficient = (indicatorValue == isLessEqual) ? m : -m;
241 double newRhs = constraintData.rhs;
242 if (indicatorValue) {
243 newRhs += isLessEqual ? m : -m;
245 addIndicatorRow(mCoefficient, newRhs, isLessEqual);
248 switch (constraintData.relationType) {
250 addSingleIndicatorConstraint(
true, indicatorValue);
253 addSingleIndicatorConstraint(
false, indicatorValue);
256 addSingleIndicatorConstraint(
true, indicatorValue);
257 addSingleIndicatorConstraint(
false, indicatorValue);
265template<
typename ValueType,
bool RawMode>
266void HighsLpSolver<ValueType, RawMode>::optimize()
const {
271 highs.changeObjectiveSense(this->getOptimizationDirection() == OptimizationDirection::Minimize ? ObjSense::kMinimize : ObjSense::kMaximize);
274 HighsStatus status = highs.run();
275 STORM_LOG_THROW(status != HighsStatus::kError, storm::exceptions::InvalidStateException,
276 "Unable to optimize the model with HiGHS (model status: " << highs.modelStatusToString(highs.getModelStatus()) <<
").");
278 this->currentModelHasBeenOptimized =
true;
279 modelStatus = highs.getModelStatus();
282template<
typename ValueType,
bool RawMode>
283bool HighsLpSolver<ValueType, RawMode>::isInfeasible()
const {
284 STORM_LOG_THROW(this->currentModelHasBeenOptimized, storm::exceptions::InvalidStateException,
285 "Illegal call to HighsLpSolver<ValueType, RawMode>::isInfeasible: model has not been optimized.");
286 return modelStatus == HighsModelStatus::kInfeasible;
289template<
typename ValueType,
bool RawMode>
290bool HighsLpSolver<ValueType, RawMode>::isUnbounded()
const {
291 STORM_LOG_THROW(this->currentModelHasBeenOptimized, storm::exceptions::InvalidStateException,
292 "Illegal call to HighsLpSolver<ValueType, RawMode>::isUnbounded: model has not been optimized.");
293 return modelStatus == HighsModelStatus::kUnbounded || modelStatus == HighsModelStatus::kUnboundedOrInfeasible;
296template<
typename ValueType,
bool RawMode>
297bool HighsLpSolver<ValueType, RawMode>::isOptimal()
const {
298 if (!this->currentModelHasBeenOptimized) {
301 return modelStatus == HighsModelStatus::kOptimal;
304template<
typename ValueType,
bool RawMode>
305ValueType HighsLpSolver<ValueType, RawMode>::getContinuousValue(Variable
const& variable)
const {
306 STORM_LOG_THROW(this->isOptimal(), storm::exceptions::InvalidAccessException,
307 "Unable to get HiGHS solution from a model that has not been solved optimally.");
309 uint64_t variableIndex;
310 if constexpr (RawMode) {
311 variableIndex = variable;
313 STORM_LOG_THROW(variableToIndexMap.count(variable) != 0, storm::exceptions::InvalidAccessException,
314 "Accessing value of unknown variable '" << variable.getName() <<
"'.");
315 variableIndex = variableToIndexMap.at(variable);
317 STORM_LOG_ASSERT(variableIndex < nextVariableIndex,
"Variable Index exceeds highest value.");
322template<
typename ValueType,
bool RawMode>
323int_fast64_t HighsLpSolver<ValueType, RawMode>::getIntegerValue(Variable
const& variable)
const {
324 STORM_LOG_THROW(this->isOptimal(), storm::exceptions::InvalidAccessException,
325 "Unable to get HiGHS solution from a model that has not been solved optimally.");
327 uint64_t variableIndex;
328 if constexpr (RawMode) {
329 variableIndex = variable;
331 STORM_LOG_THROW(variableToIndexMap.count(variable) != 0, storm::exceptions::InvalidAccessException,
332 "Accessing value of unknown variable '" << variable.getName() <<
"'.");
333 variableIndex = variableToIndexMap.at(variable);
335 STORM_LOG_ASSERT(variableIndex < nextVariableIndex,
"Variable Index exceeds highest value.");
337 return std::llround(highs.getSolution().col_value[variableIndex]);
340template<
typename ValueType,
bool RawMode>
341bool HighsLpSolver<ValueType, RawMode>::getBinaryValue(Variable
const& variable)
const {
342 STORM_LOG_THROW(this->isOptimal(), storm::exceptions::InvalidAccessException,
343 "Unable to get HiGHS solution from a model that has not been solved optimally.");
345 uint64_t variableIndex;
346 if constexpr (RawMode) {
347 variableIndex = variable;
349 STORM_LOG_THROW(variableToIndexMap.count(variable) != 0, storm::exceptions::InvalidAccessException,
350 "Accessing value of unknown variable '" << variable.getName() <<
"'.");
351 variableIndex = variableToIndexMap.at(variable);
353 STORM_LOG_ASSERT(variableIndex < nextVariableIndex,
"Variable Index exceeds highest value.");
355 return highs.getSolution().col_value[variableIndex] > 0.5;
358template<
typename ValueType,
bool RawMode>
359ValueType HighsLpSolver<ValueType, RawMode>::getObjectiveValue()
const {
360 STORM_LOG_THROW(this->isOptimal(), storm::exceptions::InvalidAccessException,
361 "Unable to get HiGHS solution from a model that has not been solved optimally.");
365template<
typename ValueType,
bool RawMode>
366void HighsLpSolver<ValueType, RawMode>::writeModelToFile(std::string
const& filename)
const {
367 HighsStatus status = highs.writeModel(filename);
368 STORM_LOG_THROW(status != HighsStatus::kError, storm::exceptions::InvalidStateException,
"Unable to write HiGHS model to file '" << filename <<
"'.");
371template<
typename ValueType,
bool RawMode>
372void HighsLpSolver<ValueType, RawMode>::push() {
373 if constexpr (RawMode) {
374 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Incremental LP solving not supported in raw mode.");
376 IncrementalLevel lvl;
377 lvl.firstRowIndex = highs.getNumRow();
378 lvl.firstVariableIndex = nextVariableIndex;
379 incrementalData.push_back(lvl);
383template<
typename ValueType,
bool RawMode>
384void HighsLpSolver<ValueType, RawMode>::pop() {
385 if constexpr (RawMode) {
386 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Incremental LP solving not supported in raw mode.");
388 if (incrementalData.empty()) {
392 IncrementalLevel
const lvl = incrementalData.back();
393 incrementalData.pop_back();
394 this->currentModelHasBeenOptimized =
false;
397 HighsInt
const numRows = highs.getNumRow();
398 if (numRows > lvl.firstRowIndex) {
399 HighsStatus status = highs.deleteRows(lvl.firstRowIndex, numRows - 1);
400 STORM_LOG_THROW(status != HighsStatus::kError, storm::exceptions::InvalidStateException,
"Unable to delete constraints from HiGHS model.");
403 if (nextVariableIndex > lvl.firstVariableIndex) {
404 HighsStatus status = highs.deleteCols(
static_cast<HighsInt
>(lvl.firstVariableIndex),
static_cast<HighsInt
>(nextVariableIndex - 1));
405 STORM_LOG_THROW(status != HighsStatus::kError, storm::exceptions::InvalidStateException,
"Unable to delete variables from HiGHS model.");
406 for (
auto it = variableToIndexMap.begin(); it != variableToIndexMap.end();) {
407 if (it->second >= lvl.firstVariableIndex) {
408 it = variableToIndexMap.erase(it);
413 variableBounds.resize(lvl.firstVariableIndex);
414 nextVariableIndex = lvl.firstVariableIndex;
419template<
typename ValueType,
bool RawMode>
420void HighsLpSolver<ValueType, RawMode>::setMaximalMILPGap(ValueType
const& gap,
bool relative) {
422 HighsStatus status = relative ? highs.setOptionValue(
"mip_rel_gap", gapAsDouble) : highs.setOptionValue(
"mip_abs_gap", gapAsDouble);
423 STORM_LOG_THROW(status != HighsStatus::kError, storm::exceptions::InvalidStateException,
"Unable to set HiGHS MILP gap.");
426template<
typename ValueType,
bool RawMode>
427ValueType HighsLpSolver<ValueType, RawMode>::getMILPGap(
bool relative)
const {
428 auto const& info = highs.getInfo();
430 double relativeGap = info.mip_gap / 100.0;
441template<
typename ValueType,
bool RawMode>
444 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
445 "version of storm with HiGHS support.");
448template<
typename ValueType,
bool RawMode>
451 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
452 "version of storm with HiGHS support.");
455template<
typename ValueType,
bool RawMode>
458 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
459 "version of storm with HiGHS support.");
462template<
typename ValueType,
bool RawMode>
465 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
466 "version of storm with HiGHS support.");
469template<
typename ValueType,
bool RawMode>
472template<
typename ValueType,
bool RawMode>
474 std::optional<ValueType>
const&,
475 std::optional<ValueType>
const&, ValueType) {
477 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
478 "version of storm with HiGHS support.");
481template<
typename ValueType,
bool RawMode>
484 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
485 "version of storm with HiGHS support.");
488template<
typename ValueType,
bool RawMode>
491 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
492 "version of storm with HiGHS support.");
495template<
typename ValueType,
bool RawMode>
498 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
499 "version of storm with HiGHS support.");
502template<
typename ValueType,
bool RawMode>
505 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
506 "version of storm with HiGHS support.");
509template<
typename ValueType,
bool RawMode>
512 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
513 "version of storm with HiGHS support.");
516template<
typename ValueType,
bool RawMode>
519 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
520 "version of storm with HiGHS support.");
523template<
typename ValueType,
bool RawMode>
526 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
527 "version of storm with HiGHS support.");
530template<
typename ValueType,
bool RawMode>
533 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
534 "version of storm with HiGHS support.");
537template<
typename ValueType,
bool RawMode>
540 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
541 "version of storm with HiGHS support.");
544template<
typename ValueType,
bool RawMode>
547 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
548 "version of storm with HiGHS support.");
551template<
typename ValueType,
bool RawMode>
554 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
555 "version of storm with HiGHS support.");
558template<
typename ValueType,
bool RawMode>
561 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
562 "version of storm with HiGHS support.");
565template<
typename ValueType,
bool RawMode>
568 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
569 "version of storm with HiGHS support.");
572template<
typename ValueType,
bool RawMode>
575 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
576 "version of storm with HiGHS support.");
579template<
typename ValueType,
bool RawMode>
582 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
583 "version of storm with HiGHS support.");
586template<
typename ValueType,
bool RawMode>
589 "This version of storm was compiled without support for HiGHS. Yet, a method was called that requires this support. Please choose a "
590 "version of storm with HiGHS support.");
A class that implements the LpSolver interface using HiGHS.
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.
HighsLpSolver(std::string const &name, OptimizationDirection const &optDir)
Constructs a solver with the given name and model sense.
virtual void addConstraint(std::string const &name, Constraint const &constraint) override
Adds a the given constraint to the LP problem.
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 void pop() override
Pops a backtracking point from the solver's stack.
virtual int_fast64_t getIntegerValue(Variable const &variable) 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
typename LpSolver< ValueType, RawMode >::Variable Variable
virtual ValueType getMILPGap(bool relative) const override
Returns the obtained gap after a call to optimize().
virtual void writeModelToFile(std::string const &filename) const override
Writes the current LP problem to the given file.
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,...
HighsLpSolver()
Constructs a solver without a name.
virtual void push() override
Pushes a backtracking point on the solver's stack.
typename LpSolver< ValueType, RawMode >::VariableType VariableType
virtual bool isOptimal() const override
Retrieves whether the model was found to be optimal, i.e.
virtual bool getBinaryValue(Variable const &variable) const override
Retrieves the value of the binary variable with the given name.
virtual bool isInfeasible() const override
Retrieves whether the model was found to be infeasible.
virtual ValueType getContinuousValue(Variable const &variable) const override
Retrieves the value of the continuous variable with the given name.
virtual ValueType getObjectiveValue() const override
Retrieves the value of the objective function.
virtual bool isUnbounded() const override
Retrieves whether the model was found to be infeasible.
An interface that captures the functionality of an LP solver.
#define STORM_LOG_ERROR(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.
ValueType abs(ValueType const &number)
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.
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...