Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BinaryDtmcTransformer.cpp
Go to the documentation of this file.
2
3#pragma clang diagnostic push
4#pragma clang diagnostic ignored "-Wthread-safety-negative"
5#pragma clang diagnostic ignored "-Wundefined-reinterpret-cast"
6#pragma clang diagnostic ignored "-Wunused-template"
7#include <carl/formula/Constraint.h>
8#pragma clang diagnostic pop
9#include <queue>
10
19
20namespace storm {
21namespace transformer {
22std::shared_ptr<storm::models::sparse::Dtmc<RationalFunction>> BinaryDtmcTransformer::transform(storm::models::sparse::Dtmc<RationalFunction> const& dtmc,
23 bool keepStateValuations) const {
24 auto data = transformTransitions(dtmc);
26 components.stateLabeling = transformStateLabeling(dtmc, data);
27 for (auto const& rewModel : dtmc.getRewardModels()) {
28 components.rewardModels.emplace(rewModel.first, transformRewardModel(dtmc, rewModel.second, data));
29 }
30 components.transitionMatrix = std::move(data.simpleMatrix);
31 if (keepStateValuations && dtmc.hasStateValuations()) {
32 components.stateValuations = dtmc.getStateValuations().selectEntities(data.simpleStateToOriginalState);
33 }
34
35 return std::make_shared<storm::models::sparse::Dtmc<RationalFunction>>(std::move(components.transitionMatrix), std::move(components.stateLabeling),
36 std::move(components.rewardModels));
37}
38
40 uint64_t state;
41 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> row;
42};
43
44typename BinaryDtmcTransformer::TransformationData BinaryDtmcTransformer::transformTransitions(
46 auto const& matrix = dtmc.getTransitionMatrix();
47
48 // Initialize a FIFO Queue that stores the start and the end of each row
49 std::queue<StateWithRow> queue;
50 for (uint64_t state = 0; state < matrix.getRowCount(); ++state) {
51 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> diyRow;
52 for (auto const& entry : matrix.getRow(state)) {
53 diyRow.push_back(entry);
54 }
55 queue.emplace(StateWithRow{state, diyRow});
56 }
57
59 uint64_t currRow = 0;
60 uint64_t currAuxState = queue.size();
61 std::vector<uint64_t> origStates;
62
63 while (!queue.empty()) {
64 auto stateWithRow = std::move(queue.front());
65 queue.pop();
66
67 std::set<RationalFunctionVariable> variablesInRow;
68
69 for (auto const& entry : stateWithRow.row) {
70 for (auto const& variable : entry.getValue().gatherVariables()) {
71 variablesInRow.emplace(variable);
72 }
73 }
74
75 if (variablesInRow.size() == 0) {
76 // Insert the row directly
77 for (auto const& entry : stateWithRow.row) {
78 builder.addNextValue(currRow, entry.getColumn(), entry.getValue());
79 }
80 ++currRow;
81 } else if (variablesInRow.size() == 1) {
82 auto parameter = *variablesInRow.begin();
83 auto parameterPol = RawPolynomial(parameter);
84 auto oneMinusParameter = RawPolynomial(1) - parameterPol;
85
86 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> outgoing;
87 // p * .. state
88 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateLeft;
89 // (1-p) * .. state
90 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateRight;
91
92 RationalFunction sumOfLeftBranch;
93 RationalFunction sumOfRightBranch;
94
95 for (auto const& entry : stateWithRow.row) {
96 if (entry.getValue().isConstant()) {
97 outgoing.push_back(entry);
98 }
99 auto nominator = entry.getValue().nominator();
100 auto denominator = entry.getValue().denominator();
101 auto byP = RawPolynomial(nominator).divideBy(parameterPol);
102 if (byP.remainder.isZero()) {
103 auto probability = RationalFunction(carl::makePolynomial<Polynomial>(byP.quotient), denominator);
104 newStateLeft.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(entry.getColumn(), probability));
105 sumOfLeftBranch += probability;
106 continue;
107 }
108 auto byOneMinusP = RawPolynomial(nominator).divideBy(oneMinusParameter);
109 if (byOneMinusP.remainder.isZero()) {
110 auto probability = RationalFunction(carl::makePolynomial<Polynomial>(byOneMinusP.quotient), denominator);
111 newStateRight.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(entry.getColumn(), probability));
112 sumOfRightBranch += probability;
113 continue;
114 }
115 STORM_LOG_ERROR("Invalid transition!");
116 }
117 sumOfLeftBranch.simplify();
118 sumOfRightBranch.simplify();
119 for (auto& entry : newStateLeft) {
120 entry.setValue(entry.getValue() / sumOfLeftBranch);
121 }
122 for (auto& entry : newStateRight) {
123 entry.setValue(entry.getValue() / sumOfRightBranch);
124 }
125
126 queue.push(StateWithRow{currAuxState, newStateLeft});
127 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(
128 currAuxState, (sumOfLeftBranch)*RationalFunction(carl::makePolynomial<Polynomial>(parameter))));
129 ++currAuxState;
130 queue.push(StateWithRow{currAuxState, newStateRight});
131 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(
132 currAuxState, (sumOfRightBranch) * (utility::one<RationalFunction>() - RationalFunction(carl::makePolynomial<Polynomial>(parameter)))));
133 ++currAuxState;
134
135 for (auto const& entry : outgoing) {
136 builder.addNextValue(currRow, entry.getColumn(), entry.getValue());
137 }
138 ++currRow;
139 } else {
140 STORM_LOG_ERROR("More than one variable in row " << currRow << "!");
141 }
142 origStates.push_back(stateWithRow.state);
143 }
144 TransformationData result;
145 result.simpleMatrix = builder.build(currRow, currAuxState, currAuxState);
146 result.simpleStateToOriginalState = std::move(origStates);
147 return result;
148}
149
150storm::models::sparse::StateLabeling BinaryDtmcTransformer::transformStateLabeling(storm::models::sparse::Dtmc<RationalFunction> const& dtmc,
151 TransformationData const& data) const {
152 storm::models::sparse::StateLabeling labeling(data.simpleMatrix.getRowCount());
153 for (auto const& labelName : dtmc.getStateLabeling().getLabels()) {
154 storm::storage::BitVector newStates = dtmc.getStateLabeling().getStates(labelName);
155 newStates.resize(data.simpleMatrix.getRowCount(), false);
156 if (labelName != "init") {
157 for (uint64_t newState = dtmc.getNumberOfStates(); newState < data.simpleMatrix.getRowCount(); ++newState) {
158 newStates.set(newState, newStates[data.simpleStateToOriginalState[newState]]);
159 }
160 }
161 labeling.addLabel(labelName, std::move(newStates));
162 }
163 return labeling;
164}
165
166storm::models::sparse::StandardRewardModel<RationalFunction> BinaryDtmcTransformer::transformRewardModel(
167 storm::models::sparse::Dtmc<RationalFunction> const& dtmc, storm::models::sparse::StandardRewardModel<RationalFunction> const& rewardModel,
168 TransformationData const& data) const {
169 std::optional<std::vector<RationalFunction>> stateRewards, actionRewards;
170 STORM_LOG_THROW(rewardModel.hasStateActionRewards(), storm::exceptions::NotSupportedException, "Only state rewards supported.");
171 if (rewardModel.hasStateRewards()) {
172 stateRewards = rewardModel.getStateRewardVector();
173 stateRewards->resize(data.simpleMatrix.getRowCount(), storm::utility::zero<RationalFunction>());
174 }
175 return storm::models::sparse::StandardRewardModel<RationalFunction>(std::move(stateRewards), std::move(actionRewards));
176}
177} // namespace transformer
178} // namespace storm
This class represents a discrete-time Markov chain.
Definition Dtmc.h:13
std::set< std::string > getLabels() const
Retrieves the set of labels contained in this labeling.
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:198
std::unordered_map< std::string, RewardModelType > const & getRewardModels() const
Retrieves the reward models.
Definition Model.cpp:690
bool hasStateValuations() const
Retrieves whether this model was build with state valuations.
Definition Model.cpp:350
storm::storage::sparse::Valuations const & getStateValuations() const
Retrieves the valuations of the states of the model.
Definition Model.cpp:355
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
Definition Model.cpp:320
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:163
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.
storm::storage::BitVector const & getStates(std::string const &label) const
Returns the labeling of states associated with the given label.
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)
Valuations selectEntities(storm::storage::BitVector const &selectedEntities) const
Derive new valuations from this by selecting the given entities.
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:29
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
NumberTraits< RationalType >::IntegerType denominator(RationalType const &number)
ValueType zero()
Definition constants.cpp:24
ValueType one()
Definition constants.cpp:19
carl::RationalFunction< Polynomial, true > RationalFunction
carl::MultivariatePolynomial< RationalFunctionCoefficient > RawPolynomial
std::unordered_map< std::string, RewardModelType > rewardModels
storm::storage::SparseMatrix< ValueType > transitionMatrix
storm::models::sparse::StateLabeling stateLabeling
std::optional< storm::storage::sparse::Valuations > stateValuations
std::vector< storage::MatrixEntry< uint64_t, RationalFunction > > row