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.

258 lines
14 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/solver/NativeNondeterministicLinearEquationSolver.h"
  4. #include "src/settings/Settings.h"
  5. #include "src/modelchecker/prctl/SparseMdpPrctlModelChecker.h"
  6. #include "src/modelchecker/prctl/TopologicalValueIterationMdpPrctlModelChecker.h"
  7. #include "src/parser/AutoParser.h"
  8. TEST(TopologicalValueIterationMdpPrctlModelCheckerTest, SmallLinEqSystem) {
  9. storm::storage::SparseMatrixBuilder<double> matrixBuilder(4, 4);
  10. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 1, 0.1));
  11. ASSERT_NO_THROW(matrixBuilder.addNextValue(0, 2, 0.9));
  12. ASSERT_NO_THROW(matrixBuilder.addNextValue(1, 1, 1.0));
  13. ASSERT_NO_THROW(matrixBuilder.addNextValue(2, 3, 1.0));
  14. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 2, 0.8));
  15. ASSERT_NO_THROW(matrixBuilder.addNextValue(3, 3, 0.2));
  16. storm::storage::SparseMatrix<double> matrix;
  17. ASSERT_NO_THROW(matrix = matrixBuilder.build());
  18. ASSERT_EQ(4, matrix.getRowCount());
  19. ASSERT_EQ(4, matrix.getColumnCount());
  20. ASSERT_EQ(6, matrix.getEntryCount());
  21. // Solve the Linear Equation System
  22. storm::solver::TopologicalValueIterationNondeterministicLinearEquationSolver<double> topoSolver;
  23. std::vector<double> x(4);
  24. std::vector<double> b = { 1, 2, 3, 4 };
  25. std::vector<uint_fast64_t> choices = { 0, 1, 2, 3, 4 };
  26. ASSERT_NO_THROW(topoSolver.solveEquationSystem(true, matrix, x, b, choices));
  27. storm::settings::Settings* s = storm::settings::Settings::getInstance();
  28. ASSERT_LT(std::abs(x.at(0) - 2.9), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  29. ASSERT_LT(std::abs(x.at(1) - 2), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  30. ASSERT_LT(std::abs(x.at(2) - 3), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  31. ASSERT_LT(std::abs(x.at(3) - 3.2), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  32. }
  33. TEST(TopologicalValueIterationMdpPrctlModelCheckerTest, Dice) {
  34. storm::settings::Settings* s = storm::settings::Settings::getInstance();
  35. storm::parser::AutoParser<double> parser(STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.tra", STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.lab", "", STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.flip.trans.rew");
  36. //storm::parser::AutoParser<double> parser(STORM_CPP_BASE_PATH "/examples/mdp/scc/scc.tra", STORM_CPP_BASE_PATH "/examples/mdp/scc/scc.lab", "");
  37. ASSERT_EQ(parser.getType(), storm::models::MDP);
  38. std::shared_ptr<storm::models::Mdp<double>> mdp = parser.getModel<storm::models::Mdp<double>>();
  39. // ASSERT_EQ(mdp->getNumberOfStates(), 11ull);
  40. // ASSERT_EQ(mdp->getNumberOfTransitions(), 18ull);
  41. storm::modelchecker::prctl::TopologicalValueIterationMdpPrctlModelChecker<double> mc(*mdp);
  42. // storm::property::prctl::Ap<double>* apFormula = new storm::property::prctl::Ap<double>("end");
  43. storm::property::prctl::Ap<double>* apFormula = new storm::property::prctl::Ap<double>("two");
  44. storm::property::prctl::Eventually<double>* eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  45. storm::property::prctl::ProbabilisticNoBoundOperator<double>* probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, true);
  46. std::vector<double> result = mc.checkNoBoundOperator(*probFormula);
  47. ASSERT_LT(std::abs(result[0] - 0.0277777612209320068), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  48. delete probFormula;
  49. /*
  50. apFormula = new storm::property::prctl::Ap<double>("two");
  51. eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  52. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, false);
  53. result = mc.checkNoBoundOperator(*probFormula);
  54. ASSERT_LT(std::abs(result[0] - 0.0277777612209320068), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  55. delete probFormula;
  56. apFormula = new storm::property::prctl::Ap<double>("three");
  57. eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  58. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, true);
  59. result = mc.checkNoBoundOperator(*probFormula);
  60. ASSERT_LT(std::abs(result[0] - 0.0555555224418640136), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  61. delete probFormula;
  62. apFormula = new storm::property::prctl::Ap<double>("three");
  63. eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  64. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, false);
  65. result = mc.checkNoBoundOperator(*probFormula);
  66. ASSERT_LT(std::abs(result[0] - 0.0555555224418640136), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  67. delete probFormula;
  68. apFormula = new storm::property::prctl::Ap<double>("four");
  69. eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  70. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, true);
  71. result = mc.checkNoBoundOperator(*probFormula);
  72. ASSERT_LT(std::abs(result[0] - 0.083333283662796020508), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  73. delete probFormula;
  74. apFormula = new storm::property::prctl::Ap<double>("four");
  75. eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  76. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, false);
  77. result = mc.checkNoBoundOperator(*probFormula);
  78. ASSERT_LT(std::abs(result[0] - 0.083333283662796020508), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  79. delete probFormula;
  80. apFormula = new storm::property::prctl::Ap<double>("done");
  81. storm::property::prctl::ReachabilityReward<double>* reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  82. storm::property::prctl::RewardNoBoundOperator<double>* rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, true);
  83. result = mc.checkNoBoundOperator(*rewardFormula);
  84. ASSERT_LT(std::abs(result[0] - 7.333329499), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  85. delete rewardFormula;
  86. apFormula = new storm::property::prctl::Ap<double>("done");
  87. reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  88. rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, false);
  89. result = mc.checkNoBoundOperator(*rewardFormula);;
  90. ASSERT_LT(std::abs(result[0] - 7.333329499), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  91. delete rewardFormula;
  92. storm::parser::AutoParser<double> stateRewardParser(STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.tra", STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.lab", STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.flip.state.rew", "");
  93. ASSERT_EQ(stateRewardParser.getType(), storm::models::MDP);
  94. std::shared_ptr<storm::models::Mdp<double>> stateRewardMdp = stateRewardParser.getModel<storm::models::Mdp<double>>();
  95. storm::modelchecker::prctl::SparseMdpPrctlModelChecker<double> stateRewardModelChecker(*stateRewardMdp, std::shared_ptr<storm::solver::NativeNondeterministicLinearEquationSolver<double>>(new storm::solver::NativeNondeterministicLinearEquationSolver<double>()));
  96. apFormula = new storm::property::prctl::Ap<double>("done");
  97. reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  98. rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, true);
  99. result = stateRewardModelChecker.checkNoBoundOperator(*rewardFormula);
  100. ASSERT_LT(std::abs(result[0] - 7.333329499), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  101. delete rewardFormula;
  102. apFormula = new storm::property::prctl::Ap<double>("done");
  103. reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  104. rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, false);
  105. result = stateRewardModelChecker.checkNoBoundOperator(*rewardFormula);
  106. ASSERT_LT(std::abs(result[0] - 7.333329499), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  107. delete rewardFormula;
  108. storm::parser::AutoParser<double> stateAndTransitionRewardParser(STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.tra", STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.lab", STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.flip.state.rew", STORM_CPP_BASE_PATH "/examples/mdp/two_dice/two_dice.flip.trans.rew");
  109. ASSERT_EQ(stateAndTransitionRewardParser.getType(), storm::models::MDP);
  110. std::shared_ptr<storm::models::Mdp<double>> stateAndTransitionRewardMdp = stateAndTransitionRewardParser.getModel<storm::models::Mdp<double>>();
  111. storm::modelchecker::prctl::SparseMdpPrctlModelChecker<double> stateAndTransitionRewardModelChecker(*stateAndTransitionRewardMdp, std::shared_ptr<storm::solver::NativeNondeterministicLinearEquationSolver<double>>(new storm::solver::NativeNondeterministicLinearEquationSolver<double>()));
  112. apFormula = new storm::property::prctl::Ap<double>("done");
  113. reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  114. rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, true);
  115. result = stateAndTransitionRewardModelChecker.checkNoBoundOperator(*rewardFormula);
  116. ASSERT_LT(std::abs(result[0] - 14.666658998), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  117. delete rewardFormula;
  118. apFormula = new storm::property::prctl::Ap<double>("done");
  119. reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  120. rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, false);
  121. result = stateAndTransitionRewardModelChecker.checkNoBoundOperator(*rewardFormula);
  122. ASSERT_LT(std::abs(result[0] - 14.666658998), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  123. delete rewardFormula;*/
  124. }
  125. TEST(TopologicalValueIterationMdpPrctlModelCheckerTest, AsynchronousLeader) {
  126. /*storm::settings::Settings* s = storm::settings::Settings::getInstance();
  127. storm::parser::AutoParser<double> parser(STORM_CPP_BASE_PATH "/examples/mdp/asynchronous_leader/leader4.tra", STORM_CPP_BASE_PATH "/examples/mdp/asynchronous_leader/leader4.lab", "", STORM_CPP_BASE_PATH "/examples/mdp/asynchronous_leader/leader4.trans.rew");
  128. ASSERT_EQ(parser.getType(), storm::models::MDP);
  129. std::shared_ptr<storm::models::Mdp<double>> mdp = parser.getModel<storm::models::Mdp<double>>();
  130. ASSERT_EQ(mdp->getNumberOfStates(), 3172ull);
  131. ASSERT_EQ(mdp->getNumberOfTransitions(), 7144ull);
  132. storm::modelchecker::prctl::TopologicalValueIterationMdpPrctlModelChecker<double> mc(*mdp);
  133. storm::property::prctl::Ap<double>* apFormula = new storm::property::prctl::Ap<double>("elected");
  134. storm::property::prctl::Eventually<double>* eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  135. storm::property::prctl::ProbabilisticNoBoundOperator<double>* probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, true);
  136. std::vector<double> result = mc.checkNoBoundOperator(*probFormula);
  137. ASSERT_LT(std::abs(result[0] - 1), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  138. delete probFormula;
  139. apFormula = new storm::property::prctl::Ap<double>("elected");
  140. eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  141. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, false);
  142. result = mc.checkNoBoundOperator(*probFormula);
  143. ASSERT_LT(std::abs(result[0] - 1), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  144. delete probFormula;
  145. apFormula = new storm::property::prctl::Ap<double>("elected");
  146. storm::property::prctl::BoundedEventually<double>* boundedEventuallyFormula = new storm::property::prctl::BoundedEventually<double>(apFormula, 25);
  147. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(boundedEventuallyFormula, false);
  148. result = mc.checkNoBoundOperator(*probFormula);
  149. ASSERT_LT(std::abs(result[0] - 0.0625), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  150. delete probFormula;
  151. apFormula = new storm::property::prctl::Ap<double>("elected");
  152. boundedEventuallyFormula = new storm::property::prctl::BoundedEventually<double>(apFormula, 25);
  153. probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(boundedEventuallyFormula, true);
  154. result = mc.checkNoBoundOperator(*probFormula);
  155. ASSERT_LT(std::abs(result[0] - 0.0625), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  156. delete probFormula;
  157. apFormula = new storm::property::prctl::Ap<double>("elected");
  158. storm::property::prctl::ReachabilityReward<double>* reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  159. storm::property::prctl::RewardNoBoundOperator<double>* rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, true);
  160. result = mc.checkNoBoundOperator(*rewardFormula);;
  161. ASSERT_LT(std::abs(result[0] - 4.285689611), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  162. delete rewardFormula;
  163. apFormula = new storm::property::prctl::Ap<double>("elected");
  164. reachabilityRewardFormula = new storm::property::prctl::ReachabilityReward<double>(apFormula);
  165. rewardFormula = new storm::property::prctl::RewardNoBoundOperator<double>(reachabilityRewardFormula, false);
  166. result = mc.checkNoBoundOperator(*rewardFormula);;
  167. ASSERT_LT(std::abs(result[0] - 4.285689611), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  168. delete rewardFormula;*/
  169. }