Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
IntervalterationHelper.cpp
Go to the documentation of this file.
2
3#include <cmath>
4#include <type_traits>
5
11
12namespace storm::solver::helper {
13
14template<typename ValueType, bool TrivialRowGrouping>
17 : viOperator(viOperator) {
18 // Intentionally left empty.
19}
20
21template<typename ValueType, OptimizationDirection Dir, bool Smooth, bool RoundingTowardsInf>
22class IIBackend {
23 public:
24 IIBackend(ValueType const& gamma) : gamma(gamma) {}
25 IIBackend() : gamma(storm::utility::zero<ValueType>()) {
26 STORM_LOG_ASSERT(!Smooth, "Gamma must be set for smooth interval iteration.");
27 }
28
30
31 void firstRow(std::pair<ValueType, ValueType>&& value, [[maybe_unused]] uint64_t rowGroup, [[maybe_unused]] uint64_t row) {
32 xBest = std::move(value.first);
33 yBest = std::move(value.second);
34 }
35
36 void nextRow(std::pair<ValueType, ValueType>&& value, [[maybe_unused]] uint64_t rowGroup, [[maybe_unused]] uint64_t row) {
37 xBest &= std::move(value.first);
38 yBest &= std::move(value.second);
39 }
40
41 void applyUpdate(ValueType& xCurr, ValueType& yCurr, [[maybe_unused]] uint64_t rowGroup) {
42 if constexpr (Smooth) {
43 if constexpr (RoundingTowardsInf) {
44 if (*xBest < xCurr) {
45 auto const diff = xCurr - *xBest;
46 xCurr = *xBest + gamma * diff; // = gamma * xCurr + (1-gamma) * xBest
47 }
48 } else {
49 if (*xBest > xCurr) {
50 auto const diff = *xBest - xCurr;
51 xCurr = *xBest - gamma * diff; // = gamma * xCurr + (1-gamma) * xBest
52 if constexpr (std::is_same_v<ValueType, double>) {
53 if (xCurr == *xBest) {
54 xCurr = std::nextafter(xCurr, xCurr - 1.0);
55 }
56 }
57 }
58 }
59 if (*yBest < yCurr) {
60 auto const diff = yCurr - *yBest;
61 yCurr = *yBest + gamma * diff; // = gamma * yCurr + (1-gamma) * yBest
62 if constexpr (!RoundingTowardsInf && std::is_same_v<ValueType, double>) {
63 if (yCurr == *yBest) {
64 yCurr = std::nextafter(yCurr, yCurr + 1.0);
65 }
66 }
67 }
68 } else {
69 if constexpr (RoundingTowardsInf) {
70 xCurr = std::min(xCurr, *xBest);
71 } else {
72 xCurr = std::max(xCurr, *xBest);
73 }
74 yCurr = std::min(yCurr, *yBest);
75 }
76 }
77
78 void endOfIteration() const {
79 // intentionally left empty.
80 }
81
82 bool constexpr converged() const {
83 return false;
84 }
85
86 bool constexpr abort() const {
87 return false;
88 }
89
90 private:
91 storm::utility::Extremum<RoundingTowardsInf ? storm::solver::invert(Dir) : Dir, ValueType> xBest;
93 ValueType const gamma;
94};
95
96template<typename ValueType, bool RoundingTowardsInf>
97bool checkConvergence(std::pair<std::vector<ValueType>, std::vector<ValueType>> const& xy, uint64_t& convergenceCheckState,
98 std::function<void()> const& getNextConvergenceCheckState, bool relative, ValueType const& precision) {
99 if (relative) {
100 for (; convergenceCheckState < xy.first.size(); getNextConvergenceCheckState()) {
101 ValueType const& l = RoundingTowardsInf ? -xy.first[convergenceCheckState] : xy.first[convergenceCheckState];
102 ValueType const& u = xy.second[convergenceCheckState];
104 if ((u - l) > l * precision) {
105 return false;
106 }
107 } else if (u < storm::utility::zero<ValueType>()) {
108 if ((l - u) < u * precision) {
109 return false;
110 }
111 } else { // l <= 0 <= u
112 if (l != u) {
113 return false;
114 }
115 }
116 }
117 } else {
118 for (; convergenceCheckState < xy.first.size(); getNextConvergenceCheckState()) {
119 ValueType const& l = RoundingTowardsInf ? -xy.first[convergenceCheckState] : xy.first[convergenceCheckState];
120 if (xy.second[convergenceCheckState] - l > precision) {
121 return false;
122 }
123 }
124 }
125 return true;
126}
127
128template<typename ValueType, bool TrivialRowGrouping>
129template<OptimizationDirection Dir, typename OffsetType, bool Smooth, bool RoundingTowardsInf>
130SolverStatus IntervalIterationHelper<ValueType, TrivialRowGrouping>::II(std::pair<std::vector<ValueType>, std::vector<ValueType>>& xy,
131 OffsetType const& offsets, uint64_t& numIterations, bool relative,
132 ValueType const& precision,
133 std::function<SolverStatus(IIData<ValueType> const&)> const& iterationCallback,
134 std::optional<storm::storage::BitVector> const& relevantValues,
135 ValueType const& gamma) const {
138 uint64_t convergenceCheckState = 0;
139 std::function<void()> getNextConvergenceCheckState;
140 if (relevantValues) {
141 convergenceCheckState = relevantValues->getNextSetIndex(0);
142 getNextConvergenceCheckState = [&convergenceCheckState, &relevantValues]() {
143 convergenceCheckState = relevantValues->getNextSetIndex(++convergenceCheckState);
144 };
145 } else {
146 getNextConvergenceCheckState = [&convergenceCheckState]() { ++convergenceCheckState; };
147 }
148 while (status == SolverStatus::InProgress) {
149 ++numIterations;
150 viOperator->applyInPlace(xy, offsets, backend);
151 if (checkConvergence<ValueType, RoundingTowardsInf>(xy, convergenceCheckState, getNextConvergenceCheckState, relative, precision)) {
153 } else if (iterationCallback) {
154 status = iterationCallback(IIData<ValueType>({xy.first, xy.second, status}));
155 }
156 }
157 return status;
158}
159
160template<typename ValueType, bool TrivialRowGrouping>
161SolverStatus IntervalIterationHelper<ValueType, TrivialRowGrouping>::II(std::pair<std::vector<ValueType>, std::vector<ValueType>>& xy,
162 std::pair<std::vector<ValueType>, std::vector<ValueType>> const& offsets,
163 uint64_t& numIterations, bool relative, ValueType const& precision,
164 std::optional<storm::OptimizationDirection> const& dir,
165 std::function<SolverStatus(IIData<ValueType> const&)> const& iterationCallback,
166 std::optional<storm::storage::BitVector> const& relevantValues) const {
167 if (!dir.has_value() || maximize(*dir)) {
168 return II<OptimizationDirection::Maximize>(xy, offsets, numIterations, relative, precision, iterationCallback, relevantValues);
169 } else {
170 return II<OptimizationDirection::Minimize>(xy, offsets, numIterations, relative, precision, iterationCallback, relevantValues);
171 }
172}
173
174template<typename ValueType, bool TrivialRowGrouping>
175SolverStatus IntervalIterationHelper<ValueType, TrivialRowGrouping>::roundII(std::pair<std::vector<ValueType>, std::vector<ValueType>>& xy,
176 std::pair<std::vector<ValueType>, std::vector<ValueType>> const& offsets,
177 uint64_t& numIterations, bool relative, ValueType const& precision,
178 ValueType const& gamma, [[maybe_unused]] ValueType const& delta,
179 std::optional<storm::OptimizationDirection> const& dir,
180 std::function<SolverStatus(IIData<ValueType> const&)> const& iterationCallback,
181 std::optional<storm::storage::BitVector> const& relevantValues) const {
182 if (storm::utility::isZero(gamma)) {
183 if (!dir.has_value() || maximize(*dir)) {
184 return II<OptimizationDirection::Maximize, decltype(offsets), false, true>(xy, offsets, numIterations, relative, precision, iterationCallback,
185 relevantValues);
186 } else {
187 return II<OptimizationDirection::Minimize, decltype(offsets), false, true>(xy, offsets, numIterations, relative, precision, iterationCallback,
188 relevantValues);
189 }
190 } else {
191 if (!dir.has_value() || maximize(*dir)) {
192 return II<OptimizationDirection::Maximize, decltype(offsets), true, true>(xy, offsets, numIterations, relative, precision, iterationCallback,
193 relevantValues, gamma);
194 } else {
195 return II<OptimizationDirection::Minimize, decltype(offsets), true, true>(xy, offsets, numIterations, relative, precision, iterationCallback,
196 relevantValues, gamma);
197 }
198 }
199}
200
201template<typename ValueType, bool TrivialRowGrouping>
202SolverStatus IntervalIterationHelper<ValueType, TrivialRowGrouping>::smoothII(std::pair<std::vector<ValueType>, std::vector<ValueType>>& xy,
203 std::pair<std::vector<ValueType>, std::vector<ValueType>> const& offsets,
204 uint64_t& numIterations, bool relative, ValueType const& precision,
205 ValueType const& gamma, [[maybe_unused]] ValueType const& delta,
206 std::optional<storm::OptimizationDirection> const& dir,
207 std::function<SolverStatus(IIData<ValueType> const&)> const& iterationCallback,
208 std::optional<storm::storage::BitVector> const& relevantValues) const {
209 if (!dir.has_value() || maximize(*dir)) {
210 return II<OptimizationDirection::Maximize, decltype(offsets), true>(xy, offsets, numIterations, relative, precision, iterationCallback, relevantValues,
211 gamma);
212 } else {
213 return II<OptimizationDirection::Minimize, decltype(offsets), true>(xy, offsets, numIterations, relative, precision, iterationCallback, relevantValues,
214 gamma);
215 }
216}
217
218template<typename ValueType, bool TrivialRowGrouping>
220 std::vector<ValueType>& operand, std::vector<ValueType> const& offsets, uint64_t& numIterations, bool relative, ValueType const& precision,
221 std::function<void(std::vector<ValueType>&)> const& prepareLowerBounds, std::function<void(std::vector<ValueType>&)> const& prepareUpperBounds,
222 std::optional<storm::OptimizationDirection> const& dir, std::function<SolverStatus(IIData<ValueType> const&)> const& iterationCallback,
223 std::optional<storm::storage::BitVector> const& relevantValues, storm::OptionalRef<SolutionBounds<ValueType>> solutionBounds) const {
224 // Create two vectors x and y using the given operand plus an auxiliary vector.
225 std::pair<std::vector<ValueType>, std::vector<ValueType>> xy;
226 auto& auxVector = viOperator->allocateAuxiliaryVector(operand.size());
227 xy.first.swap(operand);
228 xy.second.swap(auxVector);
229 prepareLowerBounds(xy.first);
230 prepareUpperBounds(xy.second);
231 auto doublePrec = precision + precision;
232 if constexpr (std::is_same_v<ValueType, double>) {
233 doublePrec -= precision * 1e-6; // be slightly more precise to avoid a good chunk of floating point issues
234 }
235 SolverStatus status;
236 if (!dir.has_value() || maximize(*dir)) {
237 status = II<OptimizationDirection::Maximize>(xy, offsets, numIterations, relative, doublePrec, iterationCallback, relevantValues);
238 } else {
239 status = II<OptimizationDirection::Minimize>(xy, offsets, numIterations, relative, doublePrec, iterationCallback, relevantValues);
240 }
241 if (solutionBounds.has_value()) {
242 // Interval iteration keeps xy.first below and xy.second above the solution in every iteration, so both
243 // sides are sound even if it was aborted. Read them out before they are collapsed into the estimate.
244 solutionBounds->lower = xy.first;
245 solutionBounds->upper = xy.second;
246 }
248 // get the average of lower- and upper result
250 xy.first, xy.second, xy.first, [&two](ValueType const& a, ValueType const& b) -> ValueType { return (a + b) / two; });
251 // Swap operand and aux vector back to original positions.
252 xy.first.swap(operand);
253 xy.second.swap(auxVector);
254 viOperator->freeAuxiliaryVector();
255 return status;
256}
257
258template<typename ValueType, bool TrivialRowGrouping>
259SolverStatus IntervalIterationHelper<ValueType, TrivialRowGrouping>::II(std::vector<ValueType>& operand, std::vector<ValueType> const& offsets, bool relative,
260 ValueType const& precision,
261 std::function<void(std::vector<ValueType>&)> const& prepareLowerBounds,
262 std::function<void(std::vector<ValueType>&)> const& prepareUpperBounds,
263 std::optional<storm::OptimizationDirection> const& dir,
264 std::function<SolverStatus(IIData<ValueType> const&)> const& iterationCallback,
265 std::optional<storm::storage::BitVector> const& relevantValues) const {
266 uint64_t numIterations = 0;
267 return II(operand, offsets, numIterations, relative, precision, prepareLowerBounds, prepareUpperBounds, dir, iterationCallback, relevantValues);
268}
269
274
275} // namespace storm::solver::helper
Helper class that optionally holds a reference to an object of type T.
Definition OptionalRef.h:48
void firstRow(std::pair< ValueType, ValueType > &&value, uint64_t rowGroup, uint64_t row)
void nextRow(std::pair< ValueType, ValueType > &&value, uint64_t rowGroup, uint64_t row)
void applyUpdate(ValueType &xCurr, ValueType &yCurr, uint64_t rowGroup)
SolverStatus II(std::pair< std::vector< ValueType >, std::vector< ValueType > > &xy, OffsetType const &offsets, uint64_t &numIterations, bool relative, ValueType const &precision, std::function< SolverStatus(IIData< ValueType > const &)> const &iterationCallback={}, std::optional< storm::storage::BitVector > const &relevantValues={}, ValueType const &gamma=storm::utility::zero< ValueType >()) const
SolverStatus roundII(std::pair< std::vector< ValueType >, std::vector< ValueType > > &xy, std::pair< std::vector< ValueType >, std::vector< ValueType > > const &offsets, uint64_t &numIterations, bool relative, ValueType const &precision, ValueType const &gamma, ValueType const &delta, std::optional< storm::OptimizationDirection > const &dir={}, std::function< SolverStatus(IIData< ValueType > const &)> const &iterationCallback={}, std::optional< storm::storage::BitVector > const &relevantValues={}) const
Variant of interval iteration for floating point numbers with directed rounding.
SolverStatus smoothII(std::pair< std::vector< ValueType >, std::vector< ValueType > > &xy, std::pair< std::vector< ValueType >, std::vector< ValueType > > const &offsets, uint64_t &numIterations, bool relative, ValueType const &precision, ValueType const &gamma, ValueType const &delta, std::optional< storm::OptimizationDirection > const &dir={}, std::function< SolverStatus(IIData< ValueType > const &)> const &iterationCallback={}, std::optional< storm::storage::BitVector > const &relevantValues={}) const
Smooth variant of interval iteration: Instead of taking over the bounds computed by the Bellman opera...
IntervalIterationHelper(std::shared_ptr< ValueIterationOperator< ValueType, TrivialRowGrouping > > viOperator)
This class represents the Value Iteration Operator (also known as Bellman operator).
Stores and manages an extremal (maximal or minimal) value.
Definition Extremum.h:17
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
bool checkConvergence(std::pair< std::vector< ValueType >, std::vector< ValueType > > const &xy, uint64_t &convergenceCheckState, std::function< void()> const &getNextConvergenceCheckState, bool relative, ValueType const &precision)
static ValueType diff(ValueType const &oldValue, ValueType const &newValue)
bool constexpr maximize(OptimizationDirection d)
OptimizationDirection constexpr invert(OptimizationDirection d)
void applyPointwise(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target, Operation f=Operation())
Applies the given operation pointwise on the two given vectors and writes the result to the third vec...
Definition vector.h:375
bool isZero(ValueType const &a)
Definition constants.cpp:44
ValueType zero()
Definition constants.cpp:26
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.
Sound lower and upper bounds on a solution vector.