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