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.

159 lines
8.0 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, formulas = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani"))
  30. formulas = stormpy.eliminate_reward_accumulations(jani_model, formulas)
  31. assert len(formulas) == 2
  32. model = stormpy.build_model(jani_model, formulas)
  33. assert model.nr_states == 13
  34. assert model.nr_transitions == 20
  35. assert len(model.initial_states) == 1
  36. initial_state = model.initial_states[0]
  37. assert initial_state == 0
  38. result = stormpy.model_checking(model, formulas[0])
  39. assert math.isclose(result.at(initial_state), 1 / 6)
  40. result = stormpy.model_checking(model, formulas[1])
  41. assert math.isclose(result.at(initial_state), 11 / 3)
  42. def test_model_checking_dtmc_all_labels(self):
  43. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  44. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  45. model = stormpy.build_model(program)
  46. assert model.nr_states == 13
  47. assert model.nr_transitions == 20
  48. assert len(model.initial_states) == 1
  49. initial_state = model.initial_states[0]
  50. assert initial_state == 0
  51. result = stormpy.model_checking(model, formulas[0])
  52. assert math.isclose(result.at(initial_state), 1 / 6)
  53. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"two\" ]", program)
  54. result = stormpy.model_checking(model, formulas[0])
  55. assert math.isclose(result.at(initial_state), 1 / 6)
  56. def test_model_checking_all_dtmc(self):
  57. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  58. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  59. model = stormpy.build_model(program, formulas)
  60. assert model.nr_states == 13
  61. assert model.nr_transitions == 20
  62. result = stormpy.model_checking(model, formulas[0])
  63. assert result.result_for_all_states
  64. reference = [1 / 6, 1 / 3, 0, 2 / 3, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  65. assert all(map(math.isclose, result.get_values(), reference))
  66. def test_model_checking_only_initial(self):
  67. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  68. formulas = stormpy.parse_properties_for_prism_program("Pmax=? [F{\"coin_flips\"}<=3 \"one\"]", program)
  69. model = stormpy.build_model(program, formulas)
  70. assert len(model.initial_states) == 1
  71. initial_state = model.initial_states[0]
  72. assert initial_state == 0
  73. result = stormpy.model_checking(model, formulas[0], only_initial_states=True)
  74. assert not result.result_for_all_states
  75. assert math.isclose(result.at(initial_state), 1 / 8)
  76. def test_model_checking_prob01(self):
  77. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  78. formulaPhi = stormpy.parse_properties("true")[0]
  79. formulaPsi = stormpy.parse_properties("\"six\"")[0]
  80. model = stormpy.build_model(program, [formulaPsi])
  81. phiResult = stormpy.model_checking(model, formulaPhi)
  82. phiStates = phiResult.get_truth_values()
  83. assert phiStates.number_of_set_bits() == model.nr_states
  84. psiResult = stormpy.model_checking(model, formulaPsi)
  85. psiStates = psiResult.get_truth_values()
  86. assert psiStates.number_of_set_bits() == 1
  87. (prob0, prob1) = stormpy.compute_prob01_states(model, phiStates, psiStates)
  88. assert prob0.number_of_set_bits() == 9
  89. assert prob1.number_of_set_bits() == 1
  90. (prob0, prob1) = stormpy.compute_prob01min_states(model, phiStates, psiStates)
  91. assert prob0.number_of_set_bits() == 9
  92. assert prob1.number_of_set_bits() == 1
  93. (prob0, prob1) = stormpy.compute_prob01max_states(model, phiStates, psiStates)
  94. assert prob0.number_of_set_bits() == 9
  95. assert prob1.number_of_set_bits() == 1
  96. labelprop = stormpy.core.Property("cora", formulaPsi.raw_formula)
  97. result = stormpy.model_checking(model, labelprop)
  98. assert result.get_truth_values().number_of_set_bits() == 1
  99. def test_model_checking_ctmc(self):
  100. model = stormpy.build_model_from_drn(get_example_path("ctmc", "dft.drn"))
  101. formulas = stormpy.parse_properties("T=? [ F \"failed\" ]")
  102. assert model.nr_states == 16
  103. assert model.nr_transitions == 33
  104. assert len(model.initial_states) == 1
  105. initial_state = model.initial_states[0]
  106. assert initial_state == 1
  107. result = stormpy.model_checking(model, formulas[0])
  108. assert math.isclose(result.at(initial_state), 4.166666667)
  109. def test_filter(self):
  110. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  111. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  112. model = stormpy.build_model(program, formulas)
  113. assert model.nr_states == 13
  114. assert model.nr_transitions == 20
  115. assert len(model.initial_states) == 1
  116. initial_state = model.initial_states[0]
  117. assert initial_state == 0
  118. result = stormpy.model_checking(model, formulas[0])
  119. assert math.isclose(result.at(initial_state), 1 / 6)
  120. filter = stormpy.create_filter_initial_states_sparse(model)
  121. result.filter(filter)
  122. assert result.min == result.max
  123. assert math.isclose(result.min, 1 / 6)
  124. def test_model_checking_prism_dd_dtmc(self):
  125. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  126. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  127. model = stormpy.build_symbolic_model(program, formulas)
  128. assert model.nr_states == 13
  129. assert model.nr_transitions == 20
  130. result = stormpy.check_model_dd(model, formulas[0])
  131. assert type(result) is stormpy.SymbolicQuantitativeCheckResult
  132. filter = stormpy.create_filter_initial_states_symbolic(model)
  133. result.filter(filter)
  134. assert result.min == result.max
  135. assert math.isclose(result.min, 1 / 6, rel_tol=1e-6)
  136. def test_model_checking_prism_hybrid_dtmc(self):
  137. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  138. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  139. model = stormpy.build_symbolic_model(program, formulas)
  140. assert model.nr_states == 13
  141. assert model.nr_transitions == 20
  142. result = stormpy.check_model_hybrid(model, formulas[0])
  143. assert type(result) is stormpy.HybridQuantitativeCheckResult
  144. values = result.get_values()
  145. assert len(values) == 3
  146. assert math.isclose(values[0], 1 / 6)