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 throughExperimentalRun
.use
run_once()
to perform a single run.use
run()
to perform all runs, optionally saving the data to a HDF5 dataset.use
start()
,stop()
,start_run()
,stop_run()
,update_run()
to record data without launching a simulation, for instance if you are using a different run-loop.
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:
key (Callable[[], sim.GroupRecordProbe]) – the name associated to the group
probe – the probe factory like a GroupRecordProbe class.
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:
key (Callable[[navground.sim.Dataset], sim.RecordProbe]) – the name associated to the record
probe – the probe factory like a RecordProbe class.
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:
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 callingExperimentalRun.run()
later. Else userun_once()
(orrun()
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 attributesbegin_time
[string
], ISO 8601 formatted string of the time when the experiment is run, seebegin_time
;duration_ns
[unsigned
], total duration in nanoseconds, seeduration
.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, seeduration
;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:
times
[float
] (ifRecordConfig.time
is set);poses
[float
] (ifRecordConfig.pose
is set);twists
[float
] (ifRecordConfig.twist
is set);cmds
[float
] (ifRecordConfig.cmd
is set);actuated_cmds
[float
] (ifRecordConfig.actuated_cmd
is set);targets
[float
] (ifRecordConfig.target
is set);collisions
[unsigned
] (ifRecordConfig.collisions
is set);deadlocks
[float
] (ifRecordConfig.deadlocks
is set);efficacy
[float
] (ifRecordConfig.efficacy
is set);safety_violations
[float
] (ifRecordConfig.safety_violation
is set);
and groups:
task_events
(ifRecordConfig.task_events
is set), where each agents logs, in datasettask_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
ifsave_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
it initializes a world from its scenario by calling
Scenario.init_world()
it runs the simulation, step by step, collecting data in a
ExperimentalRun
by callingExperimentalRun.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
ifsave_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:
bar – a tqdm progress bar
number_of_runs – the number of runs to track. If not provided, it will use
sim.Experiment.number_of_runs
.
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:
call
start()
For each run
i. Call
init_run()
andstart_run()
ii. from your run-loop, callupdate_run()
to record the current state of the simulated world iii. Callstop_run()
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
ifsave_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:
The system time when the experiment began.
- Type:
The duration required to perform the whole experiment.
- Type:
Determines if the experiment has finished.
- Type:
Determines if the experiment is running.
- Type:
The name of the experiment
- Type:
Default number of runs to perform
- Type:
Optional[os.PathLike]
The path where the experimental data has been saved.
-
A reference to the record config on which data to record
- Type:
dict[bool, list[Callable[[navground.sim.ExperimentalRun], None]]]
The run callbacks
- Type:
The seed/index of the next run
- Type:
The map of recorded runs.
runs are associated to the seed used to initialize them.
- Type:
Where to save the results
- Type:
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:
The default maximal number of steps to simulate during each run.
- Type:
Whether to terminate when all agents are idle or stuck.
- Type:
The default time step used for simulation during each run