Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MemoryStructureBuilder.cpp
Go to the documentation of this file.
2
10
11namespace storm {
12namespace storage {
13
14template<typename ValueType, typename RewardModelType>
17 bool onlyInitialStatesRelevant)
18 : model(model),
19 transitions(numberOfMemoryStates, std::vector<boost::optional<storm::storage::BitVector>>(numberOfMemoryStates)),
20 stateLabeling(numberOfMemoryStates),
21 initialMemoryStates(onlyInitialStatesRelevant ? model.getInitialStates().getNumberOfSetBits() : model.getNumberOfStates(), 0),
22 onlyInitialStatesRelevant(onlyInitialStatesRelevant) {
23 // Intentionally left empty
24}
25
26template<typename ValueType, typename RewardModelType>
29 : model(model),
30 transitions(memoryStructure.getTransitionMatrix()),
31 stateLabeling(memoryStructure.getStateLabeling()),
32 initialMemoryStates(memoryStructure.getInitialMemoryStates()),
33 onlyInitialStatesRelevant(memoryStructure.isOnlyInitialStatesRelevantSet()) {
34 // Intentionally left empty
35}
36
37template<typename ValueType, typename RewardModelType>
38void MemoryStructureBuilder<ValueType, RewardModelType>::setInitialMemoryState(uint_fast64_t initialModelState, uint_fast64_t initialMemoryState) {
39 STORM_LOG_THROW(!onlyInitialStatesRelevant || model.getInitialStates().get(initialModelState), storm::exceptions::InvalidOperationException,
40 "Invalid index of initial model state: " << initialMemoryState << ". This is not an initial state of the model.");
42 initialMemoryState < transitions.size(), storm::exceptions::InvalidOperationException,
43 "Invalid index of initial memory state: " << initialMemoryState << ". There are only " << transitions.size() << " states in this memory structure.");
44
45 auto initMemStateIt = initialMemoryStates.begin();
46
47 if (onlyInitialStatesRelevant) {
48 for (auto initState : model.getInitialStates()) {
49 if (initState == initialModelState) {
50 *initMemStateIt = initialMemoryState;
51 break;
52 }
53 ++initMemStateIt;
54 }
55 } else {
56 // Consider non-initial model states
57 for (uint_fast64_t state = 0; state < model.getNumberOfStates(); ++state) {
58 if (state == initialModelState) {
59 *initMemStateIt = initialMemoryState;
60 break;
61 }
62 ++initMemStateIt;
63 }
64 }
65
66 assert(initMemStateIt != initialMemoryStates.end());
67}
68
69template<typename ValueType, typename RewardModelType>
70void MemoryStructureBuilder<ValueType, RewardModelType>::setTransition(uint_fast64_t const& startState, uint_fast64_t const& goalState,
71 storm::storage::BitVector const& modelStates,
72 boost::optional<storm::storage::BitVector> const& modelChoices) {
73 auto const& modelTransitions = model.getTransitionMatrix();
74
75 STORM_LOG_THROW(startState < transitions.size(), storm::exceptions::InvalidOperationException,
76 "Invalid index of start state: " << startState << ". There are only " << transitions.size() << " states in this memory structure.");
77 STORM_LOG_THROW(goalState < transitions.size(), storm::exceptions::InvalidOperationException,
78 "Invalid index of goal state: " << startState << ". There are only " << transitions.size() << " states in this memory structure.");
79 STORM_LOG_THROW(modelStates.size() == modelTransitions.getRowGroupCount(), storm::exceptions::InvalidOperationException,
80 "The modelStates have invalid size.");
81 STORM_LOG_THROW(!modelChoices || modelChoices->size() == modelTransitions.getRowGroupCount(), storm::exceptions::InvalidOperationException,
82 "The modelChoices have invalid size.");
83
84 // translate the two bitvectors to a single BitVector that indicates the corresponding model transitions.
85
86 storm::storage::BitVector transitionVector(modelTransitions.getEntryCount(), false);
87 if (modelChoices) {
88 for (auto choice : modelChoices.get()) {
89 for (auto entryIt = modelTransitions.getRow(choice).begin(); entryIt < modelTransitions.getRow(choice).end(); ++entryIt) {
90 if (modelStates.get(entryIt->getColumn())) {
91 transitionVector.set(entryIt - modelTransitions.begin());
92 }
93 }
94 }
95 } else {
96 for (uint_fast64_t choice = 0; choice < modelTransitions.getRowCount(); ++choice) {
97 for (auto entryIt = modelTransitions.getRow(choice).begin(); entryIt < modelTransitions.getRow(choice).end(); ++entryIt) {
98 if (modelStates.get(entryIt->getColumn())) {
99 transitionVector.set(entryIt - modelTransitions.begin());
100 }
101 }
102 }
103 }
104
105 // Do not insert the transition if it is never taken.
106 if (transitionVector.empty()) {
107 transitions[startState][goalState] = boost::none;
108 } else {
109 transitions[startState][goalState] = std::move(transitionVector);
110 }
111}
112
113template<typename ValueType, typename RewardModelType>
114void MemoryStructureBuilder<ValueType, RewardModelType>::setLabel(uint_fast64_t const& state, std::string const& label) {
115 STORM_LOG_THROW(state < transitions.size(), storm::exceptions::InvalidOperationException,
116 "Can not add label to state with index " << state << ". There are only " << transitions.size() << " states in this memory structure.");
117 if (!stateLabeling.containsLabel(label)) {
118 stateLabeling.addLabel(label);
119 }
120 stateLabeling.addLabelToState(label, state);
121}
122
123template<typename ValueType, typename RewardModelType>
125 return MemoryStructure(std::move(transitions), std::move(stateLabeling), std::move(initialMemoryStates), onlyInitialStatesRelevant);
126}
127
128template<typename ValueType, typename RewardModelType>
135
136template class MemoryStructureBuilder<double>;
141
142} // namespace storage
143} // namespace storm
Base class for all sparse models.
Definition Model.h:32
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:164
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
size_t size() const
Retrieves the number of bits this bit vector can store.
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
void setInitialMemoryState(uint_fast64_t initialModelState, uint_fast64_t initialMemoryState)
Specifies for the given state of the model the corresponding initial memory state.
MemoryStructureBuilder(uint_fast64_t numberOfMemoryStates, storm::models::sparse::Model< ValueType, RewardModelType > const &model, bool onlyInitialStatesRelevant=true)
Initializes a new builder with the data from the provided memory structure.
MemoryStructure build()
Builds the memory structure.
static MemoryStructure buildTrivialMemoryStructure(storm::models::sparse::Model< ValueType, RewardModelType > const &model)
Builds a trivial memory structure for the given model (consisting of a single memory state)
void setTransition(uint_fast64_t const &startState, uint_fast64_t const &goalState, storm::storage::BitVector const &modelStates, boost::optional< storm::storage::BitVector > const &modelChoices=boost::none)
Specifies a transition of the memory structure.
void setLabel(uint_fast64_t const &state, std::string const &label)
Sets a label to the given memory state.
This class represents a (deterministic) memory structure that can be used to encode certain events (s...
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30