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/logic/Formulas.h"
  4. #include "src/solver/NativeNondeterministicLinearEquationSolver.h"
  5. #include "src/modelchecker/prctl/TopologicalValueIterationMdpPrctlModelChecker.h"
  6. #include "src/modelchecker/prctl/SparseMdpPrctlModelChecker.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. #include "storm-config.h"
  12. TEST(TopologicalValueIterationMdpPrctlModelCheckerTest, Dice) {
  13. //storm::settings::Settings* s = storm::settings::Settings::getInstance();
  14. std::shared_ptr<storm::models::Mdp<double>> mdp = storm::parser::AutoParser::parseModel(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")->as<storm::models::Mdp<double>>();
  15. ASSERT_EQ(mdp->getNumberOfStates(), 169ull);
  16. ASSERT_EQ(mdp->getNumberOfTransitions(), 436ull);
  17. storm::modelchecker::TopologicalValueIterationMdpPrctlModelChecker<double> mc(*mdp);
  18. //storm::property::prctl::Ap<double>* apFormula = new storm::property::prctl::Ap<double>("two");
  19. auto apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("two");
  20. //storm::property::prctl::Eventually<double>* eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  21. auto eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  22. //storm::property::prctl::ProbabilisticNoBoundOperator<double>* probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, true);
  23. auto probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Minimize, eventuallyFormula);
  24. //std::vector<double> result = mc.checkNoBoundOperator(*probFormula);
  25. std::unique_ptr<storm::modelchecker::CheckResult> result = mc.check(*probabilityOperatorFormula);
  26. //ASSERT_LT(std::abs(result[0] - 0.0277777612209320068), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  27. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.0277777612209320068),
  28. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  29. //delete probFormula;
  30. probabilityOperatorFormula.reset();
  31. //apFormula = new storm::property::prctl::Ap<double>("two");
  32. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("two");
  33. //eventuallyFormula = new storm::property::prctl::Eventually<double>(apFormula);
  34. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  35. //probFormula = new storm::property::prctl::ProbabilisticNoBoundOperator<double>(eventuallyFormula, false);
  36. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Maximize, eventuallyFormula);
  37. //result = mc.checkNoBoundOperator(*probFormula);
  38. result = mc.check(*probabilityOperatorFormula);
  39. //ASSERT_LT(std::abs(result[0] - 0.0277777612209320068), s->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  40. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.0277777612209320068),
  41. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  42. //delete probFormula;
  43. probabilityOperatorFormula.reset();
  44. // ---------------- test ap "three" ----------------
  45. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("three");
  46. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  47. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Minimize, eventuallyFormula);
  48. result = mc.check(*probabilityOperatorFormula);
  49. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.0555555224418640136),
  50. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  51. probabilityOperatorFormula.reset();
  52. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("three");
  53. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  54. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Maximize, eventuallyFormula);
  55. result = mc.check(*probabilityOperatorFormula);
  56. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.0555555224418640136),
  57. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  58. probabilityOperatorFormula.reset();
  59. // ---------------- test ap "four" ----------------
  60. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("four");
  61. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  62. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Minimize, eventuallyFormula);
  63. result = mc.check(*probabilityOperatorFormula);
  64. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.083333283662796020508),
  65. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  66. probabilityOperatorFormula.reset();
  67. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("four");
  68. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  69. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Maximize, eventuallyFormula);
  70. result = mc.check(*probabilityOperatorFormula);
  71. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.083333283662796020508),
  72. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  73. probabilityOperatorFormula.reset();
  74. // ---------------- test ap "done" ----------------
  75. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("done");
  76. auto reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  77. auto rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Minimize, reachabilityRewardFormula);
  78. result = mc.check(*rewardFormula);
  79. #ifdef STORM_HAVE_CUDA
  80. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.333329499),
  81. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  82. #else
  83. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.33332904),
  84. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  85. #endif
  86. rewardFormula.reset();
  87. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("done");
  88. reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  89. rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Maximize, reachabilityRewardFormula);
  90. result = mc.check(*rewardFormula);
  91. #ifdef STORM_HAVE_CUDA
  92. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.333329499),
  93. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  94. #else
  95. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.33333151),
  96. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  97. #endif
  98. rewardFormula.reset();
  99. // ------------- state rewards --------------
  100. std::shared_ptr<storm::models::Mdp<double>> stateRewardMdp = storm::parser::AutoParser::parseModel(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", "")->as<storm::models::Mdp<double>>();
  101. storm::modelchecker::TopologicalValueIterationMdpPrctlModelChecker<double> stateRewardModelChecker(*stateRewardMdp);
  102. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("done");
  103. reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  104. rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Minimize, reachabilityRewardFormula);
  105. result = stateRewardModelChecker.check(*rewardFormula);
  106. #ifdef STORM_HAVE_CUDA
  107. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.333329499),
  108. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  109. #else
  110. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.33332904),
  111. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  112. #endif
  113. rewardFormula.reset();
  114. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("done");
  115. reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  116. rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Maximize, reachabilityRewardFormula);
  117. result = stateRewardModelChecker.check(*rewardFormula);
  118. #ifdef STORM_HAVE_CUDA
  119. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.333329499),
  120. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  121. #else
  122. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 7.33333151),
  123. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  124. #endif
  125. rewardFormula.reset();
  126. // -------------------------------- state and transition reward ------------------------
  127. std::shared_ptr<storm::models::Mdp<double>> stateAndTransitionRewardMdp = storm::parser::AutoParser::parseModel(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")->as<storm::models::Mdp<double>>();
  128. storm::modelchecker::TopologicalValueIterationMdpPrctlModelChecker<double> stateAndTransitionRewardModelChecker(*stateAndTransitionRewardMdp);
  129. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("done");
  130. reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  131. rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Minimize, reachabilityRewardFormula);
  132. result = stateAndTransitionRewardModelChecker.check(*rewardFormula);
  133. #ifdef STORM_HAVE_CUDA
  134. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 14.666658998),
  135. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  136. #else
  137. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 14.6666581),
  138. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  139. #endif
  140. rewardFormula.reset();
  141. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("done");
  142. reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  143. rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Maximize, reachabilityRewardFormula);
  144. result = stateAndTransitionRewardModelChecker.check(*rewardFormula);
  145. #ifdef STORM_HAVE_CUDA
  146. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 14.666658998),
  147. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  148. #else
  149. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 14.666663),
  150. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  151. #endif
  152. rewardFormula.reset();
  153. }
  154. TEST(TopologicalValueIterationMdpPrctlModelCheckerTest, AsynchronousLeader) {
  155. std::shared_ptr<storm::models::Mdp<double>> mdp = storm::parser::AutoParser::parseModel(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")->as<storm::models::Mdp<double>>();
  156. ASSERT_EQ(mdp->getNumberOfStates(), 3172ull);
  157. ASSERT_EQ(mdp->getNumberOfTransitions(), 7144ull);
  158. storm::modelchecker::TopologicalValueIterationMdpPrctlModelChecker<double> mc(*mdp);
  159. auto apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  160. auto eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  161. auto probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Minimize, eventuallyFormula);
  162. std::unique_ptr<storm::modelchecker::CheckResult> result = mc.check(*probabilityOperatorFormula);
  163. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 1),
  164. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  165. probabilityOperatorFormula.reset();
  166. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  167. eventuallyFormula = std::make_shared<storm::logic::EventuallyFormula>(apFormula);
  168. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Maximize, eventuallyFormula);
  169. result = mc.check(*probabilityOperatorFormula);
  170. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 1),
  171. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  172. probabilityOperatorFormula.reset();
  173. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  174. auto boundedEventuallyFormula = std::make_shared<storm::logic::BoundedUntilFormula>(std::make_shared<storm::logic::BooleanLiteralFormula>(true), apFormula, 25);
  175. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Maximize, boundedEventuallyFormula);
  176. result = mc.check(*probabilityOperatorFormula);
  177. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.0625),
  178. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  179. probabilityOperatorFormula.reset();
  180. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  181. boundedEventuallyFormula = std::make_shared<storm::logic::BoundedUntilFormula>(std::make_shared<storm::logic::BooleanLiteralFormula>(true), apFormula, 25);
  182. probabilityOperatorFormula = std::make_shared<storm::logic::ProbabilityOperatorFormula>(storm::logic::OptimalityType::Minimize, boundedEventuallyFormula);
  183. result = mc.check(*probabilityOperatorFormula);
  184. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 0.0625),
  185. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  186. probabilityOperatorFormula.reset();
  187. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  188. auto reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  189. auto rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Minimize, reachabilityRewardFormula);
  190. result = mc.check(*rewardFormula);
  191. #ifdef STORM_HAVE_CUDA
  192. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 4.285689611),
  193. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  194. #else
  195. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 4.285701547),
  196. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  197. #endif
  198. probabilityOperatorFormula.reset();
  199. apFormula = std::make_shared<storm::logic::AtomicLabelFormula>("elected");
  200. reachabilityRewardFormula = std::make_shared<storm::logic::ReachabilityRewardFormula>(apFormula);
  201. rewardFormula = std::make_shared<storm::logic::RewardOperatorFormula>(storm::logic::OptimalityType::Maximize, reachabilityRewardFormula);
  202. result = mc.check(*rewardFormula);
  203. #ifdef STORM_HAVE_CUDA
  204. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 4.285689611),
  205. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  206. #else
  207. ASSERT_LT(std::abs(result->asExplicitQuantitativeCheckResult<double>()[0] - 4.285703591),
  208. storm::settings::topologicalValueIterationEquationSolverSettings().getPrecision());
  209. #endif
  210. probabilityOperatorFormula.reset();
  211. }