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