Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ValueIterationOperator.cpp
Go to the documentation of this file.
2
3#include <optional>
4
9
10namespace storm::solver::helper {
11
12template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
13template<bool Backward>
15 std::vector<IndexType> const* rowGroupIndices) {
16 if constexpr (TrivialRowGrouping) {
17 STORM_LOG_ASSERT(matrix.hasTrivialRowGrouping(), "Expected a matrix with trivial row grouping");
18 STORM_LOG_ASSERT(rowGroupIndices == nullptr, "Row groups given, but grouping is supposed to be trivial.");
19 this->rowGroupIndices = nullptr;
20 } else {
21 if (rowGroupIndices) {
22 this->rowGroupIndices = rowGroupIndices;
23 } else {
24 this->rowGroupIndices = &matrix.getRowGroupIndices();
25 }
26 }
27 this->backwards = Backward;
28 this->hasSkippedRows = false;
29 auto const numRows = matrix.getRowCount();
30 matrixValues.clear();
31 matrixColumns.clear();
32 matrixValues.reserve(matrix.getNonzeroEntryCount());
33 matrixColumns.reserve(matrix.getNonzeroEntryCount() + numRows + 1); // matrixColumns also contain indications for when a row(group) starts
34
35 // hasOnlyConstants is only used for Interval matrices, currently only populated for iMCs
36 if constexpr (std::is_same<ValueType, storm::Interval>::value) {
37 applyCache.hasOnlyConstants.clear();
38 applyCache.hasOnlyConstants.grow(matrix.getRowCount());
39 }
40
41 if constexpr (!TrivialRowGrouping) {
42 matrixColumns.push_back(StartOfRowGroupIndicator); // indicate start of first row(group)
43 for (auto groupIndex : indexRange<Backward>(0, this->rowGroupIndices->size() - 1)) {
44 STORM_LOG_ASSERT(this->rowGroupIndices->at(groupIndex) != this->rowGroupIndices->at(groupIndex + 1),
45 "There is an empty row group. This is not expected.");
46 for (auto rowIndex : indexRange<false>((*this->rowGroupIndices)[groupIndex], (*this->rowGroupIndices)[groupIndex + 1])) {
47 for (auto const& entry : matrix.getRow(rowIndex)) {
48 matrixValues.push_back(entry.getValue());
49 matrixColumns.push_back(entry.getColumn());
50 }
51 matrixColumns.push_back(StartOfRowIndicator); // Indicate start of next row
52 }
53 matrixColumns.back() = StartOfRowGroupIndicator; // This is the start of the next row group
54 }
55 } else {
56 if constexpr (std::is_same<ValueType, storm::Interval>::value) {
57 matrixColumns.push_back(StartOfRowIndicator); // Indicate start of first row
58 for (auto rowIndex : indexRange<Backward>(0, numRows)) {
59 bool hasOnlyConstants = true;
60 for (auto const& entry : matrix.getRow(rowIndex)) {
61 ValueType value = entry.getValue();
62 hasOnlyConstants &= value.upper() == value.lower();
63 matrixValues.push_back(value);
64 matrixColumns.push_back(entry.getColumn());
65 }
66 applyCache.hasOnlyConstants.set(rowIndex, hasOnlyConstants);
67 matrixColumns.push_back(StartOfRowIndicator); // Indicate start of next row
68 }
69 } else {
70 matrixColumns.push_back(StartOfRowIndicator); // Indicate start of first row
71 for (auto rowIndex : indexRange<Backward>(0, numRows)) {
72 for (auto const& entry : matrix.getRow(rowIndex)) {
73 matrixValues.push_back(entry.getValue());
74 matrixColumns.push_back(entry.getColumn());
75 }
76 matrixColumns.push_back(StartOfRowIndicator); // Indicate start of next row
77 }
78 }
79 }
80}
81
82template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
84 std::vector<IndexType> const* rowGroupIndices) {
85 setMatrix<false>(matrix, rowGroupIndices);
86}
87
88template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
90 std::vector<IndexType> const* rowGroupIndices) {
91 setMatrix<true>(matrix, rowGroupIndices);
92}
93
94template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
96 for (auto& c : matrixColumns) {
97 if (c >= StartOfRowIndicator) {
98 c &= StartOfRowGroupIndicator;
99 }
100 }
101 hasSkippedRows = false;
102}
103
104template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
105template<bool Backward>
107 std::function<bool(IndexType, IndexType)> const& ignore) {
108 STORM_LOG_ASSERT(!TrivialRowGrouping, "Tried to ignroe rows but the row grouping is trivial.");
109 auto colIt = matrixColumns.begin();
110 for (auto groupIndex : indexRange<Backward>(0, this->rowGroupIndices->size() - 1)) {
111 STORM_LOG_ASSERT(colIt != matrixColumns.end(), "VI Operator in invalid state.");
112 STORM_LOG_ASSERT(*colIt >= StartOfRowGroupIndicator, "VI Operator in invalid state.");
113 auto const rowIndexRange = useLocalRowIndices ? indexRange<false>(0ull, (*this->rowGroupIndices)[groupIndex + 1] - (*this->rowGroupIndices)[groupIndex])
114 : indexRange<false>((*this->rowGroupIndices)[groupIndex], (*this->rowGroupIndices)[groupIndex + 1]);
115 for (auto const rowIndex : rowIndexRange) {
116 if (!ignore(groupIndex, rowIndex)) {
117 *colIt &= StartOfRowGroupIndicator; // Clear number of skipped entries
118 moveToEndOfRow(colIt);
119 } else if ((*colIt & SkipNumEntriesMask) == 0) { // i.e. should ignore but is not already ignored
120 auto currColIt = colIt;
121 moveToEndOfRow(colIt);
122 *currColIt += std::distance(currColIt, colIt); // set number of skipped entries
123 }
125 !std::all_of(rowIndexRange.begin(), rowIndexRange.end(), [&ignore, &groupIndex](IndexType rowIndex) { return ignore(groupIndex, rowIndex); }),
126 "All rows in row group " << groupIndex << " are ignored.");
127 STORM_LOG_ASSERT(colIt != matrixColumns.end(), "VI Operator in invalid state.");
128 STORM_LOG_ASSERT(*colIt >= StartOfRowIndicator, "VI Operator in invalid state.");
129 }
130 STORM_LOG_ASSERT(*colIt == StartOfRowGroupIndicator, "VI Operator in invalid state.");
131 }
132 hasSkippedRows = true;
133}
134
135template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
137 std::function<bool(IndexType, IndexType)> const& ignore) {
138 if (backwards) {
139 setIgnoredRows<true>(useLocalRowIndices, ignore);
140 } else {
141 setIgnoredRows<false>(useLocalRowIndices, ignore);
142 }
143}
144
145template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
146std::vector<typename ValueIterationOperator<ValueType, TrivialRowGrouping, SolutionType>::IndexType> const&
148 STORM_LOG_ASSERT(!TrivialRowGrouping, "Tried to get row group indices for trivial row grouping");
149 return *rowGroupIndices;
150}
151
152template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
154 uint64_t size, std::optional<SolutionType> const& initialValue) {
155 STORM_LOG_ASSERT(!auxiliaryVectorUsedExternally, "Auxiliary vector already in use.");
156 if (initialValue) {
157 auxiliaryVector.assign(size, *initialValue);
158 } else {
159 auxiliaryVector.resize(size);
160 }
161 auxiliaryVectorUsedExternally = true;
162 return auxiliaryVector;
163}
164
165template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
169
170template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
171void ValueIterationOperator<ValueType, TrivialRowGrouping, SolutionType>::moveToEndOfRow(std::vector<IndexType>::iterator& matrixColumnIt) const {
172 do {
173 ++matrixColumnIt;
174 } while (*matrixColumnIt < StartOfRowIndicator);
175}
176
177template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
178bool ValueIterationOperator<ValueType, TrivialRowGrouping, SolutionType>::skipIgnoredRow(std::vector<IndexType>::const_iterator& matrixColumnIt,
179 typename std::vector<ValueType>::const_iterator& matrixValueIt) const {
180 if (IndexType entriesToSkip = (*matrixColumnIt & SkipNumEntriesMask)) {
181 matrixColumnIt += entriesToSkip;
182 matrixValueIt += entriesToSkip - 1;
183 return true;
184 }
185 return false;
186}
187
188template<typename ValueType, bool TrivialRowGrouping, typename SolutionType>
189uint64_t ValueIterationOperator<ValueType, TrivialRowGrouping, SolutionType>::skipMultipleIgnoredRows(
190 std::vector<IndexType>::const_iterator& matrixColumnIt, typename std::vector<ValueType>::const_iterator& matrixValueIt) const {
191 IndexType result{0ull};
192 while (skipIgnoredRow(matrixColumnIt, matrixValueIt)) {
193 ++result;
194 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator, "Undexpected state of VI operator");
195 // We (currently) don't use this past the end of a row group, so we may have this additional sanity check:
196 STORM_LOG_ASSERT(*matrixColumnIt < StartOfRowGroupIndicator, "Undexpected state of VI operator");
197 }
198 return result;
199}
200
201template class ValueIterationOperator<double, true>;
202template class ValueIterationOperator<double, false>;
203template class ValueIterationOperator<storm::RationalNumber, true>;
204template class ValueIterationOperator<storm::RationalNumber, false>;
205template class ValueIterationOperator<storm::Interval, true, double>;
206template class ValueIterationOperator<storm::Interval, false, double>;
207
208} // namespace storm::solver::helper
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.
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.
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.
const_rows getRow(index_type row) const
Returns an object representing the given row.
bool hasTrivialRowGrouping() const
Retrieves whether the matrix has a trivial row grouping.
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
index_type getRowCount() const
Returns the number of rows of the matrix.
index_type getNonzeroEntryCount() const
Returns the cached number of nonzero entries in the matrix.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11