Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MemoryStructureBuilder.cpp
Go to the documentation of this file.
6
8
9namespace storm {
10namespace storage {
11
12template<typename ValueType, typename RewardModelType>
15 bool onlyInitialStatesRelevant)
16 : model(model),
17 transitions(numberOfMemoryStates, std::vector<boost::optional<storm::storage::BitVector>>(numberOfMemoryStates)),
18 stateLabeling(numberOfMemoryStates),
19 initialMemoryStates(onlyInitialStatesRelevant ? model.getInitialStates().getNumberOfSetBits() : model.getNumberOfStates(), 0),
20 onlyInitialStatesRelevant(onlyInitialStatesRelevant) {
21 // Intentionally left empty
22}
23
24template<typename ValueType, typename RewardModelType>
27 : model(model),
28 transitions(memoryStructure.getTransitionMatrix()),
29 stateLabeling(memoryStructure.getStateLabeling()),
30 initialMemoryStates(memoryStructure.getInitialMemoryStates()),
31 onlyInitialStatesRelevant(memoryStructure.isOnlyInitialStatesRelevantSet()) {
32 // Intentionally left empty
33}
34
35template<typename ValueType, typename RewardModelType>
36void MemoryStructureBuilder<ValueType, RewardModelType>::setInitialMemoryState(uint_fast64_t initialModelState, uint_fast64_t initialMemoryState) {
37 STORM_LOG_THROW(!onlyInitialStatesRelevant || model.getInitialStates().get(initialModelState), storm::exceptions::InvalidOperationException,
38 "Invalid index of initial model state: " << initialMemoryState << ". This is not an initial state of the model.");
40 initialMemoryState < transitions.size(), storm::exceptions::InvalidOperationException,
41 "Invalid index of initial memory state: " << initialMemoryState << ". There are only " << transitions.size() << " states in this memory structure.");
42
43 auto initMemStateIt = initialMemoryStates.begin();
44
45 if (onlyInitialStatesRelevant) {
46 for (auto initState : model.getInitialStates()) {
47 if (initState == initialModelState) {
48 *initMemStateIt = initialMemoryState;
49 break;
50 }
51 ++initMemStateIt;
52 }
53 } else {
54 // Consider non-initial model states
55 for (uint_fast64_t state = 0; state < model.getNumberOfStates(); ++state) {
56 if (state == initialModelState) {
57 *initMemStateIt = initialMemoryState;
58 break;
59 }
60 ++initMemStateIt;
61 }
62 }
63
64 assert(initMemStateIt != initialMemoryStates.end());
65}
66
67template<typename ValueType, typename RewardModelType>
68void MemoryStructureBuilder<ValueType, RewardModelType>::setTransition(uint_fast64_t const& startState, uint_fast64_t const& goalState,
69 storm::storage::BitVector const& modelStates,
70 boost::optional<storm::storage::BitVector> const& modelChoices) {
71 auto const& modelTransitions = model.getTransitionMatrix();
72
73 STORM_LOG_THROW(startState < transitions.size(), storm::exceptions::InvalidOperationException,
74 "Invalid index of start state: " << startState << ". There are only " << transitions.size() << " states in this memory structure.");
75 STORM_LOG_THROW(goalState < transitions.size(), storm::exceptions::InvalidOperationException,
76 "Invalid index of goal state: " << startState << ". There are only " << transitions.size() << " states in this memory structure.");
77 STORM_LOG_THROW(modelStates.size() == modelTransitions.getRowGroupCount(), storm::exceptions::InvalidOperationException,
78 "The modelStates have invalid size.");
79 STORM_LOG_THROW(!modelChoices || modelChoices->size() == modelTransitions.getRowGroupCount(), storm::exceptions::InvalidOperationException,
80 "The modelChoices have invalid size.");
81
82 // translate the two bitvectors to a single BitVector that indicates the corresponding model transitions.
83
84 storm::storage::BitVector transitionVector(modelTransitions.getEntryCount(), false);
85 if (modelChoices) {
86 for (auto choice : modelChoices.get()) {
87 for (auto entryIt = modelTransitions.getRow(choice).begin(); entryIt < modelTransitions.getRow(choice).end(); ++entryIt) {
88 if (modelStates.get(entryIt->getColumn())) {
89 transitionVector.set(entryIt - modelTransitions.begin());
90 }
91 }
92 }
93 } else {
94 for (uint_fast64_t choice = 0; choice < modelTransitions.getRowCount(); ++choice) {
95 for (auto entryIt = modelTransitions.getRow(choice).begin(); entryIt < modelTransitions.getRow(choice).end(); ++entryIt) {
96 if (modelStates.get(entryIt->getColumn())) {
97 transitionVector.set(entryIt - modelTransitions.begin());
98 }
99 }
100 }
101 }
102
103 // Do not insert the transition if it is never taken.
104 if (transitionVector.empty()) {
105 transitions[startState][goalState] = boost::none;
106 } else {
107 transitions[startState][goalState] = std::move(transitionVector);
108 }
109}
110
111template<typename ValueType, typename RewardModelType>
112void MemoryStructureBuilder<ValueType, RewardModelType>::setLabel(uint_fast64_t const& state, std::string const& label) {
113 STORM_LOG_THROW(state < transitions.size(), storm::exceptions::InvalidOperationException,
114 "Can not add label to state with index " << state << ". There are only " << transitions.size() << " states in this memory structure.");
115 if (!stateLabeling.containsLabel(label)) {
116 stateLabeling.addLabel(label);
117 }
118 stateLabeling.addLabelToState(label, state);
119}
120
121template<typename ValueType, typename RewardModelType>
123 return MemoryStructure(std::move(transitions), std::move(stateLabeling), std::move(initialMemoryStates), onlyInitialStatesRelevant);
124}
125
126template<typename ValueType, typename RewardModelType>
133
134template class MemoryStructureBuilder<double>;
139
140} // namespace storage
141} // namespace storm
Base class for all sparse models.
Definition Model.h:33
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:162
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
void set(uint_fast64_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(uint_fast64_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
LabParser.cpp.
Definition cli.cpp:18