Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
NextStateGenerator.cpp
Go to the documentation of this file.
2
15
16namespace storm {
17namespace generator {
18
19template<typename ValueType, typename StateType>
21 : func(f) {
22 // Intentionally left empty
23}
24
25template<typename ValueType, typename StateType>
27 uint64_t actionIndex) {
28 auto val = generator.currentStateToSimpleValuation();
29 bool res = func(val, actionIndex);
30 return res;
31}
32
33template<typename ValueType, typename StateType>
35 VariableInformation const& variableInformation, NextStateGeneratorOptions const& options,
36 std::shared_ptr<ActionMask<ValueType, StateType>> const& mask)
37 : options(options),
38 expressionManager(expressionManager.getSharedPointer()),
39 variableInformation(variableInformation),
40 evaluator(nullptr),
41 state(nullptr),
42 comparator(storm::utility::convertNumber<ValueType>(options.getStochasticTolerance())),
43 actionMask(mask) {
45}
46
47template<typename ValueType, typename StateType>
49 NextStateGeneratorOptions const& options,
50 std::shared_ptr<ActionMask<ValueType, StateType>> const& mask)
51 : options(options),
52 expressionManager(expressionManager.getSharedPointer()),
53 variableInformation(),
54 evaluator(nullptr),
55 state(nullptr),
56 comparator(storm::utility::convertNumber<ValueType>(options.getStochasticTolerance())),
57 actionMask(mask) {}
58
59template<typename ValueType, typename StateType>
61
62template<typename ValueType, typename StateType>
66
67template<typename ValueType, typename StateType>
69 return variableInformation.getTotalBitOffset(true);
70}
72template<typename ValueType, typename StateType>
74 if (variableInformation.hasOutOfBoundsBit()) {
75 outOfBoundsState = createOutOfBoundsState(variableInformation);
76 }
77 if (options.isAddOverlappingGuardLabelSet()) {
78 overlappingGuardStates = std::vector<uint64_t>();
79 }
80}
81
82template<typename ValueType, typename StateType>
85 for (auto const& v : variableInformation.locationVariables) {
86 result.addVariable(v.variable);
87 }
88 for (auto const& v : variableInformation.booleanVariables) {
89 result.addVariable(v.variable);
90 }
91 for (auto const& v : variableInformation.integerVariables) {
92 result.addVariable(v.variable);
93 }
94 return result;
95}
97template<typename ValueType, typename StateType>
100 for (auto const& v : variableInformation.booleanVariables) {
101 if (v.observable) {
102 result.addVariable(v.variable);
103 }
105 for (auto const& v : variableInformation.integerVariables) {
106 if (v.observable) {
107 result.addVariable(v.variable);
108 }
110 for (auto const& l : variableInformation.observationLabels) {
111 result.addObservationLabel(l.name);
113 return result;
115
116template<typename ValueType, typename StateType>
118 // Since almost all subsequent operations are based on the evaluator, we load the state into it now.
119 unpackStateIntoEvaluator(state, variableInformation, *evaluator);
120
121 // Also, we need to store a pointer to the state itself, because we need to be able to access it when expanding it.
122 this->state = &state;
124
125template<typename ValueType, typename StateType>
127 if (expression.isTrue()) {
128 return true;
129 }
130 return evaluator->asBool(expression);
131}
132
133template<typename ValueType, typename StateType>
137
138template<typename ValueType, typename StateType>
140 storm::storage::sparse::StateValuationsBuilder& valuationsBuilder) const {
141 std::vector<bool> booleanValues;
142 booleanValues.reserve(variableInformation.booleanVariables.size());
143 std::vector<int64_t> integerValues;
144 integerValues.reserve(variableInformation.locationVariables.size() + variableInformation.integerVariables.size());
145 extractVariableValues(*this->state, variableInformation, integerValues, booleanValues, integerValues);
146 valuationsBuilder.addState(currentStateIndex, std::move(booleanValues), std::move(integerValues));
148
149template<typename ValueType, typename StateType>
151 storm::storage::sparse::StateValuationsBuilder valuationsBuilder = initializeObservationValuationsBuilder();
152 for (auto const& observationEntry : observabilityMap) {
153 std::vector<bool> booleanValues;
154 booleanValues.reserve(variableInformation.booleanVariables.size()); // TODO: use number of observable boolean variables
155 std::vector<int64_t> integerValues;
156 integerValues.reserve(variableInformation.locationVariables.size() +
157 variableInformation.integerVariables.size()); // TODO: use number of observable integer variables
158 std::vector<int64_t> observationLabelValues;
159 observationLabelValues.reserve(variableInformation.observationLabels.size());
160 expressions::SimpleValuation val = unpackStateIntoValuation(observationEntry.first, variableInformation, *expressionManager);
161 for (auto const& v : variableInformation.booleanVariables) {
162 if (v.observable) {
163 booleanValues.push_back(val.getBooleanValue(v.variable));
164 }
166 for (auto const& v : variableInformation.integerVariables) {
167 if (v.observable) {
168 integerValues.push_back(val.getIntegerValue(v.variable));
170 }
171 for (uint64_t labelStart = variableInformation.getTotalBitOffset(true); labelStart < observationEntry.first.size(); labelStart += 64) {
172 observationLabelValues.push_back(observationEntry.first.getAsInt(labelStart, 64));
173 }
174 valuationsBuilder.addState(observationEntry.second, std::move(booleanValues), std::move(integerValues), {}, std::move(observationLabelValues));
175 }
176 return valuationsBuilder.build();
177}
178
179template<typename ValueType, typename StateType>
181 storm::storage::sparse::StateStorage<StateType> const& stateStorage, std::vector<StateType> const& initialStateIndices,
182 std::vector<StateType> const& deadlockStateIndices, std::vector<StateType> const& unexploredStateIndices,
183 std::vector<std::pair<std::string, storm::expressions::Expression>> labelsAndExpressions) {
184 labelsAndExpressions.insert(labelsAndExpressions.end(), this->options.getExpressionLabels().begin(), this->options.getExpressionLabels().end());
185
186 // Make the labels unique.
187 std::sort(labelsAndExpressions.begin(), labelsAndExpressions.end(),
188 [](std::pair<std::string, storm::expressions::Expression> const& a, std::pair<std::string, storm::expressions::Expression> const& b) {
189 return a.first < b.first;
190 });
191 auto it = std::unique(labelsAndExpressions.begin(), labelsAndExpressions.end(),
192 [](std::pair<std::string, storm::expressions::Expression> const& a, std::pair<std::string, storm::expressions::Expression> const& b) {
193 return a.first == b.first;
194 });
195 labelsAndExpressions.resize(std::distance(labelsAndExpressions.begin(), it));
196
197 // Prepare result.
199
200 // Initialize labeling.
201 for (auto const& label : labelsAndExpressions) {
202 result.addLabel(label.first);
203 }
204
205 auto const& states = stateStorage.stateToId;
206 for (auto const& stateIndexPair : states) {
207 unpackStateIntoEvaluator(stateIndexPair.first, variableInformation, *this->evaluator);
208 unpackTransientVariableValuesIntoEvaluator(stateIndexPair.first, *this->evaluator);
209
210 for (auto const& label : labelsAndExpressions) {
211 // Add label to state, if the corresponding expression is true.
212 if (evaluator->asBool(label.second)) {
213 result.addLabelToState(label.first, stateIndexPair.second);
214 }
215 }
216 }
217
218 auto addSpecialLabel = [&result](std::string const& label, auto const& indices) {
219 if (!result.containsLabel(label)) {
220 result.addLabel(label);
221 for (auto index : indices) {
222 result.addLabelToState(label, index);
223 }
224 }
225 };
226 addSpecialLabel("init", initialStateIndices);
227 addSpecialLabel("deadlock", deadlockStateIndices);
228 if (!unexploredStateIndices.empty()) {
229 addSpecialLabel("unexplored", unexploredStateIndices);
230 }
231 if (this->options.isAddOverlappingGuardLabelSet()) {
232 STORM_LOG_THROW(!result.containsLabel("overlap_guards"), storm::exceptions::WrongFormatException,
233 "Label 'overlap_guards' is reserved when adding overlapping guard labels");
234 addSpecialLabel("overlap_guards", overlappingGuardStates.get());
235 }
236 if (this->options.isAddOutOfBoundsStateSet() && stateStorage.stateToId.contains(outOfBoundsState)) {
237 STORM_LOG_THROW(!result.containsLabel("out_of_bounds"), storm::exceptions::WrongFormatException,
238 "Label 'out_of_bounds' is reserved when adding out of bounds states.");
239 addSpecialLabel("out_of_bounds", std::vector{stateStorage.stateToId.getValue(outOfBoundsState)});
240 }
241
242 return result;
243}
244
245template<typename ValueType, typename StateType>
247 return label == "init" || label == "deadlock" || label == "unexplored" || label == "overlap_guards" || label == "out_of_bounds";
248}
249
250template<typename ValueType, typename StateType>
253 // Intentionally left empty.
254 // This method should be overwritten in case there are transient variables (e.g. JANI).
255}
256
257template<typename ValueType, typename StateType>
259 // If the model we build is a Markov Automaton, we postprocess the choices to sum all Markovian choices
260 // and make the Markovian choice the very first one (if there is any).
261 bool foundPreviousMarkovianChoice = false;
262 if (this->getModelType() == ModelType::MA) {
263 uint64_t numberOfChoicesToDelete = 0;
264
265 for (uint_fast64_t index = 0; index + numberOfChoicesToDelete < result.getNumberOfChoices();) {
266 Choice<ValueType>& choice = result.getChoices()[index];
267
268 if (choice.isMarkovian()) {
269 if (foundPreviousMarkovianChoice) {
270 // If there was a previous Markovian choice, we need to sum them. Note that we can assume
271 // that the previous Markovian choice is the very first one in the choices vector.
272 result.getChoices().front().add(choice);
273
274 // Swap the choice to the end to indicate it can be removed (if it's not already there).
275 if (index != result.getNumberOfChoices() - 1 - numberOfChoicesToDelete) {
276 choice = std::move(result.getChoices()[result.getNumberOfChoices() - 1 - numberOfChoicesToDelete]);
277 }
278 ++numberOfChoicesToDelete;
279 } else {
280 // If there is no previous Markovian choice, just move the Markovian choice to the front.
281 if (index != 0) {
282 std::swap(result.getChoices().front(), choice);
283 }
284 foundPreviousMarkovianChoice = true;
285 ++index;
286 }
287 } else {
288 ++index;
289 }
290 }
291
292 // Finally remove the choices that were added to other Markovian choices.
293 if (numberOfChoicesToDelete > 0) {
294 result.getChoices().resize(result.getChoices().size() - numberOfChoicesToDelete);
295 }
296 }
297}
298
299template<typename ValueType, typename StateType>
301 return toString(state, variableInformation);
302}
303
304template<typename ValueType, typename StateType>
306 storm::json<BaseValueType> result = unpackStateIntoJson<BaseValueType>(*state, variableInformation, onlyObservable);
307 extendStateInformation(result);
308 return result;
309}
310
311template<typename ValueType, typename StateType>
315
316template<typename ValueType, typename StateType>
320
321template<typename ValueType, typename StateType>
322std::shared_ptr<storm::storage::sparse::ChoiceOrigins> NextStateGenerator<ValueType, StateType>::generateChoiceOrigins(
323 std::vector<boost::any>& /*dataForChoiceOrigins*/) const {
324 STORM_LOG_ERROR_COND(!options.isBuildChoiceOriginsSet(), "Generating choice origins is not supported for the considered model format.");
325 return nullptr;
326}
327
328template<typename ValueType, typename StateType>
330 if (this->mask.size() == 0) {
331 this->mask = computeObservabilityMask(variableInformation);
332 }
333 uint32_t classId = unpackStateToObservabilityClass(state, evaluateObservationLabels(state), observabilityMap, mask);
334 return classId;
335}
336
337template<typename ValueType, typename StateType>
338std::map<std::string, storm::storage::PlayerIndex> NextStateGenerator<ValueType, StateType>::getPlayerNameToIndexMap() const {
339 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Generating player mappings is not supported for this model input format");
340}
341
342template<typename ValueType, typename StateType>
343void NextStateGenerator<ValueType, StateType>::remapStateIds(std::function<StateType(StateType const&)> const& /*remapping*/) {
344 if (overlappingGuardStates != boost::none) {
345 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException,
346 "Remapping of Ids during model building is not supported for overlapping guard statements.");
347 }
348 // Nothing to be done.
349}
350
351template class ActionMask<double>;
353template class NextStateGenerator<double>;
354
358
362
363template class ActionMask<storm::Interval>;
366} // namespace generator
367} // 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
virtual void extendStateInformation(storm::json< BaseValueType > &stateInfo) 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.
void remapStateIds(std::function< StateType(StateType const &)> const &remapping)
Performs a remapping of all values stored by applying the given remapping.
VariableInformation const & getVariableInformation() const
virtual void unpackTransientVariableValuesIntoEvaluator(CompressedState const &state, storm::expressions::ExpressionEvaluator< BaseValueType > &evaluator) const
Sets the values of all transient variables in the current state to the given evaluator.
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:16
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)
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