Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MemoryStructure.cpp
Go to the documentation of this file.
2
3#include <iostream>
4
8
10
11namespace storm {
12namespace storage {
13
15 std::vector<uint_fast64_t> const& initialMemoryStates, bool onlyInitialStatesRelevant)
16 : transitions(transitionMatrix),
17 stateLabeling(memoryStateLabeling),
18 initialMemoryStates(initialMemoryStates),
19 onlyInitialStatesRelevant(onlyInitialStatesRelevant) {
20 // intentionally left empty
21}
22
24 std::vector<uint_fast64_t>&& initialMemoryStates, bool onlyInitialStatesRelevant)
25 : transitions(std::move(transitionMatrix)),
26 stateLabeling(std::move(memoryStateLabeling)),
27 initialMemoryStates(std::move(initialMemoryStates)),
28 onlyInitialStatesRelevant(onlyInitialStatesRelevant) {
29 // intentionally left empty
30}
31
33 return onlyInitialStatesRelevant;
34}
35
39
41 return stateLabeling;
42}
43
44std::vector<uint_fast64_t> const& MemoryStructure::getInitialMemoryStates() const {
45 return initialMemoryStates;
46}
47
49 return transitions.size();
50}
51
52uint_fast64_t MemoryStructure::getSuccessorMemoryState(uint_fast64_t const& currentMemoryState, uint_fast64_t const& modelTransitionIndex) const {
53 for (uint_fast64_t successorMemState = 0; successorMemState < getNumberOfStates(); ++successorMemState) {
54 boost::optional<storm::storage::BitVector> const& matrixEntry = transitions[currentMemoryState][successorMemState];
55 if ((matrixEntry) && matrixEntry.get().get(modelTransitionIndex)) {
56 return successorMemState;
57 }
58 }
59 STORM_LOG_THROW(false, storm::exceptions::InvalidOperationException, "The successor memorystate for the given transition could not be found.");
60 return getNumberOfStates();
61}
62
64 uint_fast64_t lhsNumStates = this->getTransitionMatrix().size();
65 uint_fast64_t rhsNumStates = rhs.getTransitionMatrix().size();
66 uint_fast64_t resNumStates = lhsNumStates * rhsNumStates;
67
68 // Transition matrix
69 TransitionMatrix resultTransitions(resNumStates, std::vector<boost::optional<storm::storage::BitVector>>(resNumStates));
70 uint_fast64_t resState = 0;
71 for (uint_fast64_t lhsState = 0; lhsState < lhsNumStates; ++lhsState) {
72 for (uint_fast64_t rhsState = 0; rhsState < rhsNumStates; ++rhsState) {
73 assert(resState == (lhsState * rhsNumStates) + rhsState);
74 auto& resStateTransitions = resultTransitions[resState];
75 for (uint_fast64_t lhsTransitionTarget = 0; lhsTransitionTarget < lhsNumStates; ++lhsTransitionTarget) {
76 auto& lhsTransition = this->getTransitionMatrix()[lhsState][lhsTransitionTarget];
77 if (lhsTransition) {
78 for (uint_fast64_t rhsTransitionTarget = 0; rhsTransitionTarget < rhsNumStates; ++rhsTransitionTarget) {
79 auto& rhsTransition = rhs.getTransitionMatrix()[rhsState][rhsTransitionTarget];
80 if (rhsTransition) {
81 uint_fast64_t resTransitionTarget = (lhsTransitionTarget * rhsNumStates) + rhsTransitionTarget;
82 resStateTransitions[resTransitionTarget] = rhsTransition.get() & lhsTransition.get();
83 // If it is not possible to take the considered transition w.r.t. the considered model, we can delete it.
84 if (resStateTransitions[resTransitionTarget]->empty()) {
85 resStateTransitions[resTransitionTarget] = boost::none;
86 }
87 }
88 }
89 }
90 }
91 ++resState;
92 }
93 }
94
95 // State Labels
96 storm::models::sparse::StateLabeling resultLabeling(resNumStates);
97 for (std::string lhsLabel : this->getStateLabeling().getLabels()) {
98 storm::storage::BitVector const& lhsLabeledStates = this->getStateLabeling().getStates(lhsLabel);
99 storm::storage::BitVector resLabeledStates(resNumStates, false);
100 for (auto lhsState : lhsLabeledStates) {
101 for (uint_fast64_t rhsState = 0; rhsState < rhsNumStates; ++rhsState) {
102 resState = (lhsState * rhsNumStates) + rhsState;
103 resLabeledStates.set(resState, true);
104 }
105 }
106 resultLabeling.addLabel(lhsLabel, std::move(resLabeledStates));
107 }
108 for (std::string rhsLabel : rhs.getStateLabeling().getLabels()) {
110 !resultLabeling.containsLabel(rhsLabel), storm::exceptions::InvalidOperationException,
111 "Failed to build the product of two memory structures: State labelings are not disjoint as both structures contain the label " << rhsLabel << ".");
112 storm::storage::BitVector const& rhsLabeledStates = rhs.getStateLabeling().getStates(rhsLabel);
113 storm::storage::BitVector resLabeledStates(resNumStates, false);
114 for (auto rhsState : rhsLabeledStates) {
115 for (uint_fast64_t lhsState = 0; lhsState < lhsNumStates; ++lhsState) {
116 resState = (lhsState * rhsNumStates) + rhsState;
117 resLabeledStates.set(resState, true);
118 }
119 }
120 resultLabeling.addLabel(rhsLabel, std::move(resLabeledStates));
121 }
122
123 // Initial States
124 std::vector<uint_fast64_t> resultInitialMemoryStates;
125 STORM_LOG_THROW(this->getInitialMemoryStates().size() == rhs.getInitialMemoryStates().size(), storm::exceptions::InvalidOperationException,
126 "Tried to build the product of two memory structures that consider a different number of initial model states.");
127 resultInitialMemoryStates.reserve(this->getInitialMemoryStates().size());
128 auto lhsStateIt = this->getInitialMemoryStates().begin();
129 auto rhsStateIt = rhs.getInitialMemoryStates().begin();
130 for (; lhsStateIt != this->getInitialMemoryStates().end(); ++lhsStateIt, ++rhsStateIt) {
131 resultInitialMemoryStates.push_back(*lhsStateIt * rhsNumStates + *rhsStateIt);
132 }
133
134 return MemoryStructure(std::move(resultTransitions), std::move(resultLabeling), std::move(resultInitialMemoryStates));
135}
136
137template<typename ValueType, typename RewardModelType>
142
143std::string MemoryStructure::toString() const {
144 std::stringstream stream;
145
146 stream << "Memory Structure with " << getNumberOfStates() << " states: \n";
147
148 for (uint_fast64_t state = 0; state < getNumberOfStates(); ++state) {
149 stream << "State " << state << ": Labels = {";
150 bool firstLabel = true;
151 for (auto const& label : getStateLabeling().getLabelsOfState(state)) {
152 if (!firstLabel) {
153 stream << ", ";
154 }
155 firstLabel = false;
156 stream << label;
157 }
158 stream << "}, Transitions: \n";
159 for (uint_fast64_t transitionTarget = 0; transitionTarget < getNumberOfStates(); ++transitionTarget) {
160 stream << "\t From " << state << " to " << transitionTarget << ": \t";
161 auto const& transition = getTransitionMatrix()[state][transitionTarget];
162 if (transition) {
163 stream << *transition;
164 } else {
165 stream << "false";
166 }
167 stream << '\n';
168 }
169 }
170
171 return stream.str();
172}
173
180} // namespace storage
181} // namespace storm
std::set< std::string > getLabels() const
Retrieves the set of labels contained in this labeling.
void addLabel(std::string const &label)
Adds a new label to the labelings.
bool containsLabel(std::string const &label) const
Checks whether a label is registered within this labeling.
Base class for all sparse models.
Definition Model.h:33
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.
std::set< std::string > getLabelsOfState(storm::storage::sparse::state_type state) const
Retrieves the set of labels attached to the given state.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
void set(uint_fast64_t index, bool value=true)
Sets the given truth value at the given index.
This class represents a (deterministic) memory structure that can be used to encode certain events (s...
TransitionMatrix const & getTransitionMatrix() const
MemoryStructure product(MemoryStructure const &rhs) const
Builds the product of this memory structure and the given memory structure.
MemoryStructure(TransitionMatrix const &transitionMatrix, storm::models::sparse::StateLabeling const &memoryStateLabeling, std::vector< uint_fast64_t > const &initialMemoryStates, bool onlyInitialStatesRelevant=true)
Creates a memory structure with the given transition matrix, the given memory state labeling,...
std::vector< std::vector< boost::optional< storm::storage::BitVector > > > TransitionMatrix
std::vector< uint_fast64_t > const & getInitialMemoryStates() const
uint_fast64_t getSuccessorMemoryState(uint_fast64_t const &currentMemoryState, uint_fast64_t const &modelTransitionIndex) const
storm::models::sparse::StateLabeling const & getStateLabeling() const
uint_fast64_t getNumberOfStates() const
This class builds the product of the given sparse model and the given memory structure.
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
LabParser.cpp.
Definition cli.cpp:18