Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Belief.h
Go to the documentation of this file.
1#pragma once
2
5
7
8namespace storm::pomdp::beliefs {
9
10template<typename ValueType>
11class BeliefBuilder;
12
20template<typename ValueTypeArg>
21class Belief {
22 public:
23 using ValueType = ValueTypeArg;
24 friend class BeliefBuilder<Belief<ValueType>>;
25
26 Belief() = delete;
27 Belief(Belief const& other) = default;
28 Belief(Belief&& other) = default;
29 Belief<ValueType>& operator=(Belief const& other) = delete;
31
35 std::size_t size() const;
36
42
47
51 bool operator==(Belief const& other) const;
52
59 std::string toString(bool convertToDouble = true, bool fullPrecision = false) const;
60
66 template<typename SummandsType>
67 SummandsType getWeightedSum(std::vector<SummandsType> const& summands) const {
69 forEach([&sum, &summands](auto const& state, auto const& val) {
70 STORM_LOG_ASSERT(state < summands.size(), "State " << state << " is out of range for the given summands.");
71 // If SummandsType is the same as ValueType, we can directly multiply the value, otherwise convert
72 if constexpr (std::is_same_v<SummandsType, ValueType>) {
73 sum += summands[state] * val;
74 } else {
75 sum += summands[state] * storm::utility::convertNumber<SummandsType>(val);
76 }
77 });
78 return sum;
79 }
80
84 template<typename FunctionType>
85 void forEach(FunctionType const& f) const {
86 for (auto const& [state, value] : data) {
87 f(state, value);
88 }
89 }
90
94 template<typename FunctionType>
95 bool allOf(FunctionType const& f) const {
96 for (auto const& [state, value] : data) {
97 if (!f(state, value)) {
98 return false;
99 }
100 }
101 return true;
102 }
103
107 template<typename FunctionType>
108 void forEachStateInSupport(FunctionType const& f) const {
109 for (auto const& [state, value] : data) {
110 f(state);
111 }
112 }
113
121 template<typename FunctionType>
122 void forEachCombine(Belief<ValueType> const& other, FunctionType const& f, bool considerOnlyThisSupport = false) const {
123 static_assert(BeliefFlatMapIsOrdered);
124 auto const zero = storm::utility::zero<ValueType>();
125 auto it2 = other.data.cbegin();
126 auto const it2End = other.data.cend();
127 if (considerOnlyThisSupport) {
128 for (auto const& [state1, value1] : data) {
129 while (it2 != it2End && it2->first < state1) {
130 ++it2;
131 }
132 if (it2 == it2End || it2->first > state1) {
133 f(state1, value1, zero);
134 } else {
135 STORM_LOG_ASSERT(state1 == it2->first, "Unexpected state.");
136 f(state1, value1, it2->second);
137 }
138 }
139 } else {
140 auto it1 = data.cbegin();
141 auto const it1End = data.cend();
142 while (it1 != it1End && it2 != it2End) {
143 auto const state1 = it1->first;
144 auto const state2 = it2->first;
145 if (state1 == state2) {
146 f(state1, it1->second, it2->second);
147 ++it1;
148 ++it2;
149 } else if (state1 < state2) {
150 f(state1, it1->second, zero);
151 ++it1;
152 } else {
153 STORM_LOG_ASSERT(state2 < state1, "unexpected states.");
154 f(state2, zero, it2->second);
155 ++it2;
156 }
157 }
158 for (; it1 != it1End; ++it1) {
159 f(it1->first, it1->second, zero);
160 }
161 for (; it2 != it2End; ++it2) {
162 f(it2->first, zero, it2->second);
163 }
164 }
165 }
166
170 struct BeliefHash {
171 std::size_t operator()(Belief const& belief) const;
172 };
173
174 private:
180
183};
184
185} // 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 forEachCombine(Belief< ValueType > const &other, FunctionType const &f, bool considerOnlyThisSupport=false) const
Calls the function f for each state in X with the arguments (state, value1, value2),...
Definition Belief.h:122
void forEach(FunctionType const &f) const
Definition Belief.h:85
Belief(Belief &&other)=default
Belief< ValueType > & operator=(Belief &&other)=delete
Belief< ValueType > & operator=(Belief const &other)=delete
BeliefObservationType const & observation() const
Definition Belief.cpp:30
bool operator==(Belief const &other) const
Definition Belief.cpp:35
void forEachStateInSupport(FunctionType const &f) const
Definition Belief.h:108
Belief(Belief const &other)=default
SummandsType getWeightedSum(std::vector< SummandsType > const &summands) const
Definition Belief.h:67
bool allOf(FunctionType const &f) const
Definition Belief.h:95
#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
ValueType zero()
Definition constants.cpp:26
TargetType convertNumber(SourceType const &number)
std::size_t operator()(Belief const &belief) const
Definition Belief.cpp:84