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.

295 lines
17 KiB

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