From 0072a3fc980c54b61920b367ddad03e7bd6256ae Mon Sep 17 00:00:00 2001 From: Matthias Volk Date: Thu, 17 May 2018 17:35:48 +0200 Subject: [PATCH] Refactoring for sparse models --- src/storage/model.cpp | 36 ++++++++++++++---------------------- src/storage/model.h | 4 +--- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/storage/model.cpp b/src/storage/model.cpp index 9d0993b..c185ae6 100644 --- a/src/storage/model.cpp +++ b/src/storage/model.cpp @@ -15,38 +15,32 @@ // Typedefs using RationalFunction = storm::RationalFunction; -using state_type = storm::storage::sparse::state_type; -template using SparseRewardModel = storm::models::sparse::StandardRewardModel; - using ModelBase = storm::models::ModelBase; + template using SparseModel = storm::models::sparse::Model; template using SparseDtmc = storm::models::sparse::Dtmc; template using SparseMdp = storm::models::sparse::Mdp; template using SparseCtmc = storm::models::sparse::Ctmc; template using SparseMarkovAutomaton = storm::models::sparse::MarkovAutomaton; +template using SparseRewardModel = storm::models::sparse::StandardRewardModel; + -// Thin wrapper for getting initial states +// Thin wrappers template -std::vector getInitialStates(SparseModel const& model) { - std::vector initialStates; +std::vector getSparseInitialStates(SparseModel const& model) { + std::vector initialStates; for (auto entry : model.getInitialStates()) { initialStates.push_back(entry); } return initialStates; } -// Thin wrapper for getting transition matrix template storm::storage::SparseMatrix& getTransitionMatrix(SparseModel& model) { return model.getTransitionMatrix(); } -template -storm::storage::SparseMatrix getBackwardTransitionMatrix(SparseModel const& model) { - return std::move(model.getBackwardTransitions()); -} - // requires pycarl.Variable std::set probabilityVariables(SparseModel const& model) { return storm::models::sparse::getProbabilityParameters(model); @@ -123,9 +117,7 @@ void define_model(py::module& m) { .def("_as_sparse_pma", [](ModelBase &modelbase) { return modelbase.as>(); }, "Get model as sparse pMA") - .def("_as_symbolic_dtmc", [](ModelBase &modelbase) { - return modelbase.as>(); - }, "Get model as symbolic DTMC") + ; } @@ -133,18 +125,17 @@ void define_model(py::module& m) { // Bindings for sparse models void define_sparse_model(py::module& m) { - - // Models + // Models with double numbers py::class_, std::shared_ptr>, ModelBase> model(m, "_SparseModel", "A probabilistic model where transitions are represented by doubles and saved in a sparse matrix"); model.def_property_readonly("labeling", &getLabeling, "Labels") .def("labels_state", &SparseModel::getLabelsOfState, py::arg("state"), "Get labels of state") - .def_property_readonly("initial_states", &getInitialStates, "Initial states") + .def_property_readonly("initial_states", &getSparseInitialStates, "Initial states") .def_property_readonly("states", [](SparseModel& model) { return SparseModelStates(model); }, "Get states") .def_property_readonly("reward_models", [](SparseModel& model) {return model.getRewardModels(); }, "Reward models") .def_property_readonly("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix") - .def_property_readonly("backward_transition_matrix", &getBackwardTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Backward transition matrix") + .def_property_readonly("backward_transition_matrix", &SparseModel::getBackwardTransitions, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Backward transition matrix") .def("reduce_to_state_based_rewards", &SparseModel::reduceToStateBasedRewards) .def("__str__", getModelInfoPrinter()) ; @@ -169,23 +160,24 @@ void define_sparse_model(py::module& m) { .def_property_readonly("state_rewards", [](SparseRewardModel& rewardModel) {return rewardModel.getStateRewardVector();}) .def("get_state_reward", [](SparseRewardModel& rewardModel, uint64_t state) {return rewardModel.getStateReward(state);}) .def("get_state_action_reward", [](SparseRewardModel& rewardModel, uint64_t action_index) {return rewardModel.getStateActionReward(action_index);}) - .def_property_readonly("state_action_rewards", [](SparseRewardModel& rewardModel) {return rewardModel.getStateActionRewardVector();}) + .def_property_readonly("state_action_rewards", [](SparseRewardModel& rewardModel) {return rewardModel.getStateActionRewardVector();}) .def("reduce_to_state_based_rewards", [](SparseRewardModel& rewardModel, storm::storage::SparseMatrix const& transitions, bool onlyStateRewards){return rewardModel.reduceToStateBasedRewards(transitions, onlyStateRewards);}, py::arg("transition_matrix"), py::arg("only_state_rewards"), "Reduce to state-based rewards") ; + // Parametric models py::class_, std::shared_ptr>, ModelBase> modelRatFunc(m, "_SparseParametricModel", "A probabilistic model where transitions are represented by rational functions and saved in a sparse matrix"); modelRatFunc.def("collect_probability_parameters", &probabilityVariables, "Collect parameters") .def("collect_reward_parameters", &rewardVariables, "Collect reward parameters") .def_property_readonly("labeling", &getLabeling, "Labels") .def("labels_state", &SparseModel::getLabelsOfState, py::arg("state"), "Get labels of state") - .def_property_readonly("initial_states", &getInitialStates, "Initial states") + .def_property_readonly("initial_states", &getSparseInitialStates, "Initial states") .def_property_readonly("states", [](SparseModel& model) { return SparseModelStates(model); }, "Get states") .def_property_readonly("reward_models", [](SparseModel const& model) {return model.getRewardModels(); }, "Reward models") .def_property_readonly("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix") - .def_property_readonly("backward_transition_matrix", &getBackwardTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Backward transition matrix") + .def_property_readonly("backward_transition_matrix", &SparseModel::getBackwardTransitions, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Backward transition matrix") .def("reduce_to_state_based_rewards", &SparseModel::reduceToStateBasedRewards) .def("__str__", getModelInfoPrinter("ParametricModel")) ; diff --git a/src/storage/model.h b/src/storage/model.h index ac91a56..50f4c31 100644 --- a/src/storage/model.h +++ b/src/storage/model.h @@ -1,9 +1,7 @@ -#ifndef PYTHON_STORAGE_MODEL_H_ -#define PYTHON_STORAGE_MODEL_H_ +#pragma once #include "common.h" void define_model(py::module& m); void define_sparse_model(py::module& m); -#endif /* PYTHON_STORAGE_MODEL_H_ */