13template<
typename ValueType>
14class BeliefMdpBuilderTest :
public ::testing::Test {
16 using BeliefType = Belief<double>;
25 template<
typename Info>
26 static Info makeInfo(uint64_t beliefs) {
28 info.initialBeliefId = 1;
29 info.nrObservationsInPomdp = beliefs;
30 for (uint64_t i = 0;
i < beliefs; ++
i) {
31 BeliefBuilder<BeliefType> builder;
34 info.discoveredBeliefs.addBelief(builder.
build());
39 static PropertyInformation property(
PropertyInformation::Kind kind = PropertyInformation::Kind::ReachabilityProbability) {
40 return {kind, {}, std::nullopt, storm::OptimizationDirection::Maximize, {}};
43 template<
typename Info>
44 static auto build(Info
const& info, PropertyInformation
const& propertyInfo = property()) {
45 std::function<std::unordered_map<std::string, ValueType>(BeliefType
const&)> cutOff = [](BeliefType
const&) {
46 return std::unordered_map<std::string, ValueType>{{
"cutoff", value(0.75)}};
51 static void checkRow(storm::storage::SparseMatrix<ValueType>
const& matrix, uint64_t row, std::initializer_list<std::pair<uint64_t, ValueType>> expected) {
52 auto const actual = matrix.
getRow(row);
53 ASSERT_EQ(expected.size(), actual.getNumberOfEntries());
54 auto it = actual.begin();
55 for (
auto const& [column, probability] : expected) {
56 EXPECT_EQ(column, it->getColumn());
57 if constexpr (std::is_same_v<ValueType, double>) {
58 EXPECT_NEAR(probability, it->getValue(), 1e-14);
60 EXPECT_EQ(probability, it->getValue());
67using MatrixValueTypes = ::testing::Types<double, storm::RationalNumber>;
70TYPED_TEST(BeliefMdpBuilderTest, PreservesRequestedChoiceLabelingWithoutExploredRows) {
71 for (
bool const terminalInitialBelief : {
false,
true}) {
72 SCOPED_TRACE(terminalInitialBelief);
73 auto info = TestFixture::template makeInfo<typename TestFixture::StandardInfo>(1);
74 info.initialBeliefId = 0;
75 if (terminalInitialBelief) {
76 info.terminalBeliefValues.emplace(0, TestFixture::value(1));
78 ASSERT_EQ(info.matrix.rows(), 0ul);
79 ASSERT_FALSE(info.matrix.hasChoiceLabels());
81 for (
bool const generateChoiceLabeling : {
false,
true}) {
82 SCOPED_TRACE(generateChoiceLabeling);
83 info.generateChoiceLabeling = generateChoiceLabeling;
84 auto const [mdp, mapping] = TestFixture::build(info);
85 ASSERT_EQ(mdp->hasChoiceLabeling(), generateChoiceLabeling);
86 EXPECT_EQ(mdp->getNumberOfChoices(), 3ul);
87 EXPECT_EQ((std::unordered_map<uint64_t, BeliefId>{{0, 0}}), mapping);
88 if (generateChoiceLabeling) {
89 auto const& labeling = mdp->getChoiceLabeling();
90 EXPECT_EQ(terminalInitialBelief ? std::set<std::string>{} : std::set<std::string>{
"cutoff"}, labeling.getLabelsOfChoice(0));
91 for (uint64_t choice : {1ul, 2ul}) {
92 EXPECT_EQ((std::set<std::string>{
"__loop__"}), labeling.getLabelsOfChoice(choice));
99TYPED_TEST(BeliefMdpBuilderTest, MapsSuccessorsInEveryOrderAndPreservesZeros) {
100 auto info = TestFixture::template makeInfo<typename TestFixture::StandardInfo>(3);
101 info.exploredBeliefs = {{0, 2}, {1, 0}, {2, 1}};
103 for (
auto const& order : {std::vector<BeliefId>{1, 2, 0}, std::vector<BeliefId>{0, 2, 1}, std::vector<BeliefId>{2, 0, 1}}) {
104 for (
auto id : order) {
105 info.matrix.transitions.push_back({TestFixture::value(
id == 2 ? 0.0 : 0.5),
id});
107 info.matrix.endCurrentRow();
109 info.matrix.endCurrentRowGroup();
110 info.matrix.transitions.push_back({TestFixture::value(1), 2});
111 info.matrix.endCurrentRow();
112 info.matrix.endCurrentRowGroup();
113 info.matrix.transitions.push_back({TestFixture::value(1), 0});
114 info.matrix.endCurrentRow();
115 info.matrix.endCurrentRowGroup();
117 auto const [mdp, mapping] = TestFixture::build(info);
118 auto const& matrix = mdp->getTransitionMatrix();
119 for (uint64_t row = 0; row < 3; ++row) {
120 TestFixture::checkRow(matrix, row, {{0, TestFixture::value(0.5)}, {1, TestFixture::value(0)}, {2, TestFixture::value(0.5)}});
122 TestFixture::checkRow(matrix, 3, {{1, TestFixture::value(1)}});
123 TestFixture::checkRow(matrix, 4, {{2, TestFixture::value(1)}});
125 EXPECT_EQ((std::unordered_map<uint64_t, BeliefId>{{0, 1}, {1, 2}, {2, 0}}), mapping);
126 EXPECT_TRUE(mdp->getStateLabeling().getStateHasLabel(
"init", 0));
129TYPED_TEST(BeliefMdpBuilderTest, MergesDuplicatesAndPreservesFrontierTerminalAndLabels) {
130 auto info = TestFixture::template makeInfo<typename TestFixture::StandardInfo>(4);
131 info.exploredBeliefs = {{0, 1}, {1, 0}};
132 info.terminalBeliefValues = {{3, TestFixture::value(0.5)}};
133 info.matrix.transitions = {{TestFixture::value(0.25), 2},
134 {TestFixture::value(0.125), 0},
135 {TestFixture::value(0.25), 3},
136 {TestFixture::value(0.25), 1},
137 {TestFixture::value(0.125), 0}};
138 info.matrix.endCurrentRow();
140 info.matrix.transitions.push_back({TestFixture::value(1), 3});
141 info.matrix.endCurrentRow();
142 info.matrix.endCurrentRowGroup();
143 info.matrix.transitions.push_back({TestFixture::value(1), 0});
144 info.matrix.endCurrentRow();
145 info.matrix.endCurrentRowGroup();
146 info.matrix.choiceLabels = {{
"mixed"}, {
"sinks"}, {
"self"}};
148 auto const [mdp, mapping] = TestFixture::build(info);
149 auto const& matrix = mdp->getTransitionMatrix();
150 TestFixture::checkRow(matrix, 0,
151 {{0, TestFixture::value(0.25)},
152 {1, TestFixture::value(0.25)},
153 {2, TestFixture::value(0.25)},
154 {3, TestFixture::value(0.125)},
155 {4, TestFixture::value(0.125)}});
156 TestFixture::checkRow(matrix, 1, {{3, TestFixture::value(0.5)}, {4, TestFixture::value(0.5)}});
157 TestFixture::checkRow(matrix, 2, {{1, TestFixture::value(1)}});
158 TestFixture::checkRow(matrix, 3, {{3, TestFixture::value(0.75)}, {4, TestFixture::value(0.25)}});
159 TestFixture::checkRow(matrix, 4, {{3, TestFixture::value(1)}});
160 TestFixture::checkRow(matrix, 5, {{4, TestFixture::value(1)}});
162 EXPECT_EQ((std::unordered_map<uint64_t, BeliefId>{{0, 1}, {1, 0}, {2, 2}}), mapping);
163 EXPECT_TRUE(mdp->getStateLabeling().getStateHasLabel(
"truncated", 2));
164 EXPECT_TRUE(mdp->getStateLabeling().getStateHasLabel(
"target", 3));
165 EXPECT_TRUE(mdp->getStateLabeling().getStateHasLabel(
"bottom", 4));
166 ASSERT_TRUE(mdp->hasChoiceLabeling());
167 for (
auto const& [choice, label] :
168 std::vector<std::pair<uint64_t, std::string>>{{0,
"mixed"}, {1,
"sinks"}, {2,
"self"}, {3,
"cutoff"}, {4,
"__loop__"}, {5,
"__loop__"}}) {
169 EXPECT_EQ((std::set<std::string>{label}), mdp->getChoiceLabeling().getLabelsOfChoice(choice));
172 EXPECT_EQ(2, info.matrix.transitions.front().targetBelief);
175TYPED_TEST(BeliefMdpBuilderTest, KeepsMultipleRewardDimensionsAttachedToSuccessors) {
176 auto info = TestFixture::template makeInfo<typename TestFixture::RewardInfo>(4);
177 info.exploredBeliefs = {{0, 1}, {1, 0}, {2, 2}};
178 auto const n = TestFixture::value;
179 info.matrix.transitions = {{n(0.25), 3, {n(5), n(0)}}, {n(0.25), 0, {n(0), n(3)}}, {n(0.25), 2, {n(2), n(7)}}, {n(0.25), 1, {n(11), n(0)}}};
180 info.matrix.endCurrentRow();
182 info.matrix.transitions.push_back({n(0.5), 2, {n(0), n(0)}});
183 info.matrix.transitions.push_back({n(0.25), 1, {n(0), n(0)}});
184 info.matrix.transitions.push_back({n(0.25), 2, {n(0), n(0)}});
185 info.matrix.endCurrentRow();
186 info.matrix.endCurrentRowGroup();
187 for (
auto id : {0ul, 2ul}) {
188 info.matrix.transitions.push_back({n(1), id, {n(0), n(0)}});
189 info.matrix.endCurrentRow();
190 info.matrix.endCurrentRowGroup();
193 property.rewardBounds = {{
"first", std::nullopt, std::nullopt}, {
"second", std::nullopt, std::nullopt}};
194 property.targetObservations = {2, 3};
196 auto const [mdp, mapping] = TestFixture::build(info, property);
197 auto const& probabilities = mdp->getTransitionMatrix();
198 auto const& first = mdp->getRewardModel(
"first").getTransitionRewardMatrix();
199 auto const& second = mdp->getRewardModel(
"second").getTransitionRewardMatrix();
200 TestFixture::checkRow(probabilities, 0, {{0, n(0.25)}, {1, n(0.25)}, {2, n(0.25)}, {3, n(0.25)}});
201 TestFixture::checkRow(probabilities, 1, {{0, n(0.25)}, {2, n(0.75)}});
202 TestFixture::checkRow(first, 0, {{0, n(11)}, {2, n(2)}, {3, n(5)}});
203 TestFixture::checkRow(second, 0, {{1, n(3)}, {2, n(7)}});
204 for (uint64_t row = 1; row < probabilities.getRowCount(); ++row) {
205 TestFixture::checkRow(first, row, {});
206 TestFixture::checkRow(second, row, {});
208 EXPECT_EQ(probabilities.getRowGroupIndices(), first.getRowGroupIndices());
209 EXPECT_EQ(probabilities.getRowGroupIndices(), second.getRowGroupIndices());
210 EXPECT_EQ((std::unordered_map<uint64_t, BeliefId>{{0, 1}, {1, 0}, {2, 2}, {3, 3}}), mapping);
211 EXPECT_TRUE(mdp->getStateLabeling().getStateHasLabel(
"target", 2));
212 EXPECT_TRUE(mdp->getStateLabeling().getStateHasLabel(
"target", 3));
215TYPED_TEST(BeliefMdpBuilderTest, AccumulatesClippingProbabilitiesForBothDirections) {
216 auto info = TestFixture::template makeInfo<typename TestFixture::ClippingInfo>(4);
217 info.exploredBeliefs = {{0, 1}, {1, 0}};
218 auto const n = TestFixture::value;
219 info.terminalBeliefValues = {{3, n(0.5)}};
220 info.matrix.transitions = {{n(0.125), 2, {n(0.125), std::nullopt}},
221 {n(0.125), 0, {n(0.125), std::nullopt}},
222 {n(0.25), 3, {n(0.125), std::nullopt}},
223 {n(0.125), 1, {std::nullopt, std::nullopt}}};
224 info.matrix.endCurrentRow();
225 info.matrix.endCurrentRowGroup();
226 info.matrix.transitions.push_back({n(1), 0, {std::nullopt, std::nullopt}});
227 info.matrix.endCurrentRow();
228 info.matrix.endCurrentRowGroup();
230 for (
auto direction : {storm::OptimizationDirection::Minimize, storm::OptimizationDirection::Maximize}) {
231 auto property = TestFixture::property();
232 property.dir = direction;
233 auto const [mdp, mapping] = TestFixture::build(info, property);
234 auto const target = direction == storm::OptimizationDirection::Minimize ? n(0.5) : n(0.125);
235 auto const bottom = direction == storm::OptimizationDirection::Minimize ? n(0.125) : n(0.5);
236 TestFixture::checkRow(mdp->getTransitionMatrix(), 0, {{0, n(0.125)}, {1, n(0.125)}, {2, n(0.125)}, {3, target}, {4, bottom}});
240TYPED_TEST(BeliefMdpBuilderTest, AccumulatesTerminalRewardsAndClippingPenalties) {
241 auto info = TestFixture::template makeInfo<typename TestFixture::ClippingInfo>(4);
242 info.exploredBeliefs = {{0, 1}, {1, 0}};
243 auto const n = TestFixture::value;
244 info.terminalBeliefValues = {{3, n(4)}};
245 info.matrix.transitions = {{n(0.25), 2, {n(0.125), n(2)}}, {n(0.25), 0, {n(0.125), std::nullopt}}, {n(0.125), 3, {n(0.125), n(3)}}};
246 info.matrix.endCurrentRow();
247 info.matrix.transitions.push_back({n(1), 3, {std::nullopt, std::nullopt}});
248 info.matrix.endCurrentRow();
249 info.matrix.endCurrentRowGroup();
250 info.matrix.transitions.push_back({n(1), 0, {std::nullopt, std::nullopt}});
251 info.matrix.endCurrentRow();
252 info.matrix.endCurrentRowGroup();
253 info.actionRewards = {n(1), n(2), n(3)};
255 property.rewardModelName =
"reward";
257 auto const [mdp, mapping] = TestFixture::build(info, property);
258 TestFixture::checkRow(mdp->getTransitionMatrix(), 0, {{1, n(0.25)}, {2, n(0.25)}, {3, n(0.5)}});
259 TestFixture::checkRow(mdp->getTransitionMatrix(), 1, {{3, n(1)}});
260 EXPECT_EQ((std::vector<TypeParam>{n(6.5), n(6), n(3), n(0.75), n(0), n(0)}), mdp->getRewardModel(
"reward").getStateActionRewardVector());
BeliefType build()
Builds the belief and returns it.
void setObservation(BeliefObservationType const &observation)
Sets the observation of the belief.
void addValue(BeliefStateType const &state, ValueType const &value)
Adds a probability value to the given state.
const_rows getRow(index_type row) const
Returns an object representing the given row.
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
SFTBDDChecker::ValueType ValueType
ExplorationInformation< BeliefMdpValueType, BeliefType, std::optional< BeliefMdpValueType >, std::optional< BeliefMdpValueType > > ClippingExplorationInformation
Exploration information that stores optional clipping probability and reward-adjustment metadata.
ExplorationInformation< BeliefMdpValueType, BeliefType > StandardExplorationInformation
Exploration information without transition metadata.
std::pair< std::shared_ptr< models::sparse::Mdp< BeliefMdpValueType > >, std::unordered_map< uint64_t, BeliefId > > buildBeliefMdp(ExplorationInformation< BeliefMdpValueType, BeliefType, ExtraTransitionData... > const &explorationInformation, PropertyInformation const &propertyInformation, std::function< std::unordered_map< std::string, BeliefMdpValueType >(BeliefType const &)> const &computeCutOffValueMap)
Builds a belief MDP from the given exploration information and property information.
ExplorationInformation< BeliefMdpValueType, BeliefType, std::vector< BeliefMdpValueType > > RewardAwareExplorationInformation
Exploration information that stores a reward vector on each transition.
TargetType convertNumber(SourceType const &number)
TYPED_TEST(GraphTestAR, SymbolicProb01StochasticGameDieSmall)
TYPED_TEST_SUITE(GraphTestAR, TestingTypes,)