Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BeliefCollectorTest.cpp
Go to the documentation of this file.
1#include "test/storm_gtest.h"
2
3#include <optional>
4#include <type_traits>
5
11
12namespace {
13using namespace storm::pomdp::beliefs;
14
15template<typename ValueType>
16Belief<ValueType> makeBelief(BeliefStateType state = 0, BeliefObservationType observation = 0, double firstProbability = 0.5) {
18 builder.setObservation(observation);
19 builder.addValue(state, storm::utility::convertNumber<ValueType>(firstProbability));
20 builder.addValue(state + 1, storm::utility::convertNumber<ValueType>(1.0 - firstProbability));
21 return builder.build();
22}
23
24template<typename ValueType>
25ValueType const* firstValueAddress(Belief<ValueType> const& belief) {
26 ValueType const* address = nullptr;
27 belief.forEach([&address](auto const&, auto const& value) {
28 if (!address) {
29 address = &value;
30 }
31 });
32 return address;
33}
34
35template<typename ValueType>
36class BeliefCollectorTest : public ::testing::Test {
37 public:
38 using Collector = BeliefCollector<Belief<ValueType>>;
39
40 static_assert(std::is_nothrow_move_constructible_v<Collector>);
41 static_assert(std::is_nothrow_move_assignable_v<Collector>);
42 static_assert(std::is_nothrow_move_constructible_v<Belief<ValueType>>);
43 static_assert(!std::is_copy_assignable_v<Belief<ValueType>>);
44 static_assert(!std::is_move_assignable_v<Belief<ValueType>>);
45
46 static void fill(Collector& collector, uint64_t count) {
47 for (uint64_t i = 0; i < count; ++i) {
48 ASSERT_EQ(i, collector.addBelief(makeBelief<ValueType>(2 * i, 2 * (i % 7))));
49 }
50 }
51
52 static void checkLookups(Collector const& collector, uint64_t count) {
53 EXPECT_EQ(count, collector.getNumberOfBeliefIds());
54 for (uint64_t i = 0; i < count; ++i) {
55 auto const belief = makeBelief<ValueType>(2 * i, 2 * (i % 7));
56 EXPECT_TRUE(collector.containsId(i));
57 EXPECT_TRUE(collector.containsBelief(belief));
58 EXPECT_EQ(i, collector.getIdFromBelief(belief));
59 EXPECT_TRUE(belief == collector.getBeliefFromId(i));
60 }
61 }
62};
63
64using BeliefValueTypes = ::testing::Types<double, storm::RationalNumber>;
65TYPED_TEST_SUITE(BeliefCollectorTest, BeliefValueTypes, );
66
67TYPED_TEST(BeliefCollectorTest, InternsBeliefs) {
68 typename TestFixture::Collector collector;
69 auto const belief = makeBelief<TypeParam>();
70 EXPECT_FALSE(collector.containsId(0));
71 EXPECT_FALSE(collector.containsBelief(belief));
72 EXPECT_EQ(InvalidBeliefId, collector.getIdOptional(belief));
73
74 EXPECT_EQ(0, collector.getIdOrAddBelief(makeBelief<TypeParam>()));
75 EXPECT_EQ(0, collector.getIdOrAddBelief(makeBelief<TypeParam>()));
76 EXPECT_EQ(1, collector.getNumberOfBeliefIds());
77 EXPECT_EQ(0, collector.getIdFromBelief(belief));
78
79 EXPECT_EQ(1, collector.getIdOrAddBelief(makeBelief<TypeParam>(0, 0, 0.25)));
80 EXPECT_EQ(2, collector.getIdOrAddBelief(makeBelief<TypeParam>(0, 8)));
81 EXPECT_EQ(2, collector.getIdOrAddBelief(makeBelief<TypeParam>(0, 8)));
82 EXPECT_FALSE(collector.isEqual(0, 1));
83 EXPECT_FALSE(collector.isEqual(0, 2));
84 EXPECT_TRUE(collector.isEqual(0, 0));
85 EXPECT_EQ(3, collector.getNumberOfBeliefIds());
86 EXPECT_FALSE(collector.containsId(3));
87 EXPECT_EQ(InvalidBeliefId, collector.getIdOptional(makeBelief<TypeParam>(20, 0)));
88 EXPECT_EQ(InvalidBeliefId, collector.getIdOptional(makeBelief<TypeParam>(0, 3)));
89 EXPECT_FALSE(collector.containsBelief(makeBelief<TypeParam>(0, 99)));
90}
91
92TYPED_TEST(BeliefCollectorTest, MissingRequiredLookupThrowsStormException) {
93 typename TestFixture::Collector collector;
94 ASSERT_EQ(0, collector.addBelief(makeBelief<TypeParam>()));
95 auto const missingBelief = makeBelief<TypeParam>(20);
96 EXPECT_EQ(InvalidBeliefId, collector.getIdOptional(missingBelief));
97 EXPECT_THROW(collector.getIdFromBelief(missingBelief), storm::exceptions::OutOfRangeException);
98 EXPECT_EQ(0, collector.getIdFromBelief(makeBelief<TypeParam>()));
99}
100
101TYPED_TEST(BeliefCollectorTest, PreservesDistributionStorageThroughGrowth) {
102 typename TestFixture::Collector collector;
103 auto firstBelief = makeBelief<TypeParam>();
104 auto const* firstValue = firstValueAddress(firstBelief);
105 ASSERT_EQ(0, collector.addBelief(std::move(firstBelief)));
106 EXPECT_EQ(firstValue, firstValueAddress(collector.getBeliefFromId(0)));
107
108 constexpr uint64_t count = 1024;
109 for (uint64_t i = 1; i < count; ++i) {
110 ASSERT_EQ(i, collector.addBelief(makeBelief<TypeParam>(2 * i, 2 * (i % 7))));
111 }
112 EXPECT_EQ(firstValue, firstValueAddress(collector.getBeliefFromId(0)));
113 TestFixture::checkLookups(collector, count);
114 EXPECT_EQ(0, collector.getIdOrAddBelief(makeBelief<TypeParam>()));
115 EXPECT_EQ(count, collector.getNumberOfBeliefIds());
116}
117
118TYPED_TEST(BeliefCollectorTest, CopiesUseIndependentStorage) {
119 using Collector = typename TestFixture::Collector;
120 std::optional<Collector> copied;
121 constexpr uint64_t count = 64;
122 {
123 Collector original;
124 TestFixture::fill(original, count);
125 copied.emplace(original);
126 EXPECT_NE(firstValueAddress(original.getBeliefFromId(0)), firstValueAddress(copied->getBeliefFromId(0)));
127 original.addBelief(makeBelief<TypeParam>(10000, 20));
128 EXPECT_FALSE(copied->containsBelief(makeBelief<TypeParam>(10000, 20)));
129 }
130 TestFixture::checkLookups(*copied, count);
131 EXPECT_EQ(count, copied->addBelief(makeBelief<TypeParam>(10000, 20)));
132 EXPECT_EQ(count, copied->getIdFromBelief(makeBelief<TypeParam>(10000, 20)));
133}
134
135TYPED_TEST(BeliefCollectorTest, MovesPreserveIndex) {
136 using Collector = typename TestFixture::Collector;
137 Collector assigned;
138 assigned.addBelief(makeBelief<TypeParam>(10000, 20));
139 constexpr uint64_t count = 64;
140 {
141 Collector original;
142 TestFixture::fill(original, count);
143 auto const* firstValue = firstValueAddress(original.getBeliefFromId(0));
144 Collector moved(std::move(original));
145 EXPECT_EQ(firstValue, firstValueAddress(moved.getBeliefFromId(0)));
146 TestFixture::checkLookups(moved, count);
147 assigned = std::move(moved);
148 EXPECT_EQ(firstValue, firstValueAddress(assigned.getBeliefFromId(0)));
149 TestFixture::checkLookups(assigned, count);
150 }
151 TestFixture::checkLookups(assigned, count);
152 EXPECT_EQ(count, assigned.addBelief(makeBelief<TypeParam>(10000, 20)));
153 EXPECT_EQ(count, assigned.getIdFromBelief(makeBelief<TypeParam>(10000, 20)));
154}
155
156TEST(BeliefCollectorNumericsTest, UsesExistingDoubleEquality) {
158 ASSERT_EQ(0, collector.addBelief(makeBelief<double>()));
159 auto const equivalent = makeBelief<double>(0, 0, 0.5 + 1e-15);
160 ASSERT_TRUE(equivalent == collector.getBeliefFromId(0));
161 EXPECT_EQ(0, collector.getIdFromBelief(equivalent));
162 EXPECT_EQ(0, collector.getIdOrAddBelief(makeBelief<double>(0, 0, 0.5 + 1e-15)));
163 EXPECT_EQ(1, collector.getIdOrAddBelief(makeBelief<double>(0, 0, 0.5 + 1e-12)));
164 EXPECT_EQ(2, collector.getNumberOfBeliefIds());
165}
166} // namespace
TEST(RegionConstraintCheckerTest, AffineConstraintHoldsThroughout)
Interns beliefs and assigns them consecutive identifiers.
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.
BeliefId getIdFromBelief(BeliefType const &belief) const
Represents a belief of a Pomdp, i.e.
Definition Belief.h:21
void forEach(FunctionType const &f) const
Definition Belief.h:85
SFTBDDChecker::ValueType ValueType
constexpr BeliefId InvalidBeliefId
Definition types.h:21
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
std::pair< storm::RationalNumber, storm::RationalNumber > count(std::vector< storm::storage::BitVector > const &origSets, std::vector< storm::storage::BitVector > const &intersects, std::vector< storm::storage::BitVector > const &intersectsInfo, storm::RationalNumber val, bool plus, uint64_t remdepth)
TargetType convertNumber(SourceType const &number)
TYPED_TEST(GraphTestAR, SymbolicProb01StochasticGameDieSmall)
Definition GraphTest.cpp:64
TYPED_TEST_SUITE(GraphTestAR, TestingTypes,)