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/solver/NativeLinearEquationSolver.h"
  12. #include "src/settings/modules/GeneralSettings.h"
  13. #include "src/settings/modules/NativeEquationSolverSettings.h"
  14. #include "src/settings/SettingMemento.h"
  15. #include "src/parser/AutoParser.h"
  16. TEST(SparseDtmcPrctlModelCheckerTest, Die) {
  17. 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");
  18. // A parser that we use for conveniently constructing the formulas.
  19. storm::parser::FormulaParser formulaParser;
  20. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  21. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  22. ASSERT_EQ(dtmc->getNumberOfStates(), 13ull);
  23. ASSERT_EQ(dtmc->getNumberOfTransitions(), 20ull);
  24. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  25. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("P=? [F \"one\"]");
  26. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  27. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  28. EXPECT_NEAR(1.0/6.0, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  29. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"two\"]");
  30. result = checker.check(*formula);
  31. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  32. EXPECT_NEAR(1.0/6.0, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  33. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"three\"]");
  34. result = checker.check(*formula);
  35. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  36. EXPECT_NEAR(1.0/6.0, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  37. formula = formulaParser.parseSingleFormulaFromString("R=? [F \"done\"]");
  38. result = checker.check(*formula);
  39. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult4 = result->asExplicitQuantitativeCheckResult<double>();
  40. EXPECT_NEAR(3.6666650772094727, quantitativeResult4[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  41. }
  42. TEST(SparseDtmcPrctlModelCheckerTest, Crowds) {
  43. 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", "", "");
  44. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  45. // A parser that we use for conveniently constructing the formulas.
  46. storm::parser::FormulaParser formulaParser;
  47. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  48. ASSERT_EQ(8607ull, dtmc->getNumberOfStates());
  49. ASSERT_EQ(15113ull, dtmc->getNumberOfTransitions());
  50. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  51. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("P=? [F \"observe0Greater1\"]");
  52. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  53. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  54. EXPECT_NEAR(0.33288205191646525, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  55. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"observeIGreater1\"]");
  56. result = checker.check(*formula);
  57. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  58. EXPECT_NEAR(0.15222066094730619, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  59. formula = formulaParser.parseSingleFormulaFromString("P=? [F \"observeOnlyTrueSender\"]");
  60. result = checker.check(*formula);
  61. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult3 = result->asExplicitQuantitativeCheckResult<double>();
  62. EXPECT_NEAR(0.32153900158185761, quantitativeResult3[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  63. }
  64. TEST(SparseDtmcPrctlModelCheckerTest, SynchronousLeader) {
  65. 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");
  66. ASSERT_EQ(abstractModel->getType(), storm::models::ModelType::Dtmc);
  67. // A parser that we use for conveniently constructing the formulas.
  68. storm::parser::FormulaParser formulaParser;
  69. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc = abstractModel->as<storm::models::sparse::Dtmc<double>>();
  70. ASSERT_EQ(12400ull, dtmc->getNumberOfStates());
  71. ASSERT_EQ(16495ull, dtmc->getNumberOfTransitions());
  72. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::unique_ptr<storm::utility::solver::LinearEquationSolverFactory<double>>(new storm::utility::solver::NativeLinearEquationSolverFactory<double>()));
  73. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("P=? [F \"elected\"]");
  74. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  75. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  76. EXPECT_NEAR(1.0, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  77. formula = formulaParser.parseSingleFormulaFromString("P=? [F<=20 \"elected\"]");
  78. result = checker.check(*formula);
  79. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult2 = result->asExplicitQuantitativeCheckResult<double>();
  80. EXPECT_NEAR(0.9999965911265462636, quantitativeResult2[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  81. formula = formulaParser.parseSingleFormulaFromString("R=? [F \"elected\"]");
  82. result = checker.check(*formula);
  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. // A parser that we use for conveniently constructing the formulas.
  90. storm::parser::FormulaParser formulaParser;
  91. {
  92. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(2, 2, 2);
  93. matrixBuilder.addNextValue(0, 1, 1.);
  94. matrixBuilder.addNextValue(1, 0, 1.);
  95. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  96. storm::models::sparse::StateLabeling ap(2);
  97. ap.addLabel("a");
  98. ap.addLabelToState("a", 1);
  99. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  100. 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::NativeLinearEquationSolverSolutionMethod::SOR, 0.9)));
  101. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  102. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  103. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  104. EXPECT_NEAR(.5, quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  105. EXPECT_NEAR(.5, quantitativeResult1[1], storm::settings::nativeEquationSolverSettings().getPrecision());
  106. }
  107. {
  108. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(2, 2, 4);
  109. matrixBuilder.addNextValue(0, 0, .5);
  110. matrixBuilder.addNextValue(0, 1, .5);
  111. matrixBuilder.addNextValue(1, 0, .5);
  112. matrixBuilder.addNextValue(1, 1, .5);
  113. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  114. storm::models::sparse::StateLabeling ap(2);
  115. ap.addLabel("a");
  116. ap.addLabelToState("a", 1);
  117. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  118. 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::NativeLinearEquationSolverSolutionMethod::SOR, 0.9)));
  119. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  120. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  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));
  135. 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::NativeLinearEquationSolverSolutionMethod::SOR, 0.9)));
  136. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  137. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  138. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  139. EXPECT_NEAR(1. / 3., quantitativeResult1[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  140. EXPECT_NEAR(1. / 3., quantitativeResult1[1], storm::settings::nativeEquationSolverSettings().getPrecision());
  141. EXPECT_NEAR(1. / 3., quantitativeResult1[2], storm::settings::nativeEquationSolverSettings().getPrecision());
  142. }
  143. }
  144. TEST(SparseDtmcPrctlModelCheckerTest, LRA) {
  145. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  146. std::shared_ptr<storm::models::sparse::Dtmc<double>> mdp;
  147. // A parser that we use for conveniently constructing the formulas.
  148. storm::parser::FormulaParser formulaParser;
  149. {
  150. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(15, 15, 20, true);
  151. matrixBuilder.addNextValue(0, 1, 1);
  152. matrixBuilder.addNextValue(1, 4, 0.7);
  153. matrixBuilder.addNextValue(1, 6, 0.3);
  154. matrixBuilder.addNextValue(2, 0, 1);
  155. matrixBuilder.addNextValue(3, 5, 0.8);
  156. matrixBuilder.addNextValue(3, 9, 0.2);
  157. matrixBuilder.addNextValue(4, 3, 1);
  158. matrixBuilder.addNextValue(5, 3, 1);
  159. matrixBuilder.addNextValue(6, 7, 1);
  160. matrixBuilder.addNextValue(7, 8, 1);
  161. matrixBuilder.addNextValue(8, 6, 1);
  162. matrixBuilder.addNextValue(9, 10, 1);
  163. matrixBuilder.addNextValue(10, 9, 1);
  164. matrixBuilder.addNextValue(11, 9, 1);
  165. matrixBuilder.addNextValue(12, 5, 0.4);
  166. matrixBuilder.addNextValue(12, 8, 0.3);
  167. matrixBuilder.addNextValue(12, 11, 0.3);
  168. matrixBuilder.addNextValue(13, 7, 0.7);
  169. matrixBuilder.addNextValue(13, 12, 0.3);
  170. matrixBuilder.addNextValue(14, 12, 1);
  171. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  172. storm::models::sparse::StateLabeling ap(15);
  173. ap.addLabel("a");
  174. ap.addLabelToState("a", 1);
  175. ap.addLabelToState("a", 4);
  176. ap.addLabelToState("a", 5);
  177. ap.addLabelToState("a", 7);
  178. ap.addLabelToState("a", 11);
  179. ap.addLabelToState("a", 13);
  180. ap.addLabelToState("a", 14);
  181. mdp.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  182. 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::NativeLinearEquationSolverSolutionMethod::SOR, 0.9)));
  183. std::shared_ptr<storm::logic::Formula> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  184. std::unique_ptr<storm::modelchecker::CheckResult> result = std::move(checker.check(*formula));
  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. }