10 changed files with 238 additions and 100 deletions
-
1lib/stormpy/storage/__init__.py
-
36lib/stormpy/storage/action.py
-
34lib/stormpy/storage/state.py
-
2src/mod_storage.cpp
-
6src/storage/matrix.cpp
-
10src/storage/model.cpp
-
45src/storage/state.cpp
-
122src/storage/state.h
-
6tests/storage/test_matrix.py
-
76tests/storage/test_state.py
@ -1,36 +0,0 @@ |
|||||
class Action: |
|
||||
""" Represents an action in the model """ |
|
||||
|
|
||||
def __init__(self, row_group_start, row_group_end, row, matrix): |
|
||||
""" Initialize |
|
||||
:param row_group_start: Start index of the row group in the matrix |
|
||||
:param row_group_end: End index of the row group in the matrix |
|
||||
:param row: Index of the corresponding row in the matrix |
|
||||
:param matrix: Corresponding matrix |
|
||||
""" |
|
||||
self.row_group_start = row_group_start |
|
||||
self.row_group_end = row_group_end |
|
||||
self.row = row - 1 |
|
||||
self.matrix = matrix |
|
||||
assert row >= -1 and row + row_group_start <= row_group_end |
|
||||
|
|
||||
def __iter__(self): |
|
||||
return self |
|
||||
|
|
||||
def __next__(self): |
|
||||
if self.row + self.row_group_start >= self.row_group_end - 1: |
|
||||
raise StopIteration |
|
||||
else: |
|
||||
self.row += 1 |
|
||||
return self |
|
||||
|
|
||||
def __str__(self): |
|
||||
return "{}".format(self.row) |
|
||||
|
|
||||
def transitions(self): |
|
||||
""" Get transitions associated with the action |
|
||||
:return List of tranistions |
|
||||
""" |
|
||||
row = self.row_group_start + self.row |
|
||||
#return self.model.transition_matrix().get_row(self.row_group_start + self.row) |
|
||||
return self.matrix.row_iter(row, row) |
|
@ -1,34 +0,0 @@ |
|||||
from . import action |
|
||||
|
|
||||
class State: |
|
||||
""" Represents a state in the matrix """ |
|
||||
|
|
||||
def __init__(self, id, matrix): |
|
||||
""" Initialize |
|
||||
:param id: Id of the state |
|
||||
:param matrix: Corresponding matrix |
|
||||
""" |
|
||||
self.id = id - 1 |
|
||||
self.matrix = matrix |
|
||||
|
|
||||
def __iter__(self): |
|
||||
return self |
|
||||
|
|
||||
def __next__(self): |
|
||||
if self.id >= self.matrix.nr_row_groups - 1: |
|
||||
raise StopIteration |
|
||||
else: |
|
||||
self.id += 1 |
|
||||
return self |
|
||||
|
|
||||
def __str__(self): |
|
||||
return "{}".format(self.id) |
|
||||
|
|
||||
def actions(self): |
|
||||
""" Get actions associated with the state |
|
||||
:return List of actions |
|
||||
""" |
|
||||
row_group_indices = self.matrix._row_group_indices |
|
||||
start = row_group_indices[self.id] |
|
||||
end = row_group_indices[self.id+1] |
|
||||
return action.Action(start, end, 0, self.matrix) |
|
@ -0,0 +1,45 @@ |
|||||
|
#include "state.h"
|
||||
|
|
||||
|
void define_state(py::module& m) { |
||||
|
|
||||
|
// SparseModelStates
|
||||
|
py::class_<SparseModelStates<double>>(m, "SparseModelStates", "States in sparse model") |
||||
|
.def("__getitem__", &SparseModelStates<double>::getState) |
||||
|
; |
||||
|
py::class_<SparseModelStates<storm::RationalFunction>>(m, "SparseParametricModelStates", "States in sparse parametric model") |
||||
|
.def("__getitem__", &SparseModelStates<storm::RationalFunction>::getState) |
||||
|
; |
||||
|
// SparseModelState
|
||||
|
py::class_<SparseModelState<double>>(m, "SparseModelState", "State in sparse model") |
||||
|
.def("__str__", &SparseModelState<double>::toString) |
||||
|
.def_property_readonly("id", &SparseModelState<double>::getIndex, "Id") |
||||
|
.def_property_readonly("labels", &SparseModelState<double>::getLabels, "Labels") |
||||
|
.def_property_readonly("actions", &SparseModelState<double>::getActions, "Get actions") |
||||
|
; |
||||
|
py::class_<SparseModelState<storm::RationalFunction>>(m, "SparseParametricModelState", "State in sparse parametric model") |
||||
|
.def("__str__", &SparseModelState<storm::RationalFunction>::toString) |
||||
|
.def_property_readonly("id", &SparseModelState<storm::RationalFunction>::getIndex, "Id") |
||||
|
.def_property_readonly("labels", &SparseModelState<storm::RationalFunction>::getLabels, "Labels") |
||||
|
.def_property_readonly("actions", &SparseModelState<storm::RationalFunction>::getActions, "Get actions") |
||||
|
; |
||||
|
|
||||
|
// SparseModelActions
|
||||
|
py::class_<SparseModelActions<double>>(m, "SparseModelActions", "Actions for state in sparse model") |
||||
|
.def("__getitem__", &SparseModelActions<double>::getAction) |
||||
|
; |
||||
|
py::class_<SparseModelActions<storm::RationalFunction>>(m, "SparseParametricModelActions", "Actions for state in sparse parametric model") |
||||
|
.def("__getitem__", &SparseModelActions<storm::RationalFunction>::getAction) |
||||
|
; |
||||
|
// SparseModelAction
|
||||
|
py::class_<SparseModelAction<double>>(m, "SparseModelAction", "Action for state in sparse model") |
||||
|
.def("__str__", &SparseModelAction<double>::toString) |
||||
|
.def_property_readonly("id", &SparseModelAction<double>::getIndex, "Id") |
||||
|
.def_property_readonly("transitions", &SparseModelAction<double>::getTransitions, "Get transitions") |
||||
|
; |
||||
|
py::class_<SparseModelAction<storm::RationalFunction>>(m, "SparseParametricModelAction", "Action for state in sparse parametric model") |
||||
|
.def("__str__", &SparseModelAction<storm::RationalFunction>::toString) |
||||
|
.def_property_readonly("id", &SparseModelAction<storm::RationalFunction>::getIndex, "Id") |
||||
|
.def_property_readonly("transitions", &SparseModelAction<storm::RationalFunction>::getTransitions, "Get transitions") |
||||
|
; |
||||
|
|
||||
|
} |
@ -0,0 +1,122 @@ |
|||||
|
#ifndef PYTHON_STORAGE_STATE_H_ |
||||
|
#define PYTHON_STORAGE_STATE_H_ |
||||
|
|
||||
|
#include "common.h" |
||||
|
|
||||
|
#include "storm/storage/SparseMatrix.h" |
||||
|
#include "storm/models/sparse/Model.h" |
||||
|
|
||||
|
// Forward declaration |
||||
|
template<typename ValueType> |
||||
|
class SparseModelActions; |
||||
|
|
||||
|
// State definition |
||||
|
template<typename ValueType> |
||||
|
class SparseModelState { |
||||
|
using s_index = typename storm::storage::SparseMatrix<ValueType>::index_type; |
||||
|
|
||||
|
public: |
||||
|
SparseModelState(storm::models::sparse::Model<ValueType>& model, s_index stateIndex) : model(model), stateIndex(stateIndex) { |
||||
|
} |
||||
|
|
||||
|
s_index getIndex() const { |
||||
|
return this->stateIndex; |
||||
|
} |
||||
|
|
||||
|
std::set<std::string> getLabels() { |
||||
|
return this->model.getStateLabeling().getLabelsOfState(this->stateIndex); |
||||
|
} |
||||
|
|
||||
|
SparseModelActions<ValueType> getActions() { |
||||
|
return SparseModelActions<ValueType>(this->model, stateIndex); |
||||
|
} |
||||
|
|
||||
|
std::string toString() { |
||||
|
std::stringstream stream; |
||||
|
stream << this->getIndex(); |
||||
|
return stream.str(); |
||||
|
} |
||||
|
|
||||
|
private: |
||||
|
s_index stateIndex; |
||||
|
storm::models::sparse::Model<ValueType>& model; |
||||
|
}; |
||||
|
|
||||
|
template<typename ValueType> |
||||
|
class SparseModelStates { |
||||
|
using s_index = typename storm::storage::SparseMatrix<ValueType>::index_type; |
||||
|
|
||||
|
public: |
||||
|
SparseModelStates(storm::models::sparse::Model<ValueType>& model) : model(model) { |
||||
|
length = model.getNumberOfStates(); |
||||
|
} |
||||
|
|
||||
|
SparseModelState<ValueType> getState(s_index index) { |
||||
|
if (index < length) { |
||||
|
return SparseModelState<ValueType>(model, index); |
||||
|
} else { |
||||
|
throw py::index_error(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private: |
||||
|
s_index length; |
||||
|
storm::models::sparse::Model<ValueType>& model; |
||||
|
}; |
||||
|
|
||||
|
// Action definition |
||||
|
template<typename ValueType> |
||||
|
class SparseModelAction { |
||||
|
using s_index = typename storm::storage::SparseMatrix<ValueType>::index_type; |
||||
|
|
||||
|
public: |
||||
|
SparseModelAction(storm::models::sparse::Model<ValueType>& model, s_index stateIndex, s_index actionIndex) : model(model), stateIndex(stateIndex), actionIndex(actionIndex) { |
||||
|
} |
||||
|
|
||||
|
s_index getIndex() const { |
||||
|
return this->actionIndex; |
||||
|
} |
||||
|
|
||||
|
typename storm::storage::SparseMatrix<ValueType>::rows getTransitions() { |
||||
|
return model.getTransitionMatrix().getRow(stateIndex, actionIndex); |
||||
|
} |
||||
|
|
||||
|
std::string toString() { |
||||
|
std::stringstream stream; |
||||
|
stream << this->getIndex(); |
||||
|
return stream.str(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private: |
||||
|
s_index stateIndex; |
||||
|
s_index actionIndex; |
||||
|
storm::models::sparse::Model<ValueType>& model; |
||||
|
}; |
||||
|
|
||||
|
template<typename ValueType> |
||||
|
class SparseModelActions { |
||||
|
using s_index = typename storm::storage::SparseMatrix<ValueType>::index_type; |
||||
|
|
||||
|
public: |
||||
|
SparseModelActions(storm::models::sparse::Model<ValueType>& model, s_index stateIndex) : model(model), stateIndex(stateIndex) { |
||||
|
length = model.getTransitionMatrix().getRowGroupSize(stateIndex); |
||||
|
} |
||||
|
|
||||
|
SparseModelAction<ValueType> getAction(size_t index) { |
||||
|
if (index < length) { |
||||
|
return SparseModelAction<ValueType>(model, stateIndex, index); |
||||
|
} else { |
||||
|
throw py::index_error(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private: |
||||
|
s_index stateIndex; |
||||
|
s_index length; |
||||
|
storm::models::sparse::Model<ValueType>& model; |
||||
|
}; |
||||
|
|
||||
|
void define_state(py::module& m); |
||||
|
|
||||
|
#endif /* PYTHON_STORAGE_STATE_H_ */ |
Write
Preview
Loading…
Cancel
Save
Reference in new issue