Experiment#

An experiment supervises the execution of several runs of simulation.

It initializes simulations sharing the same Scenario and run them while collecting the desired data through ExperimentalRun.

Constructs a new instance.

Parameters:
  • time_step – The default simulation time step

  • steps – The default number of simulation steps

Register a probe to record a group of data to during all runs.

Parameters:

Register a probe to be added to all runs

Parameters:

factory – A function that generate the probe.

Register a probe to record data to during all runs.

Parameters:

Adds a callback to be executed before/after each run.

Parameters:
  • value – The callback

  • at_init – Whether the callback should be called when initializing the run. If not set, it will be called at the completion of a run.

Remove all run callbacks

Dumps the object to a YAML-string.

Returns:

The YAML representation

Return type:

str

Initializes a run

Parameters:
  • seed – The random seed

  • world – The world to simulate. If null, it will initialize a new world.

See start(). This is only need when using an external run-loop to simulate or when manually calling ExperimentalRun.run() later. Else use run_once() (or run() for all runs) to initialize and run at once.

Load a experiment modulation from a YAML string.

Returns:

The loaded experiment or None if loading fails.

Return type:

Experiment| None

Clear the recording

Removes a recorded run.

Parameters:

seed – The seed/index of the run

Perform several runs and optionally record the data in a HFD5 file.

The number of runs is specified by the default number_of_runs if not specified.

Runs will be indexed sequentially and their index used as a random seed.

If save_directory not empty but points to an existing directory, it creates a HDF5 file <name>_<hash>_<timestamp>/data.h5 with attributes

  • begin_time [string], ISO 8601 formatted string of the time when the experiment is run, see begin_time;

  • duration_ns [unsigned], total duration in nanoseconds, see duration.

  • experiment [string], YAML serialization of the experiment;

Moreover, at the end of each run, it saves a group run_<index> with attributes:

  • duration_ns [unsigned], total duration in nanoseconds, see duration;

  • seed [unsigned];

  • steps [unsigned], actual number of steps performed;

  • maximal_steps [unsigned], maximal number of steps that could have been performed;

  • final_sim_time [float], the simulated time at the end of the run;

  • world [string], YAML serialization of the world at the begin of the experiment.

datasets:

and groups:

  • task_events (if RecordConfig.task_events is set), where each agents logs, in dataset task_events/<id> [float], the events emitted by their task.

Apart from saving data to the HDF5 file, each run is performed similarly to run_once().

Parameters:
  • keep – Whether to keep runs in memory

  • number_of_threads – How many threads to use. When more than one, runs will be distributed in parallel over the threads.

  • start_index – The index/seed of the first run. If unspecified, it will use the experiment’s run_index

  • number_of_runs – The number of runs. If unspecified, it will use the experiment’s number_of_runs

  • data_path – A path to set optionally as path. If set, it will save an HDF5 file, but no YAML, to this path. If not set, path will be automatically set to <save_directory>/data.h5 if save_directory is set.

Run an experiment distributing its runs in parallel over multiple processes.

Use this to parallelize experiments that contains Python classes, see :py:fun:uses_python, Experiment.run parallelizes over multiple threads instead and cannot be used to run such experiments because of the GIL.

If keep=True, the experiment will query the runs from the different processes and hold them in memory. If it is configured to save the data, it will save a single HDF5 file.

If keep=False, the experiment won’t keep the runs in memory. If it is configured to save the data, it will save one HDF5 file per process ("data_<i>.h5") and one “data.h5” linking all the runs together. To access the data, you will need to load the HFD5 file.

Parameters:
  • experiment – The experiment

  • number_of_processes – The number of processes

  • keep – Whether to keep runs in memory

  • number_of_runs – The number of runs

  • start_index – The index of the first run

  • callback – An optional callback to run after each run is completed

  • bar – An optional tqdm bar to display the progresses.

  • scenario_init_callback – An optional callback to set as Experiment.scenario_init_callback.

  • use_multiprocess – Whether to use the multiprocess package instead of multiprocessing

  • load_plugins – Whether load the plugins in the processes

Perform a single run

  1. it initializes a world from its scenario by calling Scenario.init_world()

  2. it runs the simulation, step by step, collecting data in a ExperimentalRun by calling ExperimentalRun.run()

Parameters:

seed – The index (and random seed) of the run

Returns:

The recorded run.

Save all recorded runs.

Parameters:
  • directory – A path to set optionally as save_directory where to save YAML and HDF5 file.

  • path – A path to set optionally as path. If set, it will save an HDF5 file, but no YAML, to this path. If not set, path will be automatically set to <save_directory>/data.h5 if save_directory is set.

Returns the json-schema

Returns:

json-schema

Return type:

dict[str, typing.Any]

Configure a tqdm object that displays the progress of an experiment

Parameters:

Signal to start an experiment

Note that this won’t neither execute any simulation nor record data. It will just record time stamp and change the state to running.

This is only needed when using an external run-loop for the simulation. In this case:

  1. call start()

  2. For each run

i. Call init_run() and start_run() ii. from your run-loop, call update_run() to record the current state of the simulated world iii. Call stop_run()

  1. Call stop()

Use run() instead to perform the simulations and record them all at once.

Calling start() is only effective once per experiment.

Parameters:

path – A path to set optionally as path. If set, it will save an HDF5 file, but no YAML, to this path. If not set, path will be automatically set to <save_directory>/data.h5 if save_directory is set.

Start recording a run

See start(). This is only need when using an external run-loop to simulate.

Call start_run() only once per run.

Parameters:

run – The run being recorded

Signal to stop an experiment

Parameters:

save_runs – Whether to save the runs before closing

See start(). This is only need when using an external run-loop to simulate.

Stop the run recording

See start_run(). This is only need to use an external run-loop.

Parameters:

run – The run being recorded

{ function_description }

See start(). This is only need when using an external run-loop to simulate.

Call update_run() every time you need to record the current state of the world, typically after each simulation step.

Parameters:

run – The run being recorded

Type:

datetime.datetime

The system time when the experiment began.

Type:

datetime.timedelta

The duration required to perform the whole experiment.

Type:

bool

Determines if the experiment has finished.

Type:

bool

Determines if the experiment is running.

Type:

str

The name of the experiment

Type:

int

Default number of runs to perform

Type:

Optional[os.PathLike]

The path where the experimental data has been saved.

Type:

navground.sim.RecordConfig

A reference to the record config on which data to record

Type:

dict[bool, list[Callable[[navground.sim.ExperimentalRun], None]]]

The run callbacks

Type:

int

The seed/index of the next run

Type:

dict[int, navground.sim.ExperimentalRun]

The map of recorded runs.

runs are associated to the seed used to initialize them.

Type:

os.PathLike

Where to save the results

Type:

navground.sim.Scenario

The scenario used to initialize a world

Type:

Optional[Callable[[navground.sim.Scenario, int], None]]

The callback called before the scenario initializes a world.

Type:

int

The default maximal number of steps to simulate during each run.

Type:

bool

Whether to terminate when all agents are idle or stuck.

Type:

float

The default time step used for simulation during each run