17 bool keepStateValuations)
const {
18 auto data = transformTransitions(dtmc);
20 components.
stateLabeling = transformStateLabeling(dtmc, data);
22 components.
rewardModels.emplace(rewModel.first, transformRewardModel(dtmc, rewModel.second, data));
29 return std::make_shared<storm::models::sparse::Dtmc<RationalFunction>>(std::move(components.
transitionMatrix), std::move(components.
stateLabeling),
35 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>>
row;
50std::shared_ptr<RawPolynomialCache> cacheOf(
Polynomial const& polynomial) {
51 auto cache = polynomial.pCache();
55 STORM_LOG_ASSERT(polynomial.isConstant(),
"Expected a non-constant polynomial to carry a cache.");
56 return std::make_shared<RawPolynomialCache>();
71 STORM_LOG_ASSERT(!denominator.pCache() || denominator.pCache() == cache,
"Expected the denominator to share the cache of the numerator.");
86 if (!entry.getValue().isConstant()) {
87 return cacheOf(entry.getValue().nominator());
90 return std::make_shared<RawPolynomialCache>();
95typename BinaryDtmcTransformer::TransformationData BinaryDtmcTransformer::transformTransitions(
96 storm::models::sparse::Dtmc<RationalFunction>
const& dtmc)
const {
100 auto const cache = cacheOfModel(dtmc);
103 std::queue<StateWithRow> queue;
104 for (uint64_t state = 0; state < matrix.getRowCount(); ++state) {
105 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> diyRow;
106 for (
auto const& entry : matrix.getRow(state)) {
107 diyRow.push_back(entry);
109 queue.emplace(StateWithRow{state, diyRow});
112 storm::storage::SparseMatrixBuilder<RationalFunction> builder;
113 uint64_t currRow = 0;
114 uint64_t currAuxState = queue.size();
115 std::vector<uint64_t> origStates;
117 while (!queue.empty()) {
118 auto stateWithRow = std::move(queue.front());
121 std::set<RationalFunctionVariable> variablesInRow;
123 for (
auto const& entry : stateWithRow.row) {
124 for (
auto const& variable : entry.getValue().gatherVariables()) {
125 variablesInRow.emplace(variable);
129 if (variablesInRow.size() == 0) {
131 for (
auto const& entry : stateWithRow.row) {
132 builder.
addNextValue(currRow, entry.getColumn(), entry.getValue());
135 }
else if (variablesInRow.size() == 1) {
136 auto parameter = *variablesInRow.begin();
140 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> outgoing;
142 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateLeft;
144 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateRight;
149 for (
auto const& entry : stateWithRow.row) {
150 if (entry.getValue().isConstant()) {
151 outgoing.push_back(entry);
153 auto nominator = entry.getValue().nominator();
156 if (byP.remainder.isZero()) {
157 auto probability = toRationalFunction(byP.quotient, denominator, cache);
158 newStateLeft.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(entry.getColumn(), probability));
159 sumOfLeftBranch += probability;
162 auto byOneMinusP =
RawPolynomial(nominator).divideBy(oneMinusParameter);
163 if (byOneMinusP.remainder.isZero()) {
164 auto probability = toRationalFunction(byOneMinusP.quotient, denominator, cache);
165 newStateRight.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(entry.getColumn(), probability));
166 sumOfRightBranch += probability;
171 sumOfLeftBranch.simplify();
172 sumOfRightBranch.simplify();
173 for (
auto& entry : newStateLeft) {
174 entry.setValue(entry.getValue() / sumOfLeftBranch);
176 for (
auto& entry : newStateRight) {
177 entry.setValue(entry.getValue() / sumOfRightBranch);
182 queue.push(StateWithRow{currAuxState, newStateLeft});
183 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(currAuxState, (sumOfLeftBranch)*
RationalFunction(parameterPolynomial)));
185 queue.push(StateWithRow{currAuxState, newStateRight});
186 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(
190 for (
auto const& entry : outgoing) {
191 builder.
addNextValue(currRow, entry.getColumn(), entry.getValue());
197 origStates.push_back(stateWithRow.state);
199 TransformationData result;
200 result.simpleMatrix = builder.
build(currRow, currAuxState, currAuxState);
201 result.simpleStateToOriginalState = std::move(origStates);
205storm::models::sparse::StateLabeling BinaryDtmcTransformer::transformStateLabeling(storm::models::sparse::Dtmc<RationalFunction>
const& dtmc,
206 TransformationData
const& data)
const {
207 storm::models::sparse::StateLabeling labeling(data.simpleMatrix.getRowCount());
210 newStates.
resize(data.simpleMatrix.getRowCount(),
false);
211 if (labelName !=
"init") {
212 for (uint64_t newState = dtmc.
getNumberOfStates(); newState < data.simpleMatrix.getRowCount(); ++newState) {
213 newStates.
set(newState, newStates[data.simpleStateToOriginalState[newState]]);
216 labeling.addLabel(labelName, std::move(newStates));
221storm::models::sparse::StandardRewardModel<RationalFunction> BinaryDtmcTransformer::transformRewardModel(
222 storm::models::sparse::Dtmc<RationalFunction>
const& dtmc, storm::models::sparse::StandardRewardModel<RationalFunction>
const& rewardModel,
223 TransformationData
const& data)
const {
224 std::optional<std::vector<RationalFunction>> stateRewards, actionRewards;
230 return storm::models::sparse::StandardRewardModel<RationalFunction>(std::move(stateRewards), std::move(actionRewards));
This class represents a discrete-time Markov chain.
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.
std::unordered_map< std::string, RewardModelType > const & getRewardModels() const
Retrieves the reward models.
bool hasStateValuations() const
Retrieves whether this model was build with state valuations.
storm::storage::sparse::Valuations const & getStateValuations() const
Retrieves the valuations of the states of the model.
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
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.
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.
#define STORM_LOG_ERROR(message)
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
NumberTraits< RationalType >::IntegerType denominator(RationalType const &number)
carl::FactorizedPolynomial< RawPolynomial > Polynomial
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