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.

61 lines
2.1 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 = "<ShieldFileName, PreSafety, lambda=0.9> 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. result = stormpy.model_checking(model, formulas[0], extract_scheduler=True)
  24. assert result.has_scheduler
  25. assert result.has_shield
  26. shield = result.shield
  27. pre_scheduler = shield.construct()
  28. simulator = stormpy.simulator.create_simulator(model, seed=42)
  29. while not simulator.is_done():
  30. current_state = simulator.get_current_state()
  31. state_string = model.state_valuations.get_string(current_state)
  32. print(F"Simulator is in state {state_string}.")
  33. choices = [x for x in pre_scheduler.get_choice(current_state).choice_map if x[0] > 0]
  34. choice_labels = [model.choice_labeling.get_labels_of_choice(model.get_choice_index(current_state, choice[1])) for choice in choices]
  35. if not choices:
  36. break
  37. index = random.randint(0, len(choices) - 1)
  38. selected_action = choices[index]
  39. choice_label = model.choice_labeling.get_labels_of_choice(model.get_choice_index(current_state, selected_action[1]))
  40. print(F"Allowed Choices are {choice_labels}. Selected Action: {choice_label}")
  41. observation, reward = simulator.step(selected_action[1])
  42. if __name__ == '__main__':
  43. example_pre_shield_simulator()