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.

81 lines
3.8 KiB

3 months ago
  1. import stormpy
  2. import stormpy.logic
  3. from helpers.helper import get_example_path
  4. from configurations import pars
  5. import math
  6. @pars
  7. class TestModelInstantiator:
  8. def test_instantiate_dtmc(self):
  9. program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
  10. formulas = stormpy.parse_properties_for_prism_program("P=? [ F s=5 ]", program)
  11. model = stormpy.build_parametric_model(program, formulas)
  12. parameters = model.collect_probability_parameters()
  13. assert len(parameters) == 2
  14. instantiator = stormpy.pars.ModelInstantiator(model)
  15. point = {p: stormpy.RationalRF("0.4") for p in parameters}
  16. instantiated_model = instantiator.instantiate(point)
  17. assert instantiated_model.nr_states == model.nr_states
  18. assert not instantiated_model.has_parameters
  19. assert "0.4" in str(instantiated_model.transition_matrix[1])
  20. point = {p: stormpy.RationalRF("0.5") for p in parameters}
  21. instantiated_model2 = instantiator.instantiate(point)
  22. assert "0.5" in str(instantiated_model2.transition_matrix[1])
  23. def test_sample_pdtmc(self):
  24. program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
  25. formulas = stormpy.parse_properties_for_prism_program("P=? [F \"error\"]", program)
  26. model = stormpy.build_parametric_model(program, formulas)
  27. parameters = model.collect_probability_parameters()
  28. instantiator = stormpy.pars.PDtmcInstantiator(model)
  29. point = {p: stormpy.RationalRF("0.4") for p in parameters}
  30. instantiated_model = instantiator.instantiate(point)
  31. assert instantiated_model.nr_states == model.nr_states
  32. assert not instantiated_model.has_parameters
  33. assert "0.4" in str(instantiated_model.transition_matrix[1])
  34. point = {p: stormpy.RationalRF("0.5") for p in parameters}
  35. instantiated_model2 = instantiator.instantiate(point)
  36. assert "0.5" in str(instantiated_model2.transition_matrix[1])
  37. def test_pdtmc_instantiation_checker(self):
  38. program = stormpy.parse_prism_program(get_example_path("pdtmc", "herman5.pm"))
  39. formulas = stormpy.parse_properties_for_prism_program("R=? [F \"stable\"]", program)
  40. model = stormpy.build_parametric_model(program, formulas)
  41. parameters = model.collect_probability_parameters()
  42. inst_checker = stormpy.pars.PDtmcInstantiationChecker(model)
  43. inst_checker.specify_formula(stormpy.ParametricCheckTask(formulas[0].raw_formula, True))
  44. inst_checker.set_graph_preserving(True)
  45. env = stormpy.Environment()
  46. point = {p: stormpy.RationalRF(1 / 2) for p in parameters}
  47. result = inst_checker.check(env, point)
  48. assert isinstance(result, stormpy.ExplicitQuantitativeCheckResult)
  49. res = result.at(model.initial_states[0])
  50. assert isinstance(res, float)
  51. assert math.isclose(res, 29 / 15)
  52. def test_pdtmc_exact_instantiation_checker(self):
  53. program = stormpy.parse_prism_program(get_example_path("pdtmc", "herman5.pm"))
  54. formulas = stormpy.parse_properties_for_prism_program("R=? [F \"stable\"]", program)
  55. model = stormpy.build_parametric_model(program, formulas)
  56. parameters = model.collect_probability_parameters()
  57. inst_checker = stormpy.pars.PDtmcExactInstantiationChecker(model)
  58. inst_checker.specify_formula(stormpy.ParametricCheckTask(formulas[0].raw_formula, True))
  59. inst_checker.set_graph_preserving(True)
  60. env = stormpy.Environment()
  61. point = {p: stormpy.RationalRF("1/2") for p in parameters}
  62. result = inst_checker.check(env, point)
  63. assert isinstance(result, stormpy.ExplicitExactQuantitativeCheckResult)
  64. res = result.at(model.initial_states[0])
  65. assert isinstance(res, stormpy.Rational)
  66. assert res == stormpy.Rational("29/15")