Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ModelInstantiator.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <type_traits>
5#include <unordered_map>
6
15
16namespace storm {
17namespace utility {
18
24template<typename ParametricSparseModelType, typename ConstantSparseModelType>
26 public:
27 typedef typename ParametricSparseModelType::ValueType ParametricType;
30 typedef typename ConstantSparseModelType::ValueType ConstantType;
31
36 ModelInstantiator(ParametricSparseModelType const& parametricModel);
37
41 virtual ~ModelInstantiator();
42
48 ConstantSparseModelType const& instantiate(storm::utility::parametric::Valuation<ParametricType> const& valuation);
49
53 void checkValid() const;
54
55 private:
60 template<typename PMT = ParametricSparseModelType>
61 typename std::enable_if<std::is_same<PMT, storm::models::sparse::Dtmc<typename ParametricSparseModelType::ValueType>>::value ||
62 std::is_same<PMT, storm::models::sparse::Mdp<typename ParametricSparseModelType::ValueType>>::value>::type
63 initializeModelSpecificData(PMT const& parametricModel) {
65 buildDummyMatrix(parametricModel.getTransitionMatrix()));
66 components.stateLabeling = parametricModel.getStateLabeling();
67 components.rewardModels = buildDummyRewardModels(parametricModel.getRewardModels());
68 components.choiceLabeling = parametricModel.getOptionalChoiceLabeling();
69
70 this->instantiatedModel = std::make_shared<ConstantSparseModelType>(std::move(components));
71 }
72
73 template<typename PMT = ParametricSparseModelType>
74 typename std::enable_if<std::is_same<PMT, storm::models::sparse::Ctmc<typename ParametricSparseModelType::ValueType>>::value>::type
75 initializeModelSpecificData(PMT const& parametricModel) {
77 buildDummyMatrix(parametricModel.getTransitionMatrix()));
78 components.stateLabeling = parametricModel.getStateLabeling();
79 components.rewardModels = buildDummyRewardModels(parametricModel.getRewardModels());
80 components.exitRates = std::vector<ConstantType>(parametricModel.getExitRateVector().size(), storm::utility::one<ConstantType>());
81 components.rateTransitions = true;
82 components.choiceLabeling = parametricModel.getOptionalChoiceLabeling();
83 this->instantiatedModel = std::make_shared<ConstantSparseModelType>(std::move(components));
84
85 initializeVectorMapping(this->instantiatedModel->getExitRateVector(), this->functions, this->vectorMapping, parametricModel.getExitRateVector());
86 }
87
88 template<typename PMT = ParametricSparseModelType>
89 typename std::enable_if<std::is_same<PMT, storm::models::sparse::MarkovAutomaton<typename ParametricSparseModelType::ValueType>>::value>::type
90 initializeModelSpecificData(PMT const& parametricModel) {
92 buildDummyMatrix(parametricModel.getTransitionMatrix()));
93 components.stateLabeling = parametricModel.getStateLabeling();
94 components.rewardModels = buildDummyRewardModels(parametricModel.getRewardModels());
95 components.exitRates = std::vector<ConstantType>(parametricModel.getExitRates().size(), storm::utility::one<ConstantType>());
96 components.markovianStates = parametricModel.getMarkovianStates();
97 components.choiceLabeling = parametricModel.getOptionalChoiceLabeling();
98 this->instantiatedModel = std::make_shared<ConstantSparseModelType>(std::move(components));
99
100 initializeVectorMapping(this->instantiatedModel->getExitRates(), this->functions, this->vectorMapping, parametricModel.getExitRates());
101 }
102
103 template<typename PMT = ParametricSparseModelType>
104 typename std::enable_if<std::is_same<PMT, storm::models::sparse::StochasticTwoPlayerGame<typename ParametricSparseModelType::ValueType>>::value>::type
105 initializeModelSpecificData(PMT const& parametricModel) {
107 buildDummyMatrix(parametricModel.getTransitionMatrix()));
108 components.stateLabeling = parametricModel.getStateLabeling();
109 components.rewardModels = buildDummyRewardModels(parametricModel.getRewardModels());
110 components.player1Matrix = parametricModel.getPlayer1Matrix();
111 components.choiceLabeling = parametricModel.getOptionalChoiceLabeling();
112
113 this->instantiatedModel = std::make_shared<ConstantSparseModelType>(std::move(components));
114 }
115
116 template<typename PMT = ParametricSparseModelType>
117 typename std::enable_if<std::is_same<PMT, ConstantSparseModelType>::value>::type instantiate_helper(
119 for (auto& functionResult : this->functions) {
120 functionResult.second = storm::utility::parametric::substitute(functionResult.first, valuation);
121 }
122 }
123
124 template<typename PMT = ParametricSparseModelType>
125 typename std::enable_if<!std::is_same<PMT, ConstantSparseModelType>::value>::type instantiate_helper(
127 for (auto& functionResult : this->functions) {
128 if (!transformer::BigStep::lastSavedAnnotations.empty() && functionResult.first.gatherVariables().size() == 1 &&
129 transformer::BigStep::lastSavedAnnotations.count(functionResult.first)) {
130 auto const& annotation = transformer::BigStep::lastSavedAnnotations.at(functionResult.first);
131 functionResult.second = annotation.evaluate(storm::utility::convertNumber<ConstantType>(valuation.at(annotation.getParameter())));
132 } else {
133 functionResult.second = storm::utility::parametric::evaluate<ConstantType>(functionResult.first, valuation);
134 }
135 }
136 }
137
142 storm::storage::SparseMatrix<ConstantType> buildDummyMatrix(storm::storage::SparseMatrix<ParametricType> const& parametricMatrix) const;
143
148 std::unordered_map<std::string, typename ConstantSparseModelType::RewardModelType> buildDummyRewardModels(
149 std::unordered_map<std::string, typename ParametricSparseModelType::RewardModelType> const& parametricRewardModel) const;
150
161 void initializeMatrixMapping(storm::storage::SparseMatrix<ConstantType>& constantMatrix, std::unordered_map<ParametricType, ConstantType>& functions,
162 std::vector<std::pair<typename storm::storage::SparseMatrix<ConstantType>::iterator, ConstantType*>>& mapping,
163 storm::storage::SparseMatrix<ParametricType> const& parametricMatrix) const;
164
175 void initializeVectorMapping(std::vector<ConstantType>& constantVector, std::unordered_map<ParametricType, ConstantType>& functions,
176 std::vector<std::pair<typename std::vector<ConstantType>::iterator, ConstantType*>>& mapping,
177 std::vector<ParametricType> const& parametricVector) const;
178
180 std::shared_ptr<ConstantSparseModelType> instantiatedModel;
182 std::unordered_map<ParametricType, ConstantType> functions;
184 std::vector<std::pair<typename storm::storage::SparseMatrix<ConstantType>::iterator, ConstantType*>> matrixMapping;
186 std::vector<std::pair<typename std::vector<ConstantType>::iterator, ConstantType*>> vectorMapping;
187};
188} // Namespace utility
189} // namespace storm
std::vector< MatrixEntry< index_type, value_type > >::iterator iterator
static std::unordered_map< RationalFunction, Annotation > lastSavedAnnotations
Definition BigStep.h:198
ModelInstantiator(ParametricSparseModelType const &parametricModel)
Constructs a ModelInstantiator.
storm::utility::parametric::VariableType< ParametricType >::type VariableType
virtual ~ModelInstantiator()
Destructs the ModelInstantiator.
ParametricSparseModelType::ValueType ParametricType
void checkValid() const
Check validity.
ConstantSparseModelType::ValueType ConstantType
storm::utility::parametric::CoefficientType< ParametricType >::type CoefficientType
ConstantSparseModelType const & instantiate(storm::utility::parametric::Valuation< ParametricType > const &valuation)
Evaluates the occurring parametric functions and retrieves the instantiated model.
FunctionType substitute(FunctionType const &function, Valuation< FunctionType > const &valuation)
Evaluates the given function wrt.
std::map< typename VariableType< FunctionType >::type, typename CoefficientType< FunctionType >::type > Valuation
Definition parametric.h:43
double evaluate(storm::RationalFunction const &function, Valuation< storm::RationalFunction > const &valuation)
ValueType one()
Definition constants.cpp:19
TargetType convertNumber(SourceType const &number)