Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ValueIterationOperator.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <optional>
4#include <utility>
5#include <vector>
6
7#include <boost/range/adaptor/reversed.hpp>
8#include <boost/range/irange.hpp>
9
13#include "storm/utility/vector.h" // TODO
14
15namespace storm {
16class Environment;
17
18namespace storage {
19template<typename T>
20class SparseMatrix;
21}
22
23namespace solver::helper {
24
33template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
35 public:
37
45 template<bool Backward = true>
46 void setMatrix(storm::storage::SparseMatrix<ValueType> const& matrix, std::vector<IndexType> const* rowGroupIndices = nullptr);
47
54 void setMatrixForwards(storm::storage::SparseMatrix<ValueType> const& matrix, std::vector<IndexType> const* rowGroupIndices = nullptr);
55
62 void setMatrixBackwards(storm::storage::SparseMatrix<ValueType> const& matrix, std::vector<IndexType> const* rowGroupIndices = nullptr);
63
93 template<typename OperandType, typename OffsetType, typename BackendType>
94 bool apply(OperandType const& operandIn, OperandType& operandOut, OffsetType const& offsets, BackendType& backend) const {
95 // We assume in the normal apply case that we can just maximize.
96 return applyRobust<OptimizationDirection::Maximize>(operandIn, operandOut, offsets, backend);
97 }
98
99 template<OptimizationDirection RobustDir, typename OperandType, typename OffsetType, typename BackendType>
100 bool applyRobust(OperandType const& operandIn, OperandType& operandOut, OffsetType const& offsets, BackendType& backend) const {
101 if (hasSkippedRows) {
102 if (backwards) {
103 return apply<OperandType, OffsetType, BackendType, true, true, RobustDir>(operandOut, operandIn, offsets, backend);
104 } else {
105 return apply<OperandType, OffsetType, BackendType, false, true, RobustDir>(operandOut, operandIn, offsets, backend);
106 }
107 } else {
108 if (backwards) {
109 return apply<OperandType, OffsetType, BackendType, true, false, RobustDir>(operandOut, operandIn, offsets, backend);
110 } else {
111 return apply<OperandType, OffsetType, BackendType, false, false, RobustDir>(operandOut, operandIn, offsets, backend);
112 }
113 }
114 }
115
119 template<typename OperandType, typename OffsetType, typename BackendType>
120 bool applyInPlace(OperandType& operand, OffsetType const& offsets, BackendType& backend) const {
121 return apply(operand, operand, offsets, backend);
122 }
123
124 template<OptimizationDirection RobustDir, typename OperandType, typename OffsetType, typename BackendType>
125 bool applyInPlaceRobust(OperandType& operand, OffsetType const& offsets, BackendType& backend) const {
126 return applyRobust<RobustDir>(operand, operand, offsets, backend);
127 }
128
136 void setIgnoredRows(bool useLocalRowIndices, std::function<bool(IndexType, IndexType)> const& ignore);
137
141 void unsetIgnoredRows();
142
146 std::vector<IndexType> const& getRowGroupIndices() const;
147
154 std::vector<SolutionType>& allocateAuxiliaryVector(uint64_t size, std::optional<SolutionType> const& initialValue = {});
155
159 void freeAuxiliaryVector();
160
161 private:
166 template<typename OperandType, typename OffsetType, typename BackendType, bool Backward, bool SkipIgnoredRows, OptimizationDirection RobustDirection>
167 bool apply(OperandType& operandOut, OperandType const& operandIn, OffsetType const& offsets, BackendType& backend) const {
168 STORM_LOG_ASSERT(getSize(operandIn) == getSize(operandOut), "Input and Output Operands have different sizes.");
169 auto const operandSize = getSize(operandIn);
170 STORM_LOG_ASSERT(TrivialRowGrouping || rowGroupIndices->size() == operandSize + 1, "Dimension mismatch");
171 backend.startNewIteration();
172 auto matrixValueIt = matrixValues.cbegin();
173 auto matrixColumnIt = matrixColumns.cbegin();
174 for (auto groupIndex : indexRange<Backward>(0, operandSize)) {
175 STORM_LOG_ASSERT(matrixColumnIt != matrixColumns.end(), "VI Operator in invalid state.");
176 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator, "VI Operator in invalid state.");
177 // STORM_LOG_ASSERT(matrixValueIt != matrixValues.end(), "VI Operator in invalid state.");
178 if constexpr (TrivialRowGrouping) {
179 backend.firstRow(applyRow<RobustDirection>(matrixColumnIt, matrixValueIt, operandIn, offsets, groupIndex), groupIndex, groupIndex);
180 } else {
181 IndexType rowIndex = (*rowGroupIndices)[groupIndex];
182 if constexpr (SkipIgnoredRows) {
183 rowIndex += skipMultipleIgnoredRows(matrixColumnIt, matrixValueIt);
184 }
185 backend.firstRow(applyRow<RobustDirection>(matrixColumnIt, matrixValueIt, operandIn, offsets, rowIndex), groupIndex, rowIndex);
186 while (*matrixColumnIt < StartOfRowGroupIndicator) {
187 ++rowIndex;
188 if (!SkipIgnoredRows || !skipIgnoredRow(matrixColumnIt, matrixValueIt)) {
189 backend.nextRow(applyRow<RobustDirection>(matrixColumnIt, matrixValueIt, operandIn, offsets, rowIndex), groupIndex, rowIndex);
190 }
191 }
192 }
193 if constexpr (isPair<OperandType>::value) {
194 backend.applyUpdate(operandOut.first[groupIndex], operandOut.second[groupIndex], groupIndex);
195 } else {
196 backend.applyUpdate(operandOut[groupIndex], groupIndex);
197 }
198 if (backend.abort()) {
199 return backend.converged();
200 }
201 }
202 STORM_LOG_ASSERT(matrixColumnIt + 1 == matrixColumns.cend(), "Unexpected position of matrix column iterator.");
203 STORM_LOG_ASSERT(matrixValueIt == matrixValues.cend(), "Unexpected position of matrix column iterator.");
204 backend.endOfIteration();
205 return backend.converged();
206 }
207
208 // Auxiliary methods to deal with various OperandTypes and OffsetTypes
209
210 template<typename OpT, typename OffT>
211 OpT initializeRowRes(std::vector<OpT> const&, std::vector<OffT> const& offsets, uint64_t offsetIndex) const {
212 return offsets[offsetIndex];
213 }
214
215 template<typename OpT1, typename OpT2, typename OffT>
216 std::pair<OpT1, OpT2> initializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>> const&, std::vector<OffT> const& offsets,
217 uint64_t offsetIndex) const {
218 return {offsets[offsetIndex], offsets[offsetIndex]};
219 }
220
221 template<typename OpT1, typename OpT2, typename OffT1, typename OffT2>
222 std::pair<OpT1, OpT2> initializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>> const&, std::pair<std::vector<OffT1> const*, OffT2> const& offsets,
223 uint64_t offsetIndex) const {
224 return {(*offsets.first)[offsetIndex], offsets.second};
225 }
226
227 template<OptimizationDirection RobustDirection, typename OpT, typename OffT>
228 OpT robustInitializeRowRes(std::vector<OpT> const&, std::vector<OffT> const& offsets, uint64_t offsetIndex) const {
229 return offsets[offsetIndex].upper();
230 }
231
232 template<OptimizationDirection RobustDirection, typename OpT1, typename OpT2, typename OffT>
233 std::pair<OpT1, OpT2> robustInitializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>> const&, std::vector<OffT> const& offsets,
234 uint64_t offsetIndex) const {
235 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Value Iteration is not implemented with pairs and interval-models.");
236
237 return {offsets[offsetIndex].upper(), offsets[offsetIndex].upper()};
238 }
239
240 template<OptimizationDirection RobustDirection, typename OpT1, typename OpT2, typename OffT1, typename OffT2>
241 std::pair<OpT1, OpT2> robustInitializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>> const&,
242 std::pair<std::vector<OffT1> const*, OffT2> const& offsets, uint64_t offsetIndex) const {
243 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Value Iteration is not implemented with pairs and interval-models.");
244
245 return {(*offsets.first)[offsetIndex], offsets.second};
246 }
247
251 template<OptimizationDirection RobustDirection, typename OperandType, typename OffsetType>
252 auto applyRow(std::vector<IndexType>::const_iterator& matrixColumnIt, typename std::vector<ValueType>::const_iterator& matrixValueIt,
253 OperandType const& operand, OffsetType const& offsets, uint64_t offsetIndex) const {
254 if constexpr (std::is_same_v<ValueType, storm::Interval>) {
255 return applyRowRobust<RobustDirection>(matrixColumnIt, matrixValueIt, operand, offsets, offsetIndex);
256 } else {
257 return applyRowStandard(matrixColumnIt, matrixValueIt, operand, offsets, offsetIndex);
258 }
259 }
260
261 template<typename OperandType, typename OffsetType>
262 auto applyRowStandard(std::vector<IndexType>::const_iterator& matrixColumnIt, typename std::vector<ValueType>::const_iterator& matrixValueIt,
263 OperandType const& operand, OffsetType const& offsets, uint64_t offsetIndex) const {
264 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator, "VI Operator in invalid state.");
265 auto result{initializeRowRes(operand, offsets, offsetIndex)};
266 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt) {
267 if constexpr (isPair<OperandType>::value) {
268 result.first += operand.first[*matrixColumnIt] * (*matrixValueIt);
269 result.second += operand.second[*matrixColumnIt] * (*matrixValueIt);
270 } else {
271 result += operand[*matrixColumnIt] * (*matrixValueIt);
272 }
273 }
274 return result;
275 }
276
277 // Aux function for applyRowRobust
278 template<OptimizationDirection RobustDirection>
279 struct AuxCompare {
280 bool operator()(const std::pair<SolutionType, SolutionType>& a, const std::pair<SolutionType, SolutionType>& b) const {
281 if constexpr (RobustDirection == OptimizationDirection::Maximize) {
282 return a.first > b.first;
283 } else {
284 return a.first < b.first;
285 }
286 }
287 };
288
289 template<OptimizationDirection RobustDirection, typename OperandType, typename OffsetType>
290 auto applyRowRobust(std::vector<IndexType>::const_iterator& matrixColumnIt, typename std::vector<ValueType>::const_iterator& matrixValueIt,
291 OperandType const& operand, OffsetType const& offsets, uint64_t offsetIndex) const {
292 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator, "VI Operator in invalid state.");
293 auto result{robustInitializeRowRes<RobustDirection>(operand, offsets, offsetIndex)};
294 AuxCompare<RobustDirection> compare;
295 applyCache.robustOrder.clear();
296
297 SolutionType remainingValue{storm::utility::one<SolutionType>()};
298 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt) {
299 auto const lower = matrixValueIt->lower();
300 if constexpr (isPair<OperandType>::value) {
301 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Value Iteration is not implemented with pairs and interval-models.");
302 // Notice the unclear semantics here in terms of how to order things.
303 } else {
304 result += operand[*matrixColumnIt] * lower;
305 }
306 remainingValue -= lower;
307 auto const diameter = matrixValueIt->upper() - lower;
308 if (!storm::utility::isZero(diameter)) {
309 applyCache.robustOrder.emplace_back(operand[*matrixColumnIt], diameter);
310 }
311 }
312 if (storm::utility::isZero(remainingValue) || storm::utility::isOne(remainingValue)) {
313 return result;
314 }
315
316 std::sort(applyCache.robustOrder.begin(), applyCache.robustOrder.end(), compare);
317
318 for (auto const& pair : applyCache.robustOrder) {
319 auto availableMass = std::min(pair.second, remainingValue);
320 result += availableMass * pair.first;
321 remainingValue -= availableMass;
322 if (storm::utility::isZero(remainingValue)) {
323 return result;
324 }
325 }
326 STORM_LOG_ASSERT(storm::utility::isAlmostZero(remainingValue), "Remaining value should be zero (all prob mass taken) but is " << remainingValue);
327 return result;
328 }
329
330 // Auxiliary helpers used for metaprogramming
331 template<bool Backward>
332 auto indexRange(IndexType start, IndexType end) const {
333 if constexpr (Backward) {
334 return boost::adaptors::reverse(boost::irange(start, end));
335 } else {
336 return boost::irange(start, end);
337 }
338 }
339
340 template<typename T>
341 uint64_t getSize(std::vector<T> const& vec) const {
342 return vec.size();
343 }
344
345 template<typename T1, typename T2>
346 uint64_t getSize(std::pair<T1, T2> const& pairOfVec) const {
347 return pairOfVec.first.size();
348 }
349
350 template<typename>
351 struct isPair : std::false_type {};
352
353 template<typename T1, typename T2>
354 struct isPair<std::pair<T1, T2>> : std::true_type {};
355
359 template<bool Backward = true>
360 void setIgnoredRows(bool useLocalRowIndices, std::function<bool(IndexType, IndexType)> const& ignore);
361
365 void moveToEndOfRow(std::vector<IndexType>::iterator& matrixColumnIt) const;
366
370 bool skipIgnoredRow(std::vector<IndexType>::const_iterator& matrixColumnIt, typename std::vector<ValueType>::const_iterator& matrixValueIt) const;
371
375 uint64_t skipMultipleIgnoredRows(std::vector<IndexType>::const_iterator& matrixColumnIt,
376 typename std::vector<ValueType>::const_iterator& matrixValueIt) const;
377
381 std::vector<ValueType> matrixValues;
382
387 std::vector<IndexType> matrixColumns;
388
392 std::vector<IndexType> const* rowGroupIndices;
393
397 bool backwards{true};
398
402 bool hasSkippedRows{false};
403
407 std::vector<SolutionType> auxiliaryVector;
408
412 bool auxiliaryVectorUsedExternally{false};
413
414 // Due to a GCC bug we have to add this dummy template type here
415 // https://stackoverflow.com/questions/49707184/explicit-specialization-in-non-namespace-scope-does-not-compile-in-gcc
416 template<typename ApplyValueType, typename Dummy>
417 struct ApplyCache {};
418
419 template<typename Dummy>
420 struct ApplyCache<storm::Interval, Dummy> {
421 mutable std::vector<std::pair<SolutionType, SolutionType>> robustOrder;
422 };
423
427 ApplyCache<ValueType, int> applyCache;
428
432 IndexType const StartOfRowIndicator = 1ull << 63; // 10000..0
433
437 IndexType const StartOfRowGroupIndicator = StartOfRowIndicator + (1ull << 62); // 11000..0
438
442 IndexType const SkipNumEntriesMask = ~StartOfRowGroupIndicator; // 00111..1
443};
444
445} // namespace solver::helper
446} // namespace storm
This class represents the Value Iteration Operator (also known as Bellman operator).
std::vector< SolutionType > & allocateAuxiliaryVector(uint64_t size, std::optional< SolutionType > const &initialValue={})
Allocates additional storage that can be used e.g.
void setIgnoredRows(bool useLocalRowIndices, std::function< bool(IndexType, IndexType)> const &ignore)
Sets rows that will be skipped when applying the operator.
bool applyRobust(OperandType const &operandIn, OperandType &operandOut, OffsetType const &offsets, BackendType &backend) const
std::vector< IndexType > const & getRowGroupIndices() const
void setMatrix(storm::storage::SparseMatrix< ValueType > const &matrix, std::vector< IndexType > const *rowGroupIndices=nullptr)
Initializes this operator with the given data.
void freeAuxiliaryVector()
Clears the auxiliary vector, invalidating any references to it.
bool applyInPlaceRobust(OperandType &operand, OffsetType const &offsets, BackendType &backend) const
bool apply(OperandType const &operandIn, OperandType &operandOut, OffsetType const &offsets, BackendType &backend) const
Applies the operator with the given operands, offsets, and backend.
bool applyInPlace(OperandType &operand, OffsetType const &offsets, BackendType &backend) const
Same as apply but with operandOut==operandIn.
void setMatrixForwards(storm::storage::SparseMatrix< ValueType > const &matrix, std::vector< IndexType > const *rowGroupIndices=nullptr)
Initializes this operator with the given data for forward iterations (starting with the smallest row ...
void setMatrixBackwards(storm::storage::SparseMatrix< ValueType > const &matrix, std::vector< IndexType > const *rowGroupIndices=nullptr)
Initializes this operator with the given data for backward iterations (starting with the largest row ...
A class that holds a possibly non-square matrix in the compressed row storage format.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
bool isOne(ValueType const &a)
Definition constants.cpp:36
bool isAlmostZero(ValueType const &a)
Definition constants.cpp:56
bool isZero(ValueType const &a)
Definition constants.cpp:41
LabParser.cpp.
Definition cli.cpp:18
carl::Interval< double > Interval