Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
StatePermuter.cpp
Go to the documentation of this file.
2
17
18namespace storm::transformer {
19
20template<typename ValueType>
21std::shared_ptr<storm::models::sparse::Model<ValueType>> permuteStates(storm::models::sparse::Model<ValueType> const& originalModel,
22 std::vector<uint64_t> const& permutation) {
23 STORM_LOG_ASSERT(originalModel.getNumberOfStates() == permutation.size(), "Invalid permutation size.");
24 STORM_LOG_ASSERT(storm::utility::permutation::isValidPermutation(permutation), "Invalid permutation.");
25 auto const inversePermutation = storm::utility::permutation::invertPermutation(permutation);
27 if (!originalModel.getTransitionMatrix().hasTrivialRowGrouping()) {
28 optionalGroupIndices.reset(originalModel.getTransitionMatrix().getRowGroupIndices());
29 }
30
31 // transitions, state labels, reward models
32 auto permutedMatrix = originalModel.getTransitionMatrix().permuteRowGroupsAndColumns(inversePermutation, permutation);
33 auto permutedStateLabels = originalModel.getStateLabeling();
34 permutedStateLabels.permuteItems(inversePermutation);
35 storm::storage::sparse::ModelComponents<ValueType> components(std::move(permutedMatrix), std::move(permutedStateLabels));
36 for (auto const& [name, origRewardModel] : originalModel.getRewardModels()) {
37 auto permutedRewardModel = origRewardModel.permuteStates(inversePermutation, optionalGroupIndices, permutation);
38 components.rewardModels.emplace(name, std::move(permutedRewardModel));
39 }
40
41 // Choice labeling, choice origins, state valuations
42 if (originalModel.hasChoiceLabeling()) {
43 auto permutedChoiceLabeling = originalModel.getChoiceLabeling();
44 permutedChoiceLabeling.permuteItems(inversePermutation);
45 components.choiceLabeling = std::move(permutedChoiceLabeling);
46 }
47 STORM_LOG_WARN_COND(!originalModel.hasStateValuations(), "State valuations will be dropped as permuting them is currently not implemented.");
48 STORM_LOG_WARN_COND(!originalModel.hasChoiceOrigins(), "Choice origins will be dropped as permuting them is currently not implemented.");
49
50 // Model type specific components
51 if (originalModel.isOfType(storm::models::ModelType::Pomdp)) {
52 auto const& pomdp = *originalModel.template as<storm::models::sparse::Pomdp<ValueType>>();
53 components.observabilityClasses = storm::utility::vector::applyInversePermutation(inversePermutation, pomdp.getObservations());
54 STORM_LOG_WARN_COND(!pomdp.hasObservationValuations(), "Observation valuations will be dropped as permuting them is currently not implemented.");
55 }
57 auto const& ma = *originalModel.template as<storm::models::sparse::MarkovAutomaton<ValueType>>();
58 components.markovianStates = ma.getMarkovianStates().permute(inversePermutation);
59 components.exitRates = storm::utility::vector::applyInversePermutation(inversePermutation, ma.getExitRates());
60 components.rateTransitions = false; // Note that originalModel.getTransitionMatrix() contains probabilities
61 } else if (originalModel.isOfType(storm::models::ModelType::Ctmc)) {
62 auto const& ctmc = *originalModel.template as<storm::models::sparse::Ctmc<ValueType>>();
63 components.exitRates = storm::utility::vector::applyInversePermutation(inversePermutation, ctmc.getExitRateVector());
64 components.rateTransitions = true;
65 } else {
67 storm::exceptions::UnexpectedException, "Unhandled model type.");
68 }
69 return storm::utility::builder::buildModelFromComponents(originalModel.getType(), std::move(components));
70}
71
72template std::shared_ptr<storm::models::sparse::Model<double>> permuteStates(storm::models::sparse::Model<double> const& originalModel,
73 std::vector<uint64_t> const& permutation);
74template std::shared_ptr<storm::models::sparse::Model<storm::RationalNumber>> permuteStates(
75 storm::models::sparse::Model<storm::RationalNumber> const& originalModel, std::vector<uint64_t> const& permutation);
76template std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> permuteStates(
77 storm::models::sparse::Model<storm::RationalFunction> const& originalModel, std::vector<uint64_t> const& permutation);
78template std::shared_ptr<storm::models::sparse::Model<storm::Interval>> permuteStates(storm::models::sparse::Model<storm::Interval> const& originalModel,
79 std::vector<uint64_t> const& permutation);
80} // namespace storm::transformer
Helper class that optionally holds a reference to an object of type T.
Definition OptionalRef.h:48
void reset()
Unsets the reference.
virtual ModelType getType() const
Return the actual type of the model.
Definition ModelBase.cpp:7
bool isOfType(storm::models::ModelType const &modelType) const
Checks whether the model is of the given type.
Definition ModelBase.cpp:27
void permuteItems(std::vector< uint64_t > const &inversePermutation)
Base class for all sparse models.
Definition Model.h:32
storm::models::sparse::ChoiceLabeling const & getChoiceLabeling() const
Retrieves the labels for the choices of the model.
Definition Model.cpp:336
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:199
std::unordered_map< std::string, RewardModelType > const & getRewardModels() const
Retrieves the reward models.
Definition Model.cpp:691
bool hasStateValuations() const
Retrieves whether this model was build with state valuations.
Definition Model.cpp:351
bool hasChoiceLabeling() const
Retrieves whether this model has a labeling of the choices.
Definition Model.cpp:331
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
Definition Model.cpp:321
bool hasChoiceOrigins() const
Retrieves whether this model was build with choice origins.
Definition Model.cpp:371
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:164
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:38
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
std::shared_ptr< storm::models::sparse::Model< ValueType > > permuteStates(storm::models::sparse::Model< ValueType > const &originalModel, std::vector< uint64_t > const &permutation)
Applies the given permutation to the states of the given model.
std::shared_ptr< storm::models::sparse::Model< ValueType, RewardModelType > > buildModelFromComponents(storm::models::ModelType modelType, storm::storage::sparse::ModelComponents< ValueType, RewardModelType > &&components)
Definition builder.cpp:20
std::vector< index_type > invertPermutation(std::vector< index_type > const &permutation)
Inverts the given permutation.
bool isValidPermutation(std::vector< index_type > const &permutation)
Returns true if the given vector is a permutation of the numbers 0, 1, ..., n-1 for n = permutation....
std::vector< T > applyInversePermutation(std::vector< uint64_t > const &inversePermutation, std::vector< T > const &source)
Definition vector.h:1114
std::unordered_map< std::string, RewardModelType > rewardModels
boost::optional< storm::storage::BitVector > markovianStates
std::optional< storm::models::sparse::ChoiceLabeling > choiceLabeling
std::optional< std::vector< uint32_t > > observabilityClasses
boost::optional< std::vector< ValueType > > exitRates