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.

140 lines
7.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. import stormpy
  2. import stormpy.logic
  3. from helpers.helper import get_example_path
  4. import math
  5. class TestModelChecking:
  6. def test_model_checking_prism_dtmc(self):
  7. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  8. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  9. model = stormpy.build_model(program, formulas)
  10. assert model.nr_states == 13
  11. assert model.nr_transitions == 20
  12. assert len(model.initial_states) == 1
  13. initial_state = model.initial_states[0]
  14. assert initial_state == 0
  15. result = stormpy.model_checking(model, formulas[0])
  16. assert math.isclose(result.at(initial_state), 1 / 6)
  17. def test_model_checking_prism_mdp(self):
  18. program = stormpy.parse_prism_program(get_example_path("mdp", "coin2-2.nm"))
  19. formulas = stormpy.parse_properties_for_prism_program("Pmin=? [ F \"finished\" & \"all_coins_equal_1\"]", program)
  20. model = stormpy.build_model(program, formulas)
  21. assert model.nr_states == 272
  22. assert model.nr_transitions == 492
  23. assert len(model.initial_states) == 1
  24. initial_state = model.initial_states[0]
  25. assert initial_state == 0
  26. result = stormpy.model_checking(model, formulas[0])
  27. assert math.isclose(result.at(initial_state), 49 / 128, rel_tol=1e-5)
  28. def test_model_checking_jani_dtmc(self):
  29. jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani"))
  30. formulas = [properties["Probability to throw a six"], properties["Expected number of coin flips"]]
  31. formulas = stormpy.eliminate_reward_accumulations(jani_model, formulas)
  32. assert len(formulas) == 2
  33. model = stormpy.build_model(jani_model, formulas)
  34. assert model.nr_states == 13
  35. assert model.nr_transitions == 20
  36. assert len(model.initial_states) == 1
  37. initial_state = model.initial_states[0]
  38. assert initial_state == 0
  39. result = stormpy.model_checking(model, formulas[0])
  40. assert math.isclose(result.at(initial_state), 1 / 6)
  41. result = stormpy.model_checking(model, formulas[1])
  42. assert math.isclose(result.at(initial_state), 11 / 3)
  43. def test_model_checking_dtmc_all_labels(self):
  44. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  45. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  46. model = stormpy.build_model(program)
  47. assert model.nr_states == 13
  48. assert model.nr_transitions == 20
  49. assert len(model.initial_states) == 1
  50. initial_state = model.initial_states[0]
  51. assert initial_state == 0
  52. result = stormpy.model_checking(model, formulas[0])
  53. assert math.isclose(result.at(initial_state), 1 / 6)
  54. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"two\" ]", program)
  55. result = stormpy.model_checking(model, formulas[0])
  56. assert math.isclose(result.at(initial_state), 1 / 6)
  57. def test_model_checking_all_dtmc(self):
  58. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  59. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  60. model = stormpy.build_model(program, formulas)
  61. assert model.nr_states == 13
  62. assert model.nr_transitions == 20
  63. result = stormpy.model_checking(model, formulas[0])
  64. assert result.result_for_all_states
  65. reference = [1 / 6, 1 / 3, 0, 2 / 3, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  66. assert all(map(math.isclose, result.get_values(), reference))
  67. def test_model_checking_only_initial(self):
  68. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  69. formulas = stormpy.parse_properties_for_prism_program("Pmax=? [F{\"coin_flips\"}<=3 \"one\"]", program)
  70. model = stormpy.build_model(program, formulas)
  71. assert len(model.initial_states) == 1
  72. initial_state = model.initial_states[0]
  73. assert initial_state == 0
  74. result = stormpy.model_checking(model, formulas[0], only_initial_states=True)
  75. assert not result.result_for_all_states
  76. assert math.isclose(result.at(initial_state), 1 / 8)
  77. def test_model_checking_prob01(self):
  78. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  79. formulaPhi = stormpy.parse_properties("true")[0]
  80. formulaPsi = stormpy.parse_properties("\"six\"")[0]
  81. model = stormpy.build_model(program, [formulaPsi])
  82. phiResult = stormpy.model_checking(model, formulaPhi)
  83. phiStates = phiResult.get_truth_values()
  84. assert phiStates.number_of_set_bits() == model.nr_states
  85. psiResult = stormpy.model_checking(model, formulaPsi)
  86. psiStates = psiResult.get_truth_values()
  87. assert psiStates.number_of_set_bits() == 1
  88. (prob0, prob1) = stormpy.compute_prob01_states(model, phiStates, psiStates)
  89. assert prob0.number_of_set_bits() == 9
  90. assert prob1.number_of_set_bits() == 1
  91. (prob0, prob1) = stormpy.compute_prob01min_states(model, phiStates, psiStates)
  92. assert prob0.number_of_set_bits() == 9
  93. assert prob1.number_of_set_bits() == 1
  94. (prob0, prob1) = stormpy.compute_prob01max_states(model, phiStates, psiStates)
  95. assert prob0.number_of_set_bits() == 9
  96. assert prob1.number_of_set_bits() == 1
  97. labelprop = stormpy.core.Property("cora", formulaPsi.raw_formula)
  98. result = stormpy.model_checking(model, labelprop)
  99. assert result.get_truth_values().number_of_set_bits() == 1
  100. def test_model_checking_ctmc(self):
  101. model = stormpy.build_model_from_drn(get_example_path("ctmc", "dft.drn"))
  102. formulas = stormpy.parse_properties("T=? [ F \"failed\" ]")
  103. assert model.nr_states == 16
  104. assert model.nr_transitions == 33
  105. assert len(model.initial_states) == 1
  106. initial_state = model.initial_states[0]
  107. assert initial_state == 1
  108. result = stormpy.model_checking(model, formulas[0])
  109. assert math.isclose(result.at(initial_state), 4.166666667)
  110. def test_model_checking_prism_dd_dtmc(self):
  111. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  112. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  113. model = stormpy.build_symbolic_model(program, formulas)
  114. assert model.nr_states == 13
  115. assert model.nr_transitions == 20
  116. result = stormpy.check_model_dd(model, formulas[0])
  117. assert type(result) is stormpy.SymbolicQuantitativeCheckResult
  118. def test_model_checking_prism_hybrid_dtmc(self):
  119. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  120. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  121. model = stormpy.build_symbolic_model(program, formulas)
  122. assert model.nr_states == 13
  123. assert model.nr_transitions == 20
  124. result = stormpy.check_model_hybrid(model, formulas[0])
  125. assert type(result) is stormpy.HybridQuantitativeCheckResult
  126. values = result.get_values()
  127. assert len(values) == 3
  128. assert math.isclose(values[0], 1 / 6)