Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
AffinePMdpDecomposition.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
11
13
14namespace {
15constexpr char const* NotAffine =
16 "SCP requires an affine pMDP (every transition probability, and every state-action reward for reward properties, must have total degree <= 1 in the "
17 "parameters), but ";
18} // namespace
19
20UnpackedValue AffinePMdpDecomposition::decomposeAffine(storm::RationalFunction const& value) {
21 std::set<storm::RationalFunctionVariable> occurring;
23
24 UnpackedValue unpacked;
26 for (auto const& var : occurring) {
28 }
29 unpacked.constant = storm::utility::parametric::evaluate<double>(value, zeroValuation);
30
31 unpacked.parameterCoefficients.reserve(occurring.size());
32 for (auto const& var : occurring) {
33 storm::RationalFunction derivative = value.derivative(var);
34 STORM_LOG_ASSERT(derivative.isConstant(), "Derivative of an affine function w.r.t. one of its own parameters must be constant.");
35 unpacked.parameterCoefficients.emplace_back(var, storm::utility::convertNumber<double>(derivative.constantPart()));
36 }
37
38 parameters.insert(occurring.begin(), occurring.end());
39 return unpacked;
40}
41
44 auto const& matrix = model.getTransitionMatrix();
45 auto const& rowGroupIndices = matrix.getRowGroupIndices();
46 auto stateOfRow = [&](uint64_t row) { return std::upper_bound(rowGroupIndices.begin(), rowGroupIndices.end(), row) - rowGroupIndices.begin() - 1; };
47
48 rows.reserve(matrix.getRowCount());
49 for (uint64_t row = 0; row < matrix.getRowCount(); ++row) {
50 std::vector<UnpackedTransition> unpackedRow;
51 unpackedRow.reserve(matrix.getRow(row).getNumberOfEntries());
52
53 for (auto const& entry : matrix.getRow(row)) {
54 STORM_LOG_THROW(storm::utility::parametric::isLinear(entry.getValue()), storm::exceptions::NotSupportedException,
55 NotAffine << "the transition from state " << stateOfRow(row) << " (row " << row << ") to state " << entry.getColumn() << " is '"
56 << entry.getValue() << "', which is not affine.");
57 UnpackedValue decomposed = decomposeAffine(entry.getValue());
58 UnpackedTransition unpacked;
59 unpacked.successor = entry.getColumn();
60 unpacked.constant = decomposed.constant;
61 unpacked.parameterCoefficients = std::move(decomposed.parameterCoefficients);
62 unpackedRow.push_back(std::move(unpacked));
63 }
64 rows.push_back(std::move(unpackedRow));
65 }
66
67 if (rewardModel != nullptr) {
68 std::vector<storm::RationalFunction> rewardVector = rewardModel->getTotalRewardVector(matrix);
69 rewardRows.reserve(rewardVector.size());
70 for (uint64_t row = 0; row < rewardVector.size(); ++row) {
72 storm::utility::parametric::isLinear(rewardVector[row]), storm::exceptions::NotSupportedException,
73 NotAffine << "the reward of state " << stateOfRow(row) << " (row " << row << ") is '" << rewardVector[row] << "', which is not affine.");
74 rewardRows.push_back(decomposeAffine(rewardVector[row]));
75 }
76 }
77}
78
79std::vector<UnpackedTransition> const& AffinePMdpDecomposition::getRow(uint64_t row) const {
80 return rows.at(row);
81}
82
84 return rows.size();
85}
86
88 return rewardRows.at(row);
89}
90
91std::set<storm::RationalFunctionVariable> const& AffinePMdpDecomposition::getParameters() const {
92 return parameters;
93}
94
95} // namespace storm::pars::scp
Base class for all sparse models.
Definition Model.h:34
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:226
std::vector< ValueType > getTotalRewardVector(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix) const
Creates a vector representing the complete reward vector based on the state-, state-action- and trans...
AffinePMdpDecomposition(storm::models::sparse::Model< storm::RationalFunction > const &model, storm::models::sparse::StandardRewardModel< storm::RationalFunction > const *rewardModel=nullptr)
std::set< storm::RationalFunctionVariable > const & getParameters() const
All parameters occurring anywhere in the model's transition matrix, or (if a reward model was given t...
uint64_t getNumberOfRows() const
The number of rows (matrix rows, i.e. choices) that were decomposed.
std::vector< UnpackedTransition > const & getRow(uint64_t row) const
The unpacked transitions of the given matrix row (a state for a DTMC, or a single state-action choice...
UnpackedValue const & getReward(uint64_t row) const
The affine decomposition of the reward accrued by taking the given matrix row, from the reward model ...
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
bool isLinear(FunctionType const &function)
Checks whether the function is linear (in one parameter).
std::map< typename VariableType< FunctionType >::type, typename CoefficientType< FunctionType >::type > Valuation
Definition parametric.h:43
void gatherOccurringVariables(FunctionType const &function, std::set< typename VariableType< FunctionType >::type > &variableSet)
Add all variables that occur in the given function to the the given set.
double evaluate(storm::RationalFunction const &function, Valuation< storm::RationalFunction > const &valuation)
ValueType zero()
Definition constants.cpp:26
TargetType convertNumber(SourceType const &number)
carl::RationalFunction< Polynomial, true > RationalFunction
The affine part of a single transition (row, successor): UnpackedValue plus which successor state it ...
std::vector< std::pair< storm::RationalFunctionVariable, double > > parameterCoefficients
The affine decomposition of a single scalar quantity, so that everything downstream only ever touches...
std::vector< std::pair< storm::RationalFunctionVariable, double > > parameterCoefficients