diff --git a/stormpy/lib/stormpy/storage/state.py b/stormpy/lib/stormpy/storage/state.py index 1faa5fc88..1c31f1f1a 100644 --- a/stormpy/lib/stormpy/storage/state.py +++ b/stormpy/lib/stormpy/storage/state.py @@ -28,7 +28,7 @@ class State: """ Get actions associated with the state :return List of actions """ - row_group_indices = self.model.transition_matrix().row_group_indices() + row_group_indices = self.model.transition_matrix()._row_group_indices() start = row_group_indices[self.id] end = row_group_indices[self.id+1] return stormpy.action.Action(start, end, 0, self.model) diff --git a/stormpy/src/storage/matrix.cpp b/stormpy/src/storage/matrix.cpp index 79f47633d..526801232 100644 --- a/stormpy/src/storage/matrix.cpp +++ b/stormpy/src/storage/matrix.cpp @@ -6,6 +6,7 @@ typedef storm::storage::SparseMatrix::index_type entry_index; void define_sparse_matrix(py::module& m) { + // MatrixEntry py::class_>(m, "SparseMatrixEntry", "Entry of sparse matrix") .def("__str__", [](storm::storage::MatrixEntry const& entry) { std::stringstream stream; @@ -13,8 +14,8 @@ void define_sparse_matrix(py::module& m) { return stream.str(); }) //def_property threw "pointer being freed not allocated" after exiting - .def("val", &storm::storage::MatrixEntry::getValue, "Value") - .def("set_val", &storm::storage::MatrixEntry::setValue, "Set value") + .def("value", &storm::storage::MatrixEntry::getValue, "Value") + .def("set_value", &storm::storage::MatrixEntry::setValue, py::arg("value"), "Set value") .def("column", &storm::storage::MatrixEntry::getColumn, "Column") ; @@ -30,13 +31,13 @@ void define_sparse_matrix(py::module& m) { .def("nr_rows", &storm::storage::SparseMatrix::getRowCount, "Number of rows") .def("nr_columns", &storm::storage::SparseMatrix::getColumnCount, "Number of columns") .def("nr_entries", &storm::storage::SparseMatrix::getEntryCount, "Number of non-zero entries") - .def("row_group_indices", &storm::storage::SparseMatrix::getRowGroupIndices, "Number of non-zero entries") + .def("_row_group_indices", &storm::storage::SparseMatrix::getRowGroupIndices, "Get starting rows of row groups") .def("get_row", [](storm::storage::SparseMatrix& matrix, entry_index row) { return matrix.getRows(row, row+1); - }, py::keep_alive<0, 1>() /* keep_alive seems to avoid problem with wrong values */, "Get rows from start to end") + }, py::return_value_policy::reference, py::keep_alive<1, 0>(), py::arg("row"), "Get row") .def("get_rows", [](storm::storage::SparseMatrix& matrix, entry_index start, entry_index end) { return matrix.getRows(start, end); - }, "Get rows from start to 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 const& matrix, entry_index row) { std::stringstream stream; auto rows = matrix.getRows(row, row+1); @@ -47,6 +48,7 @@ void define_sparse_matrix(py::module& m) { }) ; + // Rows py::class_::rows>(m, "SparseMatrixRows", "Set of rows in a sparse matrix") .def("__iter__", [](storm::storage::SparseMatrix::rows& rows) { return py::make_iterator(rows.begin(), rows.end()); diff --git a/stormpy/src/storage/model.cpp b/stormpy/src/storage/model.cpp index 04b1eed78..7932dc2d1 100644 --- a/stormpy/src/storage/model.cpp +++ b/stormpy/src/storage/model.cpp @@ -17,7 +17,8 @@ std::vector getInitialStates(storm::models:: } // Thin wrapper for getting transition matrix -storm::storage::SparseMatrix& getTransitionMatrix(storm::models::sparse::Model& model) { +template +storm::storage::SparseMatrix& getTransitionMatrix(storm::models::sparse::Model& model) { return model.getTransitionMatrix(); } @@ -48,20 +49,21 @@ void define_model(py::module& m) { // Models py::class_, std::shared_ptr>>(m, "SparseModel", "A probabilistic model where transitions are represented by doubles and saved in a sparse matrix", py::base()) - .def("labels", [](storm::models::sparse::Model const& model) { + .def("labels", [](storm::models::sparse::Model& model) { return model.getStateLabeling().getLabels(); }, "Get labels") .def("labels_state", &storm::models::sparse::Model::getLabelsOfState, "Get labels") .def("initial_states", &getInitialStates, "Get initial states") - .def("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Get transition matrix") + .def("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Get transition matrix") ; py::class_, std::shared_ptr>>(m, "SparseDtmc", "DTMC in sparse representation", py::base>()) ; py::class_, std::shared_ptr>>(m, "SparseMdp", "MDP in sparse representation", py::base>()) ; + py::class_, std::shared_ptr>>(m, "SparseParametricModel", "A probabilistic model where transitions are represented by rational functions and saved in a sparse matrix", py::base()) .def("collect_probability_parameters", &storm::models::sparse::getProbabilityParameters, "Collect parameters") - .def("labels", [](storm::models::sparse::Model const& model) { + .def("labels", [](storm::models::sparse::Model& model) { return model.getStateLabeling().getLabels(); }, "Get labels") .def("labels_state", &storm::models::sparse::Model::getLabelsOfState, "Get labels") diff --git a/stormpy/tests/storage/test_matrix.py b/stormpy/tests/storage/test_matrix.py index 23beb8ece..0f02f3193 100644 --- a/stormpy/tests/storage/test_matrix.py +++ b/stormpy/tests/storage/test_matrix.py @@ -9,20 +9,20 @@ class TestMatrix: assert matrix.nr_columns() == model.nr_states() assert matrix.nr_entries() == 27 #model.nr_transitions() for e in matrix: - assert e.val() == 0.5 or e.val() == 0 or (e.val() == 1 and e.column() > 6) + assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column() > 6) def test_change_sparse_matrix(self): model = stormpy.parse_explicit_model("../examples/dtmc/die/die.tra", "../examples/dtmc/die/die.lab") matrix = model.transition_matrix() for e in matrix: - assert e.val() == 0.5 or e.val() == 0 or e.val() == 1 + assert e.value() == 0.5 or e.value() == 0 or e.value() == 1 i = 0 for e in matrix: - e.set_val(i) + e.set_value(i) i += 0.1 i = 0 for e in matrix: - assert e.val() == i + assert e.value() == i i += 0.1 def test_change_sparse_matrix_modelchecking(self): @@ -31,7 +31,7 @@ class TestMatrix: matrix = model.transition_matrix() # Check matrix for e in matrix: - assert e.val() == 0.5 or e.val() == 0 or e.val() == 1 + assert e.value() == 0.5 or e.value() == 0 or e.value() == 1 # First model checking formulas = stormpy.parse_formulas("P=? [ F \"one\" ]") result = stormpy.model_checking(model, formulas[0]) @@ -40,14 +40,14 @@ class TestMatrix: # Change probabilities i = 0 for e in matrix: - if e.val() == 0.5: + if e.value() == 0.5: if i % 2 == 0: - e.set_val(0.3) + e.set_value(0.3) else: - e.set_val(0.7) + e.set_value(0.7) i += 1 for e in matrix: - assert e.val() == 0.3 or e.val() == 0.7 or e.val() == 1 or e.val() == 0 + assert e.value() == 0.3 or e.value() == 0.7 or e.value() == 1 or e.value() == 0 # Second model checking result = stormpy.model_checking(model, formulas[0]) assert result == 0.06923076923076932 @@ -56,10 +56,10 @@ class TestMatrix: for state in stormpy.state.State(0, model): for action in state.actions(): for transition in action.transitions(): - if transition.val() == 0.3: - transition.set_val(0.8) - elif transition.val() == 0.7: - transition.set_val(0.2) + if transition.value() == 0.3: + transition.set_value(0.8) + elif transition.value() == 0.7: + transition.set_value(0.2) # Third model checking result = stormpy.model_checking(model, formulas[0]) assert result == 0.3555555555555556 or result == 0.3555555555555557 diff --git a/stormpy/tests/storage/test_model_iterators.py b/stormpy/tests/storage/test_model_iterators.py index 9b090b9bb..2705aa3e1 100644 --- a/stormpy/tests/storage/test_model_iterators.py +++ b/stormpy/tests/storage/test_model_iterators.py @@ -50,7 +50,7 @@ class TestModelIterators: i += 1 assert state.id == transition_orig[0] assert transition.column() == transition_orig[1] - assert transition.val() == transition_orig[2] + assert transition.value() == transition_orig[2] def test_transitions_mdp(self): model = stormpy.parse_explicit_model("../examples/mdp/two_dice/two_dice.tra", "../examples/mdp/two_dice/two_dice.lab") @@ -60,5 +60,5 @@ class TestModelIterators: for action in state.actions(): i += 1 for transition in action.transitions(): - assert transition.val() == 0.5 or transition.val() == 1 + assert transition.value() == 0.5 or transition.value() == 1 assert i == 1 or i == 2