Storm
A Modern Probabilistic Model Checker
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AddUncertainty.cpp
Go to the documentation of this file.
11
12namespace storm::transformer {
13
14template<typename ValueType>
15AddUncertainty<ValueType>::AddUncertainty(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& originalModel) : origModel(originalModel) {}
16
17template<typename ValueType>
18std::shared_ptr<storm::models::sparse::Model<Interval>> AddUncertainty<ValueType>::transform(double additiveUncertainty, double minimalTransitionProbability) {
19 // we first build the matrix and later copy the row grouping.
20 auto newMatrixBuilder =
21 storage::SparseMatrixBuilder<storm::Interval>(origModel->getTransitionMatrix().getRowCount(), origModel->getTransitionMatrix().getColumnCount(),
22 origModel->getTransitionMatrix().getNonzeroEntryCount(), true, false);
23 // Build transition matrix (without row grouping)
24 for (uint64_t rowIndex = 0; rowIndex < origModel->getTransitionMatrix().getRowCount(); ++rowIndex) {
25 for (auto const& entry : origModel->getTransitionMatrix().getRow(rowIndex)) {
26 newMatrixBuilder.addNextValue(rowIndex, entry.getColumn(), addUncertainty(entry.getValue(), additiveUncertainty, minimalTransitionProbability));
27 }
28 }
29 storm::storage::sparse::ModelComponents<storm::Interval> modelComponents(newMatrixBuilder.build(), origModel->getStateLabeling());
30 if (!origModel->getTransitionMatrix().hasTrivialRowGrouping()) {
31 modelComponents.transitionMatrix.setRowGroupIndices(origModel->getTransitionMatrix().getRowGroupIndices());
32 }
33 // Change value type of standard reward model.
34 std::unordered_map<std::string, models::sparse::StandardRewardModel<storm::Interval>> newRewardModels;
35 for (auto const& rewModel : origModel->getRewardModels()) {
36 auto const& oldRewModel = rewModel.second;
37 std::optional<std::vector<storm::Interval>> stateRewards;
38 std::optional<std::vector<storm::Interval>> stateActionRewards;
39 if (oldRewModel.hasStateRewards()) {
40 stateRewards = utility::vector::convertNumericVector<storm::Interval>(oldRewModel.getStateRewardVector());
41 }
42 if (oldRewModel.hasStateActionRewards()) {
43 stateActionRewards = utility::vector::convertNumericVector<storm::Interval>(oldRewModel.getStateActionRewardVector());
44 }
45 STORM_LOG_THROW(!oldRewModel.hasTransitionRewards(), exceptions::NotImplementedException, "Transition rewards are not supported.");
46 models::sparse::StandardRewardModel<storm::Interval> newRewModel(std::move(stateRewards), std::move(stateActionRewards));
47 newRewardModels.emplace(rewModel.first, std::move(newRewModel));
48 }
49
50 // remaining model components.
51 modelComponents.rewardModels = std::move(newRewardModels);
52 modelComponents.stateValuations = origModel->getOptionalStateValuations();
53 modelComponents.choiceLabeling = origModel->getOptionalChoiceLabeling();
54 modelComponents.choiceOrigins = origModel->getOptionalChoiceOrigins();
55
56 switch (origModel->getType()) {
58 return std::make_shared<storm::models::sparse::Dtmc<storm::Interval>>(std::move(modelComponents));
60 return std::make_shared<storm::models::sparse::Mdp<storm::Interval>>(std::move(modelComponents));
61 default:
62 STORM_LOG_THROW(false, exceptions::NotImplementedException, "Only DTMC and MDP model types are currently supported.");
63 }
64}
65
66template<typename ValueType>
67storm::Interval AddUncertainty<ValueType>::addUncertainty(ValueType const& vt, double additiveUncertainty, double minimalValue) {
68 if (utility::isOne(vt)) {
69 return storm::Interval(1.0, 1.0);
70 }
71 double center = storm::utility::convertNumber<double>(vt);
72 STORM_LOG_THROW(center >= minimalValue, storm::exceptions::InvalidArgumentException, "Transition probability is smaller than minimal value");
73 double lowerBound = std::max(center - additiveUncertainty, minimalValue);
74 double upperBound = std::min(center + additiveUncertainty, 1.0 - minimalValue);
75 STORM_LOG_ASSERT(lowerBound > 0, "Lower bound must be strictly above zero.");
76 STORM_LOG_ASSERT(upperBound < 1, "Upper bound must be strictly below one.");
77 return storm::Interval(lowerBound, upperBound);
78}
79
80template class AddUncertainty<double>;
81} // namespace storm::transformer
Base class for all sparse models.
Definition Model.h:33
A class that can be used to build a sparse matrix by adding value by value.
This class is a convenience transformer to add uncertainty.
std::shared_ptr< storm::models::sparse::Model< Interval > > transform(double additiveUncertainty, double minimalValue=0.0001)
AddUncertainty(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &originalModel)
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
bool isOne(ValueType const &a)
Definition constants.cpp:36
carl::Interval< double > Interval
std::optional< storm::storage::sparse::StateValuations > stateValuations
std::unordered_map< std::string, RewardModelType > rewardModels
storm::storage::SparseMatrix< ValueType > transitionMatrix
std::optional< std::shared_ptr< storm::storage::sparse::ChoiceOrigins > > choiceOrigins
std::optional< storm::models::sparse::ChoiceLabeling > choiceLabeling