Defining Custom Experiment Logic with Java Code

Custom experiment may be executed from another experiment - in this case the latter should be passed as an argument to the constructor of the custom experiment. Experiments are executed by calling run() function.

A sample action code of some Main agent which runs custom experiment: 

// Create custom experiment, pass currently running experiment
// as an argument to the constructor

MyCustomExperiment e = new MyCustomExperiment(getExperiment());

// setup some custom fields defined in the
// additional class code of MyCustomExperiment:

e.day = getDayOfWeek();

// run experiment

e.run();

// collect results from custom fields defined in the
// additional class code of MyCustomExperiment:

traceln( "result: " + e.myResult );

(In the example above, custom experiment has Code (which is actually inside the run() function) which gets field day (defined in the additional class code), uses it in the experiment and then sets some result value tothe field myResult).

Usage notes

Use createEngine() to create new Engine instance. Use Engine.runFast() function to run experiment in the fastest possible mode.

Don't forget to setup stop time or stop date to the engine before.

Use ExperimentOptimization.createOptimization(Engine) to create new OptQuest Optimization instance.

The simplest example of run() code:  

 // Create Engine, initialize random number generator:

 Engine engine = createEngine();

 // Set stop time

 engine.setStopTime( 100 );

 // Create new top-level agent:

 Main root = new Main(engine, null, null);

 // Setup parameters of top-level agent here

 root.myParameter = 10;

 // Prepare Engine for simulation:

 engine.start( root );

 // Start simulation in fast mode:

 engine.runFast();

 // Obtain results of simulation here

 traceln( root.myResult );

 // Destroy the model:

 engine.stop();

You can use the following functions to control the Custom experiment. Custom experiment uses the API of AnyLogic engine.

Creating experiment

Function

Description

Engine createEngine()

Creates engine. 

void run()

Use createEngine().

Error handling

Function

Description

void onError
(Throwable error)

This function may be overridden to perform custom handling of the errors that occurred during the model execution (i.e., errors in the action code of events, dynamic events, transitions, entry/exit codes of states, formulas, etc.). 

By default, this function does nothing as its definition is empty. To override it, you can define the function, name it onError and define a single argument of the java.lang.Throwable for it.

Parameter:
error - an error which has occurred during event execution.

void onError
(Throwable error, Agent root)

Similar to onError(Throwable error) function except that it provides one more argument to access the top-level (root) agent of the model.

Parameters:
error - an error which has occurred during event execution.
root - the top-level (root) agent of the model. Useful for experiments with multiple runs executed in parallel. May be null in some cases (e.g. on errors during top-level agent creation).


Command-line arguments

Function

Description

String[] getCommandLineArguments()

Returns an array of command-line arguments passed to this experiment on model start. Never returns null: if no arguments are passed, an empty array is returned.

This function is designed for usage inside the run() function.