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.

281 lines
15 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/parser/FormulaParser.h"
  4. #include "src/settings/SettingMemento.h"
  5. #include "src/logic/Formulas.h"
  6. #include "src/utility/solver.h"
  7. #include "src/models/sparse/StandardRewardModel.h"
  8. #include "src/modelchecker/prctl/SparseDtmcPrctlModelChecker.h"
  9. #include "src/modelchecker/results/ExplicitQuantitativeCheckResult.h"
  10. #include "src/settings/SettingsManager.h"
  11. #include "src/settings/modules/GeneralSettings.h"
  12. #include "src/settings/modules/NativeEquationSolverSettings.h"
  13. #include "src/settings/SettingMemento.h"
  14. #include "src/parser/AutoParser.h"
  15. TEST(SparseDtmcPrctlModelCheckerTest, Die) {
  16. 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");
  17. // A parser that we use for conveniently constructing the formulas.
  18. storm::parser::FormulaParser formulaParser;
  19. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  20. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  21. ASSERT_EQ(dtmc->getNumberOfStates(), 13ull);
  22. ASSERT_EQ(dtmc->getNumberOfTransitions(), 20ull);
  23. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  24. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("P=? [F \"one\"]");
  25. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  26. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  27. EXPECT_NEAR(1.0/6.0, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  28. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"two\"]");
  29. result = checker.check(*formula);
  30. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  31. EXPECT_NEAR(1.0/6.0, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  32. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"three\"]");
  33. result = checker.check(*formula);
  34. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  35. EXPECT_NEAR(1.0/6.0, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  36. formula = formulaParser.parseSingleFormulaFromString("R=? [F \"done\"]");
  37. result = checker.check(*formula);
  38. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult4 = result->asExplicitQuantitativeCheckResult<double>();
  39. EXPECT_NEAR(3.6666650772094727, quantitativeResult4[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  40. }
  41. TEST(SparseDtmcPrctlModelCheckerTest, Crowds) {
  42. 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", "", "");
  43. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  44. // A parser that we use for conveniently constructing the formulas.
  45. storm::parser::FormulaParser formulaParser;
  46. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  47. ASSERT_EQ(8607ull, dtmc->getNumberOfStates());
  48. ASSERT_EQ(15113ull, dtmc->getNumberOfTransitions());
  49. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  50. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("P=? [F \"observe0Greater1\"]");
  51. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  52. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  53. EXPECT_NEAR(0.33288205191646525, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  54. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"observeIGreater1\"]");
  55. result = checker.check(*formula);
  56. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  57. EXPECT_NEAR(0.15222066094730619, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  58. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"observeOnlyTrueSender\"]");
  59. result = checker.check(*formula);
  60. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  61. EXPECT_NEAR(0.32153900158185761, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  62. }
  63. TEST(SparseDtmcPrctlModelCheckerTest, SynchronousLeader) {
  64. 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");
  65. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  66. // A parser that we use for conveniently constructing the formulas.
  67. storm::parser::FormulaParser formulaParser;
  68. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  69. ASSERT_EQ(12400ull, dtmc->getNumberOfStates());
  70. ASSERT_EQ(16495ull, dtmc->getNumberOfTransitions());
  71. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  72. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("P=? [F \"elected\"]");
  73. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  74. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  75. EXPECT_NEAR(1.0, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  76. formula = formulaParser.parseSingleFormulaFromString("P=? [F<=20 \"elected\"]");
  77. result = checker.check(*formula);
  78. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  79. EXPECT_NEAR(0.9999965911265462636, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  80. formula = formulaParser.parseSingleFormulaFromString("R=? [F \"elected\"]");
  81. result = checker.check(*formula);
  82. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  83. EXPECT_NEAR(1.0448979589010925, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  84. }
  85. TEST(SparseDtmcPrctlModelCheckerTest, LRASingleBscc) {
  86. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  87. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc;
  88. // A parser that we use for conveniently constructing the formulas.
  89. storm::parser::FormulaParser formulaParser;
  90. {
  91. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(2, 2, 2);
  92. matrixBuilder.addNextValue(0, 1, 1.);
  93. matrixBuilder.addNextValue(1, 0, 1.);
  94. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  95. storm::models::sparse::StateLabeling ap(2);
  96. ap.addLabel("a");
  97. ap.addLabelToState("a", 1);
  98. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  99. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<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)));
  100. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  101. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  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));
  117. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<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. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  119. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  120. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  121. EXPECT_NEAR(.5, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  122. EXPECT_NEAR(.5, quantitativeResult1[1], storm::settings::nativeEquationSolverSettings().getPrecision());
  123. }
  124. {
  125. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(3, 3, 3);
  126. matrixBuilder.addNextValue(0, 1, 1);
  127. matrixBuilder.addNextValue(1, 2, 1);
  128. matrixBuilder.addNextValue(2, 0, 1);
  129. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  130. storm::models::sparse::StateLabeling ap(3);
  131. ap.addLabel("a");
  132. ap.addLabelToState("a", 2);
  133. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  134. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<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)));
  135. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  136. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  137. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  138. EXPECT_NEAR(1. / 3., quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  139. EXPECT_NEAR(1. / 3., quantitativeResult1[1], storm::settings::nativeEquationSolverSettings().getPrecision());
  140. EXPECT_NEAR(1. / 3., quantitativeResult1[2], storm::settings::nativeEquationSolverSettings().getPrecision());
  141. }
  142. }
  143. TEST(SparseDtmcPrctlModelCheckerTest, LRA) {
  144. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  145. std::shared_ptr<storm::models::sparse::Dtmc<double>> mdp;
  146. // A parser that we use for conveniently constructing the formulas.
  147. storm::parser::FormulaParser formulaParser;
  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));
  181. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<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. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  183. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  184. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  185. EXPECT_NEAR(0.3 / 3., quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  186. EXPECT_NEAR(0.0, quantitativeResult1[3], storm::settings::nativeEquationSolverSettings().getPrecision());
  187. EXPECT_NEAR(1. / 3., quantitativeResult1[6], storm::settings::nativeEquationSolverSettings().getPrecision());
  188. EXPECT_NEAR(0.0, quantitativeResult1[9], storm::settings::nativeEquationSolverSettings().getPrecision());
  189. EXPECT_NEAR(0.3/3., quantitativeResult1[12], storm::settings::nativeEquationSolverSettings().getPrecision());
  190. EXPECT_NEAR(.79 / 3., quantitativeResult1[13], storm::settings::nativeEquationSolverSettings().getPrecision());
  191. EXPECT_NEAR(0.3 / 3., quantitativeResult1[14], storm::settings::nativeEquationSolverSettings().getPrecision());
  192. }
  193. }