Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SolutionBounds.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstdint>
5#include <optional>
6#include <type_traits>
7#include <vector>
8
13
14namespace storm::solver {
15
21template<typename ValueType>
23 std::optional<std::vector<ValueType>> lower;
24 std::optional<std::vector<ValueType>> upper;
25
29 bool hasLower() const {
30 return lower.has_value();
31 }
32
36 bool hasUpper() const {
37 return upper.has_value();
38 }
39
43 bool hasAny() const {
44 return hasLower() || hasUpper();
45 }
46
50 bool encloses(std::vector<ValueType> const& values) const {
51 auto const isBoundedBy = [&values](std::optional<std::vector<ValueType>> const& bound, bool fromBelow) {
52 if (!bound.has_value()) {
53 return true;
54 }
55 if (bound->size() != values.size()) {
56 return false;
57 }
58 for (uint64_t i = 0; i < values.size(); ++i) {
59 if (fromBelow ? (*bound)[i] > values[i] : (*bound)[i] < values[i]) {
60 return false;
61 }
62 }
63 return true;
64 };
65 return isBoundedBy(lower, true) && isBoundedBy(upper, false);
66 }
67
71 bool isExact() const {
72 return hasLower() && hasUpper() && *lower == *upper;
73 }
74
78 bool isWithinPrecision(std::vector<ValueType> const& values, ValueType const& precision, bool relative) const {
79 if (!hasLower() || !hasUpper()) {
80 return false;
81 }
82 STORM_LOG_THROW(lower->size() == values.size() && upper->size() == values.size(), storm::exceptions::InvalidArgumentException,
83 "The given values do not match the size of the bounds.");
84 ValueType const zero = storm::utility::zero<ValueType>();
85 for (uint64_t i = 0; i < values.size(); ++i) {
86 ValueType const& low = (*lower)[i];
87 ValueType const& up = (*upper)[i];
88 ValueType allowed = precision;
89 if (relative) {
90 // The enclosed value is at least as far from zero as the bound closer to it, unless the two
91 // straddle zero, where only coinciding bounds say anything.
92 allowed = (low <= zero && up >= zero) ? zero : precision * std::min(storm::utility::abs(low), storm::utility::abs(up));
93 }
94 // Negated, so that a distance that is not a number is rejected.
95 if (!(values[i] - low <= allowed) || !(up - values[i] <= allowed)) {
96 return false;
97 }
98 }
99 return true;
100 }
101
105 void setExact(std::vector<ValueType> const& values) {
106 lower = values;
107 upper = values;
108 }
109
114 auto const oneMinus = [](ValueType const& value) { return storm::utility::one<ValueType>() - value; };
115 if (hasLower()) {
116 std::ranges::transform(*lower, lower->begin(), oneMinus);
117 }
118 if (hasUpper()) {
119 std::ranges::transform(*upper, upper->begin(), oneMinus);
120 }
121 std::swap(lower, upper);
122 }
123
128 template<typename Function>
129 auto transform(Function const& function) const {
131 if (hasLower()) {
132 result.lower = function(*lower);
133 }
134 if (hasUpper()) {
135 result.upper = function(*upper);
136 }
137 return result;
138 }
139
144 void widenTo(std::vector<ValueType> const& values) {
145 if (hasLower()) {
146 std::ranges::transform(*lower, values, lower->begin(), [](ValueType const& bound, ValueType const& value) { return std::min(bound, value); });
147 }
148 if (hasUpper()) {
149 std::ranges::transform(*upper, values, upper->begin(), [](ValueType const& bound, ValueType const& value) { return std::max(bound, value); });
150 }
151 }
152
156 void clear() {
157 lower = std::nullopt;
158 upper = std::nullopt;
159 }
160};
161
162} // namespace storm::solver
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
ValueType abs(ValueType const &number)
ValueType zero()
Definition constants.cpp:26
ValueType one()
Definition constants.cpp:21
Sound lower and upper bounds on a solution vector.
bool hasAny() const
Returns true if at least one of the bounds is set.
std::optional< std::vector< ValueType > > lower
bool hasUpper() const
Returns true if the upper bound is set.
auto transform(Function const &function) const
Applies the given function to whichever of the bounds are set, e.g.
bool hasLower() const
Returns true if the lower bound is set.
std::optional< std::vector< ValueType > > upper
void widenTo(std::vector< ValueType > const &values)
Widens whichever bounds are set until they enclose the given values.
void invertProbabilityBounds()
Turns bounds on a probability p into bounds on 1-p, i.e.
void clear()
Clears both bounds.
bool isWithinPrecision(std::vector< ValueType > const &values, ValueType const &precision, bool relative) const
Returns true if both bounds are set and neither is further from its value than the given precision al...
bool encloses(std::vector< ValueType > const &values) const
Returns true if every one of the given values lies within the bounds that are set.
bool isExact() const
Returns true if both bounds are set and coincide.
void setExact(std::vector< ValueType > const &values)
Sets both bounds to the given values, i.e.