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