Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
VerificationTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
10
11namespace {
12
15
16// The initial state branches to hidden states 1 and 2 with probabilities 1/4 and 3/4.
17// Action a reaches goal 3 with probabilities 1/5 and 4/5; action b reverses these.
18// All other transitions go to absorbing failure state 4. The optimal value is 13/20.
19Pomdp makePomdp(bool reverseActions = false, bool hiddenTarget = false) {
20 storm::storage::SparseMatrixBuilder<double> matrix(0, 0, 0, false, true);
21 std::vector<std::string> actionLabels;
22 uint64_t row = 0;
23 auto addAction = [&](std::string const& label, std::initializer_list<std::pair<uint64_t, double>> transitions) {
24 for (auto const& [state, probability] : transitions) {
25 matrix.addNextValue(row, state, probability);
26 }
27 actionLabels.push_back(label);
28 ++row;
29 };
30 matrix.newRowGroup(row);
31 addAction("start", {{1, 0.25}, {2, 0.75}});
32 matrix.newRowGroup(row);
33 addAction("a", {{3, 0.2}, {4, 0.8}});
34 addAction("b", {{3, 0.8}, {4, 0.2}});
35 matrix.newRowGroup(row);
36 if (reverseActions) {
37 addAction("b", {{3, 0.2}, {4, 0.8}});
38 addAction("a", {{3, 0.8}, {4, 0.2}});
39 } else {
40 addAction("a", {{3, 0.8}, {4, 0.2}});
41 addAction("b", {{3, 0.2}, {4, 0.8}});
42 }
43 matrix.newRowGroup(row);
44 addAction("a", {{3, 1.0}});
45 addAction("b", {{3, 1.0}});
46 matrix.newRowGroup(row);
47 addAction("stay", {{4, 1.0}});
48
50 labels.addLabel("init");
51 labels.addLabelToState("init", 0);
52 labels.addLabel("goal");
53 labels.addLabelToState("goal", 3);
54 labels.addLabel("safe");
55 labels.addLabelToState("safe", 0);
56 labels.addLabelToState("safe", 1);
58 for (uint64_t choice = 0; choice < row; ++choice) {
59 if (!choices.containsLabel(actionLabels[choice])) {
60 choices.addLabel(actionLabels[choice]);
61 }
62 choices.addLabelToChoice(actionLabels[choice], choice);
63 }
64 storm::storage::sparse::ModelComponents<double> components(matrix.build(), std::move(labels));
65 components.choiceLabeling = std::move(choices);
66 components.observabilityClasses = hiddenTarget ? std::vector<uint32_t>{0, 1, 1, 1, 2} : std::vector<uint32_t>{0, 1, 1, 2, 3};
67 return Pomdp(std::move(components), !reverseActions);
68}
69
70class PomdpVerificationTest : public ::testing::TestWithParam<bool> {
71 protected:
72 void SetUp() override {
73 env.solver().minMax().setMethod(storm::solver::MinMaxMethod::ValueIteration);
74 env.solver().minMax().setPrecision(storm::utility::convertNumber<storm::RationalNumber>(1e-10));
75 }
76
77 auto verify(Pomdp const& pomdp, storm::logic::Formula const& formula) {
78 if (GetParam()) {
79 return storm::pomdp::api::verifyWithBeliefDiscretization(env, pomdp, formula, 4);
80 }
81 return storm::pomdp::api::verifyWithBeliefUnfolding(env, pomdp, formula, Options{});
82 }
83
84 storm::Environment env;
85 storm::parser::FormulaParser parser;
86};
87
88TEST_P(PomdpVerificationTest, CanonicalizesActionOrderWithoutChangingInput) {
89#ifndef STORM_HAVE_Z3
90 GTEST_SKIP() << "POMDP preprocessing requires Z3.";
91#endif
92 auto pomdp = makePomdp(true);
93 ASSERT_FALSE(pomdp.isCanonic());
94 auto const matrix = pomdp.getTransitionMatrix();
95 auto const choices = pomdp.getChoiceLabeling();
96 auto formula = parser.parseSingleFormulaFromString("Pmax=? [F \"goal\"]");
97
98 auto const result = verify(pomdp, *formula);
99 EXPECT_NEAR(result.value, 0.65, 1e-8);
100 EXPECT_TRUE(result.completedExploration);
101 EXPECT_FALSE(pomdp.isCanonic());
102 EXPECT_EQ(pomdp.getTransitionMatrix(), matrix);
103 EXPECT_EQ(pomdp.getChoiceLabeling(), choices);
104}
105
106TEST_P(PomdpVerificationTest, UntilFailureCannotLaterReachTarget) {
107#ifndef STORM_HAVE_Z3
108 GTEST_SKIP() << "POMDP preprocessing requires Z3.";
109#endif
110 auto pomdp = makePomdp();
111 ASSERT_EQ(pomdp.getObservation(1), pomdp.getObservation(2));
112 auto const matrix = pomdp.getTransitionMatrix();
113 auto until = parser.parseSingleFormulaFromString("Pmax=? [\"safe\" U \"goal\"]");
114 auto reachability = parser.parseSingleFormulaFromString("Pmax=? [F \"goal\"]");
115
116 // State 2 violates safe, even though both of its actions can reach the goal later.
117 // Only the 1/4 branch through state 1 contributes, using b with success probability 4/5.
118 auto const result = verify(pomdp, *until);
119 EXPECT_NEAR(result.value, 0.2, 1e-8);
120 EXPECT_TRUE(result.completedExploration);
121 EXPECT_NEAR(verify(pomdp, *reachability).value, 0.65, 1e-8);
122 EXPECT_EQ(pomdp.getTransitionMatrix(), matrix);
123}
124
125TEST_P(PomdpVerificationTest, RejectsThresholds) {
126 auto pomdp = makePomdp();
127 auto formula = parser.parseSingleFormulaFromString("Pmax>=0.5 [F \"goal\"]");
128 STORM_SILENT_EXPECT_THROW(verify(pomdp, *formula), storm::exceptions::NotSupportedException);
129}
130
131TEST_P(PomdpVerificationTest, RejectsNestedFormulas) {
132 auto pomdp = makePomdp();
133 auto formula = parser.parseSingleFormulaFromString("Pmax=? [F (Pmax>=0.5 [F \"goal\"])]");
134 STORM_SILENT_EXPECT_THROW(verify(pomdp, *formula), storm::exceptions::NotSupportedException);
135}
136
137TEST_P(PomdpVerificationTest, RejectsMissingOptimizationDirection) {
138 auto pomdp = makePomdp();
139 for (auto const& query : {"P=? [F \"goal\"]", "R=? [F \"goal\"]"}) {
140 SCOPED_TRACE(query);
141 auto formula = parser.parseSingleFormulaFromString(query);
142 STORM_SILENT_EXPECT_THROW(verify(pomdp, *formula), storm::exceptions::InvalidPropertyException);
143 }
144}
145
146TEST_P(PomdpVerificationTest, SupportsRationalPolicyProbabilitiesWithDoubleModelAndBeliefs) {
147#ifndef STORM_HAVE_Z3
148 GTEST_SKIP() << "POMDP preprocessing requires Z3.";
149#endif
150 auto pomdp = makePomdp();
151 auto formula = parser.parseSingleFormulaFromString("Pmax=? [F \"goal\"]");
152 Options options;
153 options.generatePolicy = !GetParam();
154
155 for (bool const defaultEnvironment : {false, true}) {
156 SCOPED_TRACE(defaultEnvironment);
158 if (GetParam()) {
160 false, options)
161 : storm::pomdp::api::verifyWithBeliefDiscretization<double, double, double, storm::RationalNumber>(
162 env, pomdp, *formula, 4, false, options);
163 }
164 return defaultEnvironment
166 : storm::pomdp::api::verifyWithBeliefUnfolding<double, double, double, storm::RationalNumber>(env, pomdp, *formula, options);
167 }();
168 EXPECT_NEAR(result.value, 0.65, 1e-8);
169 EXPECT_TRUE(result.completedExploration);
170 if (GetParam()) {
171 EXPECT_FALSE(result.policy.has_value());
172 } else {
173 ASSERT_TRUE(result.policy.has_value());
174 auto const& policy = *result.policy;
175 auto const initialNode = policy.getInitialNodeId();
176 ASSERT_TRUE(policy.hasOutputForObservationInNode(initialNode, 0));
177 auto const [initialAction, hiddenNode] = policy.getActionAndSuccessorForObservationInNode(initialNode, 0);
178 EXPECT_EQ(initialAction, 0ul);
179 ASSERT_TRUE(policy.hasOutputForObservationInNode(hiddenNode, 1));
180 // The optimal action at the hidden belief is a (local action 0), with value 13/20.
181 EXPECT_EQ(policy.getActionForObservationInNode(hiddenNode, 1), 0ul);
182 }
183 }
184}
185
186INSTANTIATE_TEST_SUITE_P(BeliefApi, PomdpVerificationTest, ::testing::Values(false, true),
187 [](auto const& info) { return info.param ? "Discretization" : "Unfolding"; });
188
189TEST(PomdpVerificationClippingTest, RefinesTargetsAndRemapsResolutionsWithoutChangingInput) {
190#if !defined(STORM_HAVE_Z3) || !defined(STORM_HAVE_GLPK)
191 GTEST_SKIP() << "Clipping test requires Z3 preprocessing and GLPK.";
192#endif
193 auto pomdp = makePomdp(false, true);
194 ASSERT_EQ(pomdp.getNrObservations(), 3ul);
195 ASSERT_EQ(pomdp.getObservation(1), pomdp.getObservation(3));
196 auto const observations = pomdp.getObservations();
198 auto formula = parser.parseSingleFormulaFromString("Pmax=? [F \"goal\"]");
200 env.solver().setLpSolverType(storm::solver::LpSolverType::Glpk);
201 Options options;
202 options.maxExplorationSize = 1;
203 options.useClipping = true;
204 // The hidden belief (1/4, 3/4) is off the resolution-3 grid, forcing clipping.
205 options.clippingResolutions = {2, 3, 5};
206
207 auto const result = storm::pomdp::api::verifyWithBeliefUnfolding(env, pomdp, *formula, options);
208
209 // Explicitly separate the target, preserving the original observation IDs for other states.
210 // The fresh observation 3 must inherit resolution 3 from original observation 1.
211 auto refined = pomdp;
212 refined.updateObservations({0, 1, 1, 3, 2}, true);
213 auto refinedOptions = options;
214 refinedOptions.clippingResolutions = {2, 3, 5, 3};
215 auto const expected = storm::pomdp::api::verifyWithBeliefUnfolding(env, refined, *formula, refinedOptions);
216 EXPECT_NEAR(result.value, 0.65, 1e-8);
217 EXPECT_NEAR(result.value, expected.value, 1e-8);
218 EXPECT_EQ(result.statistics.discoveredBeliefs, expected.statistics.discoveredBeliefs);
219 EXPECT_GT(result.statistics.exploredBeliefs, 1ul);
220 EXPECT_EQ(pomdp.getObservations(), observations);
221 ASSERT_TRUE(options.clippingResolutions.has_value());
222 EXPECT_EQ(*options.clippingResolutions, (std::vector<uint64_t>{2, 3, 5}));
223 EXPECT_EQ(options.maxExplorationSize, 1ul);
224}
225
226TEST(PomdpVerificationBudgetTest, AcceptsExplicitUnfoldingBudgetAndRejectsDiscretizationBudget) {
227#ifndef STORM_HAVE_Z3
228 GTEST_SKIP() << "POMDP preprocessing requires Z3.";
229#endif
230 auto pomdp = makePomdp();
232 auto formula = parser.parseSingleFormulaFromString("Pmax=? [F \"goal\"]");
233 Options options;
234 options.maxExplorationSize = 0;
235
236 auto const result = storm::pomdp::api::verifyWithBeliefUnfolding(pomdp, *formula, options);
237 EXPECT_FALSE(result.completedExploration);
238 EXPECT_EQ(result.statistics.exploredBeliefs, 0ul);
239 EXPECT_EQ(result.statistics.discoveredBeliefs, 1ul);
240 EXPECT_GE(result.value, 0.0);
241 EXPECT_LE(result.value, 0.65 + 1e-8);
242 STORM_SILENT_EXPECT_THROW(storm::pomdp::api::verifyWithBeliefDiscretization(pomdp, *formula, 4, false, options), storm::exceptions::NotSupportedException);
243}
244
245} // namespace
TEST(RegionConstraintCheckerTest, AffineConstraintHoldsThroughout)
INSTANTIATE_TEST_SUITE_P(LpSolvers, ScpLpEncoderTest, testing::ValuesIn(storm::pars::scp::testutils::lpSolversUnderTest()), storm::pars::scp::testutils::LpSolverTest::solverName)
TEST_P(ScpLpEncoderTest, RealOptimization_AtLeast_MaxViolationObjective)
SolverEnvironment & solver()
void setLpSolverType(storm::solver::LpSolverType const &value, bool isSetFromDefault=false)
This class manages the labeling of the choice space with a number of (atomic) labels.
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.
std::shared_ptr< storm::logic::Formula const > parseSingleFormulaFromString(std::string const &formulaString) const
Parses the formula given by the provided string.
A class that can be used to build a sparse matrix by adding value by value.
bool verify(storm::Environment const &env, std::shared_ptr< Model > const &model, std::shared_ptr< storm::logic::Formula const > const &formula, Region const &region, Engine engine)
FragmentSpecification reachability()
beliefs::BeliefBasedModelCheckerResult< BeliefMdpValueType, PolicyValueType > verifyWithBeliefUnfolding(storm::Environment const &env, storm::models::sparse::Pomdp< PomdpValueType > const &pomdp, storm::logic::Formula const &formula, beliefs::BeliefBasedModelCheckerOptions< BeliefMdpValueType > const &options)
Approximates an unbounded reachability probability or expected reachability reward by unfolding the b...
template beliefs::BeliefBasedModelCheckerResult< double, storm::RationalNumber > verifyWithBeliefUnfolding< double, double, double, storm::RationalNumber >(storm::Environment const &, storm::models::sparse::Pomdp< double > const &, storm::logic::Formula const &, beliefs::BeliefBasedModelCheckerOptions< double > const &)
template beliefs::BeliefBasedModelCheckerResult< double, storm::RationalNumber > verifyWithBeliefDiscretization< double, double, double, storm::RationalNumber >(storm::Environment const &, storm::models::sparse::Pomdp< double > const &, storm::logic::Formula const &, uint64_t, bool, beliefs::BeliefBasedModelCheckerOptions< double > const &)
beliefs::BeliefBasedModelCheckerResult< BeliefMdpValueType, PolicyValueType > verifyWithBeliefDiscretization(storm::Environment const &env, storm::models::sparse::Pomdp< PomdpValueType > const &pomdp, storm::logic::Formula const &formula, uint64_t resolution, bool useDynamic, beliefs::BeliefBasedModelCheckerOptions< BeliefMdpValueType > const &options)
Approximates an unbounded reachability probability or expected reachability reward by Freudenthal dis...
storm::bisimulation::Options Options
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.
#define STORM_SILENT_EXPECT_THROW(statement, expected_exception)
Definition storm_gtest.h:21