Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BeliefCollector.cpp
Go to the documentation of this file.
5
6namespace storm::pomdp::beliefs {
7
8template<typename BeliefType>
9bool BeliefCollector<BeliefType>::isEqual(BeliefId const& first, BeliefId const& second) const {
10 return first == second || getBeliefFromId(first) == getBeliefFromId(second);
11}
12
13template<typename BeliefType>
15 return gatheredBeliefs.size();
16}
17
18template<typename BeliefType>
19BeliefType const& BeliefCollector<BeliefType>::getBeliefFromId(BeliefId const& id) const {
20 STORM_LOG_ASSERT(id < gatheredBeliefs.size(), "Unexpected belief id " << id << ". Ids are in [0," << getNumberOfBeliefIds() << ").");
21 return gatheredBeliefs[id];
22}
23
24template<typename BeliefType>
26 STORM_LOG_ASSERT(belief.observation() < beliefToIdMap.size(),
27 "Unknown belief observation " << belief.observation() << ". Observations are in [0," << beliefToIdMap.size());
28 auto const id = getIdOptional(belief);
29 STORM_LOG_THROW(id != InvalidBeliefId, storm::exceptions::OutOfRangeException, "Belief " << belief.toString() << " is not present in this collector.");
30 return id;
31}
32
33template<typename BeliefType>
34bool BeliefCollector<BeliefType>::containsBelief(BeliefType const& belief) const {
35 return getIdOptional(belief) != InvalidBeliefId;
36}
37
38template<typename BeliefType>
40 return id < gatheredBeliefs.size();
41}
42
43template<typename BeliefType>
45 if (auto const obs = belief.observation(); obs < beliefToIdMap.size()) {
46 auto const [begin, end] = beliefToIdMap[obs].equal_range(typename BeliefType::BeliefHash{}(belief));
47 for (auto it = begin; it != end; ++it) {
48 if (gatheredBeliefs[it->second] == belief) {
49 return it->second;
50 }
51 }
52 }
53 return InvalidBeliefId;
54}
55
56template<typename BeliefType>
58 if (auto id = getIdOptional(belief); id != InvalidBeliefId) {
59 return id;
60 }
61 return addBelief(std::move(belief));
62}
63
64template<typename BeliefType>
66 auto const obs = inputBelief.observation();
67 auto const hash = typename BeliefType::BeliefHash{}(inputBelief);
68 if (obs >= beliefToIdMap.size()) {
69 beliefToIdMap.resize(static_cast<uint64_t>(obs) + 1);
70 }
71 auto const id = gatheredBeliefs.size();
72 gatheredBeliefs.push_back(std::move(inputBelief));
73 beliefToIdMap[obs].emplace(hash, id);
74 return id;
75}
76
77template class BeliefCollector<Belief<double>>;
79
80} // namespace storm::pomdp::beliefs
Interns beliefs and assigns them consecutive identifiers.
bool containsBelief(BeliefType const &belief) const
BeliefId getIdOptional(BeliefType const &belief) const
BeliefType const & getBeliefFromId(BeliefId const &id) const
BeliefId addBelief(BeliefType &&inputBelief)
Adds a fresh belief and returns its ID.
BeliefId getIdOrAddBelief(BeliefType &&belief)
Checks if the given belief has already been collected before.
bool containsId(BeliefId const &id) const
BeliefId getIdFromBelief(BeliefType const &belief) const
bool isEqual(BeliefId const &first, BeliefId const &second) const
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
constexpr BeliefId InvalidBeliefId
Definition types.h:21
uint64_t BeliefId
Definition types.h:12