Last updated on 16th July 2024
The RunnerBackend and ModelRunner classes need to be used together to setup and run a simulation that can run multiple times.
This guide assumes you have a Model created which implements simudyne.core.Model
The following imports are assumed
Imports required for multirun (Java)
import simudyne.core.runner.ModelRunner;
import simudyne.core.runner.RunResult;
import simudyne.core.runner.RunnerBackend;
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.
Create a model runner (Java)
RunnerBackend runnerBackend = RunnerBackend.create();
Create the ModelRunner for a specific model with RunnerBackend#forModel
passing the class of the model or RunnerBackend#forConfig
passing the ModelConfiguration
for the model
Create a model runner for a specific model (Java)
ModelRunner modelRunner = runnerBackend.forModel(myModel.class);
Use the ModelRunner
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).
withInput
can optionally be used to set values of @Input
variables in the model.
Setting inputs (Java)
modelRunner
.forRuns(100)
.forTicks(50)
.withInput("{\"myBooleanInput\": false}")
.withInput("{\"myStringInput\": \"aString\"}");
To run the model and wait for the results, use ModelRunner#run
. Alternatively, the model can be run as an asynchronous process in the background. This means that while its running, the progress can be tracked.
// To run the model and wait for it to complete
RunResult runResult = modelRunner.run();
// To run the model a a background process and track the progress
Future<RunResult> runResultFuture = modelRunner.runAsync();
modelRunner.getProgress();
// Wait for final result
RunResult runResult = Await.result(runResultFuture, Duration.Inf());
Running a multirun distributed simulation depends on the package core-runner-spark
which needs to be imported, and a config file created as explained in
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.
Multirun with spark (Java)
RunnerBackend runnerBackend = new SparkRunnerBackend();
ModelRunner modelRunner = runnerBackend.forModel(myModel.class);
Extracting information from the RunResult
is explained in Multirun Output