You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
8.7 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #ifdef STORM_HAVE_CARL
  4. #include "src/adapters/CarlAdapter.h"
  5. #include "src/settings/SettingsManager.h"
  6. #include "src/settings/modules/GeneralSettings.h"
  7. #include "src/settings/modules/RegionSettings.h"
  8. #include "src/models/sparse/Dtmc.h"
  9. #include "src/parser/PrismParser.h"
  10. #include "src/parser/FormulaParser.h"
  11. #include "src/logic/Formulas.h"
  12. #include "src/models/ModelBase.h"
  13. #include "src/models/sparse/Model.h"
  14. #include "src/models/sparse/Dtmc.h"
  15. #include "builder/ExplicitPrismModelBuilder.h"
  16. #include "modelchecker/region/SparseMdpRegionModelChecker.h"
  17. #include "modelchecker/region/ParameterRegion.h"
  18. TEST(SparseMdpRegionModelCheckerTest, two_dice_Prob) {
  19. std::string const& programFile = STORM_CPP_BASE_PATH "/examples/pmdp/two_dice/two_dice.nm";
  20. std::string const& formulaAsString = "P<=0.17 [F \"doubles\" ]";
  21. std::string const& constantsAsString = ""; //e.g. pL=0.9,TOACK=0.5
  22. //Build model, formula, region model checker
  23. boost::optional<storm::prism::Program> program=storm::parser::PrismParser::parse(programFile).simplify().simplify();
  24. program->checkValidity();
  25. storm::parser::FormulaParser formulaParser(program.get().getManager().getSharedPointer());
  26. std::vector<std::shared_ptr<storm::logic::Formula>> formulas = formulaParser.parseFromString(formulaAsString);
  27. typename storm::builder::ExplicitPrismModelBuilder<storm::RationalFunction>::Options options=storm::builder::ExplicitPrismModelBuilder<storm::RationalFunction>::Options(*formulas[0]);
  28. options.addConstantDefinitionsFromString(program.get(), constantsAsString);
  29. options.preserveFormula(*formulas[0]);
  30. std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> model = storm::builder::ExplicitPrismModelBuilder<storm::RationalFunction>().translateProgram(program.get(), options)->as<storm::models::sparse::Model<storm::RationalFunction>>();
  31. ASSERT_EQ(storm::models::ModelType::Mdp, model->getType());
  32. std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp = model->template as<storm::models::sparse::Mdp<storm::RationalFunction>>();
  33. storm::modelchecker::region::SparseMdpRegionModelChecker<storm::models::sparse::Mdp<storm::RationalFunction>, double> modelchecker(*mdp);
  34. ASSERT_TRUE(modelchecker.canHandle(*formulas[0]));
  35. modelchecker.specifyFormula(formulas[0]);
  36. //start testing
  37. auto allSatRegion=storm::modelchecker::region::ParameterRegion<storm::RationalFunction>::parseRegion("0.495<=p1<=0.5,0.5<=p2<=0.505");
  38. auto exBothRegion=storm::modelchecker::region::ParameterRegion<storm::RationalFunction>::parseRegion("0.45<=p1<=0.55,0.45<=p2<=0.55");
  39. auto allVioRegion=storm::modelchecker::region::ParameterRegion<storm::RationalFunction>::parseRegion("0.6<=p1<=0.7,0.6<=p2<=0.6");
  40. EXPECT_NEAR(0.1666665285, modelchecker.getReachabilityValue(allSatRegion.getLowerBounds()), storm::settings::generalSettings().getPrecision());
  41. EXPECT_NEAR(0.1666665529, modelchecker.getReachabilityValue(allSatRegion.getUpperBounds()), storm::settings::generalSettings().getPrecision());
  42. EXPECT_NEAR(0.1716553235, modelchecker.getReachabilityValue(exBothRegion.getLowerBounds()), storm::settings::generalSettings().getPrecision());
  43. EXPECT_NEAR(0.1709666953, modelchecker.getReachabilityValue(exBothRegion.getUpperBounds()), storm::settings::generalSettings().getPrecision());
  44. EXPECT_NEAR(0.1826972576, modelchecker.getReachabilityValue(allVioRegion.getLowerBounds()), storm::settings::generalSettings().getPrecision());
  45. EXPECT_NEAR(0.1964429282, modelchecker.getReachabilityValue(allVioRegion.getUpperBounds()), storm::settings::generalSettings().getPrecision());
  46. //test approximative method
  47. storm::settings::mutableRegionSettings().modifyModes(storm::settings::modules::RegionSettings::ApproxMode::TESTFIRST, storm::settings::modules::RegionSettings::SampleMode::INSTANTIATE, storm::settings::modules::RegionSettings::SmtMode::OFF);
  48. ASSERT_TRUE(storm::settings::regionSettings().doApprox());
  49. ASSERT_TRUE(storm::settings::regionSettings().doSample());
  50. ASSERT_FALSE(storm::settings::regionSettings().doSmt());
  51. modelchecker.checkRegion(allSatRegion);
  52. EXPECT_EQ((storm::modelchecker::region::RegionCheckResult::ALLSAT), allSatRegion.getCheckResult());
  53. modelchecker.checkRegion(exBothRegion);
  54. EXPECT_EQ((storm::modelchecker::region::RegionCheckResult::EXISTSBOTH), exBothRegion.getCheckResult());
  55. modelchecker.checkRegion(allVioRegion);
  56. EXPECT_EQ((storm::modelchecker::region::RegionCheckResult::ALLVIOLATED), allVioRegion.getCheckResult());
  57. storm::settings::mutableRegionSettings().resetModes();
  58. carl::VariablePool::getInstance().clear();
  59. }
  60. TEST(SparseMdpRegionModelCheckerTest, coin_Prob) {
  61. std::string const& programFile = STORM_CPP_BASE_PATH "/examples/pmdp/consensus/coin2_2.nm";
  62. std::string const& formulaAsString = "P>0.25 [F \"finished\"&\"all_coins_equal_1\" ]";
  63. std::string const& constantsAsString = ""; //e.g. pL=0.9,TOACK=0.5
  64. //Build model, formula, region model checker
  65. boost::optional<storm::prism::Program> program=storm::parser::PrismParser::parse(programFile).simplify().simplify();
  66. program->checkValidity();
  67. storm::parser::FormulaParser formulaParser(program.get().getManager().getSharedPointer());
  68. std::vector<std::shared_ptr<storm::logic::Formula>> formulas = formulaParser.parseFromString(formulaAsString);
  69. typename storm::builder::ExplicitPrismModelBuilder<storm::RationalFunction>::Options options=storm::builder::ExplicitPrismModelBuilder<storm::RationalFunction>::Options(*formulas[0]);
  70. options.addConstantDefinitionsFromString(program.get(), constantsAsString);
  71. options.preserveFormula(*formulas[0]);
  72. std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> model = storm::builder::ExplicitPrismModelBuilder<storm::RationalFunction>().translateProgram(program.get(), options)->as<storm::models::sparse::Model<storm::RationalFunction>>();
  73. ASSERT_EQ(storm::models::ModelType::Mdp, model->getType());
  74. std::shared_ptr<storm::models::sparse::Mdp<storm::RationalFunction>> mdp = model->template as<storm::models::sparse::Mdp<storm::RationalFunction>>();
  75. storm::modelchecker::region::SparseMdpRegionModelChecker<storm::models::sparse::Mdp<storm::RationalFunction>, double> modelchecker(*mdp);
  76. ASSERT_TRUE(modelchecker.canHandle(*formulas[0]));
  77. modelchecker.specifyFormula(formulas[0]);
  78. //start testing
  79. auto allSatRegion=storm::modelchecker::region::ParameterRegion<storm::RationalFunction>::parseRegion("0.3<=p1<=0.45,0.2<=p2<=0.54");
  80. auto exBothRegion=storm::modelchecker::region::ParameterRegion<storm::RationalFunction>::parseRegion("0.4<=p1<=0.65,0.5<=p2<=0.7");
  81. auto allVioRegion=storm::modelchecker::region::ParameterRegion<storm::RationalFunction>::parseRegion("0.4<=p1<=0.7,0.55<=p2<=0.6");
  82. EXPECT_NEAR(0.95127874851, modelchecker.getReachabilityValue(allSatRegion.getLowerBounds()), storm::settings::generalSettings().getPrecision());
  83. EXPECT_NEAR(0.26787251126, modelchecker.getReachabilityValue(allSatRegion.getUpperBounds()), storm::settings::generalSettings().getPrecision());
  84. EXPECT_NEAR(0.41880006098, modelchecker.getReachabilityValue(exBothRegion.getLowerBounds()), storm::settings::generalSettings().getPrecision());
  85. EXPECT_NEAR(0.01535089684, modelchecker.getReachabilityValue(exBothRegion.getUpperBounds()), storm::settings::generalSettings().getPrecision());
  86. EXPECT_NEAR(0.24952791523, modelchecker.getReachabilityValue(allVioRegion.getLowerBounds()), storm::settings::generalSettings().getPrecision());
  87. EXPECT_NEAR(0.01711494956, modelchecker.getReachabilityValue(allVioRegion.getUpperBounds()), storm::settings::generalSettings().getPrecision());
  88. //test approximative method
  89. storm::settings::mutableRegionSettings().modifyModes(storm::settings::modules::RegionSettings::ApproxMode::TESTFIRST, storm::settings::modules::RegionSettings::SampleMode::INSTANTIATE, storm::settings::modules::RegionSettings::SmtMode::OFF);
  90. ASSERT_TRUE(storm::settings::regionSettings().doApprox());
  91. ASSERT_TRUE(storm::settings::regionSettings().doSample());
  92. ASSERT_FALSE(storm::settings::regionSettings().doSmt());
  93. modelchecker.checkRegion(allSatRegion);
  94. EXPECT_EQ((storm::modelchecker::region::RegionCheckResult::ALLSAT), allSatRegion.getCheckResult());
  95. modelchecker.checkRegion(exBothRegion);
  96. EXPECT_EQ((storm::modelchecker::region::RegionCheckResult::EXISTSBOTH), exBothRegion.getCheckResult());
  97. modelchecker.checkRegion(allVioRegion);
  98. EXPECT_EQ((storm::modelchecker::region::RegionCheckResult::ALLVIOLATED), allVioRegion.getCheckResult());
  99. storm::settings::mutableRegionSettings().resetModes();
  100. carl::VariablePool::getInstance().clear();
  101. }
  102. #endif