Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ModelInstantiator.cpp
Go to the documentation of this file.
3
5
6namespace storm {
7namespace utility {
8
9template<typename ParametricSparseModelType, typename ConstantSparseModelType>
11 // Now pre-compute the information for the equation system.
12 initializeModelSpecificData(parametricModel);
13 initializeMatrixMapping(this->instantiatedModel->getTransitionMatrix(), this->functions, this->matrixMapping, parametricModel.getTransitionMatrix());
14
15 for (auto& rewModel : this->instantiatedModel->getRewardModels()) {
16 if (rewModel.second.hasStateRewards()) {
17 initializeVectorMapping(rewModel.second.getStateRewardVector(), this->functions, this->vectorMapping,
18 parametricModel.getRewardModel(rewModel.first).getStateRewardVector());
19 }
20 if (rewModel.second.hasStateActionRewards()) {
21 initializeVectorMapping(rewModel.second.getStateActionRewardVector(), this->functions, this->vectorMapping,
22 parametricModel.getRewardModel(rewModel.first).getStateActionRewardVector());
23 }
24 if (rewModel.second.hasTransitionRewards()) {
25 initializeMatrixMapping(rewModel.second.getTransitionRewardMatrix(), this->functions, this->matrixMapping,
26 parametricModel.getRewardModel(rewModel.first).getTransitionRewardMatrix());
27 }
28 }
29}
30
31template<typename ParametricSparseModelType, typename ConstantType>
35
36template<typename ParametricSparseModelType, typename ConstantSparseModelType>
39 storm::storage::SparseMatrix<ParametricType> const& parametricMatrix) const {
40 storm::storage::SparseMatrixBuilder<ConstantType> matrixBuilder(parametricMatrix.getRowCount(), parametricMatrix.getColumnCount(),
41 parametricMatrix.getEntryCount(), true, !parametricMatrix.hasTrivialRowGrouping(),
42 parametricMatrix.hasTrivialRowGrouping() ? 0 : parametricMatrix.getRowGroupCount());
43 if (parametricMatrix.hasTrivialRowGrouping()) {
44 for (uint_fast64_t row = 0; row < parametricMatrix.getRowCount(); ++row) {
45 auto parametricRow = parametricMatrix.getRow(row);
46 ConstantType dummyValue = storm::utility::one<ConstantType>() / storm::utility::convertNumber<ConstantType>(parametricRow.getNumberOfEntries());
47 for (auto const& paramEntry : parametricRow) {
48 matrixBuilder.addNextValue(row, paramEntry.getColumn(), dummyValue);
49 }
50 }
51 } else {
52 for (uint_fast64_t rowGroup = 0; rowGroup < parametricMatrix.getRowGroupCount(); ++rowGroup) {
53 matrixBuilder.newRowGroup(parametricMatrix.getRowGroupIndices()[rowGroup]);
54 for (uint_fast64_t row = parametricMatrix.getRowGroupIndices()[rowGroup]; row < parametricMatrix.getRowGroupIndices()[rowGroup + 1]; ++row) {
55 auto parametricRow = parametricMatrix.getRow(row);
56 ConstantType dummyValue = storm::utility::one<ConstantType>() / storm::utility::convertNumber<ConstantType>(parametricRow.getNumberOfEntries());
57 for (auto const& paramEntry : parametricRow) {
58 matrixBuilder.addNextValue(row, paramEntry.getColumn(), dummyValue);
59 }
60 }
61 }
62 }
63 return matrixBuilder.build();
64}
65
66template<typename ParametricSparseModelType, typename ConstantSparseModelType>
67std::unordered_map<std::string, typename ConstantSparseModelType::RewardModelType>
68ModelInstantiator<ParametricSparseModelType, ConstantSparseModelType>::buildDummyRewardModels(
69 std::unordered_map<std::string, typename ParametricSparseModelType::RewardModelType> const& parametricRewardModel) const {
70 std::unordered_map<std::string, typename ConstantSparseModelType::RewardModelType> result;
71 for (auto const& paramRewardModel : parametricRewardModel) {
72 auto const& rewModel = paramRewardModel.second;
73 std::optional<std::vector<ConstantType>> optionalStateRewardVector;
74 if (rewModel.hasStateRewards()) {
75 optionalStateRewardVector = std::vector<ConstantType>(rewModel.getStateRewardVector().size());
76 }
77 std::optional<std::vector<ConstantType>> optionalStateActionRewardVector;
78 if (rewModel.hasStateActionRewards()) {
79 optionalStateActionRewardVector = std::vector<ConstantType>(rewModel.getStateActionRewardVector().size());
80 }
81 std::optional<storm::storage::SparseMatrix<ConstantType>> optionalTransitionRewardMatrix;
82 if (rewModel.hasTransitionRewards()) {
83 optionalTransitionRewardMatrix = buildDummyMatrix(rewModel.getTransitionRewardMatrix());
84 }
85 result.insert(std::make_pair(paramRewardModel.first, storm::models::sparse::StandardRewardModel<ConstantType>(
86 std::move(optionalStateRewardVector), std::move(optionalStateActionRewardVector),
87 std::move(optionalTransitionRewardMatrix))));
88 }
89 return result;
90}
91
92template<typename ParametricSparseModelType, typename ConstantSparseModelType>
93void ModelInstantiator<ParametricSparseModelType, ConstantSparseModelType>::initializeMatrixMapping(
94 storm::storage::SparseMatrix<ConstantType>& constantMatrix, std::unordered_map<ParametricType, ConstantType>& functions,
95 std::vector<std::pair<typename storm::storage::SparseMatrix<ConstantType>::iterator, ConstantType*>>& mapping,
96 storm::storage::SparseMatrix<ParametricType> const& parametricMatrix) const {
97 ConstantType dummyValue = storm::utility::one<ConstantType>();
98 auto constantEntryIt = constantMatrix.begin();
99 auto parametricEntryIt = parametricMatrix.begin();
100 while (parametricEntryIt != parametricMatrix.end()) {
101 STORM_LOG_ASSERT(parametricEntryIt->getColumn() == constantEntryIt->getColumn(),
102 "Entries of parametric and constant matrix are not at the same position");
103 if (storm::utility::isConstant(parametricEntryIt->getValue())) {
104 // Constant entries can be inserted directly
105 constantEntryIt->setValue(storm::utility::convertNumber<ConstantType>(parametricEntryIt->getValue()));
106 } else {
107 // insert the new function and store that the current constantMatrix entry needs to be set to the value of this function
108 auto functionsIt = functions.insert(std::make_pair(parametricEntryIt->getValue(), dummyValue)).first;
109 mapping.emplace_back(std::make_pair(constantEntryIt, &(functionsIt->second)));
110 // Note that references to elements of an unordered map remain valid after calling unordered_map::insert.
111 }
112 ++constantEntryIt;
113 ++parametricEntryIt;
114 }
115 STORM_LOG_ASSERT(constantEntryIt == constantMatrix.end(), "Parametric matrix seems to have more or less entries then the constant matrix");
116 constantMatrix.updateNonzeroEntryCount();
117}
118
119template<typename ParametricSparseModelType, typename ConstantSparseModelType>
120void ModelInstantiator<ParametricSparseModelType, ConstantSparseModelType>::initializeVectorMapping(
121 std::vector<ConstantType>& constantVector, std::unordered_map<ParametricType, ConstantType>& functions,
122 std::vector<std::pair<typename std::vector<ConstantType>::iterator, ConstantType*>>& mapping, std::vector<ParametricType> const& parametricVector) const {
123 ConstantType dummyValue = storm::utility::one<ConstantType>();
124 auto constantEntryIt = constantVector.begin();
125 auto parametricEntryIt = parametricVector.begin();
126 while (parametricEntryIt != parametricVector.end()) {
127 if (storm::utility::isConstant(storm::utility::simplify(*parametricEntryIt))) {
128 // Constant entries can be inserted directly
129 *constantEntryIt = storm::utility::convertNumber<ConstantType>(*parametricEntryIt);
130 } else {
131 // insert the new function and store that the current constantVector entry needs to be set to the value of this function
132 auto functionsIt = functions.insert(std::make_pair(*parametricEntryIt, dummyValue)).first;
133 mapping.emplace_back(std::make_pair(constantEntryIt, &(functionsIt->second)));
134 // Note that references to elements of an unordered map remain valid after calling unordered_map::insert.
135 }
136 ++constantEntryIt;
137 ++parametricEntryIt;
138 }
139 STORM_LOG_ASSERT(constantEntryIt == constantVector.end(), "Parametric vector seems to have more or less entries then the constant vector");
140}
141
142template<typename ParametricSparseModelType, typename ConstantSparseModelType>
145 // Write results into the placeholders
146 instantiate_helper(valuation);
147
148 // Write the instantiated values to the matrices and vectors according to the stored mappings
149 for (auto& entryValuePair : this->matrixMapping) {
150 entryValuePair.first->setValue(*(entryValuePair.second));
151 }
152 for (auto& entryValuePair : this->vectorMapping) {
153 *(entryValuePair.first) = *(entryValuePair.second);
154 }
155
156 return *this->instantiatedModel;
157}
158
159template<typename ParametricSparseModelType, typename ConstantSparseModelType>
163
164#ifdef STORM_HAVE_CARL
171
179
180// For stormpy:
186#endif
187} // namespace utility
188} // namespace storm
This class represents a continuous-time Markov chain.
Definition Ctmc.h:15
This class represents a discrete-time Markov chain.
Definition Dtmc.h:14
This class represents a Markov automaton.
This class represents a (discrete-time) Markov decision process.
Definition Mdp.h:14
This class represents a (discrete-time) stochastic two-player game.
A class that can be used to build a sparse matrix by adding value by value.
A class that holds a possibly non-square matrix in the compressed row storage format.
const_rows getRow(index_type row) const
Returns an object representing the given row.
index_type getEntryCount() const
Returns the number of entries in the matrix.
void updateNonzeroEntryCount() const
Recompute the nonzero entry count.
const_iterator begin(index_type row) const
Retrieves an iterator that points to the beginning of the given row.
const_iterator end(index_type row) const
Retrieves an iterator that points past the end of the given row.
index_type getRowGroupCount() const
Returns the number of row groups in the matrix.
index_type getColumnCount() const
Returns the number of columns of the matrix.
bool hasTrivialRowGrouping() const
Retrieves whether the matrix has a trivial row grouping.
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
std::vector< MatrixEntry< index_type, value_type > >::iterator iterator
index_type getRowCount() const
Returns the number of rows of the matrix.
This class allows efficient instantiation of the given parametric model.
ModelInstantiator(ParametricSparseModelType const &parametricModel)
Constructs a ModelInstantiator.
virtual ~ModelInstantiator()
Destructs the ModelInstantiator.
void checkValid() const
Check validity.
ConstantSparseModelType const & instantiate(storm::utility::parametric::Valuation< ParametricType > const &valuation)
Evaluates the occurring parametric functions and retrieves the instantiated model.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
std::map< typename VariableType< FunctionType >::type, typename CoefficientType< FunctionType >::type > Valuation
Definition parametric.h:41
bool isConstant(ValueType const &)
Definition constants.cpp:66
ValueType simplify(ValueType value)
LabParser.cpp.
Definition cli.cpp:18