{"id":616,"date":"2026-04-07T21:07:57","date_gmt":"2026-04-08T01:07:57","guid":{"rendered":"http:\/\/stephendavies.org\/data420\/?p=616"},"modified":"2026-04-07T21:07:57","modified_gmt":"2026-04-08T01:07:57","slug":"batch_run-code","status":"publish","type":"post","link":"http:\/\/stephendavies.org\/data420\/index.php\/2026\/04\/07\/batch_run-code\/","title":{"rendered":"batch_run() code"},"content":{"rendered":"<p>Here&#8217;s the example we did in class of using <tt>batch_run<\/tt>:<\/p>\n<pre style=\"font-size:small;\">\r\nfrom mesa.batchrunner import batch_run\r\n\r\n    ... your amazing simulation model here ...\r\n\r\nparams = {\r\n    \"N\": 150,\r\n    \"leave_prob\": np.arange(0,1,.1),\r\n    \"edge_prob\": np.arange(0,.5,.1),\r\n    \"prob_blue\": 0.5,\r\n    \"fig\":None,\r\n    \"ax\":None,\r\n    \"do_plot\":False,\r\n    \"seed\":None,\r\n}\r\n\r\nif __name__ == \"__main__\":\r\n    results = batch_run(\r\n        model_cls=Society,\r\n        parameters=params,\r\n        rng=range(10),\r\n        max_steps=None,\r\n        data_collection_period=-1,\r\n        number_processes=None,\r\n    )\r\n   print(pd.DataFrame(results).groupby(['leave_prob','edge_prob'])['Step'].mean())\r\n<\/pre>\n<p>This is the version that does a double-parameter-sweep on two i.v.&#8217;s (independent variables). One way to plot this intelligently, btw, is to use a <b>heat map<\/b>, which has the two i.v.&#8217;s on either axis and then uses the color of each cell to display the d.v. Here&#8217;s my preferred method (which uses the <tt>seaborn<\/tt> plotting library, which you must pip install):<\/p>\n<pre style=\"font-size:small;\">\r\nheat = (\r\n    results.groupby([\"iv_1\", \"iv_2\"])[\"dv\"]\r\n       .mean()\r\n       .reset_index()\r\n       .pivot(index=\"iv_1\", columns=\"iv_2\", values=\"dv\")\r\n)\r\nsns.heatmap(heat)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the example we did in class of using batch_run: from mesa.batchrunner import batch_run &#8230; your amazing simulation model here &#8230; params = { &#8220;N&#8221;: 150, &#8220;leave_prob&#8221;: np.arange(0,1,.1), &#8220;edge_prob&#8221;: np.arange(0,.5,.1), &#8220;prob_blue&#8221;: 0.5, &#8220;fig&#8221;:None, &#8220;ax&#8221;:None, &#8220;do_plot&#8221;:False, &#8220;seed&#8221;:None, } if __name__ == &#8220;__main__&#8221;: 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([&#8216;leave_prob&#8217;,&#8217;edge_prob&#8217;])[&#8216;Step&#8217;].mean()) This is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":2,"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":618,"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/posts\/616\/revisions\/618"}],"wp:attachment":[{"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/stephendavies.org\/data420\/index.php\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}