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