5.4 KiB
Scenarios
Scenarios have three responsibilities:
- Controlling the execution flow of your simulation
- Organizing a list of Randomizers
- Defining settings that can be configured externally from a built Unity player
By default, the Perception package includes one ready-made Scenario, the FixedLengthScenario
class. This Scenario runs each Iteration for a fixed number of frames and is compatible with the Run in Unity Simulation window for cloud simulation execution.
Scenario Lifecycle Hooks
Scenarios have a number of lifecycle hooks that are called during execution. Below is a diagram visualizing the sequence of operations run by a typical scenario:
JSON Configuration
Scenarios can be serialized to JSON, modified, and imported at runtime to configure simulation behavior even after a Unity player has been built. The following scenario settings can be serialized by default using the "Generate JSON Config" button on the scenario inspector:
- Scenario constants
- String, numeric, and boolean fields on Randomizers and Parameters
- Constant, Uniform, and Normal sampler configurations
Here is an example of how to load a Scenario JSON configuration into a Windows Unity player using the --scenario-config-file
flag:
.\PerceptionTutorial.exe --scenario-config-file scenarioConfiguration.json
Scenario Cloud Execution (Unity Simulation)
Users can utilize Unity's Unity Simulation service to execute a Scenario in the cloud through the perception package's Run in Unity Simulation window. To open this window from the Unity editor using the top menu bar, navigate to Window -> Run in Unity Simulation
.
NOTE: To execute a Scenario using the Run in Unity Simulation window, the Scenario class must implement the UnitySimulationScenario class.
From the newly opened editor window, customize the following settings to configure a new Unity Simulation run:
- Run Name - the name of the Unity Simulation run (example: TestRun0)
- Total Iterations - The number of Scenario Iterations to complete during the run
- Instance Count - The number of Unity Simulation worker instances to distribute execution between
- Sys-Param - The system parameters or the hardware configuration of Unity Simulation worker instances to execute the Scenario with. Determines per instance specifications such as the number of CPU cores, amount of memory, and presence of a GPU for accelerated execution.
- Json Configuration - This field is optional. You can select a json scenario configuration within your project to configure your Unity Simulation run instead of using the Scenario settings presently configured in the Editor.
Implementing Custom Scenarios
For use cases where the Scenario should run for an arbitrary number of frames, implementing a custom Scenario may be necessary. Below are the two most common Scenario properties a user might want to override to implement custom Scenario Iteration conditions:
- isScenarioReadyToStart - defines the conditions that determine when a Scenario can begin iterating
- isIterationComplete - defines the conditions that determine the end of a Scenario iteration
- isScenarioComplete - defines the conditions that determine the a Scenario to stop iterating
Abstract Scenario Classes
There are a few abstract scenario classes to choose from depending on your requirements when deriving a custom scenario. Below is a list of these options and when it is appropriate to derive them for your use case:
- Scenario - This is the most basic scenario class to derive if only the basic scenario lifecycle coordination tooling is necessary
- PerceptionScenario - The perception scenario abstract class introduces some useful functionality for Scenarios that intend to utilize the Perception package's data capture tooling to generate datasets.
- UnitySimulationScenario - This abstract Scenario class is useful for implementing custom perception Scenarios that are compatible with the "Run in Unity Simulation Window." The FixedLengthScenario class is an example of a Scenario deriving from the UnitySimulationScenario class.
Scenario Constants
Constants include properties such as starting iteration value or total iteration count that configure the lifecycle settings of the scenario. By deriving your own constants class you can add your own custom Scenario settings for configuring different scenario properties. Below is an example of the Constants class used in the FixedLengthScenario
class:
[Serializable]
public class Constants : UnitySimulationScenarioConstants
{
public int framesPerIteration = 1;
}
There are a few key things to note here:
- The Constants class will need to inherit from
UnitySimulationScenarioConstants
to be compatible with the Run in Unity Simulation window. Deriving fromUnitySimulationScenarioConstants
will add a few key properties to the Constants class that are needed to coordinate a Unity Simulation run. If running in Unity Simulation is not a requirement, a new scenario constants class can derive from the base ScenarioConstants class instead. - Make sure to include the
[Serializable]
attribute on a constant class. This will ensure that the Constants can be properly configured from the Unity inspector. - A Scenario class's
SerializeToJson()
andDeserializeFromJson()
methods can be overridden to implement custom serialization strategies.