Custom Experiment

Custom experiment runs experiment with custom scenario entirely written by user.

Custom experiment gives you maximum flexibility with setting parameters, managing simulation runs, making decisions. It simply gives you a code field where you can do all that (and a lot more) by using a rich Java API of AnyLogic engine (methods like run(), stop(), etc.). Refer to the Custom experiment functions section for details.

This experiment has no built-in graphical interface as well as no predefined behavior.

Creating a custom experiment

To create a custom experiment
  1. In the Projects view, right-click (Mac OS: Ctrl+click) the model item and choose New >  Experiment from the popup menu. The New Experiment dialog box is displayed.
  1. Choose  Custom option in the Experiment Type list.
  2. Type the experiment name in the Name edit box.
  3. Choose the top-level agent of the experiment from the Top-level agent drop-down list.
  4. Click Next to go to the next page of the wizard.
  5. Here you specify whether you want to load root object of this experiment from a snapshot. If so, select the checkbox on this page and choose a snapshot using the Browse button. 
  6. Click Finish.
The experiment will be created. Open the experiment's properties, and expand the Code section there. Here you can program the experiment scenario with Java code. By default you can find here code that was automatically generated on the experiment creation. This code creates simulation engine, sets experiment's stop time, creates top-level agent, starts the experiment in the fast mode and then stops it. You can study this very simple code to get some ideas on how to extend it further to achieve the required custom behavior.

To know how to save / restore model snapshots from custom experiment, please refer here.

Examples

Here are the links to demo models with custom experiments. Please examine the custom experiment's code in its Code properties section to understand the scenario implementation.

The following model is the simplest custom experiment demo. The experiment creates the engine, model, configures and runs the simulation, and outputs the result in Console: 

Demo model: Simple Custom Experiment

In the following model the custom experiment implements interaction via Console:

Demo model: Custom Experiment Interacting with Console

In the following model the custom experiment configures and runs optimization without creating the optimization experiment window:

Demo model: Optimization Without UI

In the following model the custom experiment is used to perform the calibration from the running simulation experiment. Run the simulation, and you will see the button starting the custom experiment performing the calibration:

Demo model: Calibration from Within Simulation

Custom experiment properties

General

Name – The name of the experiment.
 Since AnyLogic generates Java class for each experiment, please follow Java naming guidelines and start the name with an uppercase letter. 

Ignore – If selected, the experiment is excluded from the model.

Code

Code – Here you can type the code for this custom experiment. By default you can find here code that was automatically generated on the experiment creation. This code creates simulation engine, sets experiment's stop time, creates top-level agent, starts the experiment in the fast mode and then stops it. You can study this very simple code to get some ideas on how to extend it further to achieve the required custom behavior.

Application options

Maximum available memory – The maximum size of Java heap allocated for the model.

Java machine arguments – Specify here Java machine arguments you want to apply on launching your model. You can find the detailed information on possible arguments at Java Documentation web site: http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html

Command-line arguments – Here you can specify command line arguments you want to pass to your model. You can get the values of passed argument values in the experiment's Additional class code using the method String[] getCommandLineArguments()

Advanced Java

Imports sectionimport statements needed for correct compilation of the experiment class' code. When Java code is generated, these statements are inserted before definition of the Java class.

Additional class code – Arbitrary member variables, nested classes, constants and methods are defined here. This code will be inserted into the experiment class definition. You can access these class data members anywhere within this experiment.