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.

68 lines
2.6 KiB

  1. import stormpy
  2. import stormpy.core
  3. import stormpy.simulator
  4. import stormpy.shields
  5. import stormpy.examples
  6. import stormpy.examples.files
  7. """
  8. Simulating a model with the usage of a post shield
  9. """
  10. def example_post_shield_simulator():
  11. path = stormpy.examples.files.prism_mdp_lava_simple
  12. formula_str = "Pmax=? [G !\"AgentIsInLavaAndNotDone\"]; Pmax=? [ F \"AgentIsInLavaAndNotDone\" ];"
  13. program = stormpy.parse_prism_program(path)
  14. formulas = stormpy.parse_properties_for_prism_program(formula_str, program)
  15. options = stormpy.BuilderOptions([p.raw_formula for p in formulas])
  16. options.set_build_state_valuations(True)
  17. options.set_build_choice_labels(True)
  18. options.set_build_all_labels()
  19. model = stormpy.build_sparse_model_with_options(program, options)
  20. initial_state = model.initial_states[0]
  21. assert initial_state == 0
  22. shield_specification = stormpy.logic.ShieldExpression(stormpy.logic.ShieldingType.POST_SAFETY, stormpy.logic.ShieldComparison.RELATIVE, 0.9)
  23. result = stormpy.model_checking(model, formulas[0], extract_scheduler=True, shield_expression=shield_specification)
  24. result2 = stormpy.model_checking(model, formulas[1], extract_scheduler=True)
  25. assert result.has_shield
  26. assert result2.has_scheduler
  27. shield = result.shield
  28. scheduler = result2.scheduler
  29. post_scheduler = shield.construct()
  30. simulator = stormpy.simulator.create_simulator(model, seed=42)
  31. while not simulator.is_done():
  32. current_state = simulator.get_current_state()
  33. state_string = model.state_valuations.get_string(current_state)
  34. # print(F"Simulator is in state {state_string}.")
  35. sched_choice = scheduler.get_choice(current_state).get_deterministic_choice()
  36. # print(F"Scheduler choice {model.choice_labeling.get_labels_of_choice(model.get_choice_index(current_state, sched_choice))}")
  37. corrections = post_scheduler.get_choice(current_state).choice_map
  38. # print(corrections)
  39. correction_labels = [(model.get_label_of_choice(current_state, correction[0]), model.get_label_of_choice(current_state, correction[1])) for correction in corrections]
  40. # print(F"Correction Choices are {correction_labels}.")
  41. applied_correction = next((x[1] for x in corrections if x[0] == sched_choice), None)
  42. if applied_correction != None and applied_correction != sched_choice:
  43. print(F"Correction applied changed choice {sched_choice} to {applied_correction}")
  44. sched_choice = applied_correction
  45. observation, reward = simulator.step(sched_choice)
  46. if __name__ == '__main__':
  47. example_post_shield_simulator()