From f91ba50800e52c0e6ad2699b3d3d871cf59bc4ad Mon Sep 17 00:00:00 2001 From: Tom Janson Date: Thu, 30 Mar 2017 00:12:41 +0200 Subject: [PATCH] use type aliases in model.cpp --- src/storage/model.cpp | 90 ++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/src/storage/model.cpp b/src/storage/model.cpp index bcd2c27..352601c 100644 --- a/src/storage/model.cpp +++ b/src/storage/model.cpp @@ -10,10 +10,19 @@ #include #include + +using ModelBase = storm::models::ModelBase; +using state_type = storm::storage::sparse::state_type; +template using Dtmc = storm::models::sparse::Dtmc; +template using Mdp = storm::models::sparse::Mdp; +template using Model = storm::models::sparse::Model; +template using SparseMatrix = storm::storage::SparseMatrix; +using RationalFunction = RationalFunction; + // Thin wrapper for getting initial states template -std::vector getInitialStates(storm::models::sparse::Model const& model) { - std::vector initialStates; +std::vector getInitialStates(Model const& model) { + std::vector initialStates; for (auto entry : model.getInitialStates()) { initialStates.push_back(entry); } @@ -22,22 +31,23 @@ std::vector getInitialStates(storm::models:: // Thin wrapper for getting transition matrix template -storm::storage::SparseMatrix& getTransitionMatrix(storm::models::sparse::Model& model) { +SparseMatrix& getTransitionMatrix(Model& model) { return model.getTransitionMatrix(); } -std::set probabilityVariables(storm::models::sparse::Model const& model) { +// requires pycarl.Variable +std::set probabilityVariables(Model const& model) { return storm::models::sparse::getProbabilityParameters(model); } -std::set rewardVariables(storm::models::sparse::Model const& model) { +std::set rewardVariables(Model const& model) { return storm::models::sparse::getRewardParameters(model); } template -std::function const&)> getModelInfoPrinter(std::string name = "Model") { +std::function const&)> getModelInfoPrinter(std::string name = "Model") { // look, C++ has lambdas and stuff! - return [name](storm::models::sparse::Model const& model) { + return [name](Model const& model) { std::stringstream ss; model.printModelInformationToStream(ss); @@ -64,67 +74,67 @@ void define_model(py::module& m) { ; // ModelBase - py::class_> modelBase(m, "_ModelBase", "Base class for all models"); - modelBase.def_property_readonly("nr_states", &storm::models::ModelBase::getNumberOfStates, "Number of states") - .def_property_readonly("nr_transitions", &storm::models::ModelBase::getNumberOfTransitions, "Number of transitions") - .def_property_readonly("model_type", &storm::models::ModelBase::getType, "Model type") - .def_property_readonly("supports_parameters", &storm::models::ModelBase::supportsParameters, "Flag whether model supports parameters") - .def_property_readonly("has_parameters", &storm::models::ModelBase::hasParameters, "Flag whether model has parameters") - .def_property_readonly("is_exact", &storm::models::ModelBase::isExact, "Flag whether model is exact") - .def("_as_dtmc", &storm::models::ModelBase::as>, "Get model as DTMC") - .def("_as_pdtmc", &storm::models::ModelBase::as>, "Get model as pDTMC") - .def("_as_mdp", &storm::models::ModelBase::as>, "Get model as MDP") - .def("_as_pmdp", &storm::models::ModelBase::as>, "Get model as pMDP") + py::class_> modelBase(m, "_ModelBase", "Base class for all models"); + modelBase.def_property_readonly("nr_states", &ModelBase::getNumberOfStates, "Number of states") + .def_property_readonly("nr_transitions", &ModelBase::getNumberOfTransitions, "Number of transitions") + .def_property_readonly("model_type", &ModelBase::getType, "Model type") + .def_property_readonly("supports_parameters", &ModelBase::supportsParameters, "Flag whether model supports parameters") + .def_property_readonly("has_parameters", &ModelBase::hasParameters, "Flag whether model has parameters") + .def_property_readonly("is_exact", &ModelBase::isExact, "Flag whether model is exact") + .def("_as_dtmc", &ModelBase::as>, "Get model as DTMC") + .def("_as_pdtmc", &ModelBase::as>, "Get model as pDTMC") + .def("_as_mdp", &ModelBase::as>, "Get model as MDP") + .def("_as_pmdp", &ModelBase::as>, "Get model as pMDP") ; // Models //storm::models::sparse::Model > - py::class_, std::shared_ptr>> model(m, "_SparseModel", "A probabilistic model where transitions are represented by doubles and saved in a sparse matrix", modelBase); - model.def_property_readonly("labels", [](storm::models::sparse::Model& model) { + py::class_, std::shared_ptr>> model(m, "_SparseModel", "A probabilistic model where transitions are represented by doubles and saved in a sparse matrix", modelBase); + model.def_property_readonly("labels", [](Model& model) { return model.getStateLabeling().getLabels(); }, "Labels") - .def("labels_state", &storm::models::sparse::Model::getLabelsOfState, py::arg("state"), "Get labels of state") + .def("labels_state", &Model::getLabelsOfState, py::arg("state"), "Get labels of state") .def_property_readonly("initial_states", &getInitialStates, "Initial states") .def_property_readonly("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix") .def("__str__", getModelInfoPrinter()) ; - py::class_, std::shared_ptr>>(m, "SparseDtmc", "DTMC in sparse representation", model) + py::class_, std::shared_ptr>>(m, "SparseDtmc", "DTMC in sparse representation", model) .def("__str__", getModelInfoPrinter("DTMC")) ; - py::class_, std::shared_ptr>>(m, "SparseMdp", "MDP in sparse representation", model) + py::class_, std::shared_ptr>>(m, "SparseMdp", "MDP in sparse representation", model) .def("__str__", getModelInfoPrinter("MDP")) ; - py::class_, std::shared_ptr>> modelRatFunc(m, "_SparseParametricModel", "A probabilistic model where transitions are represented by rational functions and saved in a sparse matrix", modelBase); + py::class_, std::shared_ptr>> modelRatFunc(m, "_SparseParametricModel", "A probabilistic model where transitions are represented by rational functions and saved in a sparse matrix", modelBase); modelRatFunc.def("collect_probability_parameters", &probabilityVariables, "Collect parameters") - .def("collect_reward_parameters", &rewardVariables, "Collect reward parameters") - .def_property_readonly("labels", [](storm::models::sparse::Model& model) { + .def("collect_reward_parameters", &rewardVariables, "Collect reward parameters") + .def_property_readonly("labels", [](Model& model) { return model.getStateLabeling().getLabels(); }, "Labels") - .def("labels_state", &storm::models::sparse::Model::getLabelsOfState, py::arg("state"), "Get labels of state") - .def_property_readonly("initial_states", &getInitialStates, "Initial states") - .def_property_readonly("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix") - .def("__str__", getModelInfoPrinter("ParametricModel")) + .def("labels_state", &Model::getLabelsOfState, py::arg("state"), "Get labels of state") + .def_property_readonly("initial_states", &getInitialStates, "Initial states") + .def_property_readonly("transition_matrix", &getTransitionMatrix, py::return_value_policy::reference, py::keep_alive<1, 0>(), "Transition matrix") + .def("__str__", getModelInfoPrinter("ParametricModel")) ; - py::class_, std::shared_ptr>>(m, "SparseParametricDtmc", "pDTMC in sparse representation", modelRatFunc) - .def("__str__", getModelInfoPrinter("ParametricDTMC")) + py::class_, std::shared_ptr>>(m, "SparseParametricDtmc", "pDTMC in sparse representation", modelRatFunc) + .def("__str__", getModelInfoPrinter("ParametricDTMC")) ; - py::class_, std::shared_ptr>>(m, "SparseParametricMdp", "pMDP in sparse representation", modelRatFunc) - .def("__str__", getModelInfoPrinter("ParametricMDP")) + py::class_, std::shared_ptr>>(m, "SparseParametricMdp", "pMDP in sparse representation", modelRatFunc) + .def("__str__", getModelInfoPrinter("ParametricMDP")) ; } // Model instantiator void define_model_instantiator(py::module& m) { - py::class_,storm::models::sparse::Dtmc>>(m, "PdtmcInstantiator", "Instantiate PDTMCs to DTMCs") - .def(py::init>(), "parametric model"_a) - .def("instantiate", &storm::utility::ModelInstantiator, storm::models::sparse::Dtmc>::instantiate, "Instantiate model with given parameter values") + py::class_, Dtmc>>(m, "PdtmcInstantiator", "Instantiate PDTMCs to DTMCs") + .def(py::init>(), "parametric model"_a) + .def("instantiate", &storm::utility::ModelInstantiator, Dtmc>::instantiate, "Instantiate model with given parameter values") ; - py::class_,storm::models::sparse::Mdp>>(m, "PmdpInstantiator", "Instantiate PMDPs to MDPs") - .def(py::init>(), "parametric model"_a) - .def("instantiate", &storm::utility::ModelInstantiator, storm::models::sparse::Mdp>::instantiate, "Instantiate model with given parameter values") + py::class_,Mdp>>(m, "PmdpInstantiator", "Instantiate PMDPs to MDPs") + .def(py::init>(), "parametric model"_a) + .def("instantiate", &storm::utility::ModelInstantiator, Mdp>::instantiate, "Instantiate model with given parameter values") ; }