Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
CompressedState.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string/join.hpp>
4
13
14namespace storm {
15namespace generator {
16
17template<typename ValueType>
18void unpackStateIntoEvaluator(CompressedState const& state, VariableInformation const& variableInformation,
20 for (auto const& locationVariable : variableInformation.locationVariables) {
21 if (locationVariable.bitWidth != 0) {
22 evaluator.setIntegerValue(locationVariable.variable, state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth));
23 } else {
24 evaluator.setIntegerValue(locationVariable.variable, 0);
25 }
26 }
27 for (auto const& booleanVariable : variableInformation.booleanVariables) {
28 evaluator.setBooleanValue(booleanVariable.variable, state.get(booleanVariable.bitOffset));
29 }
30 for (auto const& integerVariable : variableInformation.integerVariables) {
31 evaluator.setIntegerValue(integerVariable.variable,
32 static_cast<int_fast64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound);
33 }
34}
35
38 storm::expressions::SimpleValuation result(manager.getSharedPointer());
39 for (auto const& locationVariable : variableInformation.locationVariables) {
40 if (locationVariable.bitWidth != 0) {
41 result.setIntegerValue(locationVariable.variable, state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth));
42 } else {
43 result.setIntegerValue(locationVariable.variable, 0);
44 }
45 }
46 for (auto const& booleanVariable : variableInformation.booleanVariables) {
47 result.setBooleanValue(booleanVariable.variable, state.get(booleanVariable.bitOffset));
48 }
49 for (auto const& integerVariable : variableInformation.integerVariables) {
50 result.setIntegerValue(integerVariable.variable,
51 static_cast<int_fast64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound);
52 }
53 return result;
54}
55
56CompressedState packStateFromValuation(expressions::SimpleValuation const& valuation, VariableInformation const& variableInformation, bool checkOutOfBounds) {
57 CompressedState result(variableInformation.getTotalBitOffset(true));
58 STORM_LOG_THROW(variableInformation.locationVariables.size() == 0, storm::exceptions::NotImplementedException, "Support for JANI is not implemented.");
59 for (auto const& booleanVariable : variableInformation.booleanVariables) {
60 result.set(booleanVariable.bitOffset, valuation.getBooleanValue(booleanVariable.variable));
61 }
62 for (auto const& integerVariable : variableInformation.integerVariables) {
63 int64_t assignedValue = valuation.getIntegerValue(integerVariable.variable);
64 if (checkOutOfBounds) {
65 STORM_LOG_THROW(assignedValue >= integerVariable.lowerBound, storm::exceptions::InvalidArgumentException,
66 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerVariable.getName() << "'.");
67 STORM_LOG_THROW(assignedValue <= integerVariable.upperBound, storm::exceptions::InvalidArgumentException,
68 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerVariable.getName() << "'.");
69 }
70 result.setFromInt(integerVariable.bitOffset, integerVariable.bitWidth, assignedValue - integerVariable.lowerBound);
72 static_cast<int_fast64_t>(result.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound == assignedValue,
73 "Writing to the bit vector bucket failed (read " << result.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth) << " but wrote "
74 << assignedValue << ").");
75 }
76
77 return result;
78}
79
80namespace detail {
82template<UnpackStateIntoValuationsMode Mode>
83void unpackIntoValuations(CompressedState const& entityEncoding, uint64_t const entityIndex, VariableInformation const& variableInformation,
86 STORM_LOG_ASSERT(entityIndex < valuations.size(),
87 "Valuation entity index " << entityIndex << " is out of bounds for valuations of size " << valuations.size() << ".");
88 STORM_LOG_ASSERT(Mode != State || entityEncoding.size() == variableInformation.getTotalBitOffset(true),
89 "State size does not match the expected size based on the variable information.");
91 Mode != Observation || entityEncoding.size() == variableInformation.getTotalBitOffset(true) + variableInformation.observationLabels.size() * 64,
92 "Observation class size does not match the expected size based on the variable information.");
93
94 if (Mode == State && variableInformation.hasOutOfBoundsBit()) {
95 valuations.writeValue(entityIndex, variableInformation.outOfBoundsBit->variable, entityEncoding.get(variableInformation.getOutOfBoundsBit()));
96 }
97 for (auto const& locationVariable : variableInformation.locationVariables) {
98 if (Mode == Observation && !locationVariable.observable) {
99 continue;
100 }
101 int64_t const value = locationVariable.bitWidth != 0 ? entityEncoding.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth) : 0;
102 valuations.writeValue(entityIndex, locationVariable.variable, value);
103 }
104 for (auto const& booleanVariable : variableInformation.booleanVariables) {
105 if (Mode == Observation && !booleanVariable.observable) {
106 continue;
107 }
108 valuations.writeValue(entityIndex, booleanVariable.variable, entityEncoding.get(booleanVariable.bitOffset));
109 }
110 for (auto const& integerVariable : variableInformation.integerVariables) {
111 if (Mode == Observation && !integerVariable.observable) {
112 continue;
113 }
114 int64_t const value = entityEncoding.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth) + integerVariable.lowerBound;
115 valuations.writeValue(entityIndex, integerVariable.variable, value);
116 }
117 if constexpr (Mode == Observation) {
118 uint64_t labelEncodingStart = variableInformation.getTotalBitOffset(true);
119 for (auto const& observationLabel : variableInformation.observationLabels) {
120 STORM_LOG_ASSERT(observationLabel.deterministic, "Only deterministic observation labels supported for unpacking into valuations.");
121 STORM_LOG_ASSERT(labelEncodingStart + 64 <= entityEncoding.size(), "Not enough bits left in the observation class encoding.");
122 int64_t const labelValue = entityEncoding.getAsInt(labelEncodingStart, 64);
123 valuations.writeCallback<false, false, bool, int64_t>(entityIndex, observationLabel.variable,
124 [labelValue](auto, auto, auto& value) { value = labelValue; });
125 }
126 }
127}
128
129} // namespace detail
130
131void unpackStateAppendToValuations(CompressedState const& state, VariableInformation const& variableInformation,
133 valuations.resize(valuations.size() + 1);
134 detail::unpackIntoValuations<detail::UnpackStateIntoValuationsMode::State>(state, valuations.size() - 1, variableInformation, valuations);
135}
136
137void unpackObservationClassIntoValuations(CompressedState const& observationClass, uint64_t const observationClassIndex,
138 VariableInformation const& variableInformation, storm::storage::sparse::ValuationsStorage& valuations) {
139 detail::unpackIntoValuations<detail::UnpackStateIntoValuationsMode::Observation>(observationClass, observationClassIndex, variableInformation, valuations);
140}
141
142std::string toString(CompressedState const& state, VariableInformation const& variableInformation) {
143 std::vector<std::string> assignments;
144 for (auto const& locationVariable : variableInformation.locationVariables) {
145 assignments.push_back(locationVariable.variable.getName() + "=");
146 assignments.back() += std::to_string(locationVariable.bitWidth == 0 ? 0 : state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth));
147 }
148 for (auto const& booleanVariable : variableInformation.booleanVariables) {
149 if (!state.get(booleanVariable.bitOffset)) {
150 assignments.push_back("!" + booleanVariable.variable.getName());
151 } else {
152 assignments.push_back(booleanVariable.variable.getName());
153 }
154 }
155 for (auto const& integerVariable : variableInformation.integerVariables) {
156 assignments.push_back(
157 integerVariable.variable.getName() + "=" +
158 std::to_string(static_cast<int_fast64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound));
159 }
160 return boost::join(assignments, " & ");
161}
162
164 storm::storage::BitVector result(variableInformation.getTotalBitOffset(true));
165 for (auto const& locationVariable : variableInformation.locationVariables) {
166 if (locationVariable.observable) {
167 for (uint64_t i = locationVariable.bitOffset; i < locationVariable.bitOffset + locationVariable.bitWidth; ++i) {
168 result.set(i, true);
169 }
170 }
171 }
172
173 for (auto const& booleanVariable : variableInformation.booleanVariables) {
174 if (booleanVariable.observable) {
175 result.set(booleanVariable.bitOffset, true);
176 }
177 }
178
179 for (auto const& integerVariable : variableInformation.integerVariables) {
180 if (integerVariable.observable) {
181 for (uint64_t i = integerVariable.bitOffset; i < integerVariable.bitOffset + integerVariable.bitWidth; ++i) {
182 result.set(i, true);
183 }
184 }
185 }
186 return result;
187}
188
189uint32_t unpackStateToObservabilityClass(CompressedState const& state, storm::storage::BitVector const& observationVector,
190 std::unordered_map<storm::storage::BitVector, uint32_t>& observabilityMap, storm::storage::BitVector const& mask) {
191 STORM_LOG_ASSERT(state.size() == mask.size(), "Mask should be as long as state.");
192 storm::storage::BitVector observeClass = state & mask;
193 if (observationVector.size() != 0) {
194 observeClass.concat(observationVector);
195 }
196
197 auto it = observabilityMap.find(observeClass);
198 if (it != observabilityMap.end()) {
199 return it->second;
200 } else {
201 uint32_t newClassIndex = observabilityMap.size();
202 observabilityMap.emplace(observeClass, newClassIndex);
203 return newClassIndex;
204 }
205}
206
207template<typename ValueType>
208storm::json<ValueType> unpackStateIntoJson(CompressedState const& state, VariableInformation const& variableInformation, bool onlyObservable) {
210 for (auto const& locationVariable : variableInformation.locationVariables) {
211 if (onlyObservable && !locationVariable.observable) {
212 continue;
213 }
214 if (locationVariable.bitWidth != 0) {
215 result[locationVariable.variable.getName()] = state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth);
216 } else {
217 result[locationVariable.variable.getName()] = 0;
218 }
219 }
220 for (auto const& booleanVariable : variableInformation.booleanVariables) {
221 if (onlyObservable && !booleanVariable.observable) {
222 continue;
223 }
224 result[booleanVariable.getName()] = state.get(booleanVariable.bitOffset);
225 }
226 for (auto const& integerVariable : variableInformation.integerVariables) {
227 if (onlyObservable && !integerVariable.observable) {
228 continue;
229 }
230 STORM_LOG_ASSERT(integerVariable.bitWidth <= 63, "Only integer variables with at most 63 bits are supported.");
231 result[integerVariable.getName()] =
232 static_cast<int64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound;
233 }
234 return result;
235}
236
238 CompressedState result(varInfo.getTotalBitOffset(roundTo64Bit));
239 STORM_LOG_ASSERT(varInfo.hasOutOfBoundsBit(), "Variable info has no out-of-bounds bit.");
240 result.set(varInfo.getOutOfBoundsBit());
241 return result;
242}
243
245 std::map<storm::expressions::Variable, storm::expressions::Expression> const& stateDescription, bool checkOutOfBounds) {
246 CompressedState result(varInfo.getTotalBitOffset(true));
247 auto boolItEnd = varInfo.booleanVariables.end();
248
249 for (auto boolIt = varInfo.booleanVariables.begin(); boolIt != boolItEnd; ++boolIt) {
250 STORM_LOG_THROW(stateDescription.count(boolIt->variable) > 0, storm::exceptions::InvalidArgumentException,
251 "Assignment for Boolean variable " << boolIt->getName() << " missing.");
252 result.set(boolIt->bitOffset, stateDescription.at(boolIt->variable).evaluateAsBool());
253 }
254
255 // Iterate over all integer assignments and carry them out.
256 auto integerItEnd = varInfo.integerVariables.end();
257 for (auto integerIt = varInfo.integerVariables.begin(); integerIt != integerItEnd; ++integerIt) {
258 STORM_LOG_THROW(stateDescription.count(integerIt->variable) > 0, storm::exceptions::InvalidArgumentException,
259 "Assignment for Integer variable " << integerIt->getName() << " missing.");
260
261 int64_t assignedValue = stateDescription.at(integerIt->variable).evaluateAsInt();
262 if (checkOutOfBounds) {
263 STORM_LOG_THROW(assignedValue >= integerIt->lowerBound, storm::exceptions::InvalidArgumentException,
264 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerIt->getName() << "'.");
265 STORM_LOG_THROW(assignedValue <= integerIt->upperBound, storm::exceptions::InvalidArgumentException,
266 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerIt->getName() << "'.");
267 }
268 result.setFromInt(integerIt->bitOffset, integerIt->bitWidth, assignedValue - integerIt->lowerBound);
269 STORM_LOG_ASSERT(static_cast<int_fast64_t>(result.getAsInt(integerIt->bitOffset, integerIt->bitWidth)) + integerIt->lowerBound == assignedValue,
270 "Writing to the bit vector bucket failed (read " << result.getAsInt(integerIt->bitOffset, integerIt->bitWidth) << " but wrote "
271 << assignedValue << ").");
272 }
273
274 STORM_LOG_THROW(varInfo.locationVariables.size() == 0, storm::exceptions::NotImplementedException, "Support for JANI is not implemented.");
275 return result;
276}
277
278template storm::json<double> unpackStateIntoJson<double>(CompressedState const& state, VariableInformation const& variableInformation, bool onlyObservable);
279template void unpackStateIntoEvaluator<double>(CompressedState const& state, VariableInformation const& variableInformation,
282 VariableInformation const& variableInformation, bool onlyObservable);
284 VariableInformation const& variableInformation, bool onlyObservable);
285template void unpackStateIntoEvaluator<storm::RationalNumber>(CompressedState const& state, VariableInformation const& variableInformation,
287template void unpackStateIntoEvaluator<storm::RationalFunction>(CompressedState const& state, VariableInformation const& variableInformation,
289} // namespace generator
290} // namespace storm
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 void setIntegerValue(Variable const &integerVariable, int_fast64_t value) override
Sets the value of the given integer variable to the provided value.
virtual int_fast64_t getIntegerValue(Variable const &integerVariable) const override
Retrieves the value of the given integer variable.
virtual void setBooleanValue(Variable const &booleanVariable, bool value) override
Sets the value of the given boolean variable to the provided value.
virtual bool getBooleanValue(Variable const &booleanVariable) const override
Retrieves the value of the given boolean variable.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
void setFromInt(uint64_t bitIndex, uint64_t numberOfBits, uint64_t value)
Sets the selected number of lowermost bits of the provided value at the given bit index.
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
uint64_t getAsInt(uint64_t bitIndex, uint64_t numberOfBits) const
Retrieves the content of the current bit vector at the given index for the given number of bits as an...
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 concat(BitVector const &extension)
Concatenate this bitvector with another bitvector.
Stores valuations of variables for a set of entities (e.g.
void resize(uint64_t newEntityCount, uint64_t classIndex=0)
Resizes the entity count to newEntityCount.
void writeValue(uint64_t entity, storm::expressions::Variable const &variable, ValueType const &value)
Directly writes value to the given variable of entity.
void writeCallback(uint64_t entity, Callback const &callback)
Writes all variables of the given entity by invoking callback for each one.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
void unpackIntoValuations(CompressedState const &entityEncoding, uint64_t const entityIndex, VariableInformation const &variableInformation, storm::storage::sparse::ValuationsStorage &valuations)
template storm::json< double > unpackStateIntoJson< double >(CompressedState const &state, VariableInformation const &variableInformation, bool onlyObservable)
void unpackStateIntoEvaluator(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< ValueType > &evaluator)
Unpacks the compressed state into the evaluator.
CompressedState createCompressedState(VariableInformation const &varInfo, std::map< storm::expressions::Variable, storm::expressions::Expression > const &stateDescription, bool checkOutOfBounds)
template storm::json< storm::RationalNumber > unpackStateIntoJson< storm::RationalNumber >(CompressedState const &state, VariableInformation const &variableInformation, bool onlyObservable)
template void unpackStateIntoEvaluator< storm::RationalFunction >(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< storm::RationalFunction > &evaluator)
template void unpackStateIntoEvaluator< double >(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< double > &evaluator)
template void unpackStateIntoEvaluator< storm::RationalNumber >(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< storm::RationalNumber > &evaluator)
void unpackStateAppendToValuations(CompressedState const &state, VariableInformation const &variableInformation, storm::storage::sparse::ValuationsStorage &valuations)
Appends the values of the variables in the given state to the valuations object.
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.
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)
CompressedState packStateFromValuation(expressions::SimpleValuation const &valuation, VariableInformation const &variableInformation, bool checkOutOfBounds)
storm::storage::BitVector CompressedState
template storm::json< storm::RationalFunction > unpackStateIntoJson< storm::RationalFunction >(CompressedState const &state, VariableInformation const &variableInformation, bool onlyObservable)
void unpackObservationClassIntoValuations(CompressedState const &observationClass, uint64_t const observationClassIndex, VariableInformation const &variableInformation, storm::storage::sparse::ValuationsStorage &valuations)
Sets the values of observable variables and observation expressions to the given observationClassInde...
storm::storage::BitVector computeObservabilityMask(VariableInformation const &variableInformation)
storm::json< ValueType > unpackStateIntoJson(CompressedState const &state, VariableInformation const &variableInformation, bool onlyObservable)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:11
uint_fast64_t getTotalBitOffset(bool roundTo64Bit=false) const
std::vector< ObservationLabelInformation > observationLabels
The observation labels.
std::optional< BooleanVariableInformation > outOfBoundsBit
std::vector< IntegerVariableInformation > integerVariables
The integer variables.
std::vector< LocationVariableInformation > locationVariables
The location variables.
std::vector< BooleanVariableInformation > booleanVariables
The boolean variables.