Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SparseScpInstantiationSearcherTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
3#include "test/storm_gtest.h"
4
10#include "storm/api/builder.h"
17
18using namespace storm::pars::scp;
19
20namespace {
21
22// Builds a FeasibilitySynthesisTask the same way storm-pars-cli's createFeasibilitySynthesisTaskFromSettings does:
23// the bound is stripped from the formula and carried separately.
24std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> makeTask(
25 std::shared_ptr<storm::logic::Formula const> const& formulaWithBound,
26 std::optional<storm::storage::ParameterRegion<storm::RationalFunction>> const& region = std::nullopt) {
27 std::shared_ptr<storm::logic::Formula> clone = formulaWithBound->clone();
28 clone->asOperatorFormula().removeBound();
29 storm::pars::FeasibilitySynthesisTask t(clone->asSharedPointer());
30 t.setBound(formulaWithBound->asOperatorFormula().getBound());
31 if (region) {
32 t.setRegion(*region);
33 }
34 return std::make_shared<storm::pars::FeasibilitySynthesisTask const>(std::move(t));
35}
36
37std::shared_ptr<storm::logic::Formula const> parseSingleFormula(std::string const& formulaString, storm::prism::Program const& program) {
39 return formulas.at(0);
40}
41
42} // namespace
43
45
46TEST_P(SparseScpInstantiationSearcherTest, FindsCertifiedWitness_Dtmc) {
47 carl::VariablePool::getInstance().clear();
48 std::string prismSource =
49 "dtmc\n"
50 "const double p;\n"
51 "module simple\n"
52 " s : [0..2] init 0;\n"
53 " [] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
54 " [] s=1 -> (s'=1);\n"
55 " [] s=2 -> (s'=2);\n"
56 "endmodule\n"
57 "label \"target\" = s=1;\n";
58 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_Dtmc");
59 program = program.preprocess("");
60
61 auto formula = parseSingleFormula("P>=0.51 [F \"target\"]", program);
62 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
64
65 auto task = makeTask(formula);
66
68 auto result = searcher.run(env(), task);
69
70 ASSERT_EQ(ScpStatus::Feasible, result.status);
71 EXPECT_GE(result.value, 0.5);
72
73 // Independently re-verify with a fresh instantiation checker, not reusing anything internal to the searcher.
74 std::shared_ptr<storm::logic::Formula> clone = formula->clone();
75 clone->asOperatorFormula().removeBound();
78 verifier.specifyFormula(verifyTask);
79 auto verifyResult = verifier.check(env(), result.valuation);
80 uint64_t initialState = dtmc->getInitialStates().getNextSetIndex(0);
81 double verifiedValue = verifyResult->template asExplicitQuantitativeCheckResult<double>().getFiniteValueVector()[initialState];
82
83 EXPECT_NEAR(result.value, verifiedValue, 1e-6);
84 EXPECT_GE(verifiedValue, 0.5);
85}
86
87// Exercises ConstantType = storm::RationalNumber end-to-end (not just that it compiles, which the
88// explicit template instantiation in SparseScpInstantiationSearcher.cpp already guarantees, but that
89// it runs and actually returns an exact value): result.value is a storm::ExtendedRationalNumber (an
90// exact rational extended with a genuine infinity) throughout, never silently downcast to double, so
91// it can be compared for *exact* equality against an independently-computed exact re-verification and
92// against a hand-built exact rational threshold -- something the double-precision test above can only
93// approximate (EXPECT_NEAR/EXPECT_GE against a double-rounded 0.51).
94TEST_P(SparseScpInstantiationSearcherTest, FindsCertifiedWitness_Dtmc_ExactPrecision) {
95 carl::VariablePool::getInstance().clear();
96 std::string prismSource =
97 "dtmc\n"
98 "const double p;\n"
99 "module simple\n"
100 " s : [0..2] init 0;\n"
101 " [] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
102 " [] s=1 -> (s'=1);\n"
103 " [] s=2 -> (s'=2);\n"
104 "endmodule\n"
105 "label \"target\" = s=1;\n";
106 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_ExactPrecision");
107 program = program.preprocess("");
108
109 auto formula = parseSingleFormula("P>=0.51 [F \"target\"]", program);
110 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
112
113 auto task = makeTask(formula);
114
116 auto result = searcher.run(env(), task);
117 static_assert(std::is_same_v<decltype(result.value), storm::ExtendedRationalNumber>,
118 "ScpResult<storm::RationalNumber>::value must be held in an exact, infinity-capable rational type, not double.");
119
120 ASSERT_EQ(ScpStatus::Feasible, result.status);
121
122 // Exact, not approximate: 51/100 built from exact integer division, not a double literal.
124 EXPECT_GE(result.value, exactThreshold);
125
126 // Independently re-verify with a fresh, RationalNumber-precision instantiation checker, not
127 // reusing anything internal to the searcher -- and expect *exact* equality, since neither side
128 // of this comparison ever passes through double.
129 std::shared_ptr<storm::logic::Formula> clone = formula->clone();
130 clone->asOperatorFormula().removeBound();
132 *dtmc);
134 verifier.specifyFormula(verifyTask);
135 auto verifyResult = verifier.check(env(), result.valuation);
136 uint64_t initialState = dtmc->getInitialStates().getNextSetIndex(0);
137 storm::RationalNumber verifiedValue =
138 verifyResult->template asExplicitQuantitativeCheckResult<storm::RationalNumber>().getFiniteValueVector()[initialState];
139
140 EXPECT_EQ(result.value, verifiedValue);
141 EXPECT_GE(verifiedValue, exactThreshold);
142}
143
145 carl::VariablePool::getInstance().clear();
146 std::string prismSource =
147 "dtmc\n"
148 "const double p;\n"
149 "module simple\n"
150 " s : [0..2] init 0;\n"
151 " [] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
152 " [] s=1 -> (s'=1);\n"
153 " [] s=2 -> (s'=2);\n"
154 "endmodule\n"
155 "label \"target\" = s=1;\n";
156 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_AtMost");
157 program = program.preprocess("");
158
159 // Reachability is exactly p; region center is 0.5, so P<=0.3 requires real downward search.
160 auto formula = parseSingleFormula("P<=0.3 [F \"target\"]", program);
161 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
163
164 auto task = makeTask(formula);
166 auto result = searcher.run(env(), task);
167
168 ASSERT_EQ(ScpStatus::Feasible, result.status);
169 EXPECT_LE(result.value, 0.3);
170}
171
172TEST_P(SparseScpInstantiationSearcherTest, ReportsNonFeasible_WhenThresholdIsUnreachableInRegion) {
173 carl::VariablePool::getInstance().clear();
174 std::string prismSource =
175 "dtmc\n"
176 "const double p;\n"
177 "module simple\n"
178 " s : [0..2] init 0;\n"
179 " [] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
180 " [] s=1 -> (s'=1);\n"
181 " [] s=2 -> (s'=2);\n"
182 "endmodule\n"
183 "label \"target\" = s=1;\n";
184 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_Infeasible");
185 program = program.preprocess("");
186
187 // Reachability is exactly p, so restricting p to [0, 0.5] makes ">= 0.99" unreachable anywhere in the region.
188 auto formula = parseSingleFormula("P>=0.99 [F \"target\"]", program);
189 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
191
193 ASSERT_EQ(1u, vars.size());
194 carl::Variable var = *vars.begin();
198
199 auto task = makeTask(formula, region);
200
201 ScpOptions options;
202 options.maxIterations = 200;
204 auto result = searcher.run(env(), task);
205
206 EXPECT_NE(ScpStatus::Feasible, result.status);
207}
208
209namespace {
210
211// One parametrized action (p) that helps, one constant "escape hatch" action (0.1) that a
212// worst-case scheduler could always fall back on. Pmax(s0) = max(p, 0.1); Pmin(s0) = min(p, 0.1).
213std::string const TwoActionMdpSource =
214 "mdp\n"
215 "const double p;\n"
216 "module simple\n"
217 " s : [0..2] init 0;\n"
218 " [a] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
219 " [b] s=0 -> 0.1 : (s'=1) + 0.9 : (s'=2);\n"
220 " [] s=1 -> (s'=1);\n"
221 " [] s=2 -> (s'=2);\n"
222 "endmodule\n"
223 "label \"target\" = s=1;\n";
224
225// Two independently parametrized actions, so Pmin(s0) = min(p, q) genuinely depends on both.
226std::string const TwoParameterMdpSource =
227 "mdp\n"
228 "const double p;\n"
229 "const double q;\n"
230 "module simple\n"
231 " s : [0..2] init 0;\n"
232 " [a] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
233 " [b] s=0 -> q : (s'=1) + (1-q) : (s'=2);\n"
234 " [] s=1 -> (s'=1);\n"
235 " [] s=2 -> (s'=2);\n"
236 "endmodule\n"
237 "label \"target\" = s=1;\n";
238
239} // namespace
240
241// AtMost (P<=lambda) on an Mdp defaults to the robust reading Pmax<=lambda: even the worst-case
242// (maximizing) scheduler must stay under the threshold. Pmax = max(p, 0.1), so this requires a
243// real downward search on p (region center 0.5 does not satisfy Pmax<=0.3).
244TEST_P(SparseScpInstantiationSearcherTest, AtMost_Mdp_UnqualifiedDefaultsToPmax) {
245 carl::VariablePool::getInstance().clear();
246 storm::prism::Program program = storm::parser::PrismParser::parseFromString(TwoActionMdpSource, "SparseScpInstantiationSearcherTest_AtMostMdp");
247 program = program.preprocess("");
248
249 auto formula = parseSingleFormula("P<=0.3 [F \"target\"]", program);
250 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
252
253 auto task = makeTask(formula);
255 auto result = searcher.run(env(), task);
256
257 ASSERT_EQ(ScpStatus::Feasible, result.status);
258 EXPECT_LE(result.value, 0.3);
259}
260
261// AtLeast (P>=lambda) on an Mdp defaults to the robust reading Pmin>=lambda: even the worst-case
262// (minimizing) scheduler must clear the threshold. With two independently parametrized actions,
263// Pmin = min(p, q), so satisfying >=0.7 genuinely requires pushing *both* parameters up.
264TEST_P(SparseScpInstantiationSearcherTest, AtLeast_Mdp_UnqualifiedDefaultsToPmin) {
265 carl::VariablePool::getInstance().clear();
266 storm::prism::Program program = storm::parser::PrismParser::parseFromString(TwoParameterMdpSource, "SparseScpInstantiationSearcherTest_AtLeastMdp");
267 program = program.preprocess("");
268
269 auto formula = parseSingleFormula("P>=0.7 [F \"target\"]", program);
270 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
272
273 auto task = makeTask(formula);
275 auto result = searcher.run(env(), task);
276
277 ASSERT_EQ(ScpStatus::Feasible, result.status);
278 EXPECT_GE(result.value, 0.7);
279}
280
281// An explicit Pmax=? formula, consistent with what AtMost would default to, must also work.
282TEST_P(SparseScpInstantiationSearcherTest, ExplicitPmax_Mdp_ConsistentWithAtMost) {
283 carl::VariablePool::getInstance().clear();
284 storm::prism::Program program = storm::parser::PrismParser::parseFromString(TwoActionMdpSource, "SparseScpInstantiationSearcherTest_ExplicitPmax");
285 program = program.preprocess("");
286
287 auto formula = parseSingleFormula("Pmax<=0.3 [F \"target\"]", program);
288 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
290
291 auto task = makeTask(formula);
293 auto result = searcher.run(env(), task);
294
295 ASSERT_EQ(ScpStatus::Feasible, result.status);
296 EXPECT_LE(result.value, 0.3);
297}
298
299// An explicit direction that contradicts the threshold's robust-reading default (Pmax paired with
300// a ">=" threshold, which would default to Pmin) must be rejected, not silently mishandled.
301TEST_P(SparseScpInstantiationSearcherTest, RejectsExplicitDirectionMismatch) {
302 carl::VariablePool::getInstance().clear();
303 storm::prism::Program program = storm::parser::PrismParser::parseFromString(TwoActionMdpSource, "SparseScpInstantiationSearcherTest_Mismatch");
304 program = program.preprocess("");
305
306 auto formula = parseSingleFormula("Pmax>=0.7 [F \"target\"]", program);
307 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
309
310 auto task = makeTask(formula);
312 STORM_SILENT_EXPECT_THROW(searcher.run(env(), task), storm::exceptions::NotSupportedException);
313}
314
315// The initial state satisfies the target label, so its value is 1 for every parameter value and no LP is built.
316TEST_P(SparseScpInstantiationSearcherTest, InitialStateIsProb1_ReturnsFeasibleImmediately) {
317 carl::VariablePool::getInstance().clear();
318 std::string prismSource =
319 "dtmc\n"
320 "module simple\n"
321 " s : [0..1] init 0;\n"
322 " [] s=0 -> (s'=0);\n"
323 " [] s=1 -> (s'=1);\n"
324 "endmodule\n"
325 "label \"target\" = s=0;\n";
326 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_Prob1Initial");
327 program = program.preprocess("");
328
329 auto formula = parseSingleFormula("P>=0.5 [F \"target\"]", program);
330 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
332
333 auto task = makeTask(formula);
335 auto result = searcher.run(env(), task);
336
337 ASSERT_EQ(ScpStatus::Feasible, result.status);
338 EXPECT_DOUBLE_EQ(1.0, result.value);
339 EXPECT_EQ(0u, result.iterations);
340}
341
342// The initial state can never reach the target label, so it's Prob0 regardless of any parameter.
343TEST_P(SparseScpInstantiationSearcherTest, InitialStateIsProb0_ReturnsNonFeasibleImmediately) {
344 carl::VariablePool::getInstance().clear();
345 std::string prismSource =
346 "dtmc\n"
347 "module simple\n"
348 " s : [0..1] init 0;\n"
349 " [] s=0 -> (s'=0);\n"
350 " [] s=1 -> (s'=1);\n"
351 "endmodule\n"
352 "label \"target\" = s=1;\n";
353 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_Prob0Initial");
354 program = program.preprocess("");
355
356 auto formula = parseSingleFormula("P>=0.1 [F \"target\"]", program);
357 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
359
360 auto task = makeTask(formula);
362 auto result = searcher.run(env(), task);
363
364 ASSERT_NE(ScpStatus::Feasible, result.status);
365 EXPECT_DOUBLE_EQ(0.0, result.value);
366 EXPECT_EQ(0u, result.iterations);
367}
368
369// A tiny iteration budget forces MaxIterationsReached specifically (as opposed to NotFound via
370// trust-region collapse, which the default budget would eventually hit on the same instance).
371TEST_P(SparseScpInstantiationSearcherTest, ReportsMaxIterationsReached_WithTightIterationBudget) {
372 carl::VariablePool::getInstance().clear();
373 std::string prismSource =
374 "dtmc\n"
375 "const double p;\n"
376 "module simple\n"
377 " s : [0..2] init 0;\n"
378 " [] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
379 " [] s=1 -> (s'=1);\n"
380 " [] s=2 -> (s'=2);\n"
381 "endmodule\n"
382 "label \"target\" = s=1;\n";
383 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_MaxIter");
384 program = program.preprocess("");
385
386 auto formula = parseSingleFormula("P>=0.99 [F \"target\"]", program);
387 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
389
391 ASSERT_EQ(1u, vars.size());
392 carl::Variable var = *vars.begin();
396
397 auto task = makeTask(formula, region);
398 ScpOptions options;
399 options.maxIterations = 2; // trust-region collapse would take ~13 iterations at the defaults.
401 auto result = searcher.run(env(), task);
402
403 EXPECT_EQ(ScpStatus::MaxIterationsReached, result.status);
404}
405
406// A strict threshold (as opposed to the non-strict >=/<= used elsewhere) must not be satisfied by
407// a value that only reaches the threshold exactly -- storm's Bound::isSatisfied is trusted to get
408// this right, but SCP's own accept path (comparing the model-checked value, not the LP's estimate)
409// needs to actually route through it rather than special-casing non-strict comparisons somewhere.
410// The region center (0.5) exactly equals the threshold, so a naive ">=" implementation would
411// wrongly short-circuit as Feasible at iteration 0; a correct one must keep searching.
412TEST_P(SparseScpInstantiationSearcherTest, StrictThreshold_RequiresStrictlyExceedingValue) {
413 carl::VariablePool::getInstance().clear();
414 std::string prismSource =
415 "dtmc\n"
416 "const double p;\n"
417 "module simple\n"
418 " s : [0..2] init 0;\n"
419 " [] s=0 -> p : (s'=1) + (1-p) : (s'=2);\n"
420 " [] s=1 -> (s'=1);\n"
421 " [] s=2 -> (s'=2);\n"
422 "endmodule\n"
423 "label \"target\" = s=1;\n";
424 storm::prism::Program program = storm::parser::PrismParser::parseFromString(prismSource, "SparseScpInstantiationSearcherTest_Strict");
425 program = program.preprocess("");
426
427 auto formula = parseSingleFormula("P>0.5 [F \"target\"]", program);
428 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
430
431 auto task = makeTask(formula);
433 auto result = searcher.run(env(), task);
434
435 ASSERT_EQ(ScpStatus::Feasible, result.status);
436 EXPECT_GT(result.value, 0.5);
437 EXPECT_GT(result.iterations, 0u);
438}
439
440// A region that doesn't cover a parameter actually occurring in the model must fail cleanly, not
441// with an uncaught std::out_of_range from an internal map lookup.
442TEST_P(SparseScpInstantiationSearcherTest, RegionMissingAModelParameter_ThrowsCleanly) {
443 carl::VariablePool::getInstance().clear();
444 storm::prism::Program program = storm::parser::PrismParser::parseFromString(TwoParameterMdpSource, "SparseScpInstantiationSearcherTest_MissingParam");
445 program = program.preprocess("");
446
447 auto formula = parseSingleFormula("P>=0.7 [F \"target\"]", program);
448 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
450
451 // Region only covers "p", but the model also has "q".
453 carl::Variable varP;
454 for (auto const& v : vars) {
455 if (v.name() == "p") {
456 varP = v;
457 }
458 }
462
463 auto task = makeTask(formula, region);
465 STORM_SILENT_EXPECT_THROW(searcher.run(env(), task), storm::exceptions::NotSupportedException);
466}
467
468namespace {
469
470// A "retry until success" DTMC: each attempt at s=0 costs 1 and succeeds (reaching the target)
471// with probability p, or loops back to try again with probability 1-p. Reachability reward is
472// exactly 1/p -- a genuinely nonlinear function of p, even though the transitions and reward are
473// both individually affine, so satisfying a threshold requires real (non-trivial-at-the-region-
474// center) search, exactly like the probability tests above.
475std::string const RetryUntilSuccessDtmcSource =
476 "dtmc\n"
477 "const double p;\n"
478 "module simple\n"
479 " s : [0..1] init 0;\n"
480 " [] s=0 -> p : (s'=1) + (1-p) : (s'=0);\n"
481 " [] s=1 -> (s'=1);\n"
482 "endmodule\n"
483 "rewards\n"
484 " [] s=0 : 1;\n"
485 "endrewards\n"
486 "label \"target\" = s=1;\n";
487
488} // namespace
489
490// AtMost (R<=lambda) on a Dtmc: reachability reward 1/p <= 1.5 needs p >= 2/3, well above the
491// region center's 0.5 (reward 2) -- requires a real upward search on p, not just accepting the
492// center. Independently re-verified with a fresh instantiation checker, not reusing anything
493// internal to the searcher, mirroring FindsCertifiedWitness_Dtmc for the probability case.
495 carl::VariablePool::getInstance().clear();
496 storm::prism::Program program = storm::parser::PrismParser::parseFromString(RetryUntilSuccessDtmcSource, "SparseScpInstantiationSearcherTest_RewardAtMost");
497 program = program.preprocess("");
498
499 auto formula = parseSingleFormula("R<=1.5 [F \"target\"]", program);
500 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
502
503 auto task = makeTask(formula);
505 auto result = searcher.run(env(), task);
506
507 ASSERT_EQ(ScpStatus::Feasible, result.status);
508 EXPECT_LE(result.value, 1.5);
509
510 std::shared_ptr<storm::logic::Formula> clone = formula->clone();
511 clone->asOperatorFormula().removeBound();
514 verifier.specifyFormula(verifyTask);
515 auto verifyResult = verifier.check(env(), result.valuation);
516 uint64_t initialState = dtmc->getInitialStates().getNextSetIndex(0);
517 double verifiedValue = verifyResult->template asExplicitQuantitativeCheckResult<double>().getFiniteValueVector()[initialState];
518
519 EXPECT_NEAR(result.value, verifiedValue, 1e-6);
520 EXPECT_LE(verifiedValue, 1.5);
521}
522
523// AtLeast (R>=lambda) on a Dtmc: reachability reward 1/p >= 5 needs p <= 0.2, below the region
524// center's 0.5 (reward 2) -- requires a real downward search.
526 carl::VariablePool::getInstance().clear();
527 storm::prism::Program program =
528 storm::parser::PrismParser::parseFromString(RetryUntilSuccessDtmcSource, "SparseScpInstantiationSearcherTest_RewardAtLeast");
529 program = program.preprocess("");
530
531 auto formula = parseSingleFormula("R>=5 [F \"target\"]", program);
532 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
534
535 auto task = makeTask(formula);
537 auto result = searcher.run(env(), task);
538
539 ASSERT_EQ(ScpStatus::Feasible, result.status);
540 EXPECT_GE(result.value, 5.0);
541}
542
543namespace {
544
545// Same retry-until-success structure, but the per-attempt cost is itself a second parameter "c"
546// that occurs nowhere in the transition matrix -- only in the reward function.
547std::string const RetryUntilSuccessWithParametricCostSource =
548 "dtmc\n"
549 "const double p;\n"
550 "const double c;\n"
551 "module simple\n"
552 " s : [0..1] init 0;\n"
553 " [] s=0 -> p : (s'=1) + (1-p) : (s'=0);\n"
554 " [] s=1 -> (s'=1);\n"
555 "endmodule\n"
556 "rewards\n"
557 " [] s=0 : c;\n"
558 "endrewards\n"
559 "label \"target\" = s=1;\n";
560
561} // namespace
562
563// A region covering only the transition parameter "p" but not the reward-only parameter "c" must
564// fail cleanly -- this is only possible at all because AffinePMdpDecomposition's reward
565// decomposition folds a reward-only parameter into getParameters() (see
566// AffinePMdpDecompositionTest.RewardDecomposition_PicksUpRewardOnlyParameter); this test is the
567// end-to-end confirmation that the searcher's own region-coverage check actually benefits from
568// that, not just the decomposition in isolation.
569TEST_P(SparseScpInstantiationSearcherTest, RewardOnlyParameterMissingFromRegion_ThrowsCleanly) {
570 carl::VariablePool::getInstance().clear();
571 storm::prism::Program program =
572 storm::parser::PrismParser::parseFromString(RetryUntilSuccessWithParametricCostSource, "SparseScpInstantiationSearcherTest_RewardOnlyParamMissing");
573 program = program.preprocess("");
574
575 auto formula = parseSingleFormula("R<=2 [F \"target\"]", program);
576 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
578
580 ASSERT_EQ(1u, vars.size());
581 carl::Variable varC = *vars.begin();
582 // Deliberately covers "c" only -- the model also has "p", which is a transition parameter, so
583 // this region is missing it (the reverse of RegionMissingAModelParameter_ThrowsCleanly, which
584 // covers a transition parameter but misses a probability one).
588
589 auto task = makeTask(formula, region);
591 STORM_SILENT_EXPECT_THROW(searcher.run(env(), task), storm::exceptions::NotSupportedException);
592}
593
594// With both "p" and the reward-only "c" covered by the region, the search must actually be able to
595// move both to satisfy the threshold -- confirms the reward-only parameter isn't just *discovered*
596// (the test above) but is genuinely usable as an LP variable throughout the search.
597TEST_P(SparseScpInstantiationSearcherTest, RewardOnlyParameter_UsableInSearch) {
598 carl::VariablePool::getInstance().clear();
599 storm::prism::Program program =
600 storm::parser::PrismParser::parseFromString(RetryUntilSuccessWithParametricCostSource, "SparseScpInstantiationSearcherTest_RewardOnlyParamUsable");
601 program = program.preprocess("");
602
603 auto formula = parseSingleFormula("R<=2 [F \"target\"]", program);
604 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
606
608 auto rewVars = storm::models::sparse::getRewardParameters(*dtmc);
609 ASSERT_EQ(1u, probVars.size());
610 ASSERT_EQ(1u, rewVars.size());
611 carl::Variable varP = *probVars.begin();
612 carl::Variable varC = *rewVars.begin();
613
614 // Region center: p=0.5, c=1.25 -> R = c/p = 2.5, which does not satisfy R<=2.
620
621 auto task = makeTask(formula, region);
623 auto result = searcher.run(env(), task);
624
625 ASSERT_EQ(ScpStatus::Feasible, result.status);
626 EXPECT_LE(result.value, 2.0);
627}
628
629namespace {
630
631// One parametrized "retry until success" action (a, reward 1 per attempt) and one constant
632// "give up" action (b) that goes straight to an unlabeled absorbing trap state that never reaches
633// the target -- exercises the RewInf handling: Rmin(s0) = 1/p (a minimizing scheduler never picks
634// the infinite-reward action b), but Rmax(s0) = infinity (a maximizing scheduler always can, and
635// does, pick b). Mirrors TwoActionMdpSource's probability-property structure.
636std::string const RetryOrGiveUpMdpSource =
637 "mdp\n"
638 "const double p;\n"
639 "module simple\n"
640 " s : [0..2] init 0;\n"
641 " [a] s=0 -> p : (s'=1) + (1-p) : (s'=0);\n"
642 " [b] s=0 -> (s'=2);\n"
643 " [] s=1 -> (s'=1);\n"
644 " [] s=2 -> (s'=2);\n"
645 "endmodule\n"
646 "rewards\n"
647 " [a] s=0 : 1;\n"
648 "endrewards\n"
649 "label \"target\" = s=1;\n";
650
651} // namespace
652
653// AtLeast (R>=lambda) on an Mdp defaults to the robust reading Rmin>=lambda. Rmin = 1/p (the
654// give-up action is never chosen by a minimizing scheduler), so satisfying >=5 needs p<=0.2 -- a
655// real downward search. This is also the row-level-exclusion case: state 0 remains a genuine
656// "maybe" LP variable (Rmin is finite), but action b's row must be dropped from its Bellman
657// constraint (it leads straight to an infinite-value successor) without dropping the whole state,
658// which is exactly what SparseScpInstantiationSearcher's excludedRows computation (via
659// SparseMatrix::getRowFilter) is for.
660TEST_P(SparseScpInstantiationSearcherTest, AtLeast_Mdp_Reward_RowLevelExclusion) {
661 carl::VariablePool::getInstance().clear();
662 storm::prism::Program program = storm::parser::PrismParser::parseFromString(RetryOrGiveUpMdpSource, "SparseScpInstantiationSearcherTest_RewardMdpAtLeast");
663 program = program.preprocess("");
664
665 auto formula = parseSingleFormula("R>=5 [F \"target\"]", program);
666 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
668
669 auto task = makeTask(formula);
671 auto result = searcher.run(env(), task);
672
673 ASSERT_EQ(ScpStatus::Feasible, result.status);
674 EXPECT_GE(result.value, 5.0);
675}
676
677// AtMost (R<=lambda) on the same Mdp defaults to the robust reading Rmax<=lambda. Rmax(s0) is
678// infinity (a maximizing scheduler always has the give-up action available), so s0 is classified
679// as a RewInf state regardless of p -- this must be reported as NotFound immediately via the
680// initial-state short-circuit (see run()'s zeroStates/oneStates/infiniteStates check), not by
681// building an LP that can never converge.
682TEST_P(SparseScpInstantiationSearcherTest, AtMost_Mdp_Reward_InitialStateIsRewInf_ReportsNotFoundImmediately) {
683 carl::VariablePool::getInstance().clear();
684 storm::prism::Program program = storm::parser::PrismParser::parseFromString(RetryOrGiveUpMdpSource, "SparseScpInstantiationSearcherTest_RewardMdpAtMost");
685 program = program.preprocess("");
686
687 auto formula = parseSingleFormula("R<=3 [F \"target\"]", program);
688 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
690
691 auto task = makeTask(formula);
693 auto result = searcher.run(env(), task);
694
695 ASSERT_NE(ScpStatus::Feasible, result.status);
696 EXPECT_EQ(0u, result.iterations);
697 EXPECT_TRUE(storm::utility::isInfinity(result.value));
698}
699
700// The exact-precision counterpart of the test above: the result's value must be a genuine
701// +infinity, not the 100000000000 sentinel that storm::utility::infinity yields for a type that has
702// no infinity of its own -- i.e. ScpResult holds it in a storm::ExtendedRationalNumber.
703TEST_P(SparseScpInstantiationSearcherTest, AtMost_Mdp_Reward_InitialStateIsRewInf_ExactPrecision) {
704 carl::VariablePool::getInstance().clear();
705 storm::prism::Program program =
706 storm::parser::PrismParser::parseFromString(RetryOrGiveUpMdpSource, "SparseScpInstantiationSearcherTest_RewardMdpAtMostExact");
707 program = program.preprocess("");
708
709 auto formula = parseSingleFormula("R<=3 [F \"target\"]", program);
710 std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp =
712
713 auto task = makeTask(formula);
715 auto result = searcher.run(env(), task);
716
717 static_assert(std::is_same_v<decltype(result.value), storm::ExtendedRationalNumber>,
718 "ScpResult<storm::RationalNumber>::value must be held in an extended (infinity-capable) rational type.");
719 ASSERT_NE(ScpStatus::Feasible, result.status);
720 EXPECT_EQ(0u, result.iterations);
721 EXPECT_TRUE(storm::utility::isInfinity(result.value));
722 EXPECT_TRUE(result.value.isPositiveInfinity());
723}
724
725namespace {
726
727std::string const TwoNamedRewardModelsDtmcSource =
728 "dtmc\n"
729 "const double p;\n"
730 "module simple\n"
731 " s : [0..1] init 0;\n"
732 " [] s=0 -> p : (s'=1) + (1-p) : (s'=0);\n"
733 " [] s=1 -> (s'=1);\n"
734 "endmodule\n"
735 "rewards \"cost1\"\n"
736 " [] s=0 : 1;\n"
737 "endrewards\n"
738 "rewards \"cost2\"\n"
739 " [] s=0 : 2;\n"
740 "endrewards\n"
741 "label \"target\" = s=1;\n";
742
743} // namespace
744
745// A formula that explicitly names its reward model ("cost1") must resolve to that specific one
746// (not, say, silently falling back to some other reward model) -- distinguishable here because
747// cost1's and cost2's per-attempt cost differ by a factor of 2, so using the wrong one would still
748// "succeed" but at a value inconsistent with the intended reward model.
749TEST_P(SparseScpInstantiationSearcherTest, NamedRewardModel_ResolvesCorrectly) {
750 carl::VariablePool::getInstance().clear();
751 storm::prism::Program program = storm::parser::PrismParser::parseFromString(TwoNamedRewardModelsDtmcSource, "SparseScpInstantiationSearcherTest_NamedRM");
752 program = program.preprocess("");
753
754 auto formulaCost1 = parseSingleFormula("R{\"cost1\"}<=1.5 [F \"target\"]", program);
755 auto formulaCost2 = parseSingleFormula("R{\"cost2\"}<=1.5 [F \"target\"]", program);
756 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
757 storm::api::buildSparseModel<storm::RationalFunction>(program, {formulaCost1, formulaCost2})
759
760 // cost1: reward 1/attempt, so R<=1.5 needs p>=2/3 (same threshold/model as AtMost_Dtmc_Reward).
761 auto task = makeTask(formulaCost1);
763 auto result = searcher.run(env(), task);
764
765 ASSERT_EQ(ScpStatus::Feasible, result.status);
766 EXPECT_LE(result.value, 1.5);
767
768 // Independently re-verify with a fresh checker specifying "cost1" explicitly, confirming the
769 // certified valuation genuinely satisfies *that* reward model's threshold.
771 std::shared_ptr<storm::logic::Formula> clone = formulaCost1->clone();
772 clone->asOperatorFormula().removeBound();
774 verifier.specifyFormula(verifyTask);
775 auto verifyResult = verifier.check(env(), result.valuation);
776 uint64_t initialState = dtmc->getInitialStates().getNextSetIndex(0);
777 double verifiedValue = verifyResult->template asExplicitQuantitativeCheckResult<double>().getFiniteValueVector()[initialState];
778 EXPECT_NEAR(result.value, verifiedValue, 1e-6);
779}
780
781// A reward formula that does not name a reward model, on a model with more than one, has no way
782// to pick one and must fail cleanly rather than silently guessing.
783TEST_P(SparseScpInstantiationSearcherTest, UnnamedRewardModel_WithoutUniqueRewardModel_ThrowsCleanly) {
784 carl::VariablePool::getInstance().clear();
785 storm::prism::Program program =
786 storm::parser::PrismParser::parseFromString(TwoNamedRewardModelsDtmcSource, "SparseScpInstantiationSearcherTest_UnnamedNoUnique");
787 program = program.preprocess("");
788
789 auto formulaCost1 = parseSingleFormula("R{\"cost1\"}<=1.5 [F \"target\"]", program);
790 auto formulaCost2 = parseSingleFormula("R{\"cost2\"}<=1.5 [F \"target\"]", program);
791 std::shared_ptr<storm::models::sparse::Dtmc<storm::RationalFunction>> dtmc =
792 storm::api::buildSparseModel<storm::RationalFunction>(program, {formulaCost1, formulaCost2})
794 ASSERT_FALSE(dtmc->hasUniqueRewardModel());
795
796 auto unnamedFormula = parseSingleFormula("R<=1.5 [F \"target\"]", program);
797 auto task = makeTask(unnamedFormula);
799 STORM_SILENT_EXPECT_THROW(searcher.run(env(), task), storm::exceptions::NotSupportedException);
800}
801
804// The suite is empty if no LP solver was compiled in.
TEST_P(SparseScpInstantiationSearcherTest, FindsCertifiedWitness_Dtmc)
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SparseScpInstantiationSearcherTest)
INSTANTIATE_TEST_SUITE_P(LpSolvers, SparseScpInstantiationSearcherTest, testing::ValuesIn(storm::pars::scp::testutils::lpSolversUnderTest()), storm::pars::scp::testutils::LpSolverTest::solverName)
Class to efficiently check a formula on a parametric model with different parameter instantiations.
virtual std::unique_ptr< CheckResult > check(Environment const &env, storm::utility::parametric::Valuation< typename SparseModelType::ValueType > const &valuation) override
void specifyFormula(CheckTask< storm::logic::Formula, typename SparseModelType::ValueType > const &checkTask)
This class represents a discrete-time Markov chain.
Definition Dtmc.h:16
This class represents a (discrete-time) Markov decision process.
Definition Mdp.h:16
Finds a parameter valuation satisfying an unbounded reachability threshold property (probability or r...
ScpResult< ConstantType > run(storm::Environment const &env, std::shared_ptr< storm::pars::FeasibilitySynthesisTask const > const &task)
static std::string solverName(testing::TestParamInfo< storm::solver::LpSolverType > const &info)
static storm::prism::Program parseFromString(std::string const &input, std::string const &filename, bool prismCompatability=false)
Parses the given input stream into the PRISM storage classes assuming it complies with the PRISM synt...
Program preprocess(std::map< storm::expressions::Variable, storm::expressions::Expression > const &constantDefinitions) const
Preprocesses the program by defining the given constant definitions, substituting constants and formu...
Definition Program.cpp:1170
storm::utility::parametric::Valuation< ParametricType > Valuation
std::vector< storm::jani::Property > parsePropertiesForPrismProgram(std::string const &inputString, storm::prism::Program const &program, boost::optional< std::set< std::string > > const &propertyFilter)
std::vector< std::shared_ptr< storm::logic::Formula const > > extractFormulasFromProperties(std::vector< storm::jani::Property > const &properties)
std::shared_ptr< storm::models::sparse::Model< ValueType > > buildSparseModel(storm::languages::SymbolicModelDescription const &model, storm::builder::BuilderOptions const &options, typename storm::builder::ExplicitModelBuilder< ValueType >::Options const &explorationOptions=typename storm::builder::ExplicitModelBuilder< ValueType >::Options())
Definition builder.h:118
std::set< storm::RationalFunctionVariable > getRewardParameters(Model< storm::RationalFunction > const &model)
Get all parameters occurring in rewards.
Definition Model.cpp:736
std::set< storm::RationalFunctionVariable > getProbabilityParameters(Model< storm::RationalFunction > const &model)
Get all probability parameters occurring on transitions.
Definition Model.cpp:732
std::vector< storm::solver::LpSolverType > lpSolversUnderTest()
bool isInfinity(ValueType const &a)
TargetType convertNumber(SourceType const &number)
storm::utility::ExtendedValueType< RationalNumber > ExtendedRationalNumber
The rational number type extended with the two infinities.
#define STORM_SILENT_EXPECT_THROW(statement, expected_exception)
Definition storm_gtest.h:21
Configuration for the SCP feasibility search (SparseScpInstantiationSearcher).
Definition ScpOptions.h:16
uint64_t maxIterations
Maximum number of SCP iterations before giving up.
Definition ScpOptions.h:31