Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
FlexibleSparseMatrix.cpp
Go to the documentation of this file.
2
6
10
11namespace storm {
12namespace storage {
13template<typename ValueType>
14FlexibleSparseMatrix<ValueType>::FlexibleSparseMatrix(index_type rows) : data(rows), columnCount(0), nonzeroEntryCount(0) {
15 // Intentionally left empty.
16}
17
18template<typename ValueType>
19FlexibleSparseMatrix<ValueType>::FlexibleSparseMatrix(storm::storage::SparseMatrix<ValueType> const& matrix, bool setAllValuesToOne, bool revertEquationSystem)
20 : data(matrix.getRowCount()),
21 columnCount(matrix.getColumnCount()),
22 nonzeroEntryCount(matrix.getNonzeroEntryCount()),
23 trivialRowGrouping(matrix.hasTrivialRowGrouping()) {
24 STORM_LOG_THROW(!revertEquationSystem || trivialRowGrouping, storm::exceptions::InvalidArgumentException, "Illegal option for creating flexible matrix.");
25
26 if (!trivialRowGrouping) {
27 rowGroupIndices = matrix.getRowGroupIndices();
28 }
29 for (index_type rowIndex = 0; rowIndex < matrix.getRowCount(); ++rowIndex) {
30 typename storm::storage::SparseMatrix<ValueType>::const_rows row = matrix.getRow(rowIndex);
31 reserveInRow(rowIndex, row.getNumberOfEntries());
32 for (auto const& element : row) {
33 // If the probability is zero, we skip this entry.
34 if (storm::utility::isZero(element.getValue())) {
35 if (revertEquationSystem && rowIndex == element.getColumn()) {
36 getRow(rowIndex).emplace_back(element.getColumn(), storm::utility::one<ValueType>());
37 } else {
38 continue;
39 }
40 }
41 if (setAllValuesToOne) {
42 if (revertEquationSystem && element.getColumn() == rowIndex && storm::utility::isOne(element.getValue())) {
43 continue;
44 } else {
45 getRow(rowIndex).emplace_back(element.getColumn(), storm::utility::one<ValueType>());
46 }
47 } else {
48 if (revertEquationSystem) {
49 if (element.getColumn() == rowIndex) {
50 if (storm::utility::isOne(element.getValue())) {
51 continue;
52 }
53 getRow(rowIndex).emplace_back(element.getColumn(), storm::utility::one<ValueType>() - element.getValue());
54 } else {
55 getRow(rowIndex).emplace_back(element.getColumn(), -element.getValue());
56 }
57 } else {
58 getRow(rowIndex).emplace_back(element);
59 }
60 }
61 }
62 }
63}
64
65template<typename ValueType>
67 this->data[row].reserve(numberOfElements);
68}
69
70template<typename ValueType>
74
75template<typename ValueType>
77 return this->data[index];
78}
79
80template<typename ValueType>
82 STORM_LOG_ASSERT(rowGroup < this->getRowGroupCount(), "Invalid rowGroup.");
83 STORM_LOG_ASSERT(offset < this->getRowGroupSize(rowGroup), "Invalid offset.");
84 return getRow(rowGroupIndices[rowGroup] + offset);
85}
86
87template<typename ValueType>
89 STORM_LOG_ASSERT(rowGroup < this->getRowGroupCount(), "Invalid rowGroup.");
90 STORM_LOG_ASSERT(offset < this->getRowGroupSize(rowGroup), "Invalid offset.");
91 return getRow(rowGroupIndices[rowGroup] + offset);
92}
93
94template<typename ValueType>
95std::vector<typename FlexibleSparseMatrix<ValueType>::index_type> const& FlexibleSparseMatrix<ValueType>::getRowGroupIndices() const {
96 return rowGroupIndices;
97}
98
99template<typename ValueType>
103
104template<typename ValueType>
108
109template<typename ValueType>
113
114template<typename ValueType>
116 return rowGroupIndices.size() - 1;
117}
118
119template<typename ValueType>
121 return rowGroupIndices[group + 1] - rowGroupIndices[group];
122}
123
124template<typename ValueType>
126 ValueType sum = storm::utility::zero<ValueType>();
127 for (auto const& element : getRow(row)) {
128 sum += element.getValue();
129 }
130 return sum;
131}
132
133template<typename ValueType>
135 this->nonzeroEntryCount = 0;
136 this->columnCount = 0;
137 for (auto const& row : this->data) {
138 for (auto const& element : row) {
139 STORM_LOG_ASSERT(!storm::utility::isZero(element.getValue()), "Entry is 0.");
140 ++this->nonzeroEntryCount;
141 this->columnCount = std::max(element.getColumn() + 1, this->columnCount);
142 }
143 }
144}
145
146template<typename ValueType>
148 for (auto const& row : this->data) {
149 if (!row.empty()) {
150 return false;
151 }
152 }
153 return true;
154}
155
156template<typename ValueType>
158 return trivialRowGrouping;
159}
160
161template<typename ValueType>
163 for (uint_fast64_t rowIndex = 0; rowIndex < this->data.size(); ++rowIndex) {
164 auto& row = this->data[rowIndex];
165 if (!rowConstraint.get(rowIndex)) {
166 row.clear();
167 row.shrink_to_fit();
168 continue;
169 }
170 row_type newRow;
171 for (auto const& element : row) {
172 if (columnConstraint.get(element.getColumn())) {
173 newRow.push_back(element);
174 }
175 }
176 row = std::move(newRow);
177 }
178}
179
180template<typename ValueType>
182 uint_fast64_t numEntries = 0;
183 for (auto const& row : this->data) {
184 numEntries += row.size();
185 }
186
187 storm::storage::SparseMatrixBuilder<ValueType> matrixBuilder(getRowCount(), getColumnCount(), numEntries, hasTrivialRowGrouping(),
188 hasTrivialRowGrouping() ? 0 : getRowGroupCount());
189 uint_fast64_t currRowIndex = 0;
190 auto rowGroupIndexIt = getRowGroupIndices().begin();
191 for (auto const& row : this->data) {
192 if (!hasTrivialRowGrouping()) {
193 while (currRowIndex >= *rowGroupIndexIt) {
194 matrixBuilder.newRowGroup(currRowIndex);
195 ++rowGroupIndexIt;
196 }
197 }
198 for (auto const& entry : row) {
199 matrixBuilder.addNextValue(currRowIndex, entry.getColumn(), entry.getValue());
200 }
201 ++currRowIndex;
202 }
203 // The matrix might end with one or more empty row groups
204 if (!hasTrivialRowGrouping()) {
205 while (currRowIndex >= *rowGroupIndexIt) {
206 matrixBuilder.newRowGroup(currRowIndex);
207 ++rowGroupIndexIt;
208 }
209 }
210 return matrixBuilder.build();
211}
212
213template<typename ValueType>
215 storm::storage::BitVector const& columnConstraint) {
216 uint_fast64_t numEntries = 0;
217 for (auto rowIndex : rowConstraint) {
218 auto const& row = data[rowIndex];
219 for (auto const& entry : row) {
220 if (columnConstraint.get(entry.getColumn())) {
221 ++numEntries;
222 }
223 }
224 }
225 uint_fast64_t numRowGroups = 0;
226 if (!hasTrivialRowGrouping()) {
227 auto lastRowGroupIndexIt = getRowGroupIndices().end() - 1;
228 auto rowGroupIndexIt = getRowGroupIndices().begin();
229 while (rowGroupIndexIt != lastRowGroupIndexIt) {
230 // Check whether the rowGroup will be nonempty
231 if (rowConstraint.getNextSetIndex(*rowGroupIndexIt) < *(++rowGroupIndexIt)) {
232 ++numRowGroups;
233 }
234 }
235 }
236
237 std::vector<uint_fast64_t> oldToNewColumnIndexMapping(getColumnCount(), getColumnCount());
238 uint_fast64_t newColumnIndex = 0;
239 for (auto oldColumnIndex : columnConstraint) {
240 oldToNewColumnIndexMapping[oldColumnIndex] = newColumnIndex++;
241 }
242
243 storm::storage::SparseMatrixBuilder<ValueType> matrixBuilder(rowConstraint.getNumberOfSetBits(), newColumnIndex, numEntries, true, !hasTrivialRowGrouping(),
244 numRowGroups);
245 uint_fast64_t currRowIndex = 0;
246 auto rowGroupIndexIt = getRowGroupIndices().begin();
247 for (auto oldRowIndex : rowConstraint) {
248 if (!hasTrivialRowGrouping() && oldRowIndex >= *rowGroupIndexIt) {
249 matrixBuilder.newRowGroup(currRowIndex);
250 // Skip empty row groups
251 do {
252 ++rowGroupIndexIt;
253 } while (oldRowIndex >= *rowGroupIndexIt);
254 }
255 auto const& row = data[oldRowIndex];
256 for (auto const& entry : row) {
257 if (columnConstraint.get(entry.getColumn())) {
258 matrixBuilder.addNextValue(currRowIndex, oldToNewColumnIndexMapping[entry.getColumn()], entry.getValue());
259 }
260 }
261 ++currRowIndex;
262 }
263 return matrixBuilder.build();
264}
265
266template<typename ValueType>
268 for (auto const& entry : this->getRow(state)) {
269 if (entry.getColumn() < state) {
270 continue;
271 } else if (entry.getColumn() > state) {
272 return false;
273 } else if (entry.getColumn() == state) {
274 return true;
275 }
276 }
277 return false;
278}
279
280template<typename ValueType>
281std::ostream& FlexibleSparseMatrix<ValueType>::printRow(std::ostream& out, index_type const& rowIndex) const {
282 index_type columnIndex = 0;
283 row_type row = this->getRow(rowIndex);
284 for (index_type column = 0; column < this->getColumnCount(); ++column) {
285 if (columnIndex < row.size() && row[columnIndex].getColumn() == column) {
286 // Insert entry
287 out << row[columnIndex].getValue() << "\t";
288 ++columnIndex;
289 } else {
290 // Insert zero
291 out << "0\t";
292 }
293 }
294 return out;
295}
296
297template<typename ValueType>
298std::ostream& operator<<(std::ostream& out, FlexibleSparseMatrix<ValueType> const& matrix) {
299 typedef typename FlexibleSparseMatrix<ValueType>::index_type FlexibleIndex;
300
301 // Print column numbers in header.
302 out << "\t\t";
303 for (FlexibleIndex i = 0; i < matrix.getColumnCount(); ++i) {
304 out << i << "\t";
305 }
306 out << '\n';
307
308 if (!matrix.hasTrivialRowGrouping()) {
309 // Iterate over all row groups
310 FlexibleIndex rowGroupCount = matrix.getRowGroupCount();
311 for (FlexibleIndex rowGroup = 0; rowGroup < rowGroupCount; ++rowGroup) {
312 out << "\t---- group " << rowGroup << "/" << (rowGroupCount - 1) << " ---- \n";
313 FlexibleIndex endRow = matrix.rowGroupIndices[rowGroup + 1];
314 // Iterate over all rows.
315 for (FlexibleIndex row = matrix.rowGroupIndices[rowGroup]; row < endRow; ++row) {
316 // Print the actual row.
317 out << rowGroup << "\t(\t";
318 matrix.printRow(out, row);
319 out << "\t)\t" << rowGroup << '\n';
320 }
321 }
322
323 } else {
324 // Iterate over all rows
325 for (FlexibleIndex row = 0; row < matrix.getRowCount(); ++row) {
326 // Print the actual row.
327 out << row << "\t(\t";
328 matrix.printRow(out, row);
329 out << "\t)\t" << row << '\n';
330 }
331 }
332
333 // Print column numbers in footer.
334 out << "\t\t";
335 for (FlexibleIndex i = 0; i < matrix.getColumnCount(); ++i) {
336 out << i << "\t";
337 }
338 out << '\n';
339 return out;
340}
341
342// Explicitly instantiate the matrix.
343template class FlexibleSparseMatrix<double>;
344template std::ostream& operator<<(std::ostream& out, FlexibleSparseMatrix<double> const& matrix);
345
346#ifdef STORM_HAVE_CARL
348template std::ostream& operator<<(std::ostream& out, FlexibleSparseMatrix<storm::RationalNumber> const& matrix);
349
351template std::ostream& operator<<(std::ostream& out, FlexibleSparseMatrix<storm::RationalFunction> const& matrix);
352#endif
353
354} // namespace storage
355} // namespace storm
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
uint_fast64_t getNextSetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
uint_fast64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
bool get(uint_fast64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
The flexible sparse matrix is used during state elimination.
void reserveInRow(index_type row, index_type numberOfElements)
Reserves space for elements in row.
index_type getRowGroupSize(index_type group) const
Returns the size of the given row group.
index_type getColumnCount() const
Returns the number of columns of the matrix.
value_type getRowSum(index_type row) const
Computes the sum of the entries in a given row.
void updateDimensions()
Recomputes the number of columns and the number of non-zero entries.
index_type getNonzeroEntryCount() const
Returns the cached number of nonzero entries in the matrix.
std::ostream & printRow(std::ostream &out, index_type const &rowIndex) const
Print row.
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
storm::storage::SparseMatrix< ValueType > createSparseMatrix()
Creates a sparse matrix from the flexible sparse matrix.
row_type & getRow(index_type)
Returns an object representing the given row.
bool empty() const
Checks if the matrix has no elements.
void filterEntries(storm::storage::BitVector const &rowConstraint, storm::storage::BitVector const &columnConstraint)
Erases all entries whose row and column does not satisfy the given rowConstraint and the given column...
index_type getRowCount() const
Returns the number of rows of the matrix.
FlexibleSparseMatrix()=default
Constructs an empty flexible sparse matrix.
bool hasTrivialRowGrouping() const
Retrieves whether the matrix has a (possibly) trivial row grouping.
bool rowHasDiagonalElement(storm::storage::sparse::state_type state)
Checks whether the given state has a self-loop with an arbitrary probability in the probability matri...
index_type getRowGroupCount() const
Returns the number of row groups in the matrix.
std::vector< storm::storage::MatrixEntry< index_type, value_type > > row_type
This class represents a number of consecutive rows of the matrix.
index_type getNumberOfEntries() const
Retrieves the number of entries in the rows.
A class that can be used to build a sparse matrix by adding value by value.
void addNextValue(index_type row, index_type column, value_type const &value)
Sets the matrix entry at the given row and column to the given value.
void newRowGroup(index_type startingRow)
Starts a new row group in the matrix.
SparseMatrix< value_type > build(index_type overriddenRowCount=0, index_type overriddenColumnCount=0, index_type overriddenRowGroupCount=0)
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.
const_iterator end(index_type row) const
Retrieves an iterator that points past the end of the given row.
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.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
std::ostream & operator<<(std::ostream &out, ParameterRegion< ParametricType > const &region)
bool isOne(ValueType const &a)
Definition constants.cpp:36
bool isZero(ValueType const &a)
Definition constants.cpp:41
LabParser.cpp.
Definition cli.cpp:18