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.

294 lines
16 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/solver/NativeLinearEquationSolver.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(NativeDtmcPrctlModelCheckerTest, 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::make_unique<storm::solver::NativeLinearEquationSolverFactory<double>>());
  25. std::shared_ptr<storm::logic::Formula const> 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::getModule<storm::settings::modules::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::getModule<storm::settings::modules::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::getModule<storm::settings::modules::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::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  41. }
  42. TEST(NativeDtmcPrctlModelCheckerTest, 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::make_unique<storm::solver::NativeLinearEquationSolverFactory<double>>());
  51. std::shared_ptr<storm::logic::Formula const> 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::getModule<storm::settings::modules::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::getModule<storm::settings::modules::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::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  63. }
  64. TEST(NativeDtmcPrctlModelCheckerTest, 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::make_unique<storm::solver::NativeLinearEquationSolverFactory<double>>());
  73. std::shared_ptr<storm::logic::Formula const> 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::getModule<storm::settings::modules::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::getModule<storm::settings::modules::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::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  85. }
  86. TEST(NativeDtmcPrctlModelCheckerTest, 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. auto factory = std::make_unique<storm::solver::NativeLinearEquationSolverFactory<double>>();
  101. factory->getSettings().setSolutionMethod(storm::solver::NativeLinearEquationSolverSettings<double>::SolutionMethod::SOR);
  102. factory->getSettings().setOmega(0.9);
  103. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::move(factory));
  104. std::shared_ptr<storm::logic::Formula const> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  105. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  106. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  107. EXPECT_NEAR(.5, quantitativeResult1[0], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  108. EXPECT_NEAR(.5, quantitativeResult1[1], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  109. }
  110. {
  111. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(2, 2, 4);
  112. matrixBuilder.addNextValue(0, 0, .5);
  113. matrixBuilder.addNextValue(0, 1, .5);
  114. matrixBuilder.addNextValue(1, 0, .5);
  115. matrixBuilder.addNextValue(1, 1, .5);
  116. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  117. storm::models::sparse::StateLabeling ap(2);
  118. ap.addLabel("a");
  119. ap.addLabelToState("a", 1);
  120. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  121. auto factory = std::make_unique<storm::solver::NativeLinearEquationSolverFactory<double>>();
  122. factory->getSettings().setSolutionMethod(storm::solver::NativeLinearEquationSolverSettings<double>::SolutionMethod::SOR);
  123. factory->getSettings().setOmega(0.9);
  124. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::move(factory));
  125. std::shared_ptr<storm::logic::Formula const> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  126. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  127. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  128. EXPECT_NEAR(.5, quantitativeResult1[0], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  129. EXPECT_NEAR(.5, quantitativeResult1[1], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  130. }
  131. {
  132. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(3, 3, 3);
  133. matrixBuilder.addNextValue(0, 1, 1);
  134. matrixBuilder.addNextValue(1, 2, 1);
  135. matrixBuilder.addNextValue(2, 0, 1);
  136. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  137. storm::models::sparse::StateLabeling ap(3);
  138. ap.addLabel("a");
  139. ap.addLabelToState("a", 2);
  140. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  141. auto factory = std::make_unique<storm::solver::NativeLinearEquationSolverFactory<double>>();
  142. factory->getSettings().setSolutionMethod(storm::solver::NativeLinearEquationSolverSettings<double>::SolutionMethod::SOR);
  143. factory->getSettings().setOmega(0.9);
  144. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::move(factory));
  145. std::shared_ptr<storm::logic::Formula const> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  146. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  147. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  148. EXPECT_NEAR(1. / 3., quantitativeResult1[0], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  149. EXPECT_NEAR(1. / 3., quantitativeResult1[1], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  150. EXPECT_NEAR(1. / 3., quantitativeResult1[2], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  151. }
  152. }
  153. TEST(NativeDtmcPrctlModelCheckerTest, LRA) {
  154. storm::storage::SparseMatrixBuilder<double> matrixBuilder;
  155. std::shared_ptr<storm::models::sparse::Dtmc<double>> dtmc;
  156. // A parser that we use for conveniently constructing the formulas.
  157. storm::parser::FormulaParser formulaParser;
  158. {
  159. matrixBuilder = storm::storage::SparseMatrixBuilder<double>(15, 15, 20, true);
  160. matrixBuilder.addNextValue(0, 1, 1);
  161. matrixBuilder.addNextValue(1, 4, 0.7);
  162. matrixBuilder.addNextValue(1, 6, 0.3);
  163. matrixBuilder.addNextValue(2, 0, 1);
  164. matrixBuilder.addNextValue(3, 5, 0.8);
  165. matrixBuilder.addNextValue(3, 9, 0.2);
  166. matrixBuilder.addNextValue(4, 3, 1);
  167. matrixBuilder.addNextValue(5, 3, 1);
  168. matrixBuilder.addNextValue(6, 7, 1);
  169. matrixBuilder.addNextValue(7, 8, 1);
  170. matrixBuilder.addNextValue(8, 6, 1);
  171. matrixBuilder.addNextValue(9, 10, 1);
  172. matrixBuilder.addNextValue(10, 9, 1);
  173. matrixBuilder.addNextValue(11, 9, 1);
  174. matrixBuilder.addNextValue(12, 5, 0.4);
  175. matrixBuilder.addNextValue(12, 8, 0.3);
  176. matrixBuilder.addNextValue(12, 11, 0.3);
  177. matrixBuilder.addNextValue(13, 7, 0.7);
  178. matrixBuilder.addNextValue(13, 12, 0.3);
  179. matrixBuilder.addNextValue(14, 12, 1);
  180. storm::storage::SparseMatrix<double> transitionMatrix = matrixBuilder.build();
  181. storm::models::sparse::StateLabeling ap(15);
  182. ap.addLabel("a");
  183. ap.addLabelToState("a", 1);
  184. ap.addLabelToState("a", 4);
  185. ap.addLabelToState("a", 5);
  186. ap.addLabelToState("a", 7);
  187. ap.addLabelToState("a", 11);
  188. ap.addLabelToState("a", 13);
  189. ap.addLabelToState("a", 14);
  190. dtmc.reset(new storm::models::sparse::Dtmc<double>(transitionMatrix, ap));
  191. auto factory = std::make_unique<storm::solver::NativeLinearEquationSolverFactory<double>>();
  192. factory->getSettings().setSolutionMethod(storm::solver::NativeLinearEquationSolverSettings<double>::SolutionMethod::SOR);
  193. factory->getSettings().setOmega(0.9);
  194. storm::modelchecker::SparseDtmcPrctlModelChecker<storm::models::sparse::Dtmc<double>> checker(*dtmc, std::move(factory));
  195. std::shared_ptr<storm::logic::Formula const> formula = formulaParser.parseSingleFormulaFromString("LRA=? [\"a\"]");
  196. std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(*formula);
  197. storm::modelchecker::ExplicitQuantitativeCheckResult<double>& quantitativeResult1 = result->asExplicitQuantitativeCheckResult<double>();
  198. EXPECT_NEAR(0.3 / 3., quantitativeResult1[0], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  199. EXPECT_NEAR(0.0, quantitativeResult1[3], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  200. EXPECT_NEAR(1. / 3., quantitativeResult1[6], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  201. EXPECT_NEAR(0.0, quantitativeResult1[9], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  202. EXPECT_NEAR(0.3/3., quantitativeResult1[12], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  203. EXPECT_NEAR(.79 / 3., quantitativeResult1[13], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  204. EXPECT_NEAR(0.3 / 3., quantitativeResult1[14], storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  205. }
  206. }