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.

71 lines
2.4 KiB

2 months ago
  1. import stormpy
  2. import stormpy.core
  3. import stormpy.simulator
  4. import stormpy.examples
  5. import stormpy.examples.files
  6. import random
  7. """
  8. Simulator for nondeterministic models
  9. """
  10. def example_simulator_02():
  11. path = stormpy.examples.files.prism_mdp_maze
  12. prism_program = stormpy.parse_prism_program(path)
  13. model = stormpy.build_model(prism_program)
  14. simulator = stormpy.simulator.create_simulator(model, seed=42)
  15. # 5 paths of at most 20 steps.
  16. paths = []
  17. for m in range(5):
  18. path = []
  19. state, reward = simulator.restart()
  20. path = [f"{state}"]
  21. for n in range(20):
  22. actions = simulator.available_actions()
  23. select_action = random.randint(0,len(actions)-1)
  24. #print(f"Randomly select action nr: {select_action} from actions {actions}")
  25. path.append(f"--act={actions[select_action]}-->")
  26. state, reward = simulator.step(actions[select_action])
  27. #print(state)
  28. path.append(f"{state}")
  29. if simulator.is_done():
  30. #print("Trapped!")
  31. break
  32. paths.append(path)
  33. for path in paths:
  34. print(" ".join(path))
  35. options = stormpy.BuilderOptions()
  36. options.set_build_state_valuations()
  37. options.set_build_choice_labels(True)
  38. model = stormpy.build_sparse_model_with_options(prism_program, options)
  39. print(model)
  40. simulator = stormpy.simulator.create_simulator(model, seed=42)
  41. simulator.set_observation_mode(stormpy.simulator.SimulatorObservationMode.PROGRAM_LEVEL)
  42. simulator.set_action_mode(stormpy.simulator.SimulatorActionMode.GLOBAL_NAMES)
  43. # 5 paths of at most 20 steps.
  44. paths = []
  45. for m in range(5):
  46. path = []
  47. state, reward = simulator.restart()
  48. path = [f"{state}"]
  49. for n in range(20):
  50. actions = simulator.available_actions()
  51. select_action = random.randint(0,len(actions)-1)
  52. #print(f"Randomly select action nr: {select_action} from actions {actions}")
  53. path.append(f"--act={actions[select_action]}-->")
  54. state, reward = simulator.step(actions[select_action])
  55. #print(state)
  56. path.append(f"{state}")
  57. if simulator.is_done():
  58. #print("Trapped!")
  59. break
  60. paths.append(path)
  61. for path in paths:
  62. print(" ".join(path))
  63. if __name__ == '__main__':
  64. example_simulator_02()