Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BinaryDtmcTransformer.cpp
Go to the documentation of this file.
2
3#include <carl/formula/Constraint.h>
4#include <queue>
5
14
15namespace storm {
16namespace transformer {
17std::shared_ptr<storm::models::sparse::Dtmc<RationalFunction>> BinaryDtmcTransformer::transform(storm::models::sparse::Dtmc<RationalFunction> const& dtmc,
18 bool keepStateValuations) const {
19 auto data = transformTransitions(dtmc);
21 components.stateLabeling = transformStateLabeling(dtmc, data);
22 for (auto const& rewModel : dtmc.getRewardModels()) {
23 components.rewardModels.emplace(rewModel.first, transformRewardModel(dtmc, rewModel.second, data));
24 }
25 components.transitionMatrix = std::move(data.simpleMatrix);
26 if (keepStateValuations && dtmc.hasStateValuations()) {
27 components.stateValuations = dtmc.getStateValuations().blowup(data.simpleStateToOriginalState);
28 }
29
30 return std::make_shared<storm::models::sparse::Dtmc<RationalFunction>>(std::move(components.transitionMatrix), std::move(components.stateLabeling),
31 std::move(components.rewardModels));
32}
33
35 uint64_t state;
36 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> row;
37};
38
39typename BinaryDtmcTransformer::TransformationData BinaryDtmcTransformer::transformTransitions(
41 auto const& matrix = dtmc.getTransitionMatrix();
42
43 // Initialize a FIFO Queue that stores the start and the end of each row
44 std::queue<StateWithRow> queue;
45 for (uint64_t state = 0; state < matrix.getRowCount(); ++state) {
46 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> diyRow;
47 for (auto const& entry : matrix.getRow(state)) {
48 diyRow.push_back(entry);
49 }
50 queue.emplace(StateWithRow{state, diyRow});
51 }
52
54 uint64_t currRow = 0;
55 uint64_t currAuxState = queue.size();
56 std::vector<uint64_t> origStates;
57
58 while (!queue.empty()) {
59 auto stateWithRow = std::move(queue.front());
60 queue.pop();
61
62 std::set<RationalFunctionVariable> variablesInRow;
63
64 for (auto const& entry : stateWithRow.row) {
65 for (auto const& variable : entry.getValue().gatherVariables()) {
66 variablesInRow.emplace(variable);
67 }
68 }
69
70 if (variablesInRow.size() == 0) {
71 // Insert the row directly
72 for (auto const& entry : stateWithRow.row) {
73 builder.addNextValue(currRow, entry.getColumn(), entry.getValue());
74 }
75 ++currRow;
76 } else if (variablesInRow.size() == 1) {
77 auto parameter = *variablesInRow.begin();
78 auto parameterPol = RawPolynomial(parameter);
79 auto oneMinusParameter = RawPolynomial(1) - parameterPol;
80
81 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> outgoing;
82 // p * .. state
83 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateLeft;
84 // (1-p) * .. state
85 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateRight;
86
87 RationalFunction sumOfLeftBranch;
88 RationalFunction sumOfRightBranch;
89
90 for (auto const& entry : stateWithRow.row) {
91 if (entry.getValue().isConstant()) {
92 outgoing.push_back(entry);
93 }
94 auto nominator = entry.getValue().nominator();
95 auto denominator = entry.getValue().denominator();
96 auto byP = RawPolynomial(nominator).divideBy(parameterPol);
97 if (byP.remainder.isZero()) {
98 auto probability = RationalFunction(carl::makePolynomial<Polynomial>(byP.quotient), denominator);
99 newStateLeft.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(entry.getColumn(), probability));
100 sumOfLeftBranch += probability;
101 continue;
102 }
103 auto byOneMinusP = RawPolynomial(nominator).divideBy(oneMinusParameter);
104 if (byOneMinusP.remainder.isZero()) {
105 auto probability = RationalFunction(carl::makePolynomial<Polynomial>(byOneMinusP.quotient), denominator);
106 newStateRight.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(entry.getColumn(), probability));
107 sumOfRightBranch += probability;
108 continue;
109 }
110 STORM_LOG_ERROR("Invalid transition!");
111 }
112 sumOfLeftBranch.simplify();
113 sumOfRightBranch.simplify();
114 for (auto& entry : newStateLeft) {
115 entry.setValue(entry.getValue() / sumOfLeftBranch);
116 }
117 for (auto& entry : newStateRight) {
118 entry.setValue(entry.getValue() / sumOfRightBranch);
119 }
120
121 queue.push(StateWithRow{currAuxState, newStateLeft});
122 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(
123 currAuxState, (sumOfLeftBranch)*RationalFunction(carl::makePolynomial<Polynomial>(parameter))));
124 ++currAuxState;
125 queue.push(StateWithRow{currAuxState, newStateRight});
126 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(
127 currAuxState, (sumOfRightBranch) * (utility::one<RationalFunction>() - RationalFunction(carl::makePolynomial<Polynomial>(parameter)))));
128 ++currAuxState;
129
130 for (auto const& entry : outgoing) {
131 builder.addNextValue(currRow, entry.getColumn(), entry.getValue());
132 }
133 ++currRow;
134 } else {
135 STORM_LOG_ERROR("More than one variable in row " << currRow << "!");
136 }
137 origStates.push_back(stateWithRow.state);
138 }
139 TransformationData result;
140 result.simpleMatrix = builder.build(currRow, currAuxState, currAuxState);
141 result.simpleStateToOriginalState = std::move(origStates);
142 return result;
143}
144
145storm::models::sparse::StateLabeling BinaryDtmcTransformer::transformStateLabeling(storm::models::sparse::Dtmc<RationalFunction> const& dtmc,
146 TransformationData const& data) const {
147 storm::models::sparse::StateLabeling labeling(data.simpleMatrix.getRowCount());
148 for (auto const& labelName : dtmc.getStateLabeling().getLabels()) {
149 storm::storage::BitVector newStates = dtmc.getStateLabeling().getStates(labelName);
150 newStates.resize(data.simpleMatrix.getRowCount(), false);
151 if (labelName != "init") {
152 for (uint64_t newState = dtmc.getNumberOfStates(); newState < data.simpleMatrix.getRowCount(); ++newState) {
153 newStates.set(newState, newStates[data.simpleStateToOriginalState[newState]]);
154 }
155 }
156 labeling.addLabel(labelName, std::move(newStates));
157 }
158 return labeling;
159}
160
161storm::models::sparse::StandardRewardModel<RationalFunction> BinaryDtmcTransformer::transformRewardModel(
163 TransformationData const& data) const {
164 std::optional<std::vector<RationalFunction>> stateRewards, actionRewards;
165 STORM_LOG_THROW(rewardModel.hasStateActionRewards(), storm::exceptions::NotSupportedException, "Only state rewards supported.");
166 if (rewardModel.hasStateRewards()) {
167 stateRewards = rewardModel.getStateRewardVector();
168 stateRewards->resize(data.simpleMatrix.getRowCount(), storm::utility::zero<RationalFunction>());
169 }
170 return storm::models::sparse::StandardRewardModel<RationalFunction>(std::move(stateRewards), std::move(actionRewards));
171}
172} // namespace transformer
173} // namespace storm
This class represents a discrete-time Markov chain.
Definition Dtmc.h:13
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:199
std::unordered_map< std::string, RewardModelType > const & getRewardModels() const
Retrieves the reward models.
Definition Model.cpp:691
bool hasStateValuations() const
Retrieves whether this model was build with state valuations.
Definition Model.cpp:351
storm::storage::sparse::StateValuations const & getStateValuations() const
Retrieves the valuations of the states of the model.
Definition Model.cpp:356
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
Definition Model.cpp:321
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:164
std::vector< ValueType > const & getStateRewardVector() const
Retrieves the state rewards of the reward model.
bool hasStateRewards() const
Retrieves whether the reward model has state rewards.
bool hasStateActionRewards() const
Retrieves whether the reward model has state-action rewards.
This class manages the labeling of the state space with a number of (atomic) labels.
storm::storage::BitVector const & getStates(std::string const &label) const
Returns the labeling of states associated with the given label.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
void resize(uint64_t newLength, bool init=false)
Resizes the bit vector to hold the given new number of bits.
A class that can be used to build a sparse matrix by adding value by value.
void addNextValue(index_type row, index_type column, value_type const &value)
Sets the matrix entry at the given row and column to the given value.
SparseMatrix< value_type > build(index_type overriddenRowCount=0, index_type overriddenColumnCount=0, index_type overriddenRowGroupCount=0)
StateValuations blowup(std::vector< uint64_t > const &mapNewToOld) const
std::shared_ptr< storm::models::sparse::Dtmc< RationalFunction > > transform(storm::models::sparse::Dtmc< RationalFunction > const &dtmc, bool keepStateValuations=false) const
Transforms a pDTMC that has linear transitions (e.g.
#define STORM_LOG_ERROR(message)
Definition logging.h:26
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
NumberTraits< RationalType >::IntegerType denominator(RationalType const &number)
carl::RationalFunction< Polynomial, true > RationalFunction
carl::MultivariatePolynomial< RationalFunctionCoefficient > RawPolynomial
std::optional< storm::storage::sparse::StateValuations > stateValuations
std::unordered_map< std::string, RewardModelType > rewardModels
storm::storage::SparseMatrix< ValueType > transitionMatrix
storm::models::sparse::StateLabeling stateLabeling
std::vector< storage::MatrixEntry< uint64_t, RationalFunction > > row