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.

274 lines
16 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/settings/SettingMemento.h"
  4. #include "src/logic/Formulas.h"
  5. #include "src/utility/solver.h"
  6. #include "src/modelchecker/prctl/SparseDtmcPrctlModelChecker.h"
  7. #include "src/modelchecker/results/ExplicitQuantitativeCheckResult.h"
  8. #include "src/settings/SettingsManager.h"
  9. #include "src/settings/SettingMemento.h"
  10. #include "src/parser/AutoParser.h"
  11. TEST(SparseDtmcPrctlModelCheckerTest, Die) {
  12. std::shared_ptr<storm::models::sparse::Model<double>> abstractModel = storm::parser::AutoParser::parseModel(STORM_CPP_BASE_PATH "/examples/dtmc/die/die.tra", STORM_CPP_BASE_PATH "/examples/dtmc/die/die.lab", "", STORM_CPP_BASE_PATH "/examples/dtmc/die/die.coin_flips.trans.rew");
  13. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  14. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  15. ASSERT_EQ(dtmc->getNumberOfStates(), 13ull);
  16. ASSERT_EQ(dtmc->getNumberOfTransitions(), 20ull);
  17. storm::modelchecker::SparseDtmcPrctlModelChecker<double> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  18. auto labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("one");
  19. auto eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(labelFormula);
  20. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*eventuallyFormula);
  21. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  22. EXPECT_NEAR(1.0/6.0, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  23. labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("two");
  24. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(labelFormula);
  25. result = checker.check(*eventuallyFormula);
  26. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  27. EXPECT_NEAR(1.0/6.0, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  28. labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("three");
  29. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(labelFormula);
  30. result = checker.check(*eventuallyFormula);
  31. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  32. EXPECT_NEAR(1.0/6.0, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  33. auto done = std::make_shared<storm::logic::AtomicLabelFormula>("done");
  34. auto reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(done);
  35. result = checker.check(*reachabilityRewardFormula);
  36. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult4 = result->asExplicitQuantitativeCheckResult<double>();
  37. EXPECT_NEAR(3.6666650772094727, quantitativeResult4[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  38. }
  39. TEST(SparseDtmcPrctlModelCheckerTest, Crowds) {
  40. std::shared_ptr<storm::models::sparse::Model<double>> abstractModel = storm::parser::AutoParser::parseModel(STORM_CPP_BASE_PATH "/examples/dtmc/crowds/crowds5_5.tra", STORM_CPP_BASE_PATH "/examples/dtmc/crowds/crowds5_5.lab", "", "");
  41. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  42. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  43. ASSERT_EQ(8607ull, dtmc->getNumberOfStates());
  44. ASSERT_EQ(15113ull, dtmc->getNumberOfTransitions());
  45. storm::modelchecker::SparseDtmcPrctlModelChecker<double> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  46. auto labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("observe0Greater1");
  47. auto eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(labelFormula);
  48. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*eventuallyFormula);
  49. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  50. EXPECT_NEAR(0.33288205191646525, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  51. labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("observeIGreater1");
  52. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(labelFormula);
  53. result = checker.check(*eventuallyFormula);
  54. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  55. EXPECT_NEAR(0.15222066094730619, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  56. labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("observeOnlyTrueSender");
  57. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(labelFormula);
  58. result = checker.check(*eventuallyFormula);
  59. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  60. EXPECT_NEAR(0.32153900158185761, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  61. }
  62. TEST(SparseDtmcPrctlModelCheckerTest, SynchronousLeader) {
  63. std::shared_ptr<storm::models::sparse::Model<double>> abstractModel = storm::parser::AutoParser::parseModel(STORM_CPP_BASE_PATH "/examples/dtmc/synchronous_leader/leader4_8.tra", STORM_CPP_BASE_PATH "/examples/dtmc/synchronous_leader/leader4_8.lab", "", STORM_CPP_BASE_PATH "/examples/dtmc/synchronous_leader/leader4_8.pick.trans.rew");
  64. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  65. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  66. ASSERT_EQ(12400ull, dtmc->getNumberOfStates());
  67. ASSERT_EQ(16495ull, dtmc->getNumberOfTransitions());
  68. storm::modelchecker::SparseDtmcPrctlModelChecker<double> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  69. auto labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  70. auto eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(labelFormula);
  71. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*eventuallyFormula);
  72. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  73. EXPECT_NEAR(1.0, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  74. labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  75. auto trueFormula = std::make_shared<storm::logic::BooleanLiteralFormula>(true);
  76. auto boundedUntilFormula = std::make_shared<storm::logic::BoundedUntilFormula>(trueFormula, labelFormula, 20);
  77. result = checker.check(*boundedUntilFormula);
  78. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  79. EXPECT_NEAR(0.9999965911265462636, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  80. labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  81. auto reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(labelFormula);
  82. result = checker.check(*reachabilityRewardFormula);
  83. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  84. EXPECT_NEAR(1.0448979589010925, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  85. }
  86. TEST(SparseDtmcPrctlModelCheckerTest, LRASingleBscc) {
  87. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  88. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc;
  89. {
  90. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(2, 2, 2);
  91. matrixBuilder.addNextValue(0, 1, 1.);
  92. matrixBuilder.addNextValue(1, 0, 1.);
  93. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  94. storm::models::sparse::StateLabeling ap(2);
  95. ap.addLabel("a");
  96. ap.addLabelToState("a", 1);
  97. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap, boost::none, boost::none, boost::none));
  98. storm::modelchecker::SparseDtmcPrctlModelChecker<double> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>(storm::solver::NativeLinearEquationSolver<double>::SolutionMethod::SOR, 0.9)));
  99. auto labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("a");
  100. auto lraFormula = std::make_shared<storm::logic::LongRunAverageOperatorFormula>(labelFormula);
  101. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*lraFormula));
  102. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  103. EXPECT_NEAR(.5, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  104. EXPECT_NEAR(.5, quantitativeResult1[1], storm::settings::nativeEquationSolverSettings().getPrecision());
  105. }
  106. {
  107. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(2, 2, 4);
  108. matrixBuilder.addNextValue(0, 0, .5);
  109. matrixBuilder.addNextValue(0, 1, .5);
  110. matrixBuilder.addNextValue(1, 0, .5);
  111. matrixBuilder.addNextValue(1, 1, .5);
  112. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  113. storm::models::sparse::StateLabeling ap(2);
  114. ap.addLabel("a");
  115. ap.addLabelToState("a", 1);
  116. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap, boost::none, boost::none, boost::none));
  117. storm::modelchecker::SparseDtmcPrctlModelChecker<double> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>(storm::solver::NativeLinearEquationSolver<double>::SolutionMethod::SOR, 0.9)));
  118. auto labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("a");
  119. auto lraFormula = std::make_shared<storm::logic::LongRunAverageOperatorFormula>(labelFormula);
  120. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*lraFormula));
  121. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  122. EXPECT_NEAR(.5, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  123. EXPECT_NEAR(.5, quantitativeResult1[1], storm::settings::nativeEquationSolverSettings().getPrecision());
  124. }
  125. {
  126. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(3, 3, 3);
  127. matrixBuilder.addNextValue(0, 1, 1);
  128. matrixBuilder.addNextValue(1, 2, 1);
  129. matrixBuilder.addNextValue(2, 0, 1);
  130. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  131. storm::models::sparse::StateLabeling ap(3);
  132. ap.addLabel("a");
  133. ap.addLabelToState("a", 2);
  134. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap, boost::none, boost::none, boost::none));
  135. storm::modelchecker::SparseDtmcPrctlModelChecker<double> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>(storm::solver::NativeLinearEquationSolver<double>::SolutionMethod::SOR, 0.9)));
  136. auto labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("a");
  137. auto lraFormula = std::make_shared<storm::logic::LongRunAverageOperatorFormula>(labelFormula);
  138. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*lraFormula));
  139. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  140. EXPECT_NEAR(1. / 3., quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  141. EXPECT_NEAR(1. / 3., quantitativeResult1[1], storm::settings::nativeEquationSolverSettings().getPrecision());
  142. EXPECT_NEAR(1. / 3., quantitativeResult1[2], storm::settings::nativeEquationSolverSettings().getPrecision());
  143. }
  144. }
  145. TEST(SparseDtmcPrctlModelCheckerTest, LRA) {
  146. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  147. std::shared_ptr<storm::models::sparse::Dtmc<double>> mdp;
  148. {
  149. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(15, 15, 20, true);
  150. matrixBuilder.addNextValue(0, 1, 1);
  151. matrixBuilder.addNextValue(1, 4, 0.7);
  152. matrixBuilder.addNextValue(1, 6, 0.3);
  153. matrixBuilder.addNextValue(2, 0, 1);
  154. matrixBuilder.addNextValue(3, 5, 0.8);
  155. matrixBuilder.addNextValue(3, 9, 0.2);
  156. matrixBuilder.addNextValue(4, 3, 1);
  157. matrixBuilder.addNextValue(5, 3, 1);
  158. matrixBuilder.addNextValue(6, 7, 1);
  159. matrixBuilder.addNextValue(7, 8, 1);
  160. matrixBuilder.addNextValue(8, 6, 1);
  161. matrixBuilder.addNextValue(9, 10, 1);
  162. matrixBuilder.addNextValue(10, 9, 1);
  163. matrixBuilder.addNextValue(11, 9, 1);
  164. matrixBuilder.addNextValue(12, 5, 0.4);
  165. matrixBuilder.addNextValue(12, 8, 0.3);
  166. matrixBuilder.addNextValue(12, 11, 0.3);
  167. matrixBuilder.addNextValue(13, 7, 0.7);
  168. matrixBuilder.addNextValue(13, 12, 0.3);
  169. matrixBuilder.addNextValue(14, 12, 1);
  170. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  171. storm::models::sparse::StateLabeling ap(15);
  172. ap.addLabel("a");
  173. ap.addLabelToState("a", 1);
  174. ap.addLabelToState("a", 4);
  175. ap.addLabelToState("a", 5);
  176. ap.addLabelToState("a", 7);
  177. ap.addLabelToState("a", 11);
  178. ap.addLabelToState("a", 13);
  179. ap.addLabelToState("a", 14);
  180. mdp.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap, boost::none, boost::none, boost::none));
  181. storm::modelchecker::SparseDtmcPrctlModelChecker<double> checker(*mdp, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>(storm::solver::NativeLinearEquationSolver<double>::SolutionMethod::SOR, 0.9)));
  182. auto labelFormula = std::make_shared<storm::logic::AtomicLabelFormula>("a");
  183. auto lraFormula = std::make_shared<storm::logic::LongRunAverageOperatorFormula>(labelFormula);
  184. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*lraFormula));
  185. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  186. EXPECT_NEAR(0.3 / 3., quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  187. EXPECT_NEAR(0.0, quantitativeResult1[3], storm::settings::nativeEquationSolverSettings().getPrecision());
  188. EXPECT_NEAR(1. / 3., quantitativeResult1[6], storm::settings::nativeEquationSolverSettings().getPrecision());
  189. EXPECT_NEAR(0.0, quantitativeResult1[9], storm::settings::nativeEquationSolverSettings().getPrecision());
  190. EXPECT_NEAR(0.3/3., quantitativeResult1[12], storm::settings::nativeEquationSolverSettings().getPrecision());
  191. EXPECT_NEAR(.79 / 3., quantitativeResult1[13], storm::settings::nativeEquationSolverSettings().getPrecision());
  192. EXPECT_NEAR(0.3 / 3., quantitativeResult1[14], storm::settings::nativeEquationSolverSettings().getPrecision());
  193. }
  194. }