Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
NondeterministicModel.cpp
Go to the documentation of this file.
2
4#include "storm/io/export.h"
11
12namespace storm {
13namespace models {
14namespace sparse {
15
16template<typename ValueType, typename RewardModelType>
22
23template<typename ValueType, typename RewardModelType>
30template<typename ValueType, typename RewardModelType>
32 return this->getTransitionMatrix().getRowGroupIndices();
33}
34
35template<typename ValueType, typename RewardModelType>
37 auto indices = this->getNondeterministicChoiceIndices();
38 return indices[state + 1] - indices[state];
40
41template<typename ValueType, typename RewardModelType>
43 for (auto& rewardModel : this->getRewardModels()) {
44 rewardModel.second.reduceToStateBasedRewards(this->getTransitionMatrix(), false);
45 }
46}
47
48template<typename ValueType, typename RewardModelType>
49std::shared_ptr<storm::models::sparse::Model<ValueType, RewardModelType>> NondeterministicModel<ValueType, RewardModelType>::applyScheduler(
50 storm::storage::Scheduler<ValueType> const& scheduler, bool dropUnreachableStates, bool preserveModelType) const {
51 if (scheduler.isMemorylessScheduler() && scheduler.isDeterministicScheduler() && !scheduler.isPartialScheduler()) {
52 // Special case with improved handling.
53 storm::storage::BitVector actionSelection = scheduler.computeActionSupport(getNondeterministicChoiceIndices());
54 storm::storage::BitVector allStates(this->getNumberOfStates(), true);
56 options.makeRowGroupingTrivial = !preserveModelType;
57 auto res = storm::transformer::buildSubsystem(*this, allStates, actionSelection, !dropUnreachableStates, options);
58 return res.model;
59 } else {
61 if (!dropUnreachableStates) {
62 memoryProduct.setBuildFullProduct();
63 }
64 return memoryProduct.build(preserveModelType);
65 }
66}
68template<typename ValueType, typename RewardModelType>
70 this->printModelInformationHeaderToStream(out);
71 out << "Choices: \t" << this->getNumberOfChoices() << '\n';
72 this->printModelInformationFooterToStream(out);
73}
74
75template<typename ValueType, typename RewardModelType>
76void NondeterministicModel<ValueType, RewardModelType>::writeDotToStream(std::ostream& outStream, size_t maxWidthLabel, bool includeLabeling,
77 storm::storage::BitVector const* subsystem, std::vector<ValueType> const* firstValue,
78 std::vector<ValueType> const* secondValue,
79 std::vector<uint_fast64_t> const* stateColoring,
80 std::vector<std::string> const* colors, std::vector<uint_fast64_t>* scheduler,
81 bool finalizeOutput) const {
82 Model<ValueType, RewardModelType>::writeDotToStream(outStream, maxWidthLabel, includeLabeling, subsystem, firstValue, secondValue, stateColoring, colors,
83 scheduler, false);
84
85 // Write the probability distributions for all the states.
86 for (uint_fast64_t state = 0; state < this->getNumberOfStates(); ++state) {
87 uint_fast64_t rowCount = this->getNondeterministicChoiceIndices()[state + 1] - this->getNondeterministicChoiceIndices()[state];
88 bool highlightChoice = true;
89
90 // For this, we need to iterate over all available nondeterministic choices in the current state.
91 for (uint_fast64_t choice = 0; choice < rowCount; ++choice) {
92 uint_fast64_t rowIndex = this->getNondeterministicChoiceIndices()[state] + choice;
93 typename storm::storage::SparseMatrix<ValueType>::const_rows row = this->getTransitionMatrix().getRow(rowIndex);
94
95 if (scheduler != nullptr) {
96 // If the scheduler picked the current choice, we will not make it dotted, but highlight it.
97 if ((*scheduler)[state] == choice) {
98 highlightChoice = true;
99 } else {
100 highlightChoice = false;
101 }
102 }
103
104 // For each nondeterministic choice, we draw an arrow to an intermediate node to better display
105 // the grouping of transitions.
106
107 // The intermediate node:
108 outStream << "\t\"" << state << "c" << choice << "\" [shape = \"point\"";
109 // If we were given a scheduler to highlight, we do so now.
110 if (scheduler != nullptr) {
111 if (highlightChoice) {
112 outStream << ", fillcolor=\"red\"";
113 }
114 }
115 outStream << "];\n";
116
117 // The arrow to the intermediate node:
118 outStream << "\t" << state << " -> \"" << state << "c" << choice << "\"";
119 bool arrowHasLabel = false;
120 if (this->isOfType(ModelType::MarkovAutomaton)) {
121 // If this is a Markov automaton, we have to check whether the current choice is a Markovian choice and correspondingly print the exit rate
123 if (ma->isMarkovianState(state) && choice == 0) {
124 arrowHasLabel = true;
125 outStream << " [ label = \"" << ma->getExitRate(state);
126 }
127 }
128 if (this->hasChoiceLabeling()) {
129 if (arrowHasLabel) {
130 outStream << " | {";
131 } else {
132 outStream << " [ label = \"{";
133 }
134 arrowHasLabel = true;
135 storm::io::outputFixedWidth(outStream, this->getChoiceLabeling().getLabelsOfChoice(rowIndex), maxWidthLabel);
136 outStream << "}";
137 }
138 if (arrowHasLabel) {
139 outStream << "\"";
140 }
141 // If we were given a scheduler to highlight, we do so now.
142 if (scheduler != nullptr) {
143 if (arrowHasLabel) {
144 outStream << ", ";
145 } else {
146 outStream << "[ ";
147 }
148 if (highlightChoice) {
149 outStream << "color=\"red\", penwidth = 2";
150 } else {
151 outStream << "style = \"dotted\"";
152 }
153 }
154
155 if (arrowHasLabel || scheduler != nullptr) {
156 outStream << "]\n";
157 }
158 outStream << ";\n";
159
160 // Now draw all probabilistic arcs that belong to this non-deterministic choice.
161 for (auto const& transition : row) {
162 if (subsystem == nullptr || subsystem->get(transition.getColumn())) {
163 outStream << "\t\"" << state << "c" << choice << "\" -> " << transition.getColumn() << " [ label= \"" << transition.getValue() << "\" ]";
164
165 // If we were given a scheduler to highlight, we do so now.
166 if (scheduler != nullptr) {
167 if (highlightChoice) {
168 outStream << " [color=\"red\", penwidth = 2]";
169 } else {
170 outStream << " [style = \"dotted\"]";
171 }
172 }
173 outStream << ";\n";
174 }
175 }
176 }
177 }
178
179 if (finalizeOutput) {
180 outStream << "}\n";
181 }
182}
183
184template<typename ValueType, typename RewardModelType>
186 return this->getNondeterministicChoiceIndices()[stateactPair.getState()] + stateactPair.getAction();
187}
188
189template class NondeterministicModel<double>;
194} // namespace sparse
195} // namespace models
196} // namespace storm
virtual uint_fast64_t getNumberOfChoices() const =0
Returns the number of choices ine the model.
This class represents a Markov automaton.
bool isMarkovianState(storm::storage::sparse::state_type state) const
Retrieves whether the given state is a Markovian state.
ValueType const & getExitRate(storm::storage::sparse::state_type state) const
Retrieves the exit rate of the given state.
Base class for all sparse models.
Definition Model.h:32
virtual void writeDotToStream(std::ostream &outStream, size_t maxWidthLabel=30, bool includeLabeling=true, storm::storage::BitVector const *subsystem=nullptr, std::vector< ValueType > const *firstValue=nullptr, std::vector< ValueType > const *secondValue=nullptr, std::vector< uint64_t > const *stateColoring=nullptr, std::vector< std::string > const *colors=nullptr, std::vector< uint_fast64_t > *scheduler=nullptr, bool finalizeOutput=true) const
Exports the model to the dot-format and prints the result to the given stream.
The base class of sparse nondeterministic models.
std::vector< uint_fast64_t > const & getNondeterministicChoiceIndices() const
Retrieves the vector indicating which matrix rows represent non-deterministic choices of a certain st...
virtual void printModelInformationToStream(std::ostream &out) const override
Prints information about the model to the specified stream.
NondeterministicModel(ModelType modelType, storm::storage::sparse::ModelComponents< ValueType, RewardModelType > const &components)
Constructs a model from the given data.
std::shared_ptr< storm::models::sparse::Model< ValueType, RewardModelType > > applyScheduler(storm::storage::Scheduler< ValueType > const &scheduler, bool dropUnreachableStates=true, bool preserveModelType=false) const
Applies the given scheduler to this model.
virtual void reduceToStateBasedRewards() override
Converts the transition rewards of all reward models to state-based rewards.
virtual void writeDotToStream(std::ostream &outStream, size_t maxWidthLabel=30, bool includeLabeling=true, storm::storage::BitVector const *subsystem=nullptr, std::vector< ValueType > const *firstValue=nullptr, std::vector< ValueType > const *secondValue=nullptr, std::vector< uint_fast64_t > const *stateColoring=nullptr, std::vector< std::string > const *colors=nullptr, std::vector< uint_fast64_t > *scheduler=nullptr, bool finalizeOutput=true) const override
uint_fast64_t getChoiceIndex(storm::storage::StateActionPair const &stateactPair) const
For a state/action pair, get the choice index referring to the state-action pair.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
This class defines which action is chosen in a particular state of a non-deterministic model.
Definition Scheduler.h:18
bool isDeterministicScheduler() const
Retrieves whether all defined choices are deterministic.
storm::storage::BitVector computeActionSupport(std::vector< uint64_t > const &nondeterministicChoiceIndicies) const
Compute the Action Support: A bit vector that indicates all actions that are selected with positive p...
bool isPartialScheduler() const
Retrieves whether there is a reachable pair of model and memory state for which the choice is undefin...
bool isMemorylessScheduler() const
Retrieves whether the scheduler considers a trivial memory structure (i.e., a memory structure with j...
This class represents a number of consecutive rows of the matrix.
This class builds the product of the given sparse model and the given memory structure.
uint_fast64_t getState() const
uint_fast64_t getAction() const
void outputFixedWidth(std::ostream &stream, Container const &output, size_t maxWidth=30)
Output list of strings with linebreaks according to fixed width.
Definition export.h:60
SubsystemBuilderReturnType< ValueType, RewardModelType > buildSubsystem(storm::models::sparse::Model< ValueType, RewardModelType > const &originalModel, storm::storage::BitVector const &subsystemStates, storm::storage::BitVector const &subsystemActions, bool keepUnreachableStates, SubsystemBuilderOptions options)