Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
StatePermuter.cpp
Go to the documentation of this file.
2
19
20namespace storm::transformer {
21
22template<typename ValueType>
23std::shared_ptr<storm::models::sparse::Model<ValueType>> permuteStates(storm::models::sparse::Model<ValueType> const& originalModel,
24 std::vector<uint64_t> const& permutation) {
25 STORM_LOG_ASSERT(originalModel.getNumberOfStates() == permutation.size(), "Invalid permutation size.");
26 STORM_LOG_ASSERT(storm::utility::permutation::isValidPermutation(permutation), "Invalid permutation.");
27 auto const inversePermutation = storm::utility::permutation::invertPermutation(permutation);
29 if (!originalModel.getTransitionMatrix().hasTrivialRowGrouping()) {
30 optionalGroupIndices.reset(originalModel.getTransitionMatrix().getRowGroupIndices());
31 }
32
33 // transitions, state labels, reward models
34 auto permutedMatrix = originalModel.getTransitionMatrix().permuteRowGroupsAndColumns(inversePermutation, permutation);
35 auto permutedStateLabels = originalModel.getStateLabeling();
36 permutedStateLabels.permuteItems(inversePermutation);
37 storm::storage::sparse::ModelComponents<ValueType> components(std::move(permutedMatrix), std::move(permutedStateLabels));
38 for (auto const& [name, origRewardModel] : originalModel.getRewardModels()) {
39 auto permutedRewardModel = origRewardModel.permuteStates(inversePermutation, optionalGroupIndices, permutation);
40 components.rewardModels.emplace(name, std::move(permutedRewardModel));
41 }
42
43 // Choice labeling, choice origins, state valuations
44 if (originalModel.hasChoiceLabeling()) {
45 auto permutedChoiceLabeling = originalModel.getChoiceLabeling();
46 permutedChoiceLabeling.permuteItems(inversePermutation);
47 components.choiceLabeling = std::move(permutedChoiceLabeling);
48 }
49 STORM_LOG_WARN_COND(!originalModel.hasStateValuations(), "State valuations will be dropped as permuting them is currently not implemented.");
50 STORM_LOG_WARN_COND(!originalModel.hasChoiceOrigins(), "Choice origins will be dropped as permuting them is currently not implemented.");
51
52 // Model type specific components
53 if (originalModel.isOfType(storm::models::ModelType::Pomdp)) {
54 auto const& pomdp = *originalModel.template as<storm::models::sparse::Pomdp<ValueType>>();
55 components.observabilityClasses = storm::utility::vector::applyInversePermutation(inversePermutation, pomdp.getObservations());
56 STORM_LOG_WARN_COND(!pomdp.hasObservationValuations(), "Observation valuations will be dropped as permuting them is currently not implemented.");
57 }
59 auto const& ma = *originalModel.template as<storm::models::sparse::MarkovAutomaton<ValueType>>();
60 components.markovianStates = ma.getMarkovianStates().permute(inversePermutation);
61 components.exitRates = storm::utility::vector::applyInversePermutation(inversePermutation, ma.getExitRates());
62 components.rateTransitions = false; // Note that originalModel.getTransitionMatrix() contains probabilities
63 } else if (originalModel.isOfType(storm::models::ModelType::Ctmc)) {
64 auto const& ctmc = *originalModel.template as<storm::models::sparse::Ctmc<ValueType>>();
65 components.exitRates = storm::utility::vector::applyInversePermutation(inversePermutation, ctmc.getExitRateVector());
66 components.rateTransitions = true;
67 } else {
69 storm::exceptions::UnexpectedException, "Unhandled model type.");
70 }
71 return storm::utility::builder::buildModelFromComponents(originalModel.getType(), std::move(components));
72}
73
74template std::shared_ptr<storm::models::sparse::Model<double>> permuteStates(storm::models::sparse::Model<double> const& originalModel,
75 std::vector<uint64_t> const& permutation);
76template std::shared_ptr<storm::models::sparse::Model<storm::RationalNumber>> permuteStates(
77 storm::models::sparse::Model<storm::RationalNumber> const& originalModel, std::vector<uint64_t> const& permutation);
78template std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> permuteStates(
79 storm::models::sparse::Model<storm::RationalFunction> const& originalModel, 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:19
void permuteItems(std::vector< uint64_t > const &inversePermutation)
Base class for all sparse models.
Definition Model.h:33
storm::models::sparse::ChoiceLabeling const & getChoiceLabeling() const
Retrieves the labels for the choices of the model.
Definition Model.cpp:334
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:197
std::unordered_map< std::string, RewardModelType > const & getRewardModels() const
Retrieves the reward models.
Definition Model.cpp:699
bool hasStateValuations() const
Retrieves whether this model was build with state valuations.
Definition Model.cpp:349
bool hasChoiceLabeling() const
Retrieves whether this model has a labeling of the choices.
Definition Model.cpp:329
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
Definition Model.cpp:319
bool hasChoiceOrigins() const
Retrieves whether this model was build with choice origins.
Definition Model.cpp:369
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:162
#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:19
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:1267
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