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.

64 lines
2.2 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. import random
  8. """
  9. Simulating a model with the usage of a pre shield
  10. """
  11. def example_pre_shield_simulator():
  12. path = stormpy.examples.files.prism_mdp_cliff_walking
  13. formula_str = "Pmax=? [G !\"AgentIsInLavaAndNotDone\"]"
  14. program = stormpy.parse_prism_program(path)
  15. formulas = stormpy.parse_properties_for_prism_program(formula_str, program)
  16. options = stormpy.BuilderOptions([p.raw_formula for p in formulas])
  17. options.set_build_state_valuations(True)
  18. options.set_build_choice_labels(True)
  19. options.set_build_all_labels()
  20. model = stormpy.build_sparse_model_with_options(program, options)
  21. initial_state = model.initial_states[0]
  22. assert initial_state == 0
  23. shield_specification = stormpy.logic.ShieldExpression(stormpy.logic.ShieldingType.PRE_SAFETY, stormpy.logic.ShieldComparison.RELATIVE, 0.9)
  24. result = stormpy.model_checking(model, formulas[0], extract_scheduler=True, shield_expression=shield_specification)
  25. assert result.has_scheduler
  26. assert result.has_shield
  27. shield = result.shield
  28. pre_scheduler = shield.construct()
  29. simulator = stormpy.simulator.create_simulator(model, seed=42)
  30. while not simulator.is_done():
  31. current_state = simulator.get_current_state()
  32. state_string = model.state_valuations.get_string(current_state)
  33. print(F"Simulator is in state {state_string}.")
  34. choices = [x for x in pre_scheduler.get_choice(current_state).choice_map if x[0] > 0]
  35. choice_labels = [model.choice_labeling.get_labels_of_choice(model.get_choice_index(current_state, choice[1])) for choice in choices]
  36. if not choices:
  37. break
  38. index = random.randint(0, len(choices) - 1)
  39. selected_action = choices[index]
  40. choice_label = model.choice_labeling.get_labels_of_choice(model.get_choice_index(current_state, selected_action[1]))
  41. print(F"Allowed Choices are {choice_labels}. Selected Action: {choice_label}")
  42. observation, reward = simulator.step(selected_action[1])
  43. if __name__ == '__main__':
  44. example_pre_shield_simulator()