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.

71 lines
3.3 KiB

  1. import stormpy
  2. import stormpy.simulator
  3. def mdp():
  4. options = stormpy.BuilderOptions([])
  5. options.set_build_state_valuations(True)
  6. options.set_build_choice_labels(True)
  7. options.set_build_all_labels()
  8. options.set_build_all_reward_models()
  9. program = stormpy.parse_prism_program("/media/task_graph_scheduling.prism")
  10. formula_str = "R{\"time\"}min=? [ F \"tasks_complete\" ];"
  11. formulas = stormpy.parse_properties_for_prism_program(formula_str, program)
  12. model = stormpy.build_sparse_model_with_options(program, options)
  13. print(model)
  14. initial_state = model.initial_states[0]
  15. assert initial_state == 0
  16. result = stormpy.model_checking(model, formulas[0], extract_scheduler=True)
  17. assert result.has_scheduler
  18. scheduler = result.scheduler
  19. assert scheduler.memoryless
  20. assert scheduler.deterministic
  21. dtmc = model.apply_scheduler(scheduler)
  22. print(dtmc)
  23. simulator = stormpy.simulator.create_simulator(dtmc, seed=42)
  24. simulator.set_observation_mode(stormpy.simulator.SimulatorObservationMode.PROGRAM_LEVEL)
  25. step_counter = 0
  26. busy1_counter = 0
  27. busy2_counter = 0
  28. print("""
  29. t1 t3 t5
  30. A+B-->Cx(A+B)-->DxCx(A+B)--. t6
  31. \ \___\DxCx(A+B)+((A+B)+(CxD))
  32. t2 \ t4 / /
  33. CxD-->(A+B)+(CxD)------------°
  34. """)
  35. print(" " * 32 + "[+,x,x,+,x,+]")
  36. while True:
  37. if simulator.is_done():
  38. print(f"Tasks complete! Ratio: (approx.): {busy1_counter/(busy1_counter+busy2_counter):1.4f}/{busy2_counter/(busy1_counter+busy2_counter):1.4f}, Steps needed (approx.): {step_counter}")
  39. break
  40. observation, reward, labels = simulator.step()
  41. #input("")
  42. busy1 = str(observation["p1_idle"]) != ("true")
  43. busy2 = str(observation["p2_idle"]) != ("true")
  44. if busy1: busy1_counter += 1
  45. if busy2: busy2_counter += 1
  46. tasks = [str(observation[f"task{i}"]) for i in range(1,7)]
  47. print(f"Step {step_counter:3}: P1/2:{'busy' if busy1 else 'idle'}/{'busy' if busy2 else 'idle'} tasks: [{','.join(tasks)}], {str(observation['add_tick_1'])},{str(observation['mult_tick_1'])},{str(observation['add_tick_2'])},{str(observation['mult_tick_2'])},labels: {labels if len(labels) != 0 else ''}, ")
  48. step_counter += 1
  49. try:
  50. with open("/media/induced_scheduling_dtmc.dot", "w") as text_file:
  51. text_file.write(dtmc.to_dot())
  52. except Exception as e:
  53. print(e)
  54. print("Could not write to file.")
  55. input("Hit enter to evaluate: 'P=? [ F<={i} \"tasks_complete\" ]' on the DTMC:")
  56. print(dtmc)
  57. for i in range(30,61):
  58. formula_str = f"P=? [ F<={i} \"tasks_complete\" ]"
  59. formulas = stormpy.parse_properties_for_prism_program(formula_str, program)
  60. result = stormpy.model_checking(dtmc, formulas[0])
  61. print(f"Probability to finish within {i} timesteps : {result.at(dtmc.initial_states[0]):>1.12f}")
  62. if __name__ == '__main__':
  63. mdp()