Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
NextStateGenerator.cpp
Go to the documentation of this file.
4
7
9
13
15
17
18namespace storm {
19namespace generator {
20
21template<typename ValueType, typename StateType>
23 : func(f) {
24 // Intentionally left empty
25}
26
27template<typename ValueType, typename StateType>
29 uint64_t actionIndex) {
30 auto val = generator.currentStateToSimpleValuation();
31 bool res = func(val, actionIndex);
32 return res;
33}
34
35template<typename ValueType, typename StateType>
37 VariableInformation const& variableInformation, NextStateGeneratorOptions const& options,
38 std::shared_ptr<ActionMask<ValueType, StateType>> const& mask)
39 : options(options),
40 expressionManager(expressionManager.getSharedPointer()),
41 variableInformation(variableInformation),
42 evaluator(nullptr),
43 state(nullptr),
44 actionMask(mask) {
46}
47
48template<typename ValueType, typename StateType>
50 NextStateGeneratorOptions const& options,
51 std::shared_ptr<ActionMask<ValueType, StateType>> const& mask)
52 : options(options), expressionManager(expressionManager.getSharedPointer()), variableInformation(), evaluator(nullptr), state(nullptr), actionMask(mask) {}
53
54template<typename ValueType, typename StateType>
56
57template<typename ValueType, typename StateType>
61
62template<typename ValueType, typename StateType>
64 return variableInformation.getTotalBitOffset(true);
65}
66
67template<typename ValueType, typename StateType>
69 if (variableInformation.hasOutOfBoundsBit()) {
70 outOfBoundsState = createOutOfBoundsState(variableInformation);
71 }
72 if (options.isAddOverlappingGuardLabelSet()) {
73 overlappingGuardStates = std::vector<uint64_t>();
74 }
75}
76
77template<typename ValueType, typename StateType>
80 for (auto const& v : variableInformation.locationVariables) {
81 result.addVariable(v.variable);
82 }
83 for (auto const& v : variableInformation.booleanVariables) {
84 result.addVariable(v.variable);
85 }
86 for (auto const& v : variableInformation.integerVariables) {
87 result.addVariable(v.variable);
88 }
89 return result;
90}
91
92template<typename ValueType, typename StateType>
95 for (auto const& v : variableInformation.booleanVariables) {
96 if (v.observable) {
97 result.addVariable(v.variable);
98 }
99 }
100 for (auto const& v : variableInformation.integerVariables) {
101 if (v.observable) {
102 result.addVariable(v.variable);
103 }
105 for (auto const& l : variableInformation.observationLabels) {
106 result.addObservationLabel(l.name);
108 return result;
109}
111template<typename ValueType, typename StateType>
113 // Since almost all subsequent operations are based on the evaluator, we load the state into it now.
114 unpackStateIntoEvaluator(state, variableInformation, *evaluator);
116 // Also, we need to store a pointer to the state itself, because we need to be able to access it when expanding it.
117 this->state = &state;
119
120template<typename ValueType, typename StateType>
122 if (expression.isTrue()) {
123 return true;
124 }
125 return evaluator->asBool(expression);
126}
127
128template<typename ValueType, typename StateType>
130 return variableInformation;
132
133template<typename ValueType, typename StateType>
135 storm::storage::sparse::StateValuationsBuilder& valuationsBuilder) const {
136 std::vector<bool> booleanValues;
137 booleanValues.reserve(variableInformation.booleanVariables.size());
138 std::vector<int64_t> integerValues;
139 integerValues.reserve(variableInformation.locationVariables.size() + variableInformation.integerVariables.size());
140 extractVariableValues(*this->state, variableInformation, integerValues, booleanValues, integerValues);
141 valuationsBuilder.addState(currentStateIndex, std::move(booleanValues), std::move(integerValues));
142}
143
144template<typename ValueType, typename StateType>
146 storm::storage::sparse::StateValuationsBuilder valuationsBuilder = initializeObservationValuationsBuilder();
147 for (auto const& observationEntry : observabilityMap) {
148 std::vector<bool> booleanValues;
149 booleanValues.reserve(variableInformation.booleanVariables.size()); // TODO: use number of observable boolean variables
150 std::vector<int64_t> integerValues;
151 integerValues.reserve(variableInformation.locationVariables.size() +
152 variableInformation.integerVariables.size()); // TODO: use number of observable integer variables
153 std::vector<int64_t> observationLabelValues;
154 observationLabelValues.reserve(variableInformation.observationLabels.size());
155 expressions::SimpleValuation val = unpackStateIntoValuation(observationEntry.first, variableInformation, *expressionManager);
156 for (auto const& v : variableInformation.booleanVariables) {
157 if (v.observable) {
158 booleanValues.push_back(val.getBooleanValue(v.variable));
159 }
160 }
161 for (auto const& v : variableInformation.integerVariables) {
162 if (v.observable) {
163 integerValues.push_back(val.getIntegerValue(v.variable));
165 }
166 for (uint64_t labelStart = variableInformation.getTotalBitOffset(true); labelStart < observationEntry.first.size(); labelStart += 64) {
167 observationLabelValues.push_back(observationEntry.first.getAsInt(labelStart, 64));
169 valuationsBuilder.addState(observationEntry.second, std::move(booleanValues), std::move(integerValues), {}, std::move(observationLabelValues));
171 return valuationsBuilder.build();
173
174template<typename ValueType, typename StateType>
176 storm::storage::sparse::StateStorage<StateType> const& stateStorage, std::vector<StateType> const& initialStateIndices,
177 std::vector<StateType> const& deadlockStateIndices, std::vector<StateType> const& unexploredStateIndices,
178 std::vector<std::pair<std::string, storm::expressions::Expression>> labelsAndExpressions) {
179 labelsAndExpressions.insert(labelsAndExpressions.end(), this->options.getExpressionLabels().begin(), this->options.getExpressionLabels().end());
180
181 // Make the labels unique.
182 std::sort(labelsAndExpressions.begin(), labelsAndExpressions.end(),
183 [](std::pair<std::string, storm::expressions::Expression> const& a, std::pair<std::string, storm::expressions::Expression> const& b) {
184 return a.first < b.first;
185 });
186 auto it = std::unique(labelsAndExpressions.begin(), labelsAndExpressions.end(),
187 [](std::pair<std::string, storm::expressions::Expression> const& a, std::pair<std::string, storm::expressions::Expression> const& b) {
188 return a.first == b.first;
189 });
190 labelsAndExpressions.resize(std::distance(labelsAndExpressions.begin(), it));
191
192 // Prepare result.
194
195 // Initialize labeling.
196 for (auto const& label : labelsAndExpressions) {
197 result.addLabel(label.first);
198 }
199
200 auto const& states = stateStorage.stateToId;
201 for (auto const& stateIndexPair : states) {
202 unpackStateIntoEvaluator(stateIndexPair.first, variableInformation, *this->evaluator);
203 unpackTransientVariableValuesIntoEvaluator(stateIndexPair.first, *this->evaluator);
204
205 for (auto const& label : labelsAndExpressions) {
206 // Add label to state, if the corresponding expression is true.
207 if (evaluator->asBool(label.second)) {
208 result.addLabelToState(label.first, stateIndexPair.second);
209 }
210 }
211 }
212
213 auto addSpecialLabel = [&result](std::string const& label, auto const& indices) {
214 if (!result.containsLabel(label)) {
215 result.addLabel(label);
216 for (auto index : indices) {
217 result.addLabelToState(label, index);
218 }
219 }
220 };
221 addSpecialLabel("init", initialStateIndices);
222 addSpecialLabel("deadlock", deadlockStateIndices);
223 if (!unexploredStateIndices.empty()) {
224 addSpecialLabel("unexplored", unexploredStateIndices);
225 }
226 if (this->options.isAddOverlappingGuardLabelSet()) {
227 STORM_LOG_THROW(!result.containsLabel("overlap_guards"), storm::exceptions::WrongFormatException,
228 "Label 'overlap_guards' is reserved when adding overlapping guard labels");
229 addSpecialLabel("overlap_guards", overlappingGuardStates.get());
230 }
231 if (this->options.isAddOutOfBoundsStateSet() && stateStorage.stateToId.contains(outOfBoundsState)) {
232 STORM_LOG_THROW(!result.containsLabel("out_of_bounds"), storm::exceptions::WrongFormatException,
233 "Label 'out_of_bounds' is reserved when adding out of bounds states.");
234 addSpecialLabel("out_of_bounds", std::vector{stateStorage.stateToId.getValue(outOfBoundsState)});
235 }
236
237 return result;
238}
239
240template<typename ValueType, typename StateType>
242 return label == "init" || label == "deadlock" || label == "unexplored" || label == "overlap_guards" || label == "out_of_bounds";
243}
244
245template<typename ValueType, typename StateType>
248 // Intentionally left empty.
249 // This method should be overwritten in case there are transient variables (e.g. JANI).
250}
251
252template<typename ValueType, typename StateType>
254 // If the model we build is a Markov Automaton, we postprocess the choices to sum all Markovian choices
255 // and make the Markovian choice the very first one (if there is any).
256 bool foundPreviousMarkovianChoice = false;
257 if (this->getModelType() == ModelType::MA) {
258 uint64_t numberOfChoicesToDelete = 0;
259
260 for (uint_fast64_t index = 0; index + numberOfChoicesToDelete < result.getNumberOfChoices();) {
261 Choice<ValueType>& choice = result.getChoices()[index];
262
263 if (choice.isMarkovian()) {
264 if (foundPreviousMarkovianChoice) {
265 // If there was a previous Markovian choice, we need to sum them. Note that we can assume
266 // that the previous Markovian choice is the very first one in the choices vector.
267 result.getChoices().front().add(choice);
268
269 // Swap the choice to the end to indicate it can be removed (if it's not already there).
270 if (index != result.getNumberOfChoices() - 1 - numberOfChoicesToDelete) {
271 choice = std::move(result.getChoices()[result.getNumberOfChoices() - 1 - numberOfChoicesToDelete]);
272 }
273 ++numberOfChoicesToDelete;
274 } else {
275 // If there is no previous Markovian choice, just move the Markovian choice to the front.
276 if (index != 0) {
277 std::swap(result.getChoices().front(), choice);
278 }
279 foundPreviousMarkovianChoice = true;
280 ++index;
281 }
282 } else {
283 ++index;
284 }
285 }
286
287 // Finally remove the choices that were added to other Markovian choices.
288 if (numberOfChoicesToDelete > 0) {
289 result.getChoices().resize(result.getChoices().size() - numberOfChoicesToDelete);
290 }
291 }
292}
293
294template<typename ValueType, typename StateType>
296 return toString(state, variableInformation);
297}
298
299template<typename ValueType, typename StateType>
301 storm::json<ValueType> result = unpackStateIntoJson<ValueType>(*state, variableInformation, onlyObservable);
302 extendStateInformation(result);
303 return result;
304}
305
306template<typename ValueType, typename StateType>
310
311template<typename ValueType, typename StateType>
315
316template<typename ValueType, typename StateType>
317std::shared_ptr<storm::storage::sparse::ChoiceOrigins> NextStateGenerator<ValueType, StateType>::generateChoiceOrigins(
318 std::vector<boost::any>& /*dataForChoiceOrigins*/) const {
319 STORM_LOG_ERROR_COND(!options.isBuildChoiceOriginsSet(), "Generating choice origins is not supported for the considered model format.");
320 return nullptr;
321}
322
323template<typename ValueType, typename StateType>
325 if (this->mask.size() == 0) {
326 this->mask = computeObservabilityMask(variableInformation);
327 }
328 uint32_t classId = unpackStateToObservabilityClass(state, evaluateObservationLabels(state), observabilityMap, mask);
329 return classId;
330}
331
332template<typename ValueType, typename StateType>
333std::map<std::string, storm::storage::PlayerIndex> NextStateGenerator<ValueType, StateType>::getPlayerNameToIndexMap() const {
334 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Generating player mappings is not supported for this model input format");
335}
336
337template<typename ValueType, typename StateType>
338void NextStateGenerator<ValueType, StateType>::remapStateIds(std::function<StateType(StateType const&)> const& /*remapping*/) {
339 if (overlappingGuardStates != boost::none) {
340 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException,
341 "Remapping of Ids during model building is not supported for overlapping guard statements.");
342 }
343 // Nothing to be done.
344}
345
346template class NextStateGenerator<double>;
347
348template class ActionMask<double>;
350
351#ifdef STORM_HAVE_CARL
356
359#endif
360} // namespace generator
361} // namespace storm
bool isTrue() const
Checks if the expression is equal to the boolean literal true.
This class is responsible for managing a set of typed variables and all expressions using these varia...
A simple implementation of the valuation interface.
virtual int_fast64_t getIntegerValue(Variable const &integerVariable) const override
Retrieves the value of the given integer variable.
virtual bool getBooleanValue(Variable const &booleanVariable) const override
Retrieves the value of the given boolean variable.
Action masks are arguments you can give to the state generator that limit which states are generated.
virtual std::shared_ptr< storm::storage::sparse::ChoiceOrigins > generateChoiceOrigins(std::vector< boost::any > &dataForChoiceOrigins) const
NextStateGenerator(storm::expressions::ExpressionManager const &expressionManager, VariableInformation const &variableInformation, NextStateGeneratorOptions const &options, std::shared_ptr< ActionMask< ValueType, StateType > > const &=nullptr)
virtual std::map< std::string, storm::storage::PlayerIndex > getPlayerNameToIndexMap() const
virtual storm::storage::sparse::StateValuations makeObservationValuation() const
Adds the valuation for the currently loaded state.
bool isSpecialLabel(std::string const &label) const
Checks if the input label has a special purpose (e.g.
void initializeSpecialStates()
Initializes the out-of-bounds state and states with overlapping guards.
std::string stateToString(CompressedState const &state) const
virtual storm::storage::sparse::StateValuationsBuilder initializeStateValuationsBuilder() const
Initializes a builder for state valuations by adding the appropriate variables.
void postprocess(StateBehavior< ValueType, StateType > &result)
uint32_t observabilityClass(CompressedState const &state) const
NextStateGeneratorOptions const & getOptions() const
storm::expressions::SimpleValuation currentStateToSimpleValuation() const
storm::json< ValueType > currentStateToJson(bool onlyObservable=false) const
void load(CompressedState const &state)
virtual storm::storage::sparse::StateValuationsBuilder initializeObservationValuationsBuilder() const
bool satisfies(storm::expressions::Expression const &expression) const
virtual void addStateValuation(storm::storage::sparse::state_type const &currentStateIndex, storm::storage::sparse::StateValuationsBuilder &valuationsBuilder) const
Adds the valuation for the currently loaded state to the given builder.
virtual void extendStateInformation(storm::json< ValueType > &stateInfo) const
void remapStateIds(std::function< StateType(StateType const &)> const &remapping)
Performs a remapping of all values stored by applying the given remapping.
virtual void unpackTransientVariableValuesIntoEvaluator(CompressedState const &state, storm::expressions::ExpressionEvaluator< ValueType > &evaluator) const
Sets the values of all transient variables in the current state to the given evaluator.
VariableInformation const & getVariableInformation() const
virtual storm::models::sparse::StateLabeling label(storm::storage::sparse::StateStorage< StateType > const &stateStorage, std::vector< StateType > const &initialStateIndices={}, std::vector< StateType > const &deadlockStateIndices={}, std::vector< StateType > const &unexploredStateIndices={})=0
std::vector< Choice< ValueType, StateType > > const & getChoices() const
Retrieves the vector of choices.
std::size_t getNumberOfChoices() const
Retrieves the number of choices in the behavior.
A particular instance of the action mask that uses a callback function to evaluate whether an action ...
StateValuationFunctionMask(std::function< bool(storm::expressions::SimpleValuation const &, uint64_t)> const &f)
bool query(storm::generator::NextStateGenerator< ValueType, StateType > const &generator, uint64_t actionIndex) override
This method is called to check whether an action should be expanded.
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.
This class manages the labeling of the state space with a number of (atomic) labels.
void addLabelToState(std::string const &label, storm::storage::sparse::state_type state)
Adds a label to a given state.
ValueType getValue(storm::storage::BitVector const &key) const
Retrieves the value associated with the given key (if any).
bool contains(storm::storage::BitVector const &key) const
Checks if the given key is already contained in the map.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
void addState(storm::storage::sparse::state_type const &state, std::vector< bool > &&booleanValues={}, std::vector< int64_t > &&integerValues={})
Adds a new state.
void addObservationLabel(std::string const &label)
StateValuations build()
Creates the finalized state valuations object.
void addVariable(storm::expressions::Variable const &variable)
Adds a new variable to keep track of for the state valuations.
#define STORM_LOG_ERROR_COND(cond, message)
Definition macros.h:52
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void unpackStateIntoEvaluator(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< ValueType > &evaluator)
Unpacks the compressed state into the evaluator.
storm::expressions::SimpleValuation unpackStateIntoValuation(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionManager const &manager)
Converts the compressed state into an explicit representation in the form of a valuation.
void extractVariableValues(CompressedState const &state, VariableInformation const &variableInformation, std::vector< int64_t > &locationValues, std::vector< bool > &booleanValues, std::vector< int64_t > &integerValues)
Appends the values of the given variables in the given state to the corresponding result vectors.
uint32_t unpackStateToObservabilityClass(CompressedState const &state, storm::storage::BitVector const &observationVector, std::unordered_map< storm::storage::BitVector, uint32_t > &observabilityMap, storm::storage::BitVector const &mask)
std::string toString(CompressedState const &state, VariableInformation const &variableInformation)
Returns a (human readable) string representation of the variable valuation encoded by the given state...
CompressedState createOutOfBoundsState(VariableInformation const &varInfo, bool roundTo64Bit)
storm::storage::BitVector computeObservabilityMask(VariableInformation const &variableInformation)
LabParser.cpp.
Definition cli.cpp:18
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:10
bool isMarkovian() const
Retrieves whether the choice is Markovian.
Definition Choice.cpp:179
storm::storage::BitVectorHashMap< StateType > stateToId