Storm
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
28SimpleValuation::SimpleValuation(SimpleValuation const& other) : Valuation(other.getManagerAsSharedPtr()) {
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
46SimpleValuation::SimpleValuation(SimpleValuation&& other) : Valuation(other.getManagerAsSharedPtr()) {
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::getBooleanValue(Variable const& booleanVariable) const {
70 return booleanValues[booleanVariable.getOffset()];
71}
72
73int_fast64_t SimpleValuation::getIntegerValue(Variable const& integerVariable) const {
74 return integerValues[integerVariable.getOffset()];
75}
76
77int_fast64_t SimpleValuation::getBitVectorValue(Variable const& bitVectorVariable) const {
78 return integerValues[bitVectorVariable.getOffset()];
79}
80
81double SimpleValuation::getRationalValue(Variable const& rationalVariable) const {
82 return rationalValues[rationalVariable.getOffset()];
83}
84
85void SimpleValuation::setBooleanValue(Variable const& booleanVariable, bool value) {
86 booleanValues[booleanVariable.getOffset()] = value;
87}
88
89void SimpleValuation::setIntegerValue(Variable const& integerVariable, int_fast64_t value) {
90 integerValues[integerVariable.getOffset()] = value;
91}
92
93void SimpleValuation::setBitVectorValue(Variable const& bitVectorVariable, int_fast64_t value) {
94 integerValues[bitVectorVariable.getOffset()] = value;
95}
96
97void SimpleValuation::setRationalValue(Variable const& rationalVariable, double value) {
98 rationalValues[rationalVariable.getOffset()] = value;
99}
100
101std::string SimpleValuation::toPrettyString(std::set<storm::expressions::Variable> const& selectedVariables) const {
102 std::vector<std::string> assignments;
103 for (auto const& variable : selectedVariables) {
104 std::stringstream stream;
105 stream << variable.getName() << "=";
106 if (variable.hasBooleanType()) {
107 stream << std::boolalpha << this->getBooleanValue(variable) << std::noboolalpha;
108 } else if (variable.hasIntegerType()) {
109 stream << this->getIntegerValue(variable);
110 } else if (variable.hasRationalType()) {
111 stream << this->getRationalValue(variable);
112 } else {
113 STORM_LOG_THROW(false, storm::exceptions::InvalidTypeException, "Unexpected variable type.");
114 }
115 assignments.push_back(stream.str());
116 }
117 return "[" + boost::join(assignments, ", ") + "]";
118}
119
120std::string SimpleValuation::toString(bool pretty) const {
121 if (pretty) {
122 std::set<storm::expressions::Variable> allVariables;
123 for (auto const& e : getManager()) {
124 allVariables.insert(e.first);
125 }
126 return toPrettyString(allVariables);
127 } else {
128 std::stringstream sstr;
129 sstr << "[\n";
130 sstr << getManager() << '\n';
131 if (!booleanValues.empty()) {
132 for (auto element : booleanValues) {
133 sstr << element << " ";
134 }
135 sstr << '\n';
136 }
137 if (!integerValues.empty()) {
138 for (auto const& element : integerValues) {
139 sstr << element << " ";
140 }
141 sstr << '\n';
142 }
143 if (!rationalValues.empty()) {
144 for (auto const& element : rationalValues) {
145 sstr << element << " ";
146 }
147 sstr << '\n';
148 }
149 sstr << "]";
150 return sstr.str();
151 }
152}
153
156 for (auto const& variable : getManager()) {
157 if (variable.second.isBooleanType()) {
158 result[variable.first.getName()] = this->getBooleanValue(variable.first);
159 } else if (variable.second.isIntegerType()) {
160 result[variable.first.getName()] = this->getIntegerValue(variable.first);
161 } else if (variable.second.isRationalType()) {
162 result[variable.first.getName()] = storm::utility::convertNumber<storm::RationalNumber>(this->getRationalValue(variable.first));
163 } else {
164 STORM_LOG_THROW(false, storm::exceptions::InvalidTypeException, "Unexpected variable type.");
165 }
166 }
167 return result;
168}
169
170std::ostream& operator<<(std::ostream& out, SimpleValuation const& valuation) {
171 out << valuation.toString(false) << '\n';
172 return out;
173}
174
176 size_t seed = std::hash<std::vector<bool>>()(valuation->booleanValues);
177 boost::hash_combine(seed, valuation->integerValues);
178 boost::hash_combine(seed, valuation->rationalValues);
179 return seed;
180}
181
182bool SimpleValuationPointerCompare::operator()(SimpleValuation* SimpleValuation1, SimpleValuation* SimpleValuation2) const {
183 return *SimpleValuation1 == *SimpleValuation2;
184}
185
186bool SimpleValuationPointerLess::operator()(SimpleValuation* SimpleValuation1, SimpleValuation* SimpleValuation2) const {
187 return SimpleValuation1->booleanValues < SimpleValuation2->booleanValues || SimpleValuation1->integerValues < SimpleValuation2->integerValues ||
188 SimpleValuation1->rationalValues < SimpleValuation2->rationalValues;
189}
190} // namespace expressions
191} // 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.
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
The base class of all valuations of variables.
Definition Valuation.h:16
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
uint_fast64_t getOffset() const
Retrieves the offset of the variable in the group of all equally typed variables.
Definition Variable.cpp:42
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
std::ostream & operator<<(std::ostream &stream, BaseExpression const &expression)
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