Browse Source

(Hopefully) fixed row iterator

Former-commit-id: 72eacb4c7a
tempestpy_adaptions
Mavo 9 years ago
committed by Matthias Volk
parent
commit
c2f3ebbd63
  1. 4
      stormpy/lib/stormpy/storage/action.py
  2. 26
      stormpy/src/storage/matrix.cpp
  3. 17
      stormpy/tests/storage/test_model_iterators.py

4
stormpy/lib/stormpy/storage/action.py

@ -31,4 +31,6 @@ class Action:
""" Get transitions associated with the action
:return List of tranistions
"""
return self.model.transition_matrix().get_row(self.row + self.row_group_start)
row = self.row_group_start + self.row
#return self.model.transition_matrix().get_row(self.row_group_start + self.row)
return self.model.transition_matrix().row_iter(row, row)

26
stormpy/src/storage/matrix.cpp

@ -3,7 +3,9 @@
#include "src/storage/SparseMatrix.h"
typedef storm::storage::SparseMatrix<double>::index_type entry_index;
typedef unsigned int row_index;
typedef storm::storage::SparseMatrix<storm::RationalFunction>::index_type parametric_entry_index;
typedef unsigned int parametric_row_index;
void define_sparse_matrix(py::module& m) {
@ -20,16 +22,16 @@ void define_sparse_matrix(py::module& m) {
.def("column", &storm::storage::MatrixEntry<entry_index, double>::getColumn, "Column")
;
py::class_<storm::storage::MatrixEntry<entry_index, storm::RationalFunction>>(m, "ParametricSparseMatrixEntry", "Entry of parametric sparse matrix")
.def("__str__", [](storm::storage::MatrixEntry<entry_index, storm::RationalFunction> const& entry) {
py::class_<storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>>(m, "ParametricSparseMatrixEntry", "Entry of parametric sparse matrix")
.def("__str__", [](storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction> const& entry) {
std::stringstream stream;
stream << entry;
return stream.str();
})
//def_property threw "pointer being freed not allocated" after exiting
.def("value", &storm::storage::MatrixEntry<entry_index, storm::RationalFunction>::getValue, "Value")
.def("set_value", &storm::storage::MatrixEntry<entry_index, storm::RationalFunction>::setValue, py::arg("value"), "Set value")
.def("column", &storm::storage::MatrixEntry<entry_index, storm::RationalFunction>::getColumn, "Column")
.def("value", &storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>::getValue, "Value")
.def("set_value", &storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>::setValue, py::arg("value"), "Set value")
.def("column", &storm::storage::MatrixEntry<parametric_entry_index, storm::RationalFunction>::getColumn, "Column")
;
// SparseMatrix
@ -60,6 +62,10 @@ void define_sparse_matrix(py::module& m) {
}
return stream.str();
}, py::arg("row"), "Print row")
// Entry_index lead to problems
.def("row_iter", [](storm::storage::SparseMatrix<double>& matrix, row_index start, row_index end) {
return py::make_iterator(matrix.begin(start), matrix.end(end));
}, py::keep_alive<0, 1>() /* keep object alive while iterator exists */, py::arg("row_start"), py::arg("row_end"), "Get iterator from start to end")
;
py::class_<storm::storage::SparseMatrix<storm::RationalFunction>>(m, "ParametricSparseMatrix", "Parametric sparse matrix")
@ -75,13 +81,13 @@ void define_sparse_matrix(py::module& m) {
.def("nr_columns", &storm::storage::SparseMatrix<storm::RationalFunction>::getColumnCount, "Number of columns")
.def("nr_entries", &storm::storage::SparseMatrix<storm::RationalFunction>::getEntryCount, "Number of non-zero entries")
.def("_row_group_indices", &storm::storage::SparseMatrix<storm::RationalFunction>::getRowGroupIndices, "Get starting rows of row groups")
.def("get_row", [](storm::storage::SparseMatrix<storm::RationalFunction>& matrix, entry_index row) {
.def("get_row", [](storm::storage::SparseMatrix<storm::RationalFunction>& matrix, parametric_entry_index row) {
return matrix.getRows(row, row+1);
}, py::return_value_policy::reference, py::keep_alive<1, 0>(), py::arg("row"), "Get row")
.def("get_rows", [](storm::storage::SparseMatrix<storm::RationalFunction>& matrix, entry_index start, entry_index end) {
.def("get_rows", [](storm::storage::SparseMatrix<storm::RationalFunction>& matrix, parametric_entry_index start, parametric_entry_index end) {
return matrix.getRows(start, end);
}, py::return_value_policy::reference, py::keep_alive<1, 0>(), py::arg("row_start"), py::arg("row_end"), "Get rows from start to end")
.def("print_row", [](storm::storage::SparseMatrix<storm::RationalFunction> const& matrix, entry_index row) {
.def("print_row", [](storm::storage::SparseMatrix<storm::RationalFunction> const& matrix, parametric_entry_index row) {
std::stringstream stream;
auto rows = matrix.getRows(row, row+1);
for (auto transition : rows) {
@ -89,6 +95,10 @@ void define_sparse_matrix(py::module& m) {
}
return stream.str();
}, py::arg("row"), "Print row")
// Entry_index lead to problems
.def("row_iter", [](storm::storage::SparseMatrix<storm::RationalFunction>& matrix, parametric_row_index start, parametric_row_index end) {
return py::make_iterator(matrix.begin(start), matrix.end(end));
}, py::keep_alive<0, 1>() /* keep object alive while iterator exists */, py::arg("row_start"), py::arg("row_end"), "Get iterator from start to end")
;
// Rows

17
stormpy/tests/storage/test_model_iterators.py

@ -62,3 +62,20 @@ class TestModelIterators:
for transition in action.transitions():
assert transition.value() == 0.5 or transition.value() == 1
assert i == 1 or i == 2
def test_row_iterator(self):
transitions_orig = [(0, 0, 0), (0, 1, 0.5), (0, 2, 0.5), (1, 1, 0), (1, 3, 0.5), (1, 4, 0.5),
(2, 2, 0), (2, 5, 0.5), (2, 6, 0.5), (3, 1, 0.5), (3, 3, 0), (3, 7, 0.5),
(4, 4, 0), (4, 8, 0.5), (4, 9, 0.5), (5, 5, 0), (5, 10, 0.5), (5, 11, 0.5),
(6, 2, 0.5), (6, 6, 0), (6, 12, 0.5), (7, 7, 1), (8, 8, 1),
(9, 9, 1), (10, 10, 1), (11, 11, 1), (12, 12, 1)
]
model = stormpy.parse_explicit_model("../examples/dtmc/die/die.tra", "../examples/dtmc/die/die.lab")
i = 0
for state in stormpy.state.State(0, model):
for transition in model.transition_matrix().row_iter(state.id, state.id):
transition_orig = transitions_orig[i]
i += 1
assert state.id == transition_orig[0]
assert transition.column() == transition_orig[1]
assert transition.value() == transition_orig[2]
Loading…
Cancel
Save