Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
NondeterministicModel.cpp
Go to the documentation of this file.
2
5#include "storm/io/export.h"
12
13namespace storm {
14namespace models {
15namespace sparse {
16
17template<typename ValueType, typename RewardModelType>
23
24template<typename ValueType, typename RewardModelType>
31template<typename ValueType, typename RewardModelType>
33 return this->getTransitionMatrix().getRowGroupIndices();
34}
35
36template<typename ValueType, typename RewardModelType>
38 auto indices = this->getNondeterministicChoiceIndices();
39 return indices[state + 1] - indices[state];
41
42template<typename ValueType, typename RewardModelType>
44 for (auto& rewardModel : this->getRewardModels()) {
45 rewardModel.second.reduceToStateBasedRewards(this->getTransitionMatrix(), false);
46 }
47}
48
49template<typename ValueType, typename RewardModelType>
50std::shared_ptr<storm::models::sparse::Model<ValueType, RewardModelType>> NondeterministicModel<ValueType, RewardModelType>::applyScheduler(
51 storm::storage::Scheduler<ValueType> const& scheduler, bool dropUnreachableStates, bool preserveModelType) const {
52 if (scheduler.isMemorylessScheduler() && scheduler.isDeterministicScheduler() && !scheduler.isPartialScheduler()) {
53 // Special case with improved handling.
54 storm::storage::BitVector actionSelection = scheduler.computeActionSupport(getNondeterministicChoiceIndices());
55 storm::storage::BitVector allStates(this->getNumberOfStates(), true);
57 options.makeRowGroupingTrivial = !preserveModelType;
58 auto res = storm::transformer::buildSubsystem(*this, allStates, actionSelection, !dropUnreachableStates, options);
59 return res.model;
60 } else {
62 if (!dropUnreachableStates) {
63 memoryProduct.setBuildFullProduct();
64 }
65 return memoryProduct.build();
66 }
67}
69template<typename ValueType, typename RewardModelType>
71 this->printModelInformationHeaderToStream(out);
72 out << "Choices: \t" << this->getNumberOfChoices() << '\n';
73 this->printModelInformationFooterToStream(out);
74}
75
76template<typename ValueType, typename RewardModelType>
77void NondeterministicModel<ValueType, RewardModelType>::writeDotToStream(std::ostream& outStream, size_t maxWidthLabel, bool includeLabeling,
78 storm::storage::BitVector const* subsystem, std::vector<ValueType> const* firstValue,
79 std::vector<ValueType> const* secondValue,
80 std::vector<uint_fast64_t> const* stateColoring,
81 std::vector<std::string> const* colors, std::vector<uint_fast64_t>* scheduler,
82 bool finalizeOutput) const {
83 Model<ValueType, RewardModelType>::writeDotToStream(outStream, maxWidthLabel, includeLabeling, subsystem, firstValue, secondValue, stateColoring, colors,
84 scheduler, false);
85
86 // Write the probability distributions for all the states.
87 for (uint_fast64_t state = 0; state < this->getNumberOfStates(); ++state) {
88 uint_fast64_t rowCount = this->getNondeterministicChoiceIndices()[state + 1] - this->getNondeterministicChoiceIndices()[state];
89 bool highlightChoice = true;
90
91 // For this, we need to iterate over all available nondeterministic choices in the current state.
92 for (uint_fast64_t choice = 0; choice < rowCount; ++choice) {
93 uint_fast64_t rowIndex = this->getNondeterministicChoiceIndices()[state] + choice;
94 typename storm::storage::SparseMatrix<ValueType>::const_rows row = this->getTransitionMatrix().getRow(rowIndex);
95
96 if (scheduler != nullptr) {
97 // If the scheduler picked the current choice, we will not make it dotted, but highlight it.
98 if ((*scheduler)[state] == choice) {
99 highlightChoice = true;
100 } else {
101 highlightChoice = false;
102 }
103 }
104
105 // For each nondeterministic choice, we draw an arrow to an intermediate node to better display
106 // the grouping of transitions.
107
108 // The intermediate node:
109 outStream << "\t\"" << state << "c" << choice << "\" [shape = \"point\"";
110 // If we were given a scheduler to highlight, we do so now.
111 if (scheduler != nullptr) {
112 if (highlightChoice) {
113 outStream << ", fillcolor=\"red\"";
114 }
115 }
116 outStream << "];\n";
117
118 // The arrow to the intermediate node:
119 outStream << "\t" << state << " -> \"" << state << "c" << choice << "\"";
120 bool arrowHasLabel = false;
121 if (this->isOfType(ModelType::MarkovAutomaton)) {
122 // If this is a Markov automaton, we have to check whether the current choice is a Markovian choice and correspondingly print the exit rate
124 if (ma->isMarkovianState(state) && choice == 0) {
125 arrowHasLabel = true;
126 outStream << " [ label = \"" << ma->getExitRate(state);
127 }
128 }
129 if (this->hasChoiceLabeling()) {
130 if (arrowHasLabel) {
131 outStream << " | {";
132 } else {
133 outStream << " [ label = \"{";
134 }
135 arrowHasLabel = true;
136 storm::io::outputFixedWidth(outStream, this->getChoiceLabeling().getLabelsOfChoice(rowIndex), maxWidthLabel);
137 outStream << "}";
138 }
139 if (arrowHasLabel) {
140 outStream << "\"";
141 }
142 // If we were given a scheduler to highlight, we do so now.
143 if (scheduler != nullptr) {
144 if (arrowHasLabel) {
145 outStream << ", ";
146 } else {
147 outStream << "[ ";
148 }
149 if (highlightChoice) {
150 outStream << "color=\"red\", penwidth = 2";
151 } else {
152 outStream << "style = \"dotted\"";
153 }
154 }
155
156 if (arrowHasLabel || scheduler != nullptr) {
157 outStream << "]\n";
158 }
159 outStream << ";\n";
160
161 // Now draw all probabilistic arcs that belong to this non-deterministic choice.
162 for (auto const& transition : row) {
163 if (subsystem == nullptr || subsystem->get(transition.getColumn())) {
164 outStream << "\t\"" << state << "c" << choice << "\" -> " << transition.getColumn() << " [ label= \"" << transition.getValue() << "\" ]";
165
166 // If we were given a scheduler to highlight, we do so now.
167 if (scheduler != nullptr) {
168 if (highlightChoice) {
169 outStream << " [color=\"red\", penwidth = 2]";
170 } else {
171 outStream << " [style = \"dotted\"]";
172 }
173 }
174 outStream << ";\n";
175 }
176 }
177 }
178 }
179
180 if (finalizeOutput) {
181 outStream << "}\n";
182 }
183}
184
185template<typename ValueType, typename RewardModelType>
187 return this->getNondeterministicChoiceIndices()[stateactPair.getState()] + stateactPair.getAction();
188}
189
190template class NondeterministicModel<double>;
191
192#ifdef STORM_HAVE_CARL
197#endif
198} // namespace sparse
199} // namespace models
200} // 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:33
CRewardModelType RewardModelType
Definition Model.h:36
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:18
bool get(uint_fast64_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)
LabParser.cpp.
Definition cli.cpp:18