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.

129 lines
6.4 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), 0.16666666666666663)
  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), 0.3828117384)
  28. def test_model_checking_jani_dtmc(self):
  29. jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani"))
  30. formula = properties["Probability to throw a six"]
  31. model = stormpy.build_model(jani_model, [formula])
  32. assert model.nr_states == 13
  33. assert model.nr_transitions == 20
  34. assert len(model.initial_states) == 1
  35. initial_state = model.initial_states[0]
  36. assert initial_state == 0
  37. result = stormpy.model_checking(model, formula)
  38. assert math.isclose(result.at(initial_state), 0.16666666666666663)
  39. def test_model_checking_jani_dtmc(self):
  40. jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani"))
  41. formula = properties["Expected number of coin flips"]
  42. model = stormpy.build_model(jani_model, [formula])
  43. assert model.nr_states == 13
  44. assert model.nr_transitions == 20
  45. assert len(model.initial_states) == 1
  46. initial_state = model.initial_states[0]
  47. assert initial_state == 0
  48. # Unsupported formula yields None result
  49. result = stormpy.model_checking(model, formula)
  50. assert result is None
  51. def test_model_checking_dtmc_all_labels(self):
  52. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  53. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  54. model = stormpy.build_model(program)
  55. assert model.nr_states == 13
  56. assert model.nr_transitions == 20
  57. assert len(model.initial_states) == 1
  58. initial_state = model.initial_states[0]
  59. assert initial_state == 0
  60. result = stormpy.model_checking(model, formulas[0])
  61. assert math.isclose(result.at(initial_state), 0.16666666666666663)
  62. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"two\" ]", program)
  63. result = stormpy.model_checking(model, formulas[0])
  64. assert math.isclose(result.at(initial_state), 0.16666666666666663)
  65. def test_model_checking_all_dtmc(self):
  66. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  67. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  68. model = stormpy.build_model(program, formulas)
  69. assert model.nr_states == 13
  70. assert model.nr_transitions == 20
  71. result = stormpy.model_checking(model, formulas[0])
  72. assert result.result_for_all_states
  73. reference = [0.16666666666666663, 0.3333333333333333, 0, 0.6666666666666666, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  74. assert all(map(math.isclose, result.get_values(), reference))
  75. def test_model_checking_only_initial(self):
  76. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  77. formulas = stormpy.parse_properties_for_prism_program("Pmax=? [F{\"coin_flips\"}<=3 \"one\"]", program)
  78. model = stormpy.build_model(program, formulas)
  79. assert len(model.initial_states) == 1
  80. initial_state = model.initial_states[0]
  81. assert initial_state == 0
  82. result = stormpy.model_checking(model, formulas[0], only_initial_states=True)
  83. assert not result.result_for_all_states
  84. assert math.isclose(result.at(initial_state), 0.125)
  85. def test_model_checking_prob01(self):
  86. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  87. formulaPhi = stormpy.parse_properties("true")[0]
  88. formulaPsi = stormpy.parse_properties("\"six\"")[0]
  89. model = stormpy.build_model(program, [formulaPsi])
  90. phiResult = stormpy.model_checking(model, formulaPhi)
  91. phiStates = phiResult.get_truth_values()
  92. assert phiStates.number_of_set_bits() == model.nr_states
  93. psiResult = stormpy.model_checking(model, formulaPsi)
  94. psiStates = psiResult.get_truth_values()
  95. assert psiStates.number_of_set_bits() == 1
  96. (prob0, prob1) = stormpy.compute_prob01_states(model, phiStates, psiStates)
  97. assert prob0.number_of_set_bits() == 9
  98. assert prob1.number_of_set_bits() == 1
  99. (prob0, prob1) = stormpy.compute_prob01min_states(model, phiStates, psiStates)
  100. assert prob0.number_of_set_bits() == 9
  101. assert prob1.number_of_set_bits() == 1
  102. (prob0, prob1) = stormpy.compute_prob01max_states(model, phiStates, psiStates)
  103. assert prob0.number_of_set_bits() == 9
  104. assert prob1.number_of_set_bits() == 1
  105. labelprop = stormpy.core.Property("cora", formulaPsi.raw_formula)
  106. result = stormpy.model_checking(model, labelprop)
  107. assert result.get_truth_values().number_of_set_bits() == 1
  108. def test_model_checking_ctmc(self):
  109. model = stormpy.build_model_from_drn(get_example_path("ctmc", "dft.drn"))
  110. formulas = stormpy.parse_properties("T=? [ F \"failed\" ]")
  111. assert model.nr_states == 16
  112. assert model.nr_transitions == 33
  113. assert len(model.initial_states) == 1
  114. initial_state = model.initial_states[0]
  115. assert initial_state == 1
  116. result = stormpy.model_checking(model, formulas[0])
  117. assert math.isclose(result.at(initial_state), 4.166666667)