Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BeliefBuilder.cpp
Go to the documentation of this file.
4
9
10namespace storm::pomdp::beliefs {
11
12template<typename BeliefType>
14 data.reserve(size);
15}
16
17template<typename BeliefType>
19 STORM_LOG_ASSERT(value > storm::utility::zero<ValueType>() && value <= storm::utility::one<ValueType>(), "Invalid belief value " << value << ".");
20 if (auto [insertionIt, success] = data.emplace(state, value); !success) {
21 insertionIt->second += value;
22 }
23}
24
25template<typename BeliefType>
27 obs = observation;
28}
29
30template<typename BeliefType>
33 for (auto const& [state, value] : data) {
34 STORM_LOG_ASSERT(value >= storm::utility::zero<ValueType>(), "Invalid value: " << value);
35 sum += value;
36 }
37 STORM_LOG_ASSERT(sum > storm::utility::zero<ValueType>(), "Invalid sum: " << sum);
38 if (!storm::utility::isOne(sum)) {
39 for (auto& [state, value] : data) {
40 value /= sum;
41 }
42 }
43 return sum;
44}
45
46template<typename BeliefType>
48 data.shrink_to_fit();
50 if (data.size() == 1 && BeliefNumerics<ValueType>::isOne(data.begin()->second)) {
51 // If the distribution consists of only one entry and its value is sufficiently close to 1, make it exactly 1 to avoid numerical problems
52 data.begin()->second = storm::utility::one<ValueType>();
53 }
54 }
55 STORM_LOG_ASSERT(assertBelief(), "Trying to build invalid belief.");
56 return BeliefType{std::move(data), std::move(obs)};
57}
58
59template<typename BeliefType>
61 data.clear();
63}
64
65template<typename BeliefType>
66bool BeliefBuilder<BeliefType>::assertBelief() {
67 using ValueType = typename BeliefType::ValueType;
68 if (obs == InvalidObservation) {
69 STORM_LOG_ERROR("Observation of Belief not set.");
70 return false;
71 }
73 for (auto const& [state, value] : data) {
74 if (value <= storm::utility::zero<ValueType>()) {
75 STORM_LOG_ERROR("Non-positive belief value " << value << " at state " << state << ".");
76 return false;
77 }
79 STORM_LOG_ERROR("Invalid belief value " << value << " at state " << state << ".");
80 return false;
81 }
82 sum += value;
83 }
85 STORM_LOG_ERROR("belief value sum " << sum << " is larger than 1 (sum-1=" << sum - storm::utility::one<ValueType>() << ").");
86 return false;
87 }
89 STORM_LOG_ERROR("belief value sum " << sum << " is smaller than 1 (1-sum=" << storm::utility::one<ValueType>() - sum << ").");
90 return false;
91 }
92 return true;
93}
94
95template class BeliefBuilder<Belief<double>>;
97
98} // namespace storm::pomdp::beliefs
void reserve(uint64_t size)
Reserves space to build the belief more efficiently.
BeliefType build()
Builds the belief and returns it.
ValueType normalize()
Normalizes the belief, i.e., if the sum of the values is not 1, it divides all values by the sum.
void setObservation(BeliefObservationType const &observation)
Sets the observation of the belief.
typename BeliefType::ValueType ValueType
void addValue(BeliefStateType const &state, ValueType const &value)
Adds a probability value to the given state.
void reset()
Resets the builder to its initial state.
#define STORM_LOG_ERROR(message)
Definition logging.h:36
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
constexpr BeliefObservationType InvalidObservation
Required e.g. for comparing two beliefs.
Definition types.h:20
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
bool isOne(ValueType const &a)
Definition constants.cpp:39
ValueType zero()
Definition constants.cpp:26
ValueType one()
Definition constants.cpp:21
static const bool IsExact
static bool isOne(ValueType const &val)
static bool lessOrEqual(ValueType const &lhs, ValueType const &rhs)