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