Browse Source

added constructors

refactoring
hannah 5 years ago
committed by Matthias Volk
parent
commit
aa93129347
No known key found for this signature in database GPG Key ID: 83A57678F739FCD3
  1. 8
      src/storage/model.cpp
  2. 26
      src/storage/modelcomponents.cpp
  3. 2
      src/storage/modelcomponents.h

8
src/storage/model.cpp

@ -29,6 +29,7 @@
using RationalFunction = storm::RationalFunction; using RationalFunction = storm::RationalFunction;
using ModelBase = storm::models::ModelBase; using ModelBase = storm::models::ModelBase;
template<typename ValueType> using ModelComponents = storm::storage::sparse::ModelComponents<ValueType>;
template<typename ValueType> using SparseModel = storm::models::sparse::Model<ValueType>; template<typename ValueType> using SparseModel = storm::models::sparse::Model<ValueType>;
template<typename ValueType> using SparseDtmc = storm::models::sparse::Dtmc<ValueType>; template<typename ValueType> using SparseDtmc = storm::models::sparse::Dtmc<ValueType>;
template<typename ValueType> using SparseMdp = storm::models::sparse::Mdp<ValueType>; template<typename ValueType> using SparseMdp = storm::models::sparse::Mdp<ValueType>;
@ -197,10 +198,12 @@ void define_sparse_model(py::module& m) {
; ;
py::class_<SparseDtmc<double>, std::shared_ptr<SparseDtmc<double>>>(m, "SparseDtmc", "DTMC in sparse representation", model) py::class_<SparseDtmc<double>, std::shared_ptr<SparseDtmc<double>>>(m, "SparseDtmc", "DTMC in sparse representation", model)
.def(py::init<SparseDtmc<double>>(), py::arg("other_model")) .def(py::init<SparseDtmc<double>>(), py::arg("other_model"))
.def(py::init<ModelComponents<double> const&>(), py::arg("components")) //todo tests
.def("__str__", &getModelInfoPrinter) .def("__str__", &getModelInfoPrinter)
; ;
py::class_<SparseMdp<double>, std::shared_ptr<SparseMdp<double>>> mdp(m, "SparseMdp", "MDP in sparse representation", model); py::class_<SparseMdp<double>, std::shared_ptr<SparseMdp<double>>> mdp(m, "SparseMdp", "MDP in sparse representation", model);
mdp.def(py::init<SparseMdp<double>>(), py::arg("other_model")) mdp.def(py::init<SparseMdp<double>>(), py::arg("other_model"))
.def(py::init<ModelComponents<double> const&, storm::models::ModelType>(), py::arg("components"), py::arg("type")=storm::models::ModelType::Mdp) //todo tests
.def_property_readonly("nondeterministic_choice_indices", [](SparseMdp<double> const& mdp) { return mdp.getNondeterministicChoiceIndices(); }) .def_property_readonly("nondeterministic_choice_indices", [](SparseMdp<double> const& mdp) { return mdp.getNondeterministicChoiceIndices(); })
.def("get_nr_available_actions", [](SparseMdp<double> const& mdp, uint64_t stateIndex) { return mdp.getNondeterministicChoiceIndices()[stateIndex+1] - mdp.getNondeterministicChoiceIndices()[stateIndex] ; }, py::arg("state")) .def("get_nr_available_actions", [](SparseMdp<double> const& mdp, uint64_t stateIndex) { return mdp.getNondeterministicChoiceIndices()[stateIndex+1] - mdp.getNondeterministicChoiceIndices()[stateIndex] ; }, py::arg("state"))
.def("get_choice_index", [](SparseMdp<double> const& mdp, uint64_t state, uint64_t actOff) { return mdp.getNondeterministicChoiceIndices()[state]+actOff; }, py::arg("state"), py::arg("action_offset"), "gets the choice index for the offset action from the given state.") .def("get_choice_index", [](SparseMdp<double> const& mdp, uint64_t state, uint64_t actOff) { return mdp.getNondeterministicChoiceIndices()[state]+actOff; }, py::arg("state"), py::arg("action_offset"), "gets the choice index for the offset action from the given state.")
@ -209,6 +212,7 @@ void define_sparse_model(py::module& m) {
; ;
py::class_<SparsePomdp<double>, std::shared_ptr<SparsePomdp<double>>>(m, "SparsePomdp", "POMDP in sparse representation", mdp) py::class_<SparsePomdp<double>, std::shared_ptr<SparsePomdp<double>>>(m, "SparsePomdp", "POMDP in sparse representation", mdp)
.def(py::init<SparsePomdp<double>>(), py::arg("other_model")) .def(py::init<SparsePomdp<double>>(), py::arg("other_model"))
.def(py::init<ModelComponents<double> const&, bool>(), py::arg("components"), py::arg("canonicFlag")=false) //todo tests
.def("__str__", &getModelInfoPrinter) .def("__str__", &getModelInfoPrinter)
.def("get_observation", &SparsePomdp<double>::getObservation, py::arg("state")) .def("get_observation", &SparsePomdp<double>::getObservation, py::arg("state"))
.def_property_readonly("observations", &SparsePomdp<double>::getObservations) .def_property_readonly("observations", &SparsePomdp<double>::getObservations)
@ -216,10 +220,12 @@ void define_sparse_model(py::module& m) {
; ;
py::class_<SparseCtmc<double>, std::shared_ptr<SparseCtmc<double>>>(m, "SparseCtmc", "CTMC in sparse representation", model) py::class_<SparseCtmc<double>, std::shared_ptr<SparseCtmc<double>>>(m, "SparseCtmc", "CTMC in sparse representation", model)
.def(py::init<SparseCtmc<double>>(), py::arg("other_model")) .def(py::init<SparseCtmc<double>>(), py::arg("other_model"))
.def(py::init<ModelComponents<double> const&>(), py::arg("components")) //todo tests
.def("__str__", &getModelInfoPrinter) .def("__str__", &getModelInfoPrinter)
; ;
py::class_<SparseMarkovAutomaton<double>, std::shared_ptr<SparseMarkovAutomaton<double>>>(m, "SparseMA", "MA in sparse representation", model) py::class_<SparseMarkovAutomaton<double>, std::shared_ptr<SparseMarkovAutomaton<double>>>(m, "SparseMA", "MA in sparse representation", model)
.def(py::init<SparseMarkovAutomaton<double>>(), py::arg("other_model")) .def(py::init<SparseMarkovAutomaton<double>>(), py::arg("other_model"))
.def(py::init<ModelComponents<double> const&>(), py::arg("components")) //todo tests
.def_property_readonly("nondeterministic_choice_indices", [](SparseMarkovAutomaton<double> const& ma) { return ma.getNondeterministicChoiceIndices(); }) .def_property_readonly("nondeterministic_choice_indices", [](SparseMarkovAutomaton<double> const& ma) { return ma.getNondeterministicChoiceIndices(); })
.def("apply_scheduler", [](SparseMarkovAutomaton<double> const& ma, storm::storage::Scheduler<double> const& scheduler, bool dropUnreachableStates) { return ma.applyScheduler(scheduler, dropUnreachableStates); } , "apply scheduler", "scheduler"_a, "drop_unreachable_states"_a = true) .def("apply_scheduler", [](SparseMarkovAutomaton<double> const& ma, storm::storage::Scheduler<double> const& scheduler, bool dropUnreachableStates) { return ma.applyScheduler(scheduler, dropUnreachableStates); } , "apply scheduler", "scheduler"_a, "drop_unreachable_states"_a = true)
.def("__str__", &getModelInfoPrinter) .def("__str__", &getModelInfoPrinter)
@ -228,6 +234,7 @@ void define_sparse_model(py::module& m) {
; ;
py::class_<SparseRewardModel<double>>(m, "SparseRewardModel", "Reward structure for sparse models") py::class_<SparseRewardModel<double>>(m, "SparseRewardModel", "Reward structure for sparse models")
//todo init?
.def_property_readonly("has_state_rewards", &SparseRewardModel<double>::hasStateRewards) .def_property_readonly("has_state_rewards", &SparseRewardModel<double>::hasStateRewards)
.def_property_readonly("has_state_action_rewards", &SparseRewardModel<double>::hasStateActionRewards) .def_property_readonly("has_state_action_rewards", &SparseRewardModel<double>::hasStateActionRewards)
.def_property_readonly("has_transition_rewards", &SparseRewardModel<double>::hasTransitionRewards) .def_property_readonly("has_transition_rewards", &SparseRewardModel<double>::hasTransitionRewards)
@ -290,6 +297,7 @@ void define_sparse_model(py::module& m) {
; ;
py::class_<SparseRewardModel<RationalFunction>>(m, "SparseParametricRewardModel", "Reward structure for parametric sparse models") py::class_<SparseRewardModel<RationalFunction>>(m, "SparseParametricRewardModel", "Reward structure for parametric sparse models")
//todo init?
.def_property_readonly("has_state_rewards", &SparseRewardModel<RationalFunction>::hasStateRewards) .def_property_readonly("has_state_rewards", &SparseRewardModel<RationalFunction>::hasStateRewards)
.def_property_readonly("has_state_action_rewards", &SparseRewardModel<RationalFunction>::hasStateActionRewards) .def_property_readonly("has_state_action_rewards", &SparseRewardModel<RationalFunction>::hasStateActionRewards)
.def_property_readonly("has_transition_rewards", &SparseRewardModel<RationalFunction>::hasTransitionRewards) .def_property_readonly("has_transition_rewards", &SparseRewardModel<RationalFunction>::hasTransitionRewards)

26
src/storage/modelcomponents.cpp

@ -8,31 +8,37 @@
template<typename ValueType> using SparseMatrix = storm::storage::SparseMatrix<ValueType>;
using stateLabeling = storm::models::sparse::StateLabeling; using stateLabeling = storm::models::sparse::StateLabeling;
using BitVector = storm::storage::BitVector; using BitVector = storm::storage::BitVector;
template<typename ValueType> using SparseMatrix = storm::storage::SparseMatrix<ValueType>;
template<typename ValueType> using SparseRewardModel = storm::models::sparse::StandardRewardModel<ValueType>;
template<typename ValueType> using StandardRewardModel = storm::models::sparse::StandardRewardModel<ValueType>;
template<typename ValueType, typename RewardModelType = StandardRewardModel<ValueType>> using ModelComponents = storm::storage::sparse::ModelComponents<ValueType, RewardModelType>;
// other: todo
// <storm::RationalNumber> <double, storm::models::sparse::StandardRewardModel<storm::Interval>><storm::RationalFunction>;
// py::class_<ModelComponents<double, StandardRewardModel<storm::Interval>>>(m, "ModelComponents", "ModelComponents description..")
template<typename ValueType> using ModelComponents = storm::storage::sparse::ModelComponents<ValueType>;
// others: todo
// <storm::RationalFunction>;
//
// <storm::RationalNumber>
// <double, storm::models::sparse::StandardRewardModel<storm::Interval>>
// todo
// 1. create constructor for sparseModels double in models.h using model comp.
// 2. write tests
// 3. rationalfct
void define_model_components(py::module& m) { void define_model_components(py::module& m) {
py::class_<ModelComponents<double>>(m, "ModelComponents", "ModelComponents description..") py::class_<ModelComponents<double>>(m, "ModelComponents", "ModelComponents description..")
.def(py::init<SparseMatrix<double> const &, stateLabeling const &, std::unordered_map<std::string, StandardRewardModel<double>> const &,
.def(py::init<SparseMatrix<double> const &, stateLabeling const &, std::unordered_map<std::string, SparseRewardModel<double>> const &,
bool, boost::optional<BitVector> const &, boost::optional<SparseMatrix<storm::storage::sparse::state_type>> const &>(), bool, boost::optional<BitVector> const &, boost::optional<SparseMatrix<storm::storage::sparse::state_type>> const &>(),
"Construct from Prism program", py::arg("transition_matrix"), py::arg("state_labeling") = stateLabeling(), "Construct from Prism program", py::arg("transition_matrix"), py::arg("state_labeling") = stateLabeling(),
py::arg("reward_models") = std::unordered_map<std::string, StandardRewardModel<double>>(), py::arg("rate_transitions") = false,
py::arg("reward_models") = std::unordered_map<std::string, SparseRewardModel<double>>(), py::arg("rate_transitions") = false,
py::arg("markovian_states") = boost::none, py::arg("player1_matrix") = boost::none) py::arg("markovian_states") = boost::none, py::arg("player1_matrix") = boost::none)
.def(py::init<>()) // for rvalue ? todo .def(py::init<>()) // for rvalue ? todo
// general components (for all model types)
// General components (for all model types)
.def_readwrite("transition_matrix", &ModelComponents<double>::transitionMatrix) .def_readwrite("transition_matrix", &ModelComponents<double>::transitionMatrix)
.def_readwrite("state_labeling", &ModelComponents<double>::stateLabeling) .def_readwrite("state_labeling", &ModelComponents<double>::stateLabeling)
.def_readwrite("reward_models", &ModelComponents<double>::rewardModels, "Reward models associated with the model") .def_readwrite("reward_models", &ModelComponents<double>::rewardModels, "Reward models associated with the model")

2
src/storage/modelcomponents.h

@ -3,6 +3,6 @@
#include "common.h" #include "common.h"
void define_model_cmponents(py::module& m);
void define_model_components(py::module& m);
#endif /* PYSTORM_MODELCOMPONENTS_H */ #endif /* PYSTORM_MODELCOMPONENTS_H */
Loading…
Cancel
Save