| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import timeit
- import random
- import argparse
- import os
- import sys
- import math
- # 1. Catch the argument BEFORE importing the models to avoid the import trap
- if "-M" in sys.argv or "--minimal" in sys.argv:
- os.environ["PYPDEVS_MINIMAL"] = "1"
- else:
- os.environ["PYPDEVS_MINIMAL"] = "0"
- from pypdevs.simulator import Simulator
- from pypdevs.minimal import Simulator as Minimal
- from Flocking import Flocking
- def calculate_polarisation(space_state):
- """
- Calculates the Order Parameter (Polarisation) of the flock.
- 1.0 = Perfect alignment (all birds flying in the exact same direction).
- 0.0 = Complete chaos (birds flying randomly, vectors cancel out).
- """
- sum_vx, sum_vy = 0.0, 0.0
- N = len(space_state.positions)
- if N == 0: return 0.0
- for pos, vel in space_state.positions.values():
- sum_vx += vel[0]
- sum_vy += vel[1]
- return math.sqrt((sum_vx / N) ** 2 + (sum_vy / N) ** 2)
- def runthemodel(seed, num_birds, extent_x, extent_y, visual_distance, minimal=False):
- """
- Wrapper function to initialize and run the Flocking simulation for benchmarking.
- """
- model = Flocking(
- seed=seed,
- extent=(extent_x, extent_y),
- num_birds=num_birds,
- visual_distance=float(visual_distance)
- )
- if minimal:
- sim = Minimal(model)
- else:
- sim = Simulator(model)
- sim.setTerminationTime(100 + 1e-3)
- # sim.setVerbose(None)
- # import cProfile
- # import pstats
- # import io
- # start = timeit.default_timer()
- # pr = cProfile.Profile()
- # pr.enable()
- sim.simulate()
- # pr.disable()
- # s = io.StringIO()
- # sortby = pstats.SortKey.CUMULATIVE
- # ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
- # ps.print_stats()
- # print(s.getvalue())
- #
- # print("run time:", timeit.default_timer() - start)
- # final_polarization = calculate_polarisation(model.space.state)
- # print("Final polarisation:", final_polarization)
- def verify_flocking():
- model = Flocking(seed=42, extent=(100, 100), num_birds=200, visual_distance=5.0)
- sim = Minimal(model)
- target_steps = [1, 5, 25, 50, 100, 250, 500]
- for target in target_steps:
- sim.setTerminationTime(target + 1e-3)
- sim.simulate()
- pol = calculate_polarisation(model.space.state)
- print(f"Polarisation after {target} steps: {round(pol, 6)}")
- setup = ("from Flocking import Flocking\n"
- "from benchmark_flocking import runthemodel")
- n_run = 100
- if __name__ == '__main__':
- parser = argparse.ArgumentParser()
- parser.add_argument("-M", "--minimal", action="store_true")
- args = parser.parse_args()
- framework = "PyPDEVS"
- if args.minimal:
- os.environ["PYPDEVS_MINIMAL"] = "1"
- framework = "PyPDEVS-Minimal"
- print(f"Running {framework} Flocking with 200 birds, with a visual distance of 5 and in a 100x100 space")
- tt = timeit.Timer(f'runthemodel(42, 200, 100, 100, 5, {args.minimal})', setup=setup)
- a = tt.repeat(n_run, 1)
- median_time = sorted(a)[n_run // 2 + n_run % 2]
- print(f"{framework} Flocking-small Median Time for {n_run} runs (ms):", median_time * 1e3)
- print(f"Running PyPDEVS {framework} with 400 birds, with a visual distance of 15 and in a 150x150 space")
- tt = timeit.Timer(f'runthemodel(42, 400, 150, 150, 15, {args.minimal})', setup=setup)
- a = tt.repeat(n_run, 1)
- median_time = sorted(a)[n_run // 2 + n_run % 2]
- print(f"{framework} Flocking-large Median Time for {n_run} runs (ms):", median_time * 1e3)
|