| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import timeit
- import random
- import statistics
- from pypdevs.simulator import Simulator
- from RockPaperScissors import RockPaperScissors
- master_rng = random.Random(42)
- def runthemodel(seed, extent_x, extent_y):
- """
- Wrapper function to initialize and run the Flocking simulation for benchmarking.
- """
- stats = {"moves": 0, "attacks": 0, "kills": 0, "reproduces": 0, "babies": 0}
- run_seed = master_rng.randint(1, 1000000)
- # Initialize the model with the adapted signature
- model = RockPaperScissors(run_seed, extent_x, extent_y, stats)
- sim = Simulator(model)
- sim.setDSDEVS()
- # sim.setSchedulerHeapSet() # fast, maybe not complete? # changes event counts
- sim.setTerminationTime(5)
- # sim.setVerbose(None)
- # sim.setDrawModel()
- # import cProfile
- # import pstats
- # import io
- # pr = cProfile.Profile()
- # pr.enable()
- start = timeit.default_timer()
- sim.simulate()
- # pr.disable()
- # s = io.StringIO()
- # sortby = pstats.SortKey.TIME
- # ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
- # ps.print_stats()
- # print(s.getvalue())
- print("run time:", timeit.default_timer() - start)
- # print(f"Stats for {extent_x, extent_y}: {', '.join(f'{k}: {v}' for k, v in stats.items())}")
- return stats
- setup = ("from RockPaperScissors import RockPaperScissors\n"
- "from benchmark_RPS import runthemodel")
- def run_monte_carlo(runs, extent_x, extent_y):
- all_stats = {
- "moves": [], "attacks": [], "kills": [], "reproduces": [], "babies": []
- }
- print(f"Running Python Monte Carlo ({runs} runs)...")
- for i in range(runs):
- # Use a different seed for each run to build the distribution
- stats = runthemodel(seed=42 + i, extent_x=extent_x, extent_y=extent_y)
- for key in all_stats:
- all_stats[key].append(stats[key])
- print(f"\n--- Python Statistical Summary ({runs} runs) ---")
- for key, values in all_stats.items():
- mean = statistics.mean(values)
- stdev = statistics.stdev(values) if runs > 1 else 0
- print(f"{key.capitalize():<12}: Mean = {mean:>6.2f} | StdDev = {stdev:>6.2f}")
- n_run = 100
- if __name__ == '__main__':
- # run_monte_carlo(50, 50, 50)
- print("\n--- Python Single Run ---")
- final_stats = runthemodel(42, 75, 75)
- print(f"Final Stats for (75, 75): {final_stats}")
- # print(f"Running RPS in a 50x50 space")
- # tt = timeit.Timer(f'runthemodel(42, 50, 50)', setup=setup)
- # a = tt.repeat(n_run, 1)
- # median_time = sorted(a)[n_run // 2 + n_run % 2]
- # print(f"RPS-small Median Time for {n_run} runs (ms):", median_time * 1e3)
- #
- # print("Running RPS PyPDEVS in a 75x75 space")
- # tt = timeit.Timer(f'runthemodel(42, 75, 75)', setup=setup)
- # a = tt.repeat(n_run, 1)
- # median_time = sorted(a)[n_run // 2 + n_run % 2]
- # print(f"RPS-large Median Time for {n_run} runs (ms):", median_time * 1e3)
|