import timeit import random import argparse import os import sys # 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 Schelling import Schelling def runthemodel(seed, dimension, num_agents, radius, threshold, minimal=False): model = Schelling( dim=dimension, num_agents=num_agents, radius=radius, happy_threshold=threshold, seed=seed, ) if minimal: sim = Minimal(model) else: sim = Simulator(model) sim.setTerminationTime(20.01) # sim.setVerbose(None) sim.simulate() setup = ("from Schelling import Schelling\n" "from benchmark_schelling 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" # import cProfile # import pstats # import io # start = timeit.default_timer() # pr = cProfile.Profile() # pr.enable() # runthemodel(42, 100, 8000, 2, 8, args.minimal) # 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(f"Running {framework} Schelling with 1000 agents in a 40x40 space, with a radius of one and a threshold of three.") tt = timeit.Timer(f'runthemodel(42, 40, 1000, 1, 3, {args.minimal})', setup=setup) a = tt.repeat(n_run, 1) median_time = sorted(a)[n_run // 2 + n_run % 2] print(f"{framework} Schelling-small Median Time for {n_run} runs (ms):", median_time * 1e3) # print(f"Running {framework} Schelling with 8000 agents in a 100x100 space, with a radius of two and a threshold of eight.") tt = timeit.Timer(f'runthemodel(42, 100, 8000, 2, 8, {args.minimal})', setup=setup) a = tt.repeat(n_run, 1) median_time = sorted(a)[n_run // 2 + n_run % 2] print(f"{framework} Schelling-large Median Time for {n_run} runs (ms):", median_time * 1e3)