benchmark_RPS.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import timeit
  2. import random
  3. import statistics
  4. from pypdevs.simulator import Simulator
  5. from RockPaperScissors import RockPaperScissors
  6. master_rng = random.Random(42)
  7. def runthemodel(seed, extent_x, extent_y):
  8. """
  9. Wrapper function to initialize and run the Flocking simulation for benchmarking.
  10. """
  11. stats = {"moves": 0, "attacks": 0, "kills": 0, "reproduces": 0, "babies": 0}
  12. run_seed = master_rng.randint(1, 1000000)
  13. # Initialize the model with the adapted signature
  14. model = RockPaperScissors(run_seed, extent_x, extent_y, stats)
  15. sim = Simulator(model)
  16. sim.setDSDEVS()
  17. # sim.setSchedulerHeapSet() # fast, maybe not complete? # changes event counts
  18. sim.setTerminationTime(5)
  19. # sim.setVerbose(None)
  20. # sim.setDrawModel()
  21. # import cProfile
  22. # import pstats
  23. # import io
  24. # pr = cProfile.Profile()
  25. # pr.enable()
  26. start = timeit.default_timer()
  27. sim.simulate()
  28. # pr.disable()
  29. # s = io.StringIO()
  30. # sortby = pstats.SortKey.TIME
  31. # ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
  32. # ps.print_stats()
  33. # print(s.getvalue())
  34. print("run time:", timeit.default_timer() - start)
  35. # print(f"Stats for {extent_x, extent_y}: {', '.join(f'{k}: {v}' for k, v in stats.items())}")
  36. return stats
  37. setup = ("from RockPaperScissors import RockPaperScissors\n"
  38. "from benchmark_RPS import runthemodel")
  39. def run_monte_carlo(runs, extent_x, extent_y):
  40. all_stats = {
  41. "moves": [], "attacks": [], "kills": [], "reproduces": [], "babies": []
  42. }
  43. print(f"Running Python Monte Carlo ({runs} runs)...")
  44. for i in range(runs):
  45. # Use a different seed for each run to build the distribution
  46. stats = runthemodel(seed=42 + i, extent_x=extent_x, extent_y=extent_y)
  47. for key in all_stats:
  48. all_stats[key].append(stats[key])
  49. print(f"\n--- Python Statistical Summary ({runs} runs) ---")
  50. for key, values in all_stats.items():
  51. mean = statistics.mean(values)
  52. stdev = statistics.stdev(values) if runs > 1 else 0
  53. print(f"{key.capitalize():<12}: Mean = {mean:>6.2f} | StdDev = {stdev:>6.2f}")
  54. n_run = 100
  55. if __name__ == '__main__':
  56. # run_monte_carlo(50, 50, 50)
  57. print("\n--- Python Single Run ---")
  58. final_stats = runthemodel(42, 75, 75)
  59. print(f"Final Stats for (75, 75): {final_stats}")
  60. # print(f"Running RPS in a 50x50 space")
  61. # tt = timeit.Timer(f'runthemodel(42, 50, 50)', setup=setup)
  62. # a = tt.repeat(n_run, 1)
  63. # median_time = sorted(a)[n_run // 2 + n_run % 2]
  64. # print(f"RPS-small Median Time for {n_run} runs (ms):", median_time * 1e3)
  65. #
  66. # print("Running RPS PyPDEVS in a 75x75 space")
  67. # tt = timeit.Timer(f'runthemodel(42, 75, 75)', setup=setup)
  68. # a = tt.repeat(n_run, 1)
  69. # median_time = sorted(a)[n_run // 2 + n_run % 2]
  70. # print(f"RPS-large Median Time for {n_run} runs (ms):", median_time * 1e3)