Disclaimer: I have not rested this with a Bonsai Brain, there is a chance that it won't work with a Custom Experiment
Warning: Advanced Java ahead
You can run simulations by using the Custom Experiment functionality.
The example below is adapted from the Simulation Model Life Cycle articles you can find here, but with the added advancement of multi-threading. The code can be imposed on that simple example and it will work fine.
Create a custom experiment and add some custom code in the additional class code. Ideally, in this function, you provide some scenario class containing all the input data for your model, as well as a result class where you store all the result data from the model.

public void startRound( Scenario scenario, int seed, TextFile customerQueueTimeFile, Results results ) {
// Create Engine, initialize random number generator:
Engine engine = createEngine();
engine.setTimeUnit( MINUTE );
engine.getDefaultRandomGenerator().setSeed(seed);
// start date
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
try {
engine.setStartDate(dateFormatter.parse("2021-02-01"));
} catch (ParseException e) {
throw new AssertionError("Invalid date: ");
}
// Create new root object and set the parameter
Main root = new Main( engine, null, null );
root.scenario = scenario;
root.seed = seed;
root.customerQueueTimeFile = customerQueueTimeFile;
engine.start( root );
engine.runFast();
traceln("Experiment: " + scenario.scenarioName + ", replciation: " + seed + " Finished");
if (results != null) {
results.addResult("Min", root.averageWaitingTime.getStatistics().min());
results.addResult("Average", root.averageWaitingTime.getStatistics().mean());
results.addResult("Max", root.averageWaitingTime.getStatistics().max());
}
engine.stop();
}
If you then want to run these custom experiments in parallel you need to implement multi-threading.
You can do this by creating a new runnable for every replication that you want to run and then in the runnable call the function to run the custom experiment.
When there is only 1 active thread in the thread pool you can save the outputs

for (String fileName:filesToLoad) {
importFromExcel(fileName);
Results result = new Results();
resultsMap.put(selectFrom(model_setup).firstResult(false, model_setup.scenario_name), result);
for (int i = 0; i < replications; i ++) {
final int x = i;
Scenario s = getScenarioFromDB();
Runnable runnable = new Runnable() {
@Override
public void run() {
CustomExperiment customExperiment = new CustomExperiment(null);
customExperiment.startRound(s, x, customerQueueTime, result);
if (v_threadPool.getActiveCount() == 1){
outputResults();
}
}
};
v_threadPool.execute(runnable);
}
}
v_threadPool is just a ThreadPool executor to ensure you don't run too many threads at the same time.
