Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Belief.cpp
Go to the documentation of this file.
2
5
8
9#include <boost/functional/hash.hpp>
10
11namespace storm::pomdp::beliefs {
12
13template<typename ValueType>
14Belief<ValueType>::Belief(BeliefFlatMap<ValueType>&& data, BeliefObservationType&& obs) : data(std::move(data)), obs(std::move(obs)) {
15 // Intentionally empty
16}
17
18template<typename ValueType>
19std::size_t Belief<ValueType>::size() const {
20 return data.size();
21}
22
23template<typename ValueType>
25 STORM_LOG_ASSERT(!data.empty(), "Empty belief");
26 return data.begin()->first;
27}
28
29template<typename ValueType>
31 return obs;
32}
33
34template<typename ValueType>
35bool Belief<ValueType>::operator==(Belief const& other) const {
36 if (obs != other.obs) {
37 return false;
38 }
39 if (data.size() != other.size()) {
40 return false;
41 }
42 static_assert(BeliefFlatMapIsOrdered);
43 auto secondIt = other.data.cbegin();
44 for (auto const& [state, value] : data) {
45 if (state != secondIt->first) {
46 return false;
47 }
48 if (!BeliefNumerics<ValueType>::equal(value, secondIt->second)) {
49 return false;
50 }
51 ++secondIt;
52 }
53 return true;
54}
55
56template<typename ValueType>
57std::string Belief<ValueType>::toString(bool convertToDouble, bool fullPrecision) const {
58 std::stringstream ss;
59 ss << "Belief{ obs:" << obs;
60 if (convertToDouble) {
61 forEach([&ss, fullPrecision](auto const& state, auto const& val) {
62 ss << ", " << state << ":";
63 if (fullPrecision) {
64 ss << storm::utility::to_string(storm::utility::convertNumber<double>(val));
65 } else {
66 ss << storm::utility::convertNumber<double>(val);
67 }
68 });
69 } else {
70 forEach([&ss, fullPrecision](auto const& state, auto const& val) {
71 ss << ", " << state << ":";
72 if (fullPrecision) {
74 } else {
75 ss << val;
76 }
77 });
78 }
79 ss << " }";
80 return ss.str();
81}
82
83template<typename ValueType>
84std::size_t Belief<ValueType>::BeliefHash::operator()(Belief const& belief) const {
85 auto seed = static_cast<std::size_t>(belief.obs);
86 static_assert(BeliefFlatMapIsOrdered);
87 belief.forEach([&seed](auto const& state, auto const& val) {
88 boost::hash_combine(seed, state);
89 boost::hash_combine(seed, BeliefNumerics<ValueType>::valueForHash(val));
90 });
91 return seed;
92}
93
94template class Belief<double>;
96
97} // namespace storm::pomdp::beliefs
std::string toString(bool convertToDouble=true, bool fullPrecision=false) const
A (human-readable) string representation of this belief.
Definition Belief.cpp:57
BeliefStateType representativeState() const
Definition Belief.cpp:24
std::size_t size() const
Definition Belief.cpp:19
void forEach(FunctionType const &f) const
Definition Belief.h:85
BeliefObservationType const & observation() const
Definition Belief.cpp:30
bool operator==(Belief const &other) const
Definition Belief.cpp:35
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
uint64_t BeliefStateType
Shared identifiers and sparse-container types used by the belief subsystem.
Definition types.h:10
uint32_t BeliefObservationType
Definition types.h:11
constexpr bool BeliefFlatMapIsOrdered
Definition types.h:18
boost::container::flat_map< BeliefStateType, BeliefValueType > BeliefFlatMap
Definition types.h:17
std::string to_string(ValueType const &value)
std::size_t operator()(Belief const &belief) const
Definition Belief.cpp:84
static ValueType valueForHash(ValueType const &val)
static bool equal(ValueType const &lhs, ValueType const &rhs)