Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
PolicyTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
4#include <sstream>
5#include <string>
6
32
33namespace {
34
35storm::models::sparse::Pomdp<double> buildSingleStatePomdp(bool withRewards = false, bool withChoiceLabels = false) {
36 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
37 matrixBuilder.newRowGroup(0);
38 matrixBuilder.addNextValue(0, 0, 1.0);
39 matrixBuilder.addNextValue(1, 0, 1.0);
40
42 stateLabeling.addLabel("init");
43 stateLabeling.addLabelToState("init", 0);
44
45 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
46 components.observabilityClasses = std::vector<uint32_t>{0};
47 if (withChoiceLabels) {
49 choiceLabeling.addLabel("first");
50 choiceLabeling.addLabelToChoice("first", 0);
51 choiceLabeling.addLabel("second");
52 choiceLabeling.addLabelToChoice("second", 1);
53 components.choiceLabeling = std::move(choiceLabeling);
54 }
55 if (withRewards) {
56 components.rewardModels.emplace("rew", storm::models::sparse::StandardRewardModel<double>(std::vector<double>{3.0}, std::vector<double>{2.0, 6.0}));
57 }
58 return storm::models::sparse::Pomdp<double>(std::move(components), true);
59}
60
61storm::models::sparse::Pomdp<double> buildSingleStatePomdpWithObservationValuations(bool reverseObservableOrder) {
62 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
63 matrixBuilder.newRowGroup(0);
64 matrixBuilder.addNextValue(0, 0, 1.0);
65
67 stateLabeling.addLabel("init");
68 stateLabeling.addLabelToState("init", 0);
69
70 auto manager = std::make_shared<storm::expressions::ExpressionManager>();
71 auto const enabled = manager->declareBooleanVariable("enabled");
72 auto const mode = manager->declareIntegerVariable("mode");
74 if (reverseObservableOrder) {
75 valuationBuilder.addIntegerVariable(mode, 0, 1);
76 valuationBuilder.addBooleanVariable(enabled);
77 } else {
78 valuationBuilder.addBooleanVariable(enabled);
79 valuationBuilder.addIntegerVariable(mode, 0, 1);
80 }
81 storm::storage::sparse::Valuations observationValuations(valuationBuilder.buildClassDescription(), manager);
82 observationValuations.getStorage().resize(1);
83 observationValuations.getStorage().writeValue(0, enabled, true);
84 observationValuations.getStorage().writeValue(0, mode, int64_t{1});
85
86 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
87 components.observabilityClasses = std::vector<uint32_t>{0};
88 components.observationValuations = std::move(observationValuations);
89 return storm::models::sparse::Pomdp<double>(std::move(components), true);
90}
91
92storm::models::sparse::Mdp<double> buildBeliefMdpWithReorderedCutoffChoices(bool truncatedInitialState = false) {
93 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
94 matrixBuilder.newRowGroup(0);
95 matrixBuilder.addNextValue(0, 1, 1.0);
96 matrixBuilder.newRowGroup(1);
97 matrixBuilder.addNextValue(1, 2, 1.0);
98 matrixBuilder.addNextValue(2, 2, 1.0);
99 matrixBuilder.newRowGroup(3);
100 matrixBuilder.addNextValue(3, 2, 1.0);
101
103 stateLabeling.addLabel("init");
104 stateLabeling.addLabelToState("init", truncatedInitialState ? 1 : 0);
105 stateLabeling.addLabel("truncated");
106 stateLabeling.addLabelToState("truncated", 1);
107
109 choiceLabeling.addLabel("__sched_1");
110 choiceLabeling.addLabelToChoice("__sched_1", 1);
111 choiceLabeling.addLabel("__sched_0");
112 choiceLabeling.addLabelToChoice("__sched_0", 2);
113
114 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
115 components.choiceLabeling = std::move(choiceLabeling);
116 return storm::models::sparse::Mdp<double>(std::move(components));
117}
118
119storm::models::sparse::Mdp<double> buildSingleStateBeliefMdpWithUnlabelledSecondChoice() {
120 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
121 matrixBuilder.newRowGroup(0);
122 matrixBuilder.addNextValue(0, 0, 1.0);
123 matrixBuilder.addNextValue(1, 0, 1.0);
124
126 stateLabeling.addLabel("init");
127 stateLabeling.addLabelToState("init", 0);
128 stateLabeling.addLabel("truncated");
129
131 choiceLabeling.addLabel("first");
132 choiceLabeling.addLabelToChoice("first", 0);
133
134 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
135 components.choiceLabeling = std::move(choiceLabeling);
136 return storm::models::sparse::Mdp<double>(std::move(components));
137}
138
139} // namespace
140
141TEST(ObservationBasedFiniteStateController, DeterminismTracksReplacedOutputs) {
144 distribution.addProbability(0, 0.5);
145 distribution.addProbability(1, 0.5);
146
147 fsc.addRandomisedActionTransition(0, 0, distribution, 0);
148 EXPECT_FALSE(fsc.isDeterministic());
149
150 fsc.addDeterministicActionTransition(0, 0, 1, 0);
151 EXPECT_TRUE(fsc.isDeterministic());
152}
153
154TEST(ObservationBasedFiniteStateController, FormatsPartialSignatureMaps) {
156 observationSignature.values.emplace("known-observable", std::string{"known-value"});
157 std::optional observationSignatures{std::unordered_map<uint64_t, storm::pomdp::policy::ObservationSignature>{{0, observationSignature}}};
158 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{0, "known-action"}}}}};
159 storm::pomdp::policy::ObservationBasedFiniteStateController<double> fsc(0, observationSignatures, actionNames);
160 fsc.addDeterministicActionTransition(0, 0, 0, 1);
161
163 distribution.addProbability(1, 1.0);
164 fsc.addRandomisedActionTransition(1, 1, distribution, 0);
165
166 auto const description = fsc.toString();
167 EXPECT_NE(description.find("(0)[known-observable=known-value]"), std::string::npos);
168 EXPECT_NE(description.find("1 ->"), std::string::npos);
169 EXPECT_NE(description.find("(1) :"), std::string::npos);
170
171 std::stringstream jsonOutput;
172 fsc.writeJsonToStream(jsonOutput);
173 auto const json = storm::json<double>::parse(jsonOutput.str());
174 auto const& observable = json.at("nodes").at(0).at("transitions").at(0).at("observation").at("signature").at(0);
175 EXPECT_EQ(observable.at("name").get<std::string>(), "known-observable");
176 EXPECT_EQ(observable.at("type").get<std::string>(), "string");
177 EXPECT_EQ(observable.at("value").get<std::string>(), "known-value");
178}
179
180TEST(ObservationBasedFiniteStateController, EscapesSignaturesAndActionNamesInDotOutput) {
182 observationSignature.values.emplace("observation", std::string{"value\"\\\n"});
183 std::optional observationSignatures{std::unordered_map<uint64_t, storm::pomdp::policy::ObservationSignature>{{0, observationSignature}}};
184 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{0, "action\"\\\n"}}}}};
185 storm::pomdp::policy::ObservationBasedFiniteStateController<double> fsc(0, observationSignatures, actionNames);
186 fsc.addDeterministicActionTransition(0, 0, 0, 0);
187
188 std::stringstream dotOutput;
189 fsc.writeDotToStream(dotOutput);
190
191 auto const dot = dotOutput.str();
192 EXPECT_NE(dot.find("observation=value\\\"\\\\\\n"), std::string::npos);
193 EXPECT_NE(dot.find("action\\\"\\\\\\n"), std::string::npos);
194}
195
196TEST(ApplyObservationBasedFiniteStateController, RejectsMultipleInitialStates) {
197 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
198 matrixBuilder.newRowGroup(0);
199 matrixBuilder.addNextValue(0, 0, 1.0);
200 matrixBuilder.newRowGroup(1);
201 matrixBuilder.addNextValue(1, 1, 1.0);
202
204 stateLabeling.addLabel("init");
205 stateLabeling.addLabelToState("init", 0);
206 stateLabeling.addLabelToState("init", 1);
207
208 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
209 components.observabilityClasses = std::vector<uint32_t>{0, 0};
210 storm::models::sparse::Pomdp<double> pomdp(std::move(components), true);
212 fsc.addDeterministicActionTransition(0, 0, 0, 0);
213
214 EXPECT_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc), storm::exceptions::NotSupportedException);
215 EXPECT_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc, true), storm::exceptions::NotSupportedException);
216}
217
218TEST(ApplyObservationBasedFiniteStateController, RejectsNoncanonicActionOrdering) {
219 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
220 matrixBuilder.newRowGroup(0);
221 matrixBuilder.addNextValue(0, 1, 1.0);
222 matrixBuilder.addNextValue(1, 0, 1.0);
223 matrixBuilder.newRowGroup(2);
224 matrixBuilder.addNextValue(2, 0, 1.0);
225 matrixBuilder.addNextValue(3, 1, 1.0);
226
228 stateLabeling.addLabel("init");
229 stateLabeling.addLabelToState("init", 0);
231 choiceLabeling.addLabel("a");
232 choiceLabeling.addLabelToChoice("a", 0);
233 choiceLabeling.addLabelToChoice("a", 3);
234 choiceLabeling.addLabel("b");
235 choiceLabeling.addLabelToChoice("b", 1);
236 choiceLabeling.addLabelToChoice("b", 2);
237
238 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
239 components.observabilityClasses = std::vector<uint32_t>{0, 0};
240 components.choiceLabeling = std::move(choiceLabeling);
241 storm::models::sparse::Pomdp<double> pomdp(std::move(components));
242 ASSERT_FALSE(pomdp.isCanonic());
243
244 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{0, "a"}, {1, "b"}}}}};
245 storm::pomdp::policy::ObservationBasedFiniteStateController<double> namedFsc(0, std::nullopt, actionNames);
246 namedFsc.addDeterministicActionTransition(0, 0, 0, 0);
248 numericFsc.addDeterministicActionTransition(0, 0, 0, 0);
249 for (bool dontCare : {false, true}) {
250 SCOPED_TRACE(dontCare);
251 EXPECT_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, namedFsc, dontCare), storm::exceptions::IllegalArgumentException);
252 EXPECT_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, numericFsc, dontCare), storm::exceptions::IllegalArgumentException);
253 }
254
255 auto canonicPomdp = storm::transformer::MakePOMDPCanonic<double>(pomdp).transform();
256 ASSERT_TRUE(canonicPomdp->isCanonic());
257 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(*canonicPomdp, namedFsc);
258 ASSERT_EQ(inducedDtmc.getNumberOfStates(), 2);
259 ASSERT_EQ(inducedDtmc.getNumberOfTransitions(), 2);
260 for (uint64_t state : {0ul, 1ul}) {
261 auto row = inducedDtmc.getTransitionMatrix().getRow(state);
262 ASSERT_EQ(row.getNumberOfEntries(), 1);
263 EXPECT_EQ(row.begin()->getColumn(), 1);
264 EXPECT_DOUBLE_EQ(row.begin()->getValue(), 1.0);
265 }
266}
267
268TEST(ApplyObservationBasedFiniteStateController, PreservesRewardsForRandomisedActions) {
269 auto pomdp = buildSingleStatePomdp(true);
272 distribution.addProbability(0, 0.25);
273 distribution.addProbability(1, 0.75);
274 fsc.addRandomisedActionTransition(0, 0, distribution, 0);
275
276 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc);
277
278 ASSERT_TRUE(inducedDtmc.hasRewardModel("rew"));
279 auto const& rewards = inducedDtmc.getRewardModel("rew").getStateRewardVector();
280 ASSERT_EQ(rewards.size(), inducedDtmc.getNumberOfStates());
281 EXPECT_DOUBLE_EQ(rewards.at(0), 8.0);
282}
283
284TEST(ApplyObservationBasedFiniteStateController, RejectsTransitionRewards) {
285 for (bool withStateAndActionRewards : {false, true}) {
286 SCOPED_TRACE(withStateAndActionRewards);
287 for (bool emptyTransitionRewards : {false, true}) {
288 SCOPED_TRACE(emptyTransitionRewards);
289 auto pomdp = buildSingleStatePomdp(true);
291 if (!emptyTransitionRewards) {
292 // Reject transition rewards even when they only occur on an action the FSC never chooses.
293 rewardBuilder.addNextValue(1, 0, 7.0);
294 }
295 std::optional<std::vector<double>> stateRewards;
296 std::optional<std::vector<double>> actionRewards;
297 if (withStateAndActionRewards) {
298 stateRewards = std::vector<double>{3.0};
299 actionRewards = std::vector<double>{2.0, 6.0};
300 }
301 pomdp.getRewardModels().emplace(
302 "transition", storm::models::sparse::StandardRewardModel<double>(std::move(stateRewards), std::move(actionRewards), rewardBuilder.build()));
304 fsc.addDeterministicActionTransition(0, 0, 0, 0);
305 for (bool dontCare : {false, true}) {
306 SCOPED_TRACE(dontCare);
307 EXPECT_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc, dontCare), storm::exceptions::NotSupportedException);
308 }
309 }
310 }
311}
312
313TEST(ApplyObservationBasedFiniteStateController, PreservesLabelsOfUnreachableStates) {
314 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
315 matrixBuilder.newRowGroup(0);
316 matrixBuilder.addNextValue(0, 0, 1.0);
317 matrixBuilder.addNextValue(1, 1, 1.0);
318 matrixBuilder.newRowGroup(2);
319 matrixBuilder.addNextValue(2, 1, 1.0);
320
322 stateLabeling.addLabel("init");
323 stateLabeling.addLabelToState("init", 0);
324 stateLabeling.addLabel("goal");
325 stateLabeling.addLabelToState("goal", 1);
326 stateLabeling.addLabel("safe");
327 stateLabeling.addLabelToState("safe", 0);
328 stateLabeling.addLabel("empty");
329
330 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
331 components.observabilityClasses = std::vector<uint32_t>{0, 1};
332 storm::models::sparse::Pomdp<double> pomdp(std::move(components), true);
334 fsc.addDeterministicActionTransition(0, 0, 0, 0);
335
336 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc);
337 ASSERT_EQ(inducedDtmc.getNumberOfStates(), 1);
338 auto const& labeling = inducedDtmc.getStateLabeling();
339 EXPECT_EQ(labeling.getLabels(), pomdp.getStateLabeling().getLabels());
340 ASSERT_TRUE(labeling.containsLabel("goal"));
341 EXPECT_TRUE(labeling.getStates("goal").empty());
342 EXPECT_TRUE(labeling.getStates("empty").empty());
343 EXPECT_TRUE(labeling.getStateHasLabel("safe", 0));
344 EXPECT_TRUE(labeling.getStateHasLabel("init", 0));
345
347 storm::parser::FormulaParser formulaParser;
348 auto formula = formulaParser.parseSingleFormulaFromString("P=? [F \"goal\"]");
350 auto result = checker.check(env, *formula);
351 EXPECT_DOUBLE_EQ(result->asExplicitQuantitativeCheckResult<double>()[0], 0.0);
352}
353
354TEST(ApplyObservationBasedFiniteStateController, MatchesObservationValuationsIndependentOfStorageOrder) {
355 auto sourcePomdp = buildSingleStatePomdpWithObservationValuations(false);
356 auto targetPomdp = buildSingleStatePomdpWithObservationValuations(true);
357
358 auto const sourceObservationSignature = storm::pomdp::policy::getObservationSignature(sourcePomdp, 0);
359 EXPECT_EQ(sourceObservationSignature, storm::pomdp::policy::getObservationSignature(targetPomdp, 0));
360
361 std::optional observationSignatures{std::unordered_map<uint64_t, storm::pomdp::policy::ObservationSignature>{{0, sourceObservationSignature}}};
362 storm::pomdp::policy::ObservationBasedFiniteStateController<double> fsc(0, observationSignatures, std::nullopt);
363 fsc.addDeterministicActionTransition(0, 0, 0, 0);
364
365 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(targetPomdp, fsc);
366 EXPECT_EQ(inducedDtmc.getNumberOfStates(), 1);
367}
368
369TEST(ApplyObservationBasedFiniteStateController, NamedActionMappingsTakePrecedenceOverUnnamedFallbacks) {
370 for (uint64_t namedActionIndex : {0ul, 1ul}) {
371 SCOPED_TRACE(namedActionIndex);
372 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
373 matrixBuilder.newRowGroup(0);
374 matrixBuilder.addNextValue(0, 0, 1.0);
375 matrixBuilder.addNextValue(1, 0, 1.0);
376 matrixBuilder.addNextValue(2, 0, 1.0);
377
379 stateLabeling.addLabel("init");
380 stateLabeling.addLabelToState("init", 0);
382 choiceLabeling.addLabel("named");
383 choiceLabeling.addLabelToChoice("named", namedActionIndex);
384
385 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
386 components.observabilityClasses = std::vector<uint32_t>{0};
387 components.choiceLabeling = std::move(choiceLabeling);
388 components.rewardModels.emplace("rew", storm::models::sparse::StandardRewardModel<double>(std::nullopt, std::vector<double>{2.0, 6.0, 10.0}));
389 storm::models::sparse::Pomdp<double> pomdp(std::move(components), true);
390
391 uint64_t const fscNamedActionId = 1 - namedActionIndex;
392 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{fscNamedActionId, "named"}}}}};
394 fsc.addDeterministicActionTransition(0, 0, fscNamedActionId, 0);
395
397 EXPECT_DOUBLE_EQ(namedDtmc.getRewardModel("rew").getStateRewardVector().at(0), namedActionIndex == 0 ? 2.0 : 6.0);
398
399 fsc.addDeterministicActionTransition(0, 0, 2, 0);
400 auto unnamedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc);
401 EXPECT_DOUBLE_EQ(unnamedDtmc.getRewardModel("rew").getStateRewardVector().at(0), 10.0);
402 }
403}
404
405TEST(ApplyObservationBasedFiniteStateController, MatchesLabelledChoicesForUnnamedActions) {
406 auto pomdp = buildSingleStatePomdp(true, true);
407 for (std::string const actionName : {"first", "second"}) {
408 SCOPED_TRACE(actionName);
409 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{0, actionName}}}}};
411 fsc.addDeterministicActionTransition(0, 0, 1, 0);
412
413 auto unnamedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc);
414 EXPECT_EQ(unnamedDtmc.getNumberOfStates(), 1);
415 EXPECT_DOUBLE_EQ(unnamedDtmc.getRewardModel("rew").getStateRewardVector().at(0), 9.0);
416
417 fsc.addDeterministicActionTransition(0, 0, 0, 0);
419 EXPECT_DOUBLE_EQ(namedDtmc.getRewardModel("rew").getStateRewardVector().at(0), actionName == "first" ? 5.0 : 9.0);
420 }
421}
422
423TEST(ApplyObservationBasedFiniteStateController, RejectsNamedActionMissingFromUnlabelledChoices) {
424 auto pomdp = buildSingleStatePomdp(true);
425 pomdp.getOptionalChoiceLabeling() = storm::models::sparse::ChoiceLabeling(2);
426 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{0, "missing-action"}}}}};
428 fsc.addDeterministicActionTransition(0, 0, 0, 0);
429
430 for (bool dontCare : {false, true}) {
431 SCOPED_TRACE(dontCare);
432 EXPECT_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc, dontCare), storm::exceptions::UnexpectedException);
433 }
434}
435
436TEST(ApplyObservationBasedFiniteStateController, MatchesUnlabelledChoicesForUnnamedActionsOrMissingMetadata) {
437 auto pomdp = buildSingleStatePomdp(true);
438 pomdp.getOptionalChoiceLabeling() = storm::models::sparse::ChoiceLabeling(2);
439 using ActionNames = std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>;
440 std::vector<std::optional<ActionNames>> actionNameMaps{ActionNames{{0, {{0, ""}}}}, ActionNames{{0, {{1, "other-action"}}}},
441 ActionNames{{1, {{0, "other-action"}}}}, ActionNames{}, std::nullopt};
442 for (auto const& actionNames : actionNameMaps) {
444 fsc.addDeterministicActionTransition(0, 0, 0, 0);
445
446 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc);
447 EXPECT_EQ(inducedDtmc.getNumberOfStates(), 1);
448 EXPECT_DOUBLE_EQ(inducedDtmc.getRewardModel("rew").getStateRewardVector().at(0), 5.0);
449 }
450}
451
452TEST(ApplyObservationBasedFiniteStateController, UnspecifiedOutputUsesLocalActionZeroWithReorderedActionNames) {
453 auto pomdp = buildSingleStatePomdp(true, true);
454 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{0, "second"}, {1, "first"}}}}};
456
457 EXPECT_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc), storm::exceptions::UnexpectedException);
458 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc, true);
459
460 EXPECT_EQ(inducedDtmc.getNumberOfStates(), 1);
461 EXPECT_DOUBLE_EQ(inducedDtmc.getRewardModel("rew").getStateRewardVector().at(0), 5.0);
462}
463
464TEST(ApplyObservationBasedFiniteStateController, UnspecifiedOutputUsesLocalActionZeroWithIncompleteActionNames) {
465 auto pomdp = buildSingleStatePomdp(true, true);
466 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{1, "second"}}}}};
468
469 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc, true);
470
471 EXPECT_EQ(inducedDtmc.getNumberOfStates(), 1);
472 EXPECT_DOUBLE_EQ(inducedDtmc.getRewardModel("rew").getStateRewardVector().at(0), 5.0);
473}
474
475TEST(ApplyObservationBasedFiniteStateController, SpecifiedOutputRemapsActionNamesWithDontCareEnabled) {
476 auto pomdp = buildSingleStatePomdp(true, true);
477 std::optional actionNames{std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>{{0, {{0, "second"}, {1, "first"}}}}};
479 fsc.addDeterministicActionTransition(7, 0, 0, 7);
480
481 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc, true);
482
483 EXPECT_EQ(inducedDtmc.getNumberOfStates(), 1);
484 EXPECT_DOUBLE_EQ(inducedDtmc.getRewardModel("rew").getStateRewardVector().at(0), 9.0);
485}
486
487TEST(PolicyExtractor, OmitsObservationSignaturesAfterTargetSplitting) {
488 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
489 matrixBuilder.newRowGroup(0);
490 matrixBuilder.addNextValue(0, 1, 1.0);
491 matrixBuilder.newRowGroup(1);
492 matrixBuilder.addNextValue(1, 1, 1.0);
493
495 stateLabeling.addLabel("init");
496 stateLabeling.addLabelToState("init", 0);
497 stateLabeling.addLabel("truncated");
498
499 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
500 auto const beliefMdp = storm::models::sparse::Mdp<double>(components);
501 components.observabilityClasses = std::vector<uint32_t>{0, 0};
502 components.observationValuations = buildSingleStatePomdpWithObservationValuations(false).getObservationValuations();
503 auto pomdp = std::make_shared<storm::models::sparse::Pomdp<double>>(std::move(components), true);
504
505 storm::storage::Scheduler<double> beliefScheduler(2);
506 beliefScheduler.setChoice(0, 0);
507 beliefScheduler.setChoice(0, 1);
508 std::unordered_map<uint64_t, uint32_t> beliefStateToObservation{{0, 0}, {1, 0}};
509 storm::pomdp::policy::PolicyExtractor<storm::models::sparse::Pomdp<double>, double, double> originalExtractor(*pomdp, beliefMdp, beliefStateToObservation,
510 beliefScheduler);
511 EXPECT_TRUE(originalExtractor.exportPolicyAsFiniteStateController().hasIdToObservationSignatureMap());
512
513 storm::storage::BitVector targetStates(2, false);
514 targetStates.set(1);
516 auto const [splitPomdp, targetObservations] = observationCloser.transform(targetStates);
517 ASSERT_EQ(splitPomdp->getNrObservations(), 2);
518 ASSERT_TRUE(splitPomdp->hasObservationValuations());
519 ASSERT_EQ(splitPomdp->getObservationValuations().getNumberOfEntities(), 1);
520
521 beliefStateToObservation.at(1) = splitPomdp->getObservation(1);
522 storm::pomdp::policy::PolicyExtractor<storm::models::sparse::Pomdp<double>, double, double> splitExtractor(*splitPomdp, beliefMdp, beliefStateToObservation,
523 beliefScheduler);
524 auto const fsc = splitExtractor.exportPolicyAsFiniteStateController();
525 EXPECT_FALSE(fsc.hasIdToObservationSignatureMap());
526 EXPECT_EQ(fsc.getActionForObservationInNode(1, splitPomdp->getObservation(1)), 0);
527 EXPECT_NO_THROW(storm::pomdp::policy::applyObservationBasedFSCToPomdp(*splitPomdp, fsc));
528}
529
530TEST(PolicyExtractor, UsesCutoffSchedulerLabelInsteadOfLocalChoiceIndex) {
531 auto pomdp = buildSingleStatePomdp();
532 auto beliefMdp = buildBeliefMdpWithReorderedCutoffChoices();
533
534 storm::storage::Scheduler<double> beliefScheduler(3);
535 beliefScheduler.setChoice(0, 0);
536 beliefScheduler.setChoice(0, 1);
537 beliefScheduler.setChoice(0, 2);
538
539 storm::storage::Scheduler<double> schedulerZero(1);
540 schedulerZero.setChoice(0, 0);
541 storm::storage::Scheduler<double> schedulerOne(1);
542 schedulerOne.setChoice(1, 0);
543 std::optional<std::vector<storm::storage::Scheduler<double>>> approximationSchedulers(
544 std::vector<storm::storage::Scheduler<double>>{schedulerZero, schedulerOne});
545 std::unordered_map<uint64_t, uint32_t> beliefStateToObservation{{0, 0}, {1, 0}};
546
547 storm::pomdp::policy::PolicyExtractor<storm::models::sparse::Pomdp<double>, double, double> extractor(pomdp, beliefMdp, beliefStateToObservation,
548 beliefScheduler, approximationSchedulers);
549 auto fsc = extractor.exportPolicyAsFiniteStateController();
550
551 EXPECT_EQ(fsc.getActionForObservationInNode(1, 0), 1);
552}
553
554TEST(PolicyExtractor, UsesContinuationSchedulerForTruncatedInitialBelief) {
555 auto pomdp = buildSingleStatePomdp(true);
556 auto beliefMdp = buildBeliefMdpWithReorderedCutoffChoices(true);
557 storm::storage::Scheduler<double> beliefScheduler(3);
558 beliefScheduler.setChoice(0, 0);
559 beliefScheduler.setChoice(0, 1);
560 beliefScheduler.setChoice(0, 2);
561 std::unordered_map<uint64_t, uint32_t> beliefStateToObservation{{1, 0}};
562
563 for (bool randomised : {false, true}) {
564 SCOPED_TRACE(randomised);
565 storm::storage::Scheduler<double> schedulerZero(1);
566 schedulerZero.setChoice(0, 0);
567 storm::storage::Scheduler<double> schedulerOne(1);
568 if (randomised) {
570 actionDistribution.addProbability(0, 0.25);
571 actionDistribution.addProbability(1, 0.75);
572 schedulerOne.setChoice(actionDistribution, 0);
573 } else {
574 schedulerOne.setChoice(1, 0);
575 }
576 std::optional<std::vector<storm::storage::Scheduler<double>>> approximationSchedulers(
577 std::vector<storm::storage::Scheduler<double>>{schedulerZero, schedulerOne});
578
579 storm::pomdp::policy::PolicyExtractor<storm::models::sparse::Pomdp<double>, double, double> extractor(pomdp, beliefMdp, beliefStateToObservation,
580 beliefScheduler, approximationSchedulers);
581 auto fsc = extractor.exportPolicyAsFiniteStateController();
582
583 EXPECT_EQ(fsc.isDeterministic(), !randomised);
584 auto const [initialDistribution, continuationNode] = fsc.getActionDistributionAndSuccessorForObservationInNode(0, 0);
585 ASSERT_TRUE(fsc.hasOutputForObservationInNode(continuationNode, 0));
586 auto const [continuationDistribution, successorNode] = fsc.getActionDistributionAndSuccessorForObservationInNode(continuationNode, 0);
587 EXPECT_EQ(successorNode, continuationNode);
588 EXPECT_EQ(initialDistribution.size(), randomised ? 2 : 1);
589 EXPECT_EQ(continuationDistribution.size(), initialDistribution.size());
590 for (auto const& [action, probability] : schedulerOne.getChoice(0).getChoiceAsDistribution()) {
591 EXPECT_DOUBLE_EQ(initialDistribution.getProbability(action), probability);
592 EXPECT_DOUBLE_EQ(continuationDistribution.getProbability(action), probability);
593 }
594
595 auto inducedDtmc = storm::pomdp::policy::applyObservationBasedFSCToPomdp(pomdp, fsc);
596 auto const& rewards = inducedDtmc.getRewardModel("rew").getStateRewardVector();
597 for (auto const reward : rewards) {
598 EXPECT_DOUBLE_EQ(reward, randomised ? 8.0 : 9.0);
599 }
600 }
601}
602
603TEST(PolicyExtractor, RequiresObservationBasedCutoffSchedulers) {
604 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
605 matrixBuilder.newRowGroup(0);
606 matrixBuilder.addNextValue(0, 0, 1.0);
607 matrixBuilder.addNextValue(1, 0, 1.0);
608 matrixBuilder.newRowGroup(2);
609 matrixBuilder.addNextValue(2, 1, 1.0);
610 matrixBuilder.addNextValue(3, 1, 1.0);
612 labeling.addLabel("init");
613 labeling.addLabelToState("init", 0);
614 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(labeling));
615 components.observabilityClasses = std::vector<uint32_t>{0, 0};
616 storm::models::sparse::Pomdp<double> pomdp(std::move(components), true);
617
618 auto beliefMdp = buildBeliefMdpWithReorderedCutoffChoices(true);
619 storm::storage::Scheduler<double> beliefScheduler(3);
620 beliefScheduler.setChoice(0, 0);
621 beliefScheduler.setChoice(1, 1); // Select __sched_0 at the truncated initial belief.
622 beliefScheduler.setChoice(0, 2);
623 std::unordered_map<uint64_t, uint32_t> beliefStateToObservation{{1, 0}};
624
625 for (bool randomised : {false, true}) {
626 SCOPED_TRACE(randomised);
628 if (randomised) {
630 firstDistribution.addProbability(0, 0.25);
631 firstDistribution.addProbability(1, 0.75);
632 scheduler.setChoice(firstDistribution, 0);
634 secondDistribution.addProbability(0, 0.75);
635 secondDistribution.addProbability(1, 0.25);
636 scheduler.setChoice(secondDistribution, 1);
637 } else {
638 scheduler.setChoice(0, 0);
639 scheduler.setChoice(1, 1);
640 }
641 auto extractPolicy = [&]() {
642 std::optional<std::vector<storm::storage::Scheduler<double>>> approximationSchedulers({scheduler});
643 storm::pomdp::policy::PolicyExtractor<storm::models::sparse::Pomdp<double>, double, double> extractor(pomdp, beliefMdp, beliefStateToObservation,
644 beliefScheduler, approximationSchedulers);
645 return extractor.exportPolicyAsFiniteStateController();
646 };
647 STORM_SILENT_EXPECT_THROW(extractPolicy(), storm::exceptions::IllegalArgumentException);
648
649 scheduler.setChoice(scheduler.getChoice(0), 1);
650 auto const fsc = extractPolicy();
651 auto const [distribution, continuationNode] = fsc.getActionDistributionAndSuccessorForObservationInNode(0, 0);
652 EXPECT_EQ(fsc.isDeterministic(), !randomised);
653 ASSERT_EQ(distribution.size(), randomised ? 2 : 1);
654 for (auto const& [action, probability] : scheduler.getChoice(0).getChoiceAsDistribution()) {
655 EXPECT_DOUBLE_EQ(distribution.getProbability(action), probability);
656 }
657 EXPECT_TRUE(fsc.hasOutputForObservationInNode(continuationNode, 0));
658 }
659}
660
661TEST(PolicyExtractor, InducedMarkovChainClipsSelectedChoice) {
662 auto pomdp = buildSingleStatePomdp();
663 storm::storage::SparseMatrixBuilder<double> matrixBuilder(0, 0, 0, false, true);
664 matrixBuilder.newRowGroup(0);
665 matrixBuilder.addNextValue(0, 1, 1.0);
666 matrixBuilder.addNextValue(1, 1, 1.0);
667 matrixBuilder.newRowGroup(2);
668 matrixBuilder.addNextValue(2, 0, 0.25);
669 matrixBuilder.addNextValue(2, 2, 0.75);
670 matrixBuilder.addNextValue(3, 0, 0.4);
671 matrixBuilder.addNextValue(3, 3, 0.6);
672 matrixBuilder.newRowGroup(4);
673 matrixBuilder.addNextValue(4, 2, 1.0);
674 matrixBuilder.newRowGroup(5);
675 matrixBuilder.addNextValue(5, 3, 1.0);
676
678 stateLabeling.addLabel("init");
679 stateLabeling.addLabelToState("init", 0);
680 stateLabeling.addLabel("truncated");
681 stateLabeling.addLabelToState("truncated", 1);
683 for (uint64_t choice = 0; choice < 2; ++choice) {
684 auto const candidateLabel = "candidate_" + std::to_string(choice);
685 stateLabeling.addLabel(candidateLabel);
686 stateLabeling.addLabelToState(candidateLabel, 2 + choice);
687 auto const clipLabel = "clip_" + std::to_string(choice);
688 choiceLabeling.addLabel(clipLabel);
689 choiceLabeling.addLabelToChoice(clipLabel, 2 + choice);
690 }
691 storm::storage::sparse::ModelComponents<double> components(matrixBuilder.build(), std::move(stateLabeling));
692 components.choiceLabeling = std::move(choiceLabeling);
693 storm::models::sparse::Mdp<double> beliefMdp(std::move(components));
694 std::unordered_map<uint64_t, uint32_t> beliefStateToObservation{{0, 0}, {1, 0}};
695
696 for (uint64_t chosenAction = 0; chosenAction < 2; ++chosenAction) {
697 SCOPED_TRACE(chosenAction);
698 storm::storage::Scheduler<double> beliefScheduler(4);
699 beliefScheduler.setChoice(1, 0);
700 beliefScheduler.setChoice(chosenAction, 1);
701 beliefScheduler.setChoice(0, 2);
702 beliefScheduler.setChoice(0, 3);
703 storm::pomdp::policy::PolicyExtractor<storm::models::sparse::Pomdp<double>, double, double> extractor(pomdp, beliefMdp, beliefStateToObservation,
704 beliefScheduler);
705 auto inducedModel = extractor.exportPolicyAsInducedMarkovChain();
706
707 EXPECT_EQ(inducedModel->getNumberOfStates(), 3);
708 auto const& labeling = inducedModel->getStateLabeling();
709 auto const& clippingStates = labeling.getStates("clipping");
710 ASSERT_EQ(clippingStates.getNumberOfSetBits(), 1);
711 auto row = inducedModel->getTransitionMatrix().getRow(clippingStates.getNextSetIndex(0));
712 ASSERT_EQ(row.getNumberOfEntries(), 1);
713 EXPECT_DOUBLE_EQ(row.begin()->getValue(), 1.0);
714 EXPECT_TRUE(labeling.getStateHasLabel("candidate_" + std::to_string(chosenAction), row.begin()->getColumn()));
715 }
716}
717
718TEST(PolicyExtractor, AddsEmptyLabelToSelectedUnlabelledChoice) {
719 auto pomdp = buildSingleStatePomdp();
720 auto beliefMdp = buildSingleStateBeliefMdpWithUnlabelledSecondChoice();
721 storm::storage::Scheduler<double> beliefScheduler(1);
722 beliefScheduler.setChoice(1, 0);
723 std::unordered_map<uint64_t, uint32_t> beliefStateToObservation{{0, 0}};
724
725 storm::pomdp::policy::PolicyExtractor<storm::models::sparse::Pomdp<double>, double, double> extractor(pomdp, beliefMdp, beliefStateToObservation,
726 beliefScheduler);
727 auto inducedModel = extractor.exportPolicyAsInducedMarkovChain();
728
729 ASSERT_TRUE(inducedModel->hasChoiceLabeling());
730 EXPECT_TRUE(inducedModel->getChoiceLabeling().getLabelsOfChoice(0).contains(""));
731}
TEST(ObservationBasedFiniteStateController, DeterminismTracksReplacedOutputs)
virtual std::unique_ptr< CheckResult > check(Environment const &env, CheckTask< storm::logic::Formula, SolutionType > const &checkTask)
Checks the provided formula.
This class manages the labeling of the choice space with a number of (atomic) labels.
void addLabelToChoice(std::string const &label, uint64_t choice)
Adds a label to a given choice.
std::set< std::string > getLabels() const
Retrieves the set of labels contained in this labeling.
void addLabel(std::string const &label)
Adds a new label to the labelings.
This class represents a (discrete-time) Markov decision process.
Definition Mdp.h:16
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
Definition Model.cpp:348
This class represents a partially observable Markov decision process.
Definition Pomdp.h:13
This class manages the labeling of the state space with a number of (atomic) labels.
void addLabelToState(std::string const &label, storm::storage::sparse::state_type state)
Adds a label to a given state.
std::shared_ptr< storm::logic::Formula const > parseSingleFormulaFromString(std::string const &formulaString) const
Parses the formula given by the provided string.
A finite-state controller (FSC) for POMDPs that bases its decisions on observations.
bool isDeterministic() const
Reports whether all outputs added to the controller so far have been deterministic.
void writeJsonToStream(std::ostream &outStream) const
Writes a JSON representation of the controller.
void addDeterministicActionTransition(uint64_t originId, uint64_t observationId, uint64_t actionId, uint64_t targetId)
Adds a deterministic output for an observation in a memory node.
void addRandomisedActionTransition(uint64_t originId, uint64_t observationId, storm::storage::Distribution< ValueType, uint64_t > actionDistribution, uint64_t targetId)
Adds a randomised output for an observation in a memory node.
std::string toString() const
Creates a human-readable representation of the controller, listing its initial node and defined outpu...
void writeDotToStream(std::ostream &outStream) const
Writes a Graphviz DOT representation of the controller.
Extracts concrete policy representations from a scheduler for a belief MDP.
ObservationBasedFiniteStateController< PolicyValueType > exportPolicyAsFiniteStateController() const
Constructs an observation-based finite-state controller from the belief-MDP scheduler.
std::shared_ptr< ModelType > exportPolicyAsInducedMarkovChain() const
Constructs the Markov chain induced by the belief-MDP scheduler.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
void addProbability(StateType const &state, ValueType const &probability)
Assigns the given state the given probability under this distribution.
This class defines which action is chosen in a particular state of a non-deterministic model.
Definition Scheduler.h:18
SchedulerChoice< ValueType > const & getChoice(uint_fast64_t modelState, uint_fast64_t memoryState=0) const
Gets the choice defined by the scheduler for the given model and memory state.
Definition Scheduler.cpp:94
void setChoice(SchedulerChoice< ValueType > const &choice, uint_fast64_t modelState, uint_fast64_t memoryState=0)
Sets the choice defined by the scheduler for the given state.
Definition Scheduler.cpp:38
A class that can be used to build a sparse matrix by adding value by value.
void addNextValue(index_type row, index_type column, value_type const &value)
Sets the matrix entry at the given row and column to the given value.
void newRowGroup(index_type startingRow)
Starts a new row group in the matrix.
SparseMatrix< value_type > build(index_type overriddenRowCount=0, index_type overriddenColumnCount=0, index_type overriddenRowGroupCount=0)
Helper to incrementally build a ValuationClassDescription, i.e.
Provides access to valuations of variables for a set of entities (e.g.
Definition Valuations.h:28
std::shared_ptr< storm::models::sparse::Pomdp< ValueType > > transform() const
std::pair< std::shared_ptr< storm::models::sparse::Pomdp< ValueType > >, std::set< uint32_t > > transform(storm::storage::BitVector const &stateSet) const
Ensures that the given set of states is observation closed, potentially, adding new observation(s) A ...
ObservationSignature getObservationSignature(storm::models::sparse::Pomdp< PomdpValueType > const &pomdp, uint64_t const observationId)
storm::models::sparse::Dtmc< PomdpValueType > applyObservationBasedFSCToPomdp(storm::models::sparse::Pomdp< PomdpValueType > const &pomdp, storm::pomdp::policy::ObservationBasedFiniteStateController< FscValueType > const &fsc, bool treatUnspecifiedChoiceAsDontCare)
Applies an observation-based finite-state controller to a POMDP and constructs the induced DTMC.
SettingsManager const & manager()
Retrieves the settings manager.
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:11
#define STORM_SILENT_EXPECT_THROW(statement, expected_exception)
Definition storm_gtest.h:21
Identifies an observation by its observable names and typed values.
std::unordered_map< std::string, RewardModelType > rewardModels
std::optional< storm::storage::sparse::Valuations > observationValuations
std::optional< storm::models::sparse::ChoiceLabeling > choiceLabeling
std::optional< std::vector< uint32_t > > observabilityClasses