Storm
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
6
9
14
15namespace storm {
16namespace generator {
17
18template<typename ValueType>
19void unpackStateIntoEvaluator(CompressedState const& state, VariableInformation const& variableInformation,
21 for (auto const& locationVariable : variableInformation.locationVariables) {
22 if (locationVariable.bitWidth != 0) {
23 evaluator.setIntegerValue(locationVariable.variable, state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth));
24 } else {
25 evaluator.setIntegerValue(locationVariable.variable, 0);
26 }
27 }
28 for (auto const& booleanVariable : variableInformation.booleanVariables) {
29 evaluator.setBooleanValue(booleanVariable.variable, state.get(booleanVariable.bitOffset));
30 }
31 for (auto const& integerVariable : variableInformation.integerVariables) {
32 evaluator.setIntegerValue(integerVariable.variable,
33 static_cast<int_fast64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound);
34 }
35}
36
39 storm::expressions::SimpleValuation result(manager.getSharedPointer());
40 for (auto const& locationVariable : variableInformation.locationVariables) {
41 if (locationVariable.bitWidth != 0) {
42 result.setIntegerValue(locationVariable.variable, state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth));
43 } else {
44 result.setIntegerValue(locationVariable.variable, 0);
45 }
46 }
47 for (auto const& booleanVariable : variableInformation.booleanVariables) {
48 result.setBooleanValue(booleanVariable.variable, state.get(booleanVariable.bitOffset));
49 }
50 for (auto const& integerVariable : variableInformation.integerVariables) {
51 result.setIntegerValue(integerVariable.variable,
52 static_cast<int_fast64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound);
53 }
54 return result;
55}
56
57CompressedState packStateFromValuation(expressions::SimpleValuation const& valuation, VariableInformation const& variableInformation, bool checkOutOfBounds) {
58 CompressedState result(variableInformation.getTotalBitOffset(true));
59 STORM_LOG_THROW(variableInformation.locationVariables.size() == 0, storm::exceptions::NotImplementedException, "Support for JANI is not implemented");
60 for (auto const& booleanVariable : variableInformation.booleanVariables) {
61 result.set(booleanVariable.bitOffset, valuation.getBooleanValue(booleanVariable.variable));
62 }
63 for (auto const& integerVariable : variableInformation.integerVariables) {
64 int64_t assignedValue = valuation.getIntegerValue(integerVariable.variable);
65 if (checkOutOfBounds) {
66 STORM_LOG_THROW(assignedValue >= integerVariable.lowerBound, storm::exceptions::InvalidArgumentException,
67 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerVariable.getName() << "'.");
68 STORM_LOG_THROW(assignedValue <= integerVariable.upperBound, storm::exceptions::InvalidArgumentException,
69 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerVariable.getName() << "'.");
70 }
71 result.setFromInt(integerVariable.bitOffset, integerVariable.bitWidth, assignedValue - integerVariable.lowerBound);
73 static_cast<int_fast64_t>(result.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound == assignedValue,
74 "Writing to the bit vector bucket failed (read " << result.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth) << " but wrote "
75 << assignedValue << ").");
76 }
77
78 return result;
79}
80
81void extractVariableValues(CompressedState const& state, VariableInformation const& variableInformation, std::vector<int64_t>& locationValues,
82 std::vector<bool>& booleanValues, std::vector<int64_t>& integerValues) {
83 for (auto const& locationVariable : variableInformation.locationVariables) {
84 if (locationVariable.bitWidth != 0) {
85 locationValues.push_back(state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth));
86 } else {
87 locationValues.push_back(0);
88 }
89 }
90 for (auto const& booleanVariable : variableInformation.booleanVariables) {
91 booleanValues.push_back(state.get(booleanVariable.bitOffset));
92 }
93 for (auto const& integerVariable : variableInformation.integerVariables) {
94 integerValues.push_back(static_cast<int_fast64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound);
95 }
96}
97
98std::string toString(CompressedState const& state, VariableInformation const& variableInformation) {
99 std::vector<std::string> assignments;
100 for (auto const& locationVariable : variableInformation.locationVariables) {
101 assignments.push_back(locationVariable.variable.getName() + "=");
102 assignments.back() += std::to_string(locationVariable.bitWidth == 0 ? 0 : state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth));
103 }
104 for (auto const& booleanVariable : variableInformation.booleanVariables) {
105 if (!state.get(booleanVariable.bitOffset)) {
106 assignments.push_back("!" + booleanVariable.variable.getName());
107 } else {
108 assignments.push_back(booleanVariable.variable.getName());
109 }
110 }
111 for (auto const& integerVariable : variableInformation.integerVariables) {
112 assignments.push_back(
113 integerVariable.variable.getName() + "=" +
114 std::to_string(static_cast<int_fast64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound));
115 }
116 return boost::join(assignments, " & ");
117}
118
120 storm::storage::BitVector result(variableInformation.getTotalBitOffset(true));
121 for (auto const& locationVariable : variableInformation.locationVariables) {
122 if (locationVariable.observable) {
123 for (uint64_t i = locationVariable.bitOffset; i < locationVariable.bitOffset + locationVariable.bitWidth; ++i) {
124 result.set(i, true);
125 }
126 }
127 }
128
129 for (auto const& booleanVariable : variableInformation.booleanVariables) {
130 if (booleanVariable.observable) {
131 result.set(booleanVariable.bitOffset, true);
132 }
133 }
134
135 for (auto const& integerVariable : variableInformation.integerVariables) {
136 if (integerVariable.observable) {
137 for (uint64_t i = integerVariable.bitOffset; i < integerVariable.bitOffset + integerVariable.bitWidth; ++i) {
138 result.set(i, true);
139 }
140 }
141 }
142 return result;
143}
144
145uint32_t unpackStateToObservabilityClass(CompressedState const& state, storm::storage::BitVector const& observationVector,
146 std::unordered_map<storm::storage::BitVector, uint32_t>& observabilityMap, storm::storage::BitVector const& mask) {
147 STORM_LOG_ASSERT(state.size() == mask.size(), "Mask should be as long as state.");
148 storm::storage::BitVector observeClass = state & mask;
149 if (observationVector.size() != 0) {
150 observeClass.concat(observationVector);
151 }
152
153 auto it = observabilityMap.find(observeClass);
154 if (it != observabilityMap.end()) {
155 return it->second;
156 } else {
157 uint32_t newClassIndex = observabilityMap.size();
158 observabilityMap.emplace(observeClass, newClassIndex);
159 return newClassIndex;
160 }
161}
162
163template<typename ValueType>
164storm::json<ValueType> unpackStateIntoJson(CompressedState const& state, VariableInformation const& variableInformation, bool onlyObservable) {
166 for (auto const& locationVariable : variableInformation.locationVariables) {
167 if (onlyObservable && !locationVariable.observable) {
168 continue;
169 }
170 if (locationVariable.bitWidth != 0) {
171 result[locationVariable.variable.getName()] = state.getAsInt(locationVariable.bitOffset, locationVariable.bitWidth);
172 } else {
173 result[locationVariable.variable.getName()] = 0;
174 }
175 }
176 for (auto const& booleanVariable : variableInformation.booleanVariables) {
177 if (onlyObservable && !booleanVariable.observable) {
178 continue;
179 }
180 result[booleanVariable.getName()] = state.get(booleanVariable.bitOffset);
181 }
182 for (auto const& integerVariable : variableInformation.integerVariables) {
183 if (onlyObservable && !integerVariable.observable) {
184 continue;
185 }
186 STORM_LOG_ASSERT(integerVariable.bitWidth <= 63, "Only integer variables with at most 63 bits are supported");
187 result[integerVariable.getName()] =
188 static_cast<int64_t>(state.getAsInt(integerVariable.bitOffset, integerVariable.bitWidth)) + integerVariable.lowerBound;
189 }
190 return result;
191}
192
193storm::expressions::SimpleValuation unpackStateIntoValuation(CompressedState const& state, VariableInformation const& variableInformation,
195
197 CompressedState result(varInfo.getTotalBitOffset(roundTo64Bit));
198 assert(varInfo.hasOutOfBoundsBit());
199 result.set(varInfo.getOutOfBoundsBit());
200 return result;
201}
202
204 std::map<storm::expressions::Variable, storm::expressions::Expression> const& stateDescription, bool checkOutOfBounds) {
205 CompressedState result(varInfo.getTotalBitOffset(true));
206 auto boolItEnd = varInfo.booleanVariables.end();
207
208 for (auto boolIt = varInfo.booleanVariables.begin(); boolIt != boolItEnd; ++boolIt) {
209 STORM_LOG_THROW(stateDescription.count(boolIt->variable) > 0, storm::exceptions::InvalidArgumentException,
210 "Assignment for Boolean variable " << boolIt->getName() << " missing.");
211 result.set(boolIt->bitOffset, stateDescription.at(boolIt->variable).evaluateAsBool());
212 }
213
214 // Iterate over all integer assignments and carry them out.
215 auto integerItEnd = varInfo.integerVariables.end();
216 for (auto integerIt = varInfo.integerVariables.begin(); integerIt != integerItEnd; ++integerIt) {
217 STORM_LOG_THROW(stateDescription.count(integerIt->variable) > 0, storm::exceptions::InvalidArgumentException,
218 "Assignment for Integer variable " << integerIt->getName() << " missing.");
219
220 int64_t assignedValue = stateDescription.at(integerIt->variable).evaluateAsInt();
221 if (checkOutOfBounds) {
222 STORM_LOG_THROW(assignedValue >= integerIt->lowerBound, storm::exceptions::InvalidArgumentException,
223 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerIt->getName() << "'.");
224 STORM_LOG_THROW(assignedValue <= integerIt->upperBound, storm::exceptions::InvalidArgumentException,
225 "The assignment leads to an out-of-bounds value (" << assignedValue << ") for the variable '" << integerIt->getName() << "'.");
226 }
227 result.setFromInt(integerIt->bitOffset, integerIt->bitWidth, assignedValue - integerIt->lowerBound);
228 STORM_LOG_ASSERT(static_cast<int_fast64_t>(result.getAsInt(integerIt->bitOffset, integerIt->bitWidth)) + integerIt->lowerBound == assignedValue,
229 "Writing to the bit vector bucket failed (read " << result.getAsInt(integerIt->bitOffset, integerIt->bitWidth) << " but wrote "
230 << assignedValue << ").");
231 }
232
233 STORM_LOG_THROW(varInfo.locationVariables.size() == 0, storm::exceptions::NotImplementedException, "Support for JANI is not implemented");
234 return result;
235}
236
237template storm::json<double> unpackStateIntoJson<double>(CompressedState const& state, VariableInformation const& variableInformation, bool onlyObservable);
238template void unpackStateIntoEvaluator<double>(CompressedState const& state, VariableInformation const& variableInformation,
240#ifdef STORM_HAVE_CARL
241template storm::json<storm::RationalNumber> unpackStateIntoJson<storm::RationalNumber>(CompressedState const& state,
242 VariableInformation const& variableInformation, bool onlyObservable);
243template storm::json<storm::RationalFunction> unpackStateIntoJson<storm::RationalFunction>(CompressedState const& state,
244 VariableInformation const& variableInformation, bool onlyObservable);
245template void unpackStateIntoEvaluator<storm::RationalNumber>(CompressedState const& state, VariableInformation const& variableInformation,
247template void unpackStateIntoEvaluator<storm::RationalFunction>(CompressedState const& state, VariableInformation const& variableInformation,
249#endif
250} // namespace generator
251} // 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:18
void setFromInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits, uint64_t value)
Sets the selected number of lowermost bits of the provided value at the given bit index.
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.
uint_fast64_t getAsInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits) const
Retrieves the content of the current bit vector at the given index for the given number of bits as an...
bool get(uint_fast64_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.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
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 void unpackStateIntoEvaluator< double >(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< double > &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)
CompressedState packStateFromValuation(expressions::SimpleValuation const &valuation, VariableInformation const &variableInformation, bool checkOutOfBounds)
storm::storage::BitVector CompressedState
storm::storage::BitVector computeObservabilityMask(VariableInformation const &variableInformation)
storm::json< ValueType > unpackStateIntoJson(CompressedState const &state, VariableInformation const &variableInformation, bool onlyObservable)
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
uint_fast64_t getTotalBitOffset(bool roundTo64Bit=false) const
std::vector< IntegerVariableInformation > integerVariables
The integer variables.
std::vector< LocationVariableInformation > locationVariables
The location variables.
std::vector< BooleanVariableInformation > booleanVariables
The boolean variables.