{"data":{"markdownRemark":{"html":"<p>The RunnerBackend and ModelRunner classes need to be used together to setup and run a simulation that can run multiple times.</p>\n<p>This guide assumes you have a Model created which implements <code class=\"language-text\">simudyne.core.Model</code>\nThe following imports are assumed</p>\n<p class=\"code-header\">Imports required for multirun (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token keyword\">import</span> simudyne<span class=\"token punctuation\">.</span>core<span class=\"token punctuation\">.</span>runner<span class=\"token punctuation\">.</span>ModelRunner<span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> simudyne<span class=\"token punctuation\">.</span>core<span class=\"token punctuation\">.</span>runner<span class=\"token punctuation\">.</span>RunResult<span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> simudyne<span class=\"token punctuation\">.</span>core<span class=\"token punctuation\">.</span>runner<span class=\"token punctuation\">.</span>RunnerBackend<span class=\"token punctuation\">;</span></code></pre></div>\n<p>The RunnerBackend class is needed to set up the ModelRunner. The default RunnerBackend can be created by using the RunnerBackend create method. This will create an instance of the RunnerBackend that can be used to run the model locally.</p>\n<p class=\"code-header\">Create a model runner (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\">RunnerBackend runnerBackend <span class=\"token operator\">=</span> RunnerBackend<span class=\"token punctuation\">.</span><span class=\"token function\">create</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Create the ModelRunner for a specific model with <code class=\"language-text\">RunnerBackend#forModel</code> passing the class of the model or <code class=\"language-text\">RunnerBackend#forConfig</code> passing the <code class=\"language-text\">ModelConfiguration</code> for the model</p>\n<p class=\"code-header\">Create a model runner for a specific model (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\">ModelRunner modelRunner <span class=\"token operator\">=</span> runnerBackend<span class=\"token punctuation\">.</span><span class=\"token function\">forModel</span><span class=\"token punctuation\">(</span>myModel<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h2 id=\"modelrunner-for-batch-simulations\"><a href=\"#modelrunner-for-batch-simulations\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ModelRunner for Batch simulations</h2>\n<p>Use the <code class=\"language-text\">ModelRunner</code> to set the number of ticks(steps) to run each run of the model for, and the number of runs (number of times to run the full simulation).</p>\n<p><code class=\"language-text\">withInput</code> can optionally be used to set values of <code class=\"language-text\">@Input</code> variables in the model.</p>\n<p class=\"code-header\">Setting inputs (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\">modelRunner\n    <span class=\"token punctuation\">.</span><span class=\"token function\">forRuns</span><span class=\"token punctuation\">(</span><span class=\"token number\">100</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">forTicks</span><span class=\"token punctuation\">(</span><span class=\"token number\">50</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">withInput</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"{\\\"myBooleanInput\\\": false}\"</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">withInput</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"{\\\"myStringInput\\\": \\\"aString\\\"}\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>To run the model and wait for the results, use <code class=\"language-text\">ModelRunner#run</code>. Alternatively, the model can be run as an asynchronous process in the background. This means that while its running, the progress can be tracked.</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token comment\">// To run the model and wait for it to complete</span>\nRunResult runResult <span class=\"token operator\">=</span> modelRunner<span class=\"token punctuation\">.</span><span class=\"token function\">run</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// To run the model a a background process and track the progress</span>\nFuture<span class=\"token generics function\"><span class=\"token punctuation\">&lt;</span>RunResult<span class=\"token punctuation\">></span></span> runResultFuture <span class=\"token operator\">=</span> modelRunner<span class=\"token punctuation\">.</span><span class=\"token function\">runAsync</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nmodelRunner<span class=\"token punctuation\">.</span><span class=\"token function\">getProgress</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token comment\">// Wait for final result</span>\nRunResult runResult <span class=\"token operator\">=</span> Await<span class=\"token punctuation\">.</span><span class=\"token function\">result</span><span class=\"token punctuation\">(</span>runResultFuture<span class=\"token punctuation\">,</span> Duration<span class=\"token punctuation\">.</span><span class=\"token function\">Inf</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<div class=\"ui segment info message\">\n<h4>Improving multirun performance</h4>\nThe multirun results are stored in memory to be returned to the user. If only the file output matters, this behaviour can be disabled in order to improve performance by setting the config field 'core.return-data' to false.\n<p>To read more on flags and configuration, see <a href=\":version/reference/models/model-configuration\">Model Config</a>.</p>\n<p>If this field is set to false, the returned RunResult will be empty.</p>\n</div>\n<h2 id=\"modelrunner-for-scenario-simulations\"><a href=\"#modelrunner-for-scenario-simulations\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ModelRunner for Scenario simulations</h2>\n<p>A scenario is a simulation that can be setup to run a series of ticks with different input parameters for each tick.\nEach series can be run multiple times with a different seed.</p>\n<div class=\"ui segment warning message\">\n<p>The current implementation of scenario does not possess a Java API. The following example will guide you through a simple scenario simulation.\nIf you wish to see all scenario's features, please use the <a href=\":version/rest_api/scenario\">REST API documentation</a></p>\n</div>\n<p>We need to register the model and start the server.</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\">  Server<span class=\"token punctuation\">.</span><span class=\"token function\">register</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"Game of Life\"</span><span class=\"token punctuation\">,</span> GameOfLife<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  Server<span class=\"token punctuation\">.</span><span class=\"token function\">run</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The model is now available for us to query. In this example, we will update <strong>gridSize</strong> of the <strong>Game of Life</strong> Model.\nWe will set two runs with different seeds.</p>\n<p>The parameters sent to the servers are:</p>\n<ul>\n<li>\n<p>Simulation parameters:</p>\n<ul>\n<li><strong>modelName</strong> is the name the model was registered with.</li>\n<li><strong>output URI</strong> is the path you want to export the data to.</li>\n<li><strong>scenarios</strong> contain the definition of all scenarios. Here, there is only one scenario object.</li>\n</ul>\n</li>\n</ul>\n<ul>\n<li>\n<p>Scenario Parameters: </p>\n<ul>\n<li><strong>seeds</strong> is the list of seeds each run will be using. There are two seeds, so you will have two distinct runs.</li>\n<li><strong>scenarioData</strong> represents the parameters input per tick. Here we specified the tick <strong>0</strong>, which is the setup, and the tick <strong>10</strong>. The last one determines the number of ticks the simulation will run for.</li>\n</ul>\n</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\">curl -X POST \\\n  http<span class=\"token operator\">:</span>//localhost<span class=\"token operator\">:</span><span class=\"token number\">8080</span>/api/simulations/scenario \\\n  -H 'Cache-Control<span class=\"token operator\">:</span> no-cache' \\\n  -H 'Content-Type<span class=\"token operator\">:</span> application/json' \\\n  -d '<span class=\"token punctuation\">{</span>\n  <span class=\"token property\">\"modelName\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"Game of Life\"</span><span class=\"token punctuation\">,</span> \n  <span class=\"token property\">\"output\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token property\">\"uri\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"/tmp/scenarios\"</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token property\">\"scenarios\"</span><span class=\"token operator\">:</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">{</span>\n    <span class=\"token property\">\"seeds\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1234</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2314</span><span class=\"token punctuation\">]</span>\n    <span class=\"token property\">\"scenarioData\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token property\">\"0\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token property\">\"gridSize\"</span><span class=\"token operator\">:</span><span class=\"token number\">10</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n        <span class=\"token property\">\"10\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n      <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span>'</code></pre></div>\n<p> The server will return the following:</p>\n<div class=\"gatsby-highlight\" data-language=\"json\"><pre class=\"language-json\"><code class=\"language-json\"><span class=\"token punctuation\">{</span><span class=\"token property\">\"id\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"f78057c4-f7f2-477b-bed1-3a690c7be864\"</span><span class=\"token punctuation\">,</span><span class=\"token property\">\"name\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"Game of Life\"</span><span class=\"token punctuation\">,</span><span class=\"token property\">\"description\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">,</span><span class=\"token property\">\"public\"</span><span class=\"token operator\">:</span><span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span><span class=\"token property\">\"session\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"744d5b5b-ac16-4361-be3e-da5836c234bb\"</span><span class=\"token punctuation\">,</span><span class=\"token property\">\"kind\"</span><span class=\"token operator\">:</span><span class=\"token string\">\"pojo\"</span><span class=\"token punctuation\">}</span></code></pre></div>\n<p>Now that this simulation has completed, go in the <strong>/tmp/scenarios/</strong> we specified.  Open the folder named as the <strong>id</strong> returned by the server, then <strong>runs</strong>. Inside are 2 files and 2 folders.</p>\n<ul>\n<li>finished.json will be created when a simulation is completed</li>\n<li>metadata.json will contains useful information. Refer to the <a href=\":version/rest_api/scenario\">REST API documentation</a> for a complete description.</li>\n</ul>\n<p>We only have one scenario, and two runs, this information is present in the folders names: </p>\n<ul>\n<li>scenario_0001.run0001  </li>\n<li>scenario_0001.run0002</li>\n</ul>\n<p>In the results.json file in each of the results folder, you can find the seed that we setup earlier as well as all data output.\nTo have more details about the simulation output, refer to the documentation about the <a href=\":version/reference/data_output/parquet_output#output-directory-structure\">output directory structure</a>.</p>\n<h1 id=\"running-a-distributed-multirun\"><a href=\"#running-a-distributed-multirun\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Running a distributed multirun</h1>\n<h2 id=\"using-spark\"><a href=\"#using-spark\" aria-hidden=\"true\" class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using Spark</h2>\n<p>Running a distributed multirun simulation depends on different packages and imports.</p>\n<p>These elements can be found in the <a href=\":version/reference/distributed_computation/spark_setup#spark-requirements\">spark requirement tutorial</a> and the <a href=\":version/reference/distributed_computation/spark_setup#spark-model-runner\">spark runner tutorial</a> tutorials. </p>\n<p>To setup a distributed multirun, create a new instance of the SparkRunnerBackend as the RunnerBackend. All other methods are the same as for the local RunnerBackend.  </p>\n<p class=\"code-header\">Multirun with spark (Java)</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\">RunnerBackend runnerBackend <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">SparkRunnerBackend</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nModelRunner modelRunner <span class=\"token operator\">=</span> runnerBackend<span class=\"token punctuation\">.</span><span class=\"token function\">forModel</span><span class=\"token punctuation\">(</span>myModel<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Extracting information from the <code class=\"language-text\">RunResult</code> is explained in <a href=\":version/reference/run_mode/multirun_output\">Multirun Output</a></p>","headings":[{"value":"ModelRunner for Batch simulations","depth":2},{"value":"ModelRunner for Scenario simulations","depth":2},{"value":"Running a distributed multirun","depth":1},{"value":"Using Spark","depth":2}],"frontmatter":{"title":"Multirun Setup","toc":null,"experimental":null}},"site":{"siteMetadata":{"title":"Simudyne Docs","latestVersion":"2.6"}}},"pageContext":{"absolutePath":"/home/vsts/work/1/s/content/2.2/reference/run_mode/multirun_setup.md","versioned":true,"version":"2.2","kind":"reference","pagePath":"/reference/run_mode/multirun_setup","chronology":{"prev":{"name":"Run mode","path":"/reference/run_mode"},"next":{"name":"Multirun Output","path":"/reference/run_mode/multirun_output"}},"lastUpdated":"2026-04-21T13:56:54.838Z"}}