Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SparseRationalModelToDoubleTransformer.cpp
Go to the documentation of this file.
2
3#include "storm/api/builder.h"
14
15namespace storm::transformer {
16std::shared_ptr<storm::models::sparse::Model<double>> sparseRationalModelToDouble(
17 std::shared_ptr<storm::models::sparse::Model<storm::RationalNumber>> const& inputModel, double precision) {
18 STORM_LOG_THROW(inputModel, storm::exceptions::IllegalArgumentTypeException, "Cannot transform a null model.");
20 convertedComponents.transitionMatrix = inputModel->getTransitionMatrix().toValueType<double>();
21 // For models with probability transition matrices, we scale rows that do not sum up to 1 (within the given precision) to mitigate numerical errors
22 if (inputModel->getType() != storm::models::ModelType::Ctmc) {
23 for (uint64_t row = 0; row < convertedComponents.transitionMatrix.getRowCount(); ++row) {
24 auto const rowSum = convertedComponents.transitionMatrix.getRowSum(row);
25 if (!storm::utility::isApproxEqual(rowSum, 1.0, precision, false)) {
26 for (auto& entry : convertedComponents.transitionMatrix.getRow(row)) {
27 entry.setValue(entry.getValue() / rowSum);
28 }
29 }
30 }
31 }
32 convertedComponents.choiceLabeling = inputModel->getOptionalChoiceLabeling();
33 convertedComponents.stateLabeling = inputModel->getStateLabeling();
34 convertedComponents.stateValuations = inputModel->getOptionalStateValuations();
35 convertedComponents.choiceOrigins = inputModel->getOptionalChoiceOrigins();
36 for (auto const& [rewardModelName, rewardModel] : inputModel->getRewardModels()) {
37 // Transform reward models
38 std::optional<std::vector<double>> optionalStateRewardVector = std::nullopt;
39 std::optional<std::vector<double>> optionalStateActionRewardVector = std::nullopt;
40 std::optional<storm::storage::SparseMatrix<double>> optionalTransitionRewardMatrix = std::nullopt;
41 if (rewardModel.hasStateRewards()) {
42 optionalStateRewardVector = storm::utility::vector::convertNumericVector<double>(rewardModel.getStateRewardVector());
43 }
44 if (rewardModel.hasStateActionRewards()) {
45 optionalStateActionRewardVector = storm::utility::vector::convertNumericVector<double>(rewardModel.getStateActionRewardVector());
46 }
47 if (rewardModel.hasTransitionRewards()) {
48 optionalTransitionRewardMatrix = rewardModel.getTransitionRewardMatrix().toValueType<double>();
49 }
50 convertedComponents.rewardModels.emplace(
52 std::move(optionalStateRewardVector), std::move(optionalStateActionRewardVector), std::move(optionalTransitionRewardMatrix)));
53 }
54
55 // Handle special model-type-specific components
56 switch (inputModel->getType()) {
57 // For types that do not need additional components.
58 // We still add them explicitly to catch model types that the transformer does not support.
61 break;
63 convertedComponents.rateTransitions = true;
64 break;
65 }
68 std::vector<double> resultVector;
69 resultVector.reserve(ma->getExitRates().size());
70 for (auto const& oldValue : ma->getExitRates()) {
71 resultVector.push_back(storm::utility::convertNumber<double>(oldValue));
72 }
73 convertedComponents.exitRates = resultVector;
74 // Markov automata store probabilities in their transition matrix and rates separately in exitRates.
75 convertedComponents.rateTransitions = false;
76 convertedComponents.markovianStates = ma->getMarkovianStates();
77 break;
78 }
81 convertedComponents.observabilityClasses = pomdp->getObservations();
82 convertedComponents.observationValuations = pomdp->getOptionalObservationValuations();
83 break;
84 }
86 auto smg = inputModel->as<storm::models::sparse::Smg<storm::RationalNumber>>();
87 convertedComponents.statePlayerIndications = smg->getStatePlayerIndications();
88 convertedComponents.playerNameToIndexMap = smg->getPlayerNamesToIndex();
89 break;
90 }
93 convertedComponents.player1Matrix = s2pg->getPlayer1Matrix();
94 break;
95 }
96 default:
97 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException,
98 "Transformation to double is not supported for models of type " << inputModel->getType() << ".");
99 }
100 return storm::utility::builder::buildModelFromComponents(inputModel->getType(), std::move(convertedComponents));
101}
102} // namespace storm::transformer
This class represents a Markov automaton.
Base class for all sparse models.
Definition Model.h:34
This class represents a partially observable Markov decision process.
Definition Pomdp.h:13
This class represents a stochastic multiplayer game.
Definition Smg.h:16
This class represents a (discrete-time) stochastic two-player game.
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
std::shared_ptr< storm::models::sparse::Model< double > > sparseRationalModelToDouble(std::shared_ptr< storm::models::sparse::Model< storm::RationalNumber > > const &inputModel, double precision)
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< TargetType > convertNumericVector(std::vector< SourceType > const &oldVector)
Converts the given vector to the given ValueType Assumes that both, TargetType and SourceType are num...
Definition vector.h:970
bool isApproxEqual(ValueType const &a, ValueType const &b, ValueType const &precision, bool relative)
Definition constants.cpp:59
TargetType convertNumber(SourceType const &number)
std::unordered_map< std::string, RewardModelType > rewardModels
storm::storage::SparseMatrix< ValueType > transitionMatrix
boost::optional< storm::storage::BitVector > markovianStates
boost::optional< std::vector< storm::storage::PlayerIndex > > statePlayerIndications
boost::optional< storm::storage::SparseMatrix< storm::storage::sparse::state_type > > player1Matrix
std::optional< storm::storage::sparse::Valuations > observationValuations
std::optional< std::shared_ptr< storm::storage::sparse::ChoiceOrigins > > choiceOrigins
std::optional< storm::models::sparse::ChoiceLabeling > choiceLabeling
boost::optional< std::map< std::string, storm::storage::PlayerIndex > > playerNameToIndexMap
storm::models::sparse::StateLabeling stateLabeling
std::optional< storm::storage::sparse::Valuations > stateValuations
std::optional< std::vector< uint32_t > > observabilityClasses
boost::optional< std::vector< ValueType > > exitRates