10template<
typename ValueType,
bool TrivialRowGrouping,
typename SolutionType>
11template<
bool Backward>
13 std::vector<IndexType>
const* rowGroupIndices) {
14 if constexpr (TrivialRowGrouping) {
16 STORM_LOG_ASSERT(rowGroupIndices ==
nullptr,
"Row groups given, but grouping is supposed to be trivial.");
17 this->rowGroupIndices =
nullptr;
19 if (rowGroupIndices) {
20 this->rowGroupIndices = rowGroupIndices;
25 this->backwards = Backward;
26 this->hasSkippedRows =
false;
29 matrixColumns.clear();
32 if constexpr (!TrivialRowGrouping) {
33 matrixColumns.push_back(StartOfRowGroupIndicator);
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());
42 matrixColumns.push_back(StartOfRowIndicator);
44 matrixColumns.back() = StartOfRowGroupIndicator;
47 matrixColumns.push_back(StartOfRowIndicator);
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());
53 matrixColumns.push_back(StartOfRowIndicator);
58template<
typename ValueType,
bool TrivialRowGrouping,
typename SolutionType>
60 std::vector<IndexType>
const* rowGroupIndices) {
61 setMatrix<false>(matrix, rowGroupIndices);
64template<
typename ValueType,
bool TrivialRowGrouping,
typename SolutionType>
66 std::vector<IndexType>
const* rowGroupIndices) {
67 setMatrix<true>(matrix, rowGroupIndices);
70template<
typename ValueType,
bool TrivialRowGrouping,
typename SolutionType>
72 for (
auto& c : matrixColumns) {
73 if (c >= StartOfRowIndicator) {
74 c &= StartOfRowGroupIndicator;
77 hasSkippedRows =
false;
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;
94 moveToEndOfRow(colIt);
95 }
else if ((*colIt & SkipNumEntriesMask) == 0) {
96 auto currColIt = colIt;
97 moveToEndOfRow(colIt);
98 *currColIt += std::distance(currColIt, colIt);
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.");
106 STORM_LOG_ASSERT(*colIt == StartOfRowGroupIndicator,
"VI Operator in invalid state.");
108 hasSkippedRows =
true;
111template<
typename ValueType,
bool TrivialRowGrouping,
typename SolutionType>
115 setIgnoredRows<true>(useLocalRowIndices, ignore);
117 setIgnoredRows<false>(useLocalRowIndices, ignore);
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;
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.");
133 auxiliaryVector.assign(size, *initialValue);
135 auxiliaryVector.resize(size);
137 auxiliaryVectorUsedExternally =
true;
138 return auxiliaryVector;
141template<
typename ValueType,
bool TrivialRowGrouping,
typename SolutionType>
143 auxiliaryVectorUsedExternally =
false;
146template<
typename ValueType,
bool TrivialRowGrouping,
typename SolutionType>
150 }
while (*matrixColumnIt < StartOfRowIndicator);
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;
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)) {
170 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator,
"Undexpected state of VI operator");
172 STORM_LOG_ASSERT(*matrixColumnIt < StartOfRowGroupIndicator,
"Undexpected state of VI operator");
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>;
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 unsetIgnoredRows()
Clears all ignored rows.
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.
storm::storage::sparse::state_type IndexType
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)