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
23 bool keepStateValuations)
const {
24 auto data = transformTransitions(dtmc);
26 components.
stateLabeling = transformStateLabeling(dtmc, data);
28 components.
rewardModels.emplace(rewModel.first, transformRewardModel(dtmc, rewModel.second, data));
35 return std::make_shared<storm::models::sparse::Dtmc<RationalFunction>>(std::move(components.
transitionMatrix), std::move(components.
stateLabeling),
41 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>>
row;
44typename BinaryDtmcTransformer::TransformationData BinaryDtmcTransformer::transformTransitions(
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);
60 uint64_t currAuxState = queue.size();
61 std::vector<uint64_t> origStates;
63 while (!queue.empty()) {
64 auto stateWithRow = std::move(queue.front());
67 std::set<RationalFunctionVariable> variablesInRow;
69 for (
auto const& entry : stateWithRow.row) {
70 for (
auto const& variable : entry.getValue().gatherVariables()) {
71 variablesInRow.emplace(variable);
75 if (variablesInRow.size() == 0) {
77 for (
auto const& entry : stateWithRow.row) {
78 builder.
addNextValue(currRow, entry.getColumn(), entry.getValue());
81 }
else if (variablesInRow.size() == 1) {
82 auto parameter = *variablesInRow.begin();
86 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> outgoing;
88 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateLeft;
90 std::vector<storage::MatrixEntry<uint64_t, RationalFunction>> newStateRight;
95 for (
auto const& entry : stateWithRow.row) {
96 if (entry.getValue().isConstant()) {
97 outgoing.push_back(entry);
99 auto nominator = entry.getValue().nominator();
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;
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;
117 sumOfLeftBranch.simplify();
118 sumOfRightBranch.simplify();
119 for (
auto& entry : newStateLeft) {
120 entry.setValue(entry.getValue() / sumOfLeftBranch);
122 for (
auto& entry : newStateRight) {
123 entry.setValue(entry.getValue() / sumOfRightBranch);
126 queue.push(StateWithRow{currAuxState, newStateLeft});
127 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(
128 currAuxState, (sumOfLeftBranch)*
RationalFunction(carl::makePolynomial<Polynomial>(parameter))));
130 queue.push(StateWithRow{currAuxState, newStateRight});
131 outgoing.push_back(storage::MatrixEntry<uint64_t, RationalFunction>(
135 for (
auto const& entry : outgoing) {
136 builder.
addNextValue(currRow, entry.getColumn(), entry.getValue());
142 origStates.push_back(stateWithRow.state);
144 TransformationData result;
145 result.simpleMatrix = builder.
build(currRow, currAuxState, currAuxState);
146 result.simpleStateToOriginalState = std::move(origStates);
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());
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]]);
161 labeling.addLabel(labelName, std::move(newStates));
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;
175 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.
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.
#define STORM_LOG_ERROR(message)
#define STORM_LOG_THROW(cond, exception, message)
NumberTraits< RationalType >::IntegerType denominator(RationalType const &number)
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