The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

112 lines
4.8 KiB

2 months ago
  1. import stormpy
  2. import stormpy.logic
  3. from helpers.helper import get_example_path
  4. import math
  5. class TestScheduler:
  6. def test_scheduler_mdp(self):
  7. program = stormpy.parse_prism_program(get_example_path("mdp", "coin2-2.nm"))
  8. formulas = stormpy.parse_properties_for_prism_program("Pmin=? [ F \"finished\" & \"all_coins_equal_1\"]", program)
  9. model = stormpy.build_model(program, formulas)
  10. assert model.nr_states == 272
  11. assert model.nr_transitions == 492
  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], extract_scheduler=True)
  16. assert result.has_scheduler
  17. scheduler = result.scheduler
  18. assert scheduler.memoryless
  19. assert scheduler.memory_size == 1
  20. assert scheduler.deterministic
  21. for state in model.states:
  22. choice = scheduler.get_choice(state)
  23. assert choice.defined
  24. assert choice.deterministic
  25. action = choice.get_deterministic_choice()
  26. assert 0 <= action
  27. assert action < len(state.actions)
  28. def test_scheduler_ma_via_mdp(self):
  29. program = stormpy.parse_prism_program(get_example_path("ma", "simple.ma"), False, True)
  30. formulas = stormpy.parse_properties_for_prism_program("Tmin=? [ F s=4 ]", program)
  31. ma = stormpy.build_model(program, formulas)
  32. assert ma.nr_states == 5
  33. assert ma.nr_transitions == 8
  34. assert ma.model_type == stormpy.ModelType.MA
  35. # Convert MA to MDP
  36. mdp, mdp_formulas = stormpy.transform_to_discrete_time_model(ma, formulas)
  37. assert mdp.nr_states == 5
  38. assert mdp.nr_transitions == 8
  39. assert mdp.model_type == stormpy.ModelType.MDP
  40. assert len(mdp.initial_states) == 1
  41. initial_state = mdp.initial_states[0]
  42. assert initial_state == 0
  43. result = stormpy.model_checking(mdp, mdp_formulas[0], extract_scheduler=True)
  44. assert math.isclose(result.at(initial_state), 0.08333333333)
  45. assert result.has_scheduler
  46. scheduler = result.scheduler
  47. assert scheduler.memoryless
  48. assert scheduler.memory_size == 1
  49. assert scheduler.deterministic
  50. for state in mdp.states:
  51. choice = scheduler.get_choice(state)
  52. assert choice.defined
  53. assert choice.deterministic
  54. action = choice.get_deterministic_choice()
  55. if state.id == 0:
  56. assert action == 1
  57. else:
  58. assert action == 0
  59. def test_apply_scheduler_mdp(self):
  60. program = stormpy.parse_prism_program(get_example_path("mdp", "coin2-2.nm"))
  61. formulas = stormpy.parse_properties_for_prism_program("Pmin=? [ F \"finished\" & \"all_coins_equal_1\"]", program)
  62. model = stormpy.build_model(program, formulas)
  63. assert model.nr_states == 272
  64. assert model.nr_transitions == 492
  65. assert len(model.initial_states) == 1
  66. initial_state = model.initial_states[0]
  67. assert initial_state == 0
  68. result = stormpy.model_checking(model, formulas[0], extract_scheduler=True)
  69. assert result.has_scheduler
  70. scheduler = result.scheduler
  71. assert scheduler.memoryless
  72. assert scheduler.memory_size == 1
  73. assert scheduler.deterministic
  74. assert not scheduler.partial
  75. intermediate = model.apply_scheduler(scheduler, True)
  76. assert intermediate.model_type == stormpy.ModelType.MDP
  77. assert intermediate.nr_states == 126
  78. assert intermediate.nr_transitions == 156
  79. for state in intermediate.states:
  80. assert len(state.actions) == 1
  81. def test_apply_scheduler_ma(self):
  82. program = stormpy.parse_prism_program(get_example_path("ma", "simple.ma"), False, True)
  83. formulas = stormpy.parse_properties_for_prism_program("Tmin=? [ F s=4 ]", program)
  84. ma = stormpy.build_model(program, formulas)
  85. assert ma.nr_states == 5
  86. assert ma.nr_transitions == 8
  87. assert ma.model_type == stormpy.ModelType.MA
  88. initial_state = ma.initial_states[0]
  89. assert initial_state == 0
  90. result = stormpy.model_checking(ma, formulas[0], extract_scheduler=True)
  91. assert math.isclose(result.at(initial_state), 0.08333333333)
  92. assert result.has_scheduler
  93. scheduler = result.scheduler
  94. assert scheduler.memoryless
  95. assert scheduler.memory_size == 1
  96. assert scheduler.deterministic
  97. intermediate = ma.apply_scheduler(scheduler)
  98. assert intermediate.model_type == stormpy.ModelType.MA
  99. assert intermediate.nr_states == 3
  100. assert intermediate.nr_transitions == 4
  101. for state in intermediate.states:
  102. assert len(state.actions) == 1