1#include "storm-config.h"
44std::string
const dtmcWithSink = R
"(
49 [] s=0 -> 0.5 : (s'=1) + 0.5 : (s'=2);
50 [] s=1 -> 0.3 : (s'=0) + 0.7 : (s'=3);
51 [] s=2 -> 0.5 : (s'=3) + 0.5 : (s'=4);
56// All states are initial State ids then correspond to the values of s.
65std::string
const dtmcWithoutSink = R
"(
70 [] s=0 -> 0.5 : (s'=1) + 0.5 : (s'=2);
71 [] s=1 -> 0.3 : (s'=0) + 0.7 : (s'=3);
76// All states are initial State ids then correspond to the values of s.
90std::string
const mdp = R
"(
95 [a] s=0 -> 0.5 : (s'=1) + 0.5 : (s'=0);
96 [b] s=0 -> 0.2 : (s'=2) + 0.8 : (s'=4);
97 [a] s=1 -> 1 : (s'=3);
98 [b] s=1 -> 0.5 : (s'=3) + 0.5 : (s'=4);
100 [] s=3 -> 1 : (s'=3);
101 [] s=4 -> 1 : (s'=4);
104// All states are initial State ids then correspond to the values of s.
116struct CertifiedBounds {
117 double lower{-std::numeric_limits<double>::infinity()};
118 double upper{std::numeric_limits<double>::infinity()};
122 if (token ==
"inf") {
123 return std::numeric_limits<double>::infinity();
124 }
else if (token ==
"-inf") {
125 return -std::numeric_limits<double>::infinity();
128 auto const slash = token.find(
'/');
129 if (slash != std::string::npos) {
130 return std::stod(token.substr(0, slash)) / std::stod(token.substr(slash + 1));
132 return std::stod(token);
138template<
typename ValueType>
141 relevantStates.set(state);
142 std::string
const summary = certificate.
summaryString(relevantStates);
143 auto const open = summary.find(
'[');
144 auto const comma = summary.find(
", ", open);
145 auto const close = summary.find(
']', comma);
146 EXPECT_TRUE(open != std::string::npos && comma != std::string::npos && close != std::string::npos) << summary;
147 return CertifiedBounds{
parseNumber(summary.substr(open + 1, comma - open - 1)),
parseNumber(summary.substr(comma + 2, close - comma - 2))};
150class DoubleFpIIEnvironment {
152 typedef double ValueType;
153 static storm::Environment createEnvironment() {
154 storm::Environment env;
161class DoubleFpIITopologicalEnvironment {
163 typedef double ValueType;
164 static storm::Environment createEnvironment() {
165 auto env = DoubleFpIIEnvironment::createEnvironment();
171class RationalExPIEnvironment {
173 typedef storm::RationalNumber ValueType;
174 static storm::Environment createEnvironment() {
175 storm::Environment env;
181class RationalExPITopologicalEnvironment {
183 typedef storm::RationalNumber ValueType;
184 static storm::Environment createEnvironment() {
185 auto env = RationalExPIEnvironment::createEnvironment();
191template<
typename TestType>
192class CertificateTest :
public ::testing::Test {
194 typedef typename TestType::ValueType ValueType;
195 CertificateTest() : _environment(TestType::createEnvironment()) {}
197 storm::Environment
const& env()
const {
201 template<
typename ModelType,
typename ModelCheckerType>
202 std::unique_ptr<storm::modelchecker::CheckResult> checkWithCertificate(std::string
const& modelDescription, std::string
const& formulaString,
203 std::shared_ptr<ModelType>& model,
204 storm::Environment
const* customEnvironment =
nullptr)
const {
208 ModelCheckerType checker(*model);
209 storm::modelchecker::CheckTask<storm::logic::Formula, ValueType> task(*formulas[0],
true);
210 task.setProduceCertificate(
true);
211 return checker.check(customEnvironment ? *customEnvironment : env(), task);
217 void checkCertificate(storm::modelchecker::CheckResult& result, storm::models::sparse::Model<ValueType>
const& model,
double expectedValue)
const {
221 auto& certificateResult = result.template asExplicitCertificateCheckResult<ValueType>();
222 EXPECT_TRUE(certificateResult.checkValidity(model));
223 auto const bounds = getBoundsFromSummary(certificateResult.getCertificate(), model.
getNumberOfStates(), 0);
225 double const tolerance = 1e-5;
226 if (std::isinf(expectedValue)) {
227 EXPECT_TRUE(std::isinf(bounds.lower) && std::isinf(bounds.upper)) <<
"[" << bounds.lower <<
", " << bounds.upper <<
"]";
229 EXPECT_LE(bounds.lower, expectedValue + tolerance);
230 EXPECT_GE(bounds.upper, expectedValue - tolerance);
231 EXPECT_LE(bounds.upper - bounds.lower, tolerance);
236 storm::Environment _environment;
239typedef ::testing::Types<DoubleFpIIEnvironment, DoubleFpIITopologicalEnvironment, RationalExPIEnvironment, RationalExPITopologicalEnvironment>
TestingTypes;
243TYPED_TEST(CertificateTest, DtmcReachabilityProbability) {
244 typedef typename TestFixture::ValueType
ValueType;
246 std::shared_ptr<ModelType> model;
248 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(dtmcWithSink,
"P=? [F \"target\"]", model);
249 this->checkCertificate(*result, *model, 12.0 / 17.0);
252TYPED_TEST(CertificateTest, DtmcConstrainedReachabilityProbability) {
253 typedef typename TestFixture::ValueType
ValueType;
255 std::shared_ptr<ModelType> model;
258 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(dtmcWithSink,
"P=? [s<2 U s=3]", model);
259 this->checkCertificate(*result, *model, 0.35 / 0.85);
262TYPED_TEST(CertificateTest, DtmcReachabilityReward) {
263 typedef typename TestFixture::ValueType
ValueType;
265 std::shared_ptr<ModelType> model;
266 auto result = this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(
267 dtmcWithoutSink,
"R{\"steps\"}=? [F \"target\"]", model);
268 this->checkCertificate(*result, *model, 40.0 / 17.0);
271TYPED_TEST(CertificateTest, DtmcReachabilityRewardInfinite) {
272 typedef typename TestFixture::ValueType
ValueType;
274 std::shared_ptr<ModelType> model;
275 auto result = this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(
277 std::string(dtmcWithSink) +
"\nrewards \"steps\"\n s<3 : 1;\nendrewards\n",
"R{\"steps\"}=? [F \"target\"]", model);
278 this->checkCertificate(*result, *model, std::numeric_limits<double>::infinity());
281TYPED_TEST(CertificateTest, DtmcReachabilityTime) {
282 typedef typename TestFixture::ValueType
ValueType;
284 std::shared_ptr<ModelType> model;
285 auto result = this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(dtmcWithoutSink,
286 "T=? [F \"target\"]", model);
287 this->checkCertificate(*result, *model, 40.0 / 17.0);
290TYPED_TEST(CertificateTest, DtmcEmptyConstraintReachabilityProbability) {
291 typedef typename TestFixture::ValueType
ValueType;
293 std::shared_ptr<ModelType> model;
296 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(dtmcWithSink,
"P=? [s>4 U s=3]", model);
297 this->checkCertificate(*result, *model, 0.0);
300TYPED_TEST(CertificateTest, DtmcNegativeRewardsAreRejected) {
301 typedef typename TestFixture::ValueType
ValueType;
303 std::shared_ptr<ModelType> model;
304 std::string
const negativeRewards = dtmcWithoutSink +
"\nrewards \"negative\"\n s<3 : -1;\nendrewards\n";
306 negativeRewards,
"R{\"negative\"}=? [F \"target\"]", model)),
307 storm::exceptions::NotSupportedException);
310TYPED_TEST(CertificateTest, SmoothIterationWithGammaOneIsRejected) {
311 typedef typename TestFixture::ValueType
ValueType;
313 std::shared_ptr<ModelType> model;
319 dtmcWithSink,
"P=? [F \"target\"]", model, &env)),
320 storm::exceptions::InvalidEnvironmentException);
324TYPED_TEST(CertificateTest, CtmcReachability) {
325 typedef typename TestFixture::ValueType
ValueType;
332 labeling.addLabel(
"init");
333 labeling.addLabelToState(
"init", 0);
334 labeling.addLabel(
"target");
335 labeling.addLabelToState(
"target", 2);
336 ModelType model(builder.build(3, 3), std::move(labeling));
340 for (
auto const& [formulaString, expectedValue] : std::vector<std::pair<std::string, double>>{{
"P=? [F \"target\"]", 1.0}, {
"T=? [F \"target\"]", 0.75}}) {
341 auto const formula = formulaParser.parseSingleFormulaFromString(formulaString);
343 task.setProduceCertificate(
true);
344 auto result = checker.check(this->env(), task);
345 this->checkCertificate(*result, model, expectedValue);
351template<
typename ValueType>
356 if constexpr (std::is_same_v<ValueType, double>) {
357 return j.template get<double>();
359 ADD_FAILURE() <<
"Unexpected json value " << j.dump();
363TYPED_TEST(CertificateTest, JsonExportProbability) {
364 typedef typename TestFixture::ValueType
ValueType;
366 std::shared_ptr<ModelType> model;
368 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(dtmcWithSink,
"P=? [F \"target\"]", model);
370 storm::json<ValueType> const j = result->template asExplicitCertificateCheckResult<ValueType>().getCertificate().toJson();
372 EXPECT_EQ(
"reachability-probability", j.at(
"kind").template get<std::string>());
373 EXPECT_FALSE(j.contains(
"direction"));
374 EXPECT_FALSE(j.contains(
"constraint"));
375 ASSERT_EQ(1ull, j.at(
"target").at(
"states").size());
376 EXPECT_EQ(3ull, j.at(
"target").at(
"states").at(0).template get<uint64_t>());
377 EXPECT_NE(std::string::npos, j.at(
"target").at(
"label").template get<std::string>().find(
"target"));
379 auto const& lowerValues = j.at(
"lower").at(
"values");
380 auto const& upperValues = j.at(
"upper").at(
"values");
381 ASSERT_EQ(5ull, lowerValues.size());
382 ASSERT_EQ(5ull, upperValues.size());
383 EXPECT_LE(jsonToDouble<ValueType>(lowerValues.at(0)), 12.0 / 17.0 + 1e-6);
384 EXPECT_GE(jsonToDouble<ValueType>(upperValues.at(0)), 12.0 / 17.0 - 1e-6);
385 EXPECT_NEAR(12.0 / 17.0, jsonToDouble<ValueType>(lowerValues.at(0)), 1e-6);
386 EXPECT_EQ(1.0, jsonToDouble<ValueType>(lowerValues.at(3)));
387 EXPECT_EQ(0.0, jsonToDouble<ValueType>(upperValues.at(4)));
388 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
390 EXPECT_EQ(
"12/17", lowerValues.at(0).template get<std::string>());
391 EXPECT_EQ(
"31/34", lowerValues.at(1).template get<std::string>());
392 EXPECT_EQ(
"1/2", upperValues.at(2).template get<std::string>());
393 EXPECT_EQ(
"1", upperValues.at(3).template get<std::string>());
396 auto const& ranks = j.at(
"lower").at(
"ranks");
397 ASSERT_EQ(5ull, ranks.size());
398 EXPECT_EQ(2ull, ranks.at(0).template get<uint64_t>());
399 EXPECT_EQ(1ull, ranks.at(1).template get<uint64_t>());
400 EXPECT_EQ(1ull, ranks.at(2).template get<uint64_t>());
401 EXPECT_EQ(0ull, ranks.at(3).template get<uint64_t>());
402 EXPECT_EQ(
"inf", ranks.at(4).template get<std::string>());
403 EXPECT_FALSE(j.at(
"upper").contains(
"ranks"));
406TYPED_TEST(CertificateTest, JsonExportConstrainedProbability) {
407 typedef typename TestFixture::ValueType
ValueType;
409 std::shared_ptr<ModelType> model;
411 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(dtmcWithSink,
"P=? [s<2 U s=3]", model);
412 storm::json<ValueType> const j = result->template asExplicitCertificateCheckResult<ValueType>().getCertificate().toJson();
413 ASSERT_TRUE(j.contains(
"constraint"));
414 ASSERT_EQ(2ull, j.at(
"constraint").at(
"states").size());
415 EXPECT_EQ(0ull, j.at(
"constraint").at(
"states").at(0).template get<uint64_t>());
416 EXPECT_EQ(1ull, j.at(
"constraint").at(
"states").at(1).template get<uint64_t>());
417 EXPECT_TRUE(j.at(
"constraint").contains(
"label"));
420TYPED_TEST(CertificateTest, JsonExportReward) {
421 typedef typename TestFixture::ValueType
ValueType;
423 std::shared_ptr<ModelType> model;
424 auto result = this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseDtmcPrctlModelChecker<ModelType>>(
425 dtmcWithoutSink,
"R{\"steps\"}=? [F \"target\"]", model);
427 storm::json<ValueType> const j = result->template asExplicitCertificateCheckResult<ValueType>().getCertificate().toJson();
429 EXPECT_EQ(
"reachability-reward", j.at(
"kind").template get<std::string>());
430 EXPECT_FALSE(j.contains(
"direction"));
431 EXPECT_EQ(
"steps", j.at(
"reward-model").template get<std::string>());
432 ASSERT_EQ(4ull, j.at(
"choice-rewards").size());
433 for (uint64_t choice = 0; choice < 3; ++choice) {
434 EXPECT_EQ(1.0, jsonToDouble<ValueType>(j.at(
"choice-rewards").at(choice)));
436 EXPECT_EQ(0.0, jsonToDouble<ValueType>(j.at(
"choice-rewards").at(3)));
437 ASSERT_EQ(1ull, j.at(
"target").at(
"states").size());
439 ASSERT_EQ(4ull, j.at(
"lower").at(
"values").size());
440 ASSERT_EQ(4ull, j.at(
"upper").at(
"values").size());
441 EXPECT_NEAR(40.0 / 17.0, jsonToDouble<ValueType>(j.at(
"lower").at(
"values").at(0)), 1e-6);
442 EXPECT_NEAR(40.0 / 17.0, jsonToDouble<ValueType>(j.at(
"upper").at(
"values").at(0)), 1e-6);
443 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
444 EXPECT_EQ(
"40/17", j.at(
"upper").at(
"values").at(0).template get<std::string>());
445 EXPECT_EQ(
"29/17", j.at(
"upper").at(
"values").at(1).template get<std::string>());
448 for (
auto const& rank : j.at(
"lower").at(
"ranks")) {
449 EXPECT_EQ(
"inf", rank.template get<std::string>());
451 auto const& upperRanks = j.at(
"upper").at(
"ranks");
452 ASSERT_EQ(4ull, upperRanks.size());
453 EXPECT_EQ(2ull, upperRanks.at(0).template get<uint64_t>());
454 EXPECT_EQ(1ull, upperRanks.at(1).template get<uint64_t>());
455 EXPECT_EQ(1ull, upperRanks.at(2).template get<uint64_t>());
456 EXPECT_EQ(0ull, upperRanks.at(3).template get<uint64_t>());
459TYPED_TEST(CertificateTest, JsonExportMdpDirectionAndInfinity) {
460 typedef typename TestFixture::ValueType
ValueType;
463 std::shared_ptr<ModelType> model;
465 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseMdpPrctlModelChecker<ModelType>>(mdp,
"Pmin=? [F \"target\"]", model);
466 storm::json<ValueType> const j = result->template asExplicitCertificateCheckResult<ValueType>().getCertificate().toJson();
467 EXPECT_EQ(
"min", j.at(
"direction").template get<std::string>());
471 std::shared_ptr<ModelType> model;
472 auto result = this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseMdpPrctlModelChecker<ModelType>>(
473 mdp,
"R{\"steps\"}max=? [F \"target\"]", model);
474 storm::json<ValueType> const j = result->template asExplicitCertificateCheckResult<ValueType>().getCertificate().toJson();
475 EXPECT_EQ(
"max", j.at(
"direction").template get<std::string>());
476 EXPECT_EQ(
"inf", j.at(
"lower").at(
"values").at(0).template get<std::string>());
477 EXPECT_EQ(
"inf", j.at(
"upper").at(
"values").at(0).template get<std::string>());
479 EXPECT_EQ(model->getNumberOfChoices(), j.at(
"choice-rewards").size());
483TYPED_TEST(CertificateTest, MdpReachabilityProbability) {
484 typedef typename TestFixture::ValueType
ValueType;
487 std::shared_ptr<ModelType> model;
489 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseMdpPrctlModelChecker<ModelType>>(mdp,
"Pmax=? [F \"target\"]", model);
490 this->checkCertificate(*result, *model, 1.0);
493 std::shared_ptr<ModelType> model;
495 this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseMdpPrctlModelChecker<ModelType>>(mdp,
"Pmin=? [F \"target\"]", model);
496 this->checkCertificate(*result, *model, 0.2);
500TYPED_TEST(CertificateTest, MdpReachabilityReward) {
501 typedef typename TestFixture::ValueType
ValueType;
504 std::shared_ptr<ModelType> model;
505 auto result = this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseMdpPrctlModelChecker<ModelType>>(
506 mdp,
"R{\"steps\"}min=? [F \"target\"]", model);
507 this->checkCertificate(*result, *model, 3.0);
510 std::shared_ptr<ModelType> model;
511 auto result = this->
template checkWithCertificate<ModelType, storm::modelchecker::SparseMdpPrctlModelChecker<ModelType>>(
512 mdp,
"R{\"steps\"}max=? [F \"target\"]", model);
513 this->checkCertificate(*result, *model, std::numeric_limits<double>::infinity());
518class HandMadeCertificateTest :
public ::testing::Test {
520 typedef storm::RationalNumber ValueType;
522 void SetUp()
override {
525 ->as<storm::models::sparse::Dtmc<ValueType>>();
526 targetStates = storm::storage::BitVector(model->getNumberOfStates(),
false);
530 static std::vector<ValueType> rationals(std::vector<std::pair<int, int>>
const& fractions) {
531 std::vector<ValueType> result;
532 for (
auto const& [numerator, denominator] : fractions) {
538 std::shared_ptr<storm::models::sparse::Dtmc<ValueType>> model;
539 storm::storage::BitVector targetStates;
542TEST_F(HandMadeCertificateTest, UpperBound) {
545 valid.setUpperBoundsCertificate(rationals({{1, 1}, {1, 1}, {1, 1}, {1, 1}, {0, 1}}));
546 EXPECT_TRUE(valid.checkValidity(*model));
550 notInductive.setUpperBoundsCertificate(rationals({{1, 1}, {1, 1}, {2, 5}, {1, 1}, {0, 1}}));
551 EXPECT_FALSE(notInductive.checkValidity(*model));
555 negative.setUpperBoundsCertificate(rationals({{1, 1}, {1, 1}, {1, 1}, {1, 1}, {-1, 1}}));
556 EXPECT_FALSE(negative.checkValidity(*model));
561 emptyConstraint.setUpperBoundsCertificate(rationals({{0, 1}, {0, 1}, {0, 1}, {1, 1}, {0, 1}}));
562 EXPECT_TRUE(emptyConstraint.checkValidity(*model));
566 targetTooSmall.setUpperBoundsCertificate(rationals({{1, 1}, {1, 1}, {1, 1}, {1, 2}, {0, 1}}));
567 EXPECT_FALSE(targetTooSmall.checkValidity(*model));
570TEST_F(HandMadeCertificateTest, LowerBound) {
574 valid.setLowerBoundsCertificate(rationals({{0, 1}, {0, 1}, {0, 1}, {1, 1}, {0, 1}}), {2, 1, 1, 0, inf});
575 EXPECT_TRUE(valid.checkValidity(*model));
579 notInductive.setLowerBoundsCertificate(rationals({{0, 1}, {9, 10}, {0, 1}, {1, 1}, {0, 1}}), {2, 1, 1, 0, inf});
580 EXPECT_FALSE(notInductive.checkValidity(*model));
584 sinkWithPositiveBound.setLowerBoundsCertificate(rationals({{0, 1}, {0, 1}, {0, 1}, {1, 1}, {1, 10}}), {2, 1, 1, 0, inf});
585 EXPECT_FALSE(sinkWithPositiveBound.checkValidity(*model));
589 badRanks.setLowerBoundsCertificate(rationals({{0, 1}, {7, 10}, {1, 2}, {1, 1}, {0, 1}}), {inf, inf, inf, 0, inf});
590 EXPECT_FALSE(badRanks.checkValidity(*model));
593TEST(CertificateAlgorithmTest, StringConversion) {
596 std::stringstream name;
TEST_F(ConstraintCollectorTest, PolynomialDenominators)
TEST(RegionConstraintCheckerTest, AffineConstraintHoldsThroughout)
void setGamma(double value)
void setUseTopological(bool value)
void setAlgorithm(CertificateAlgorithm value)
SolverEnvironment & solver()
ModelCheckerEnvironment & modelchecker()
void setPrecision(storm::RationalNumber value)
CertificateModelCheckerEnvironment & certificate()
MinMaxSolverEnvironment & minMax()
A certificate for the result of a model checking query.
virtual std::string summaryString(storm::storage::BitVector const &relevantStates) const =0
virtual bool isExplicit() const
virtual bool isResultForAllStates() const
virtual bool isExplicitCertificateCheckResult() const
This class represents a continuous-time Markov chain.
This class represents a discrete-time Markov chain.
This class represents a (discrete-time) Markov decision process.
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
This class manages the labeling of the state space with a number of (atomic) labels.
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...
A bit vector that is internally represented as a vector of 64-bit values.
A class that can be used to build a sparse matrix by adding value by value.
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())
SFTBDDChecker::ValueType ValueType
static RankingType const InfRank
NumberType parseNumber(std::string const &value)
Parse number from string.
TargetType convertNumber(SourceType const &number)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
@ ExPI
Interval iteration with floating point numbers where lower bounds are rounded towards the sound direc...
@ FpRoundII
Smooth interval iteration with floating point numbers.
@ FpSmoothII
Interval iteration with floating point numbers.
CertificateAlgorithm certificateAlgorithmFromString(std::string const &algorithm)
TYPED_TEST(GraphTestAR, SymbolicProb01StochasticGameDieSmall)
TYPED_TEST_SUITE(GraphTestAR, TestingTypes,)
::testing::Types< Cudd, Sylvan > TestingTypes
#define STORM_SILENT_EXPECT_THROW(statement, expected_exception)