batch_run() code


Here’s the example we did in class of using batch_run:

from mesa.batchrunner import batch_run

    ... your amazing simulation model here ...

params = {
    "N": 150,
    "leave_prob": np.arange(0,1,.1),
    "edge_prob": np.arange(0,.5,.1),
    "prob_blue": 0.5,
    "fig":None,
    "ax":None,
    "do_plot":False,
    "seed":None,
}

if __name__ == "__main__":
    results = batch_run(
        model_cls=Society,
        parameters=params,
        rng=range(10),
        max_steps=None,
        data_collection_period=-1,
        number_processes=None,
    )
   print(pd.DataFrame(results).groupby(['leave_prob','edge_prob'])['Step'].mean())

This is the version that does a double-parameter-sweep on two i.v.’s (independent variables). One way to plot this intelligently, btw, is to use a heat map, which has the two i.v.’s on either axis and then uses the color of each cell to display the d.v. Here’s my preferred method (which uses the seaborn plotting library, which you must pip install):

heat = (
    results.groupby(["iv_1", "iv_2"])["dv"]
       .mean()
       .reset_index()
       .pivot(index="iv_1", columns="iv_2", values="dv")
)
sns.heatmap(heat)

Leave a Reply

Your email address will not be published. Required fields are marked *