Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SimpleValuation.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string/join.hpp>
4#include <boost/functional/hash.hpp>
5
7
10
13
14namespace storm {
15namespace expressions {
17 // Intentionally left empty.
18}
19
20SimpleValuation::SimpleValuation(std::shared_ptr<storm::expressions::ExpressionManager const> const& manager)
21 : Valuation(manager),
22 booleanValues(this->getManager().getNumberOfBooleanVariables()),
23 integerValues(this->getManager().getNumberOfIntegerVariables() + this->getManager().getNumberOfBitVectorVariables()),
24 rationalValues(this->getManager().getNumberOfRationalVariables()) {
25 // Intentionally left empty.
26}
27
29 if (this != &other) {
30 booleanValues = other.booleanValues;
31 integerValues = other.integerValues;
32 rationalValues = other.rationalValues;
33 }
34}
35
37 if (this != &other) {
38 this->setManager(other.getManagerAsSharedPtr());
39 booleanValues = other.booleanValues;
40 integerValues = other.integerValues;
41 rationalValues = other.rationalValues;
42 }
43 return *this;
44}
45
47 if (this != &other) {
48 booleanValues = std::move(other.booleanValues);
49 integerValues = std::move(other.integerValues);
50 rationalValues = std::move(other.rationalValues);
51 }
52}
53
55 if (this != &other) {
56 this->setManager(other.getManagerAsSharedPtr());
57 booleanValues = std::move(other.booleanValues);
58 integerValues = std::move(other.integerValues);
59 rationalValues = std::move(other.rationalValues);
60 }
61 return *this;
62}
63
65 return this->getManager() == other.getManager() && booleanValues == other.booleanValues && integerValues == other.integerValues &&
66 rationalValues == other.rationalValues;
67}
68
69bool SimpleValuation::contains(Variable const& variable) const {
70 // A variable belongs to this valuation only if it is managed by the same manager and if it was already
71 // known to that manager when this valuation was created. The latter is checked via the offset of the
72 // variable: offsets within a type group are assigned consecutively and are never reused, so an offset
73 // that is out of bounds belongs to a variable declared after the creation of this valuation.
74 std::shared_ptr<ExpressionManager const> const& manager = this->getManagerAsSharedPtr();
75 if (!manager || manager.get() != &variable.getManager()) {
76 return false;
77 }
78 if (variable.hasBooleanType()) {
79 return variable.getOffset() < booleanValues.size();
80 } else if (variable.hasIntegerType() || variable.hasBitVectorType()) {
81 // Integer and bit vector variables share the same storage and hence the same offset space.
82 return variable.getOffset() < integerValues.size();
83 } else if (variable.hasRationalType()) {
84 return variable.getOffset() < rationalValues.size();
85 }
86 // Arrays and strings are not stored by valuations of this type.
87 return false;
88}
89
90bool SimpleValuation::getBooleanValue(Variable const& booleanVariable) const {
91 STORM_LOG_ASSERT(contains(booleanVariable), "The valuation does not contain a value for variable '" << booleanVariable.getName() << "'.");
92 return booleanValues[booleanVariable.getOffset()];
93}
94
95int_fast64_t SimpleValuation::getIntegerValue(Variable const& integerVariable) const {
96 STORM_LOG_ASSERT(contains(integerVariable), "The valuation does not contain a value for variable '" << integerVariable.getName() << "'.");
97 return integerValues[integerVariable.getOffset()];
98}
99
100int_fast64_t SimpleValuation::getBitVectorValue(Variable const& bitVectorVariable) const {
101 STORM_LOG_ASSERT(contains(bitVectorVariable), "The valuation does not contain a value for variable '" << bitVectorVariable.getName() << "'.");
102 return integerValues[bitVectorVariable.getOffset()];
103}
104
105double SimpleValuation::getRationalValue(Variable const& rationalVariable) const {
106 STORM_LOG_ASSERT(contains(rationalVariable), "The valuation does not contain a value for variable '" << rationalVariable.getName() << "'.");
107 return rationalValues[rationalVariable.getOffset()];
108}
109
110void SimpleValuation::setBooleanValue(Variable const& booleanVariable, bool value) {
111 STORM_LOG_ASSERT(contains(booleanVariable), "The valuation does not contain a value for variable '" << booleanVariable.getName() << "'.");
112 booleanValues[booleanVariable.getOffset()] = value;
113}
114
115void SimpleValuation::setIntegerValue(Variable const& integerVariable, int_fast64_t value) {
116 STORM_LOG_ASSERT(contains(integerVariable), "The valuation does not contain a value for variable '" << integerVariable.getName() << "'.");
117 integerValues[integerVariable.getOffset()] = value;
118}
119
120void SimpleValuation::setBitVectorValue(Variable const& bitVectorVariable, int_fast64_t value) {
121 STORM_LOG_ASSERT(contains(bitVectorVariable), "The valuation does not contain a value for variable '" << bitVectorVariable.getName() << "'.");
122 integerValues[bitVectorVariable.getOffset()] = value;
123}
124
125void SimpleValuation::setRationalValue(Variable const& rationalVariable, double value) {
126 STORM_LOG_ASSERT(contains(rationalVariable), "The valuation does not contain a value for variable '" << rationalVariable.getName() << "'.");
127 rationalValues[rationalVariable.getOffset()] = value;
128}
129
130std::string SimpleValuation::toPrettyString(std::set<storm::expressions::Variable> const& selectedVariables) const {
131 std::vector<std::string> assignments;
132 for (auto const& variable : selectedVariables) {
133 std::stringstream stream;
134 stream << variable.getName() << "=";
135 if (variable.hasBooleanType()) {
136 stream << std::boolalpha << this->getBooleanValue(variable) << std::noboolalpha;
137 } else if (variable.hasIntegerType()) {
138 stream << this->getIntegerValue(variable);
139 } else if (variable.hasRationalType()) {
140 stream << this->getRationalValue(variable);
141 } else {
142 STORM_LOG_THROW(false, storm::exceptions::InvalidTypeException, "Unexpected variable type.");
143 }
144 assignments.push_back(stream.str());
145 }
146 return "[" + boost::join(assignments, ", ") + "]";
147}
148
149std::string SimpleValuation::toString(bool pretty) const {
150 if (pretty) {
151 std::set<storm::expressions::Variable> allVariables;
152 for (auto const& e : getManager()) {
153 // Skip variables that were declared after this valuation was created, as there is no value for them.
154 if (contains(e.first)) {
155 allVariables.insert(e.first);
156 }
157 }
158 return toPrettyString(allVariables);
159 } else {
160 std::stringstream sstr;
161 sstr << "[\n";
162 sstr << getManager() << '\n';
163 if (!booleanValues.empty()) {
164 for (auto element : booleanValues) {
165 sstr << element << " ";
166 }
167 sstr << '\n';
168 }
169 if (!integerValues.empty()) {
170 for (auto const& element : integerValues) {
171 sstr << element << " ";
172 }
173 sstr << '\n';
174 }
175 if (!rationalValues.empty()) {
176 for (auto const& element : rationalValues) {
177 sstr << element << " ";
178 }
179 sstr << '\n';
180 }
181 sstr << "]";
182 return sstr.str();
183 }
184}
185
188 for (auto const& variable : getManager()) {
189 // Skip variables that were declared after this valuation was created, as there is no value for them.
190 if (!contains(variable.first)) {
191 continue;
192 }
193 if (variable.second.isBooleanType()) {
194 result[variable.first.getName()] = this->getBooleanValue(variable.first);
195 } else if (variable.second.isIntegerType()) {
196 result[variable.first.getName()] = this->getIntegerValue(variable.first);
197 } else if (variable.second.isRationalType()) {
198 result[variable.first.getName()] = storm::utility::convertNumber<storm::RationalNumber>(this->getRationalValue(variable.first));
199 } else {
200 STORM_LOG_THROW(false, storm::exceptions::InvalidTypeException, "Unexpected variable type.");
201 }
202 }
203 return result;
204}
205
206std::ostream& operator<<(std::ostream& out, SimpleValuation const& valuation) {
207 out << valuation.toString(false) << '\n';
208 return out;
209}
210
212 size_t seed = std::hash<std::vector<bool>>()(valuation->booleanValues);
213 boost::hash_combine(seed, valuation->integerValues);
214 boost::hash_combine(seed, valuation->rationalValues);
215 return seed;
216}
217
218bool SimpleValuationPointerCompare::operator()(SimpleValuation* SimpleValuation1, SimpleValuation* SimpleValuation2) const {
219 return *SimpleValuation1 == *SimpleValuation2;
220}
221
222bool SimpleValuationPointerLess::operator()(SimpleValuation* SimpleValuation1, SimpleValuation* SimpleValuation2) const {
223 return SimpleValuation1->booleanValues < SimpleValuation2->booleanValues || SimpleValuation1->integerValues < SimpleValuation2->integerValues ||
224 SimpleValuation1->rationalValues < SimpleValuation2->rationalValues;
225}
226} // namespace expressions
227} // namespace storm
A simple implementation of the valuation interface.
virtual std::string toPrettyString(std::set< storm::expressions::Variable > const &selectedVariables) const
Returns a string representation of the valuation of the selected variables.
bool contains(Variable const &variable) const
Checks whether this valuation stores a value for the given variable.
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 double getRationalValue(Variable const &rationalVariable) const override
Retrieves the value of the given rational variable.
bool operator==(SimpleValuation const &other) const
Checks whether the two valuations are semantically equivalent.
virtual void setBooleanValue(Variable const &booleanVariable, bool value) override
Sets the value of the given boolean variable to the provided value.
SimpleValuation & operator=(SimpleValuation const &other)
storm::json< storm::RationalNumber > toJson() const
virtual std::string toString(bool pretty=true) const
virtual bool getBooleanValue(Variable const &booleanVariable) const override
Retrieves the value of the given boolean variable.
SimpleValuation()
Creates an empty simple valuation that is associated to no manager and has no variables.
virtual void setRationalValue(Variable const &rationalVariable, double value) override
Sets the value of the given boolean variable to the provided value.
virtual int_fast64_t getBitVectorValue(Variable const &bitVectorVariable) const override
Retrieves the value of the given bit vector variable.
virtual void setBitVectorValue(Variable const &bitVectorVariable, int_fast64_t value) override
Sets the value of the given bit vector variable to the provided value.
bool operator()(SimpleValuation *valuation1, SimpleValuation *valuation2) const
std::size_t operator()(SimpleValuation *valuation) const
bool operator()(SimpleValuation *valuation1, SimpleValuation *valuation2) const
Valuation(std::shared_ptr< ExpressionManager const > const &manager)
Creates a valuation of all non-auxiliary variables managed by the given manager.
Definition Valuation.cpp:6
ExpressionManager const & getManager() const
Retrieves the manager responsible for the variables of this valuation.
Definition Valuation.cpp:14
void setManager(std::shared_ptr< ExpressionManager const > const &manager)
Sets the manager responsible for the variables in this valuation.
Definition Valuation.cpp:22
std::shared_ptr< ExpressionManager const > const & getManagerAsSharedPtr() const
Retrieves the manager responsible for the variables of this valuation.
Definition Valuation.cpp:18
bool hasBooleanType() const
Checks whether the variable is of boolean type.
Definition Variable.cpp:59
ExpressionManager const & getManager() const
Retrieves the manager responsible for this variable.
Definition Variable.cpp:54
bool hasBitVectorType() const
Checks whether the variable is of a bit vector type.
Definition Variable.cpp:67
uint_fast64_t getOffset() const
Retrieves the offset of the variable in the group of all equally typed variables.
Definition Variable.cpp:42
bool hasIntegerType() const
Checks whether the variable is of integral type.
Definition Variable.cpp:63
bool hasRationalType() const
Checks whether the variable is of rational type.
Definition Variable.cpp:71
std::string const & getName() const
Retrieves the name of the variable.
Definition Variable.cpp:46
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
std::ostream & operator<<(std::ostream &stream, BaseExpression const &expression)
TargetType convertNumber(SourceType const &number)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:11