Class GridSensorBase
Grid-based sensor.
Namespace: Unity.MLAgents.Sensors
Syntax
public class GridSensorBase : object, ISensor, IBuiltInSensor, IDisposable
Constructors
GridSensorBase(String, Vector3, Vector3Int, String[], SensorCompressionType)
Create a GridSensorBase with the specified configuration.
Declaration
public GridSensorBase(string name, Vector3 cellScale, Vector3Int gridSize, string[] detectableTags, SensorCompressionType compression)
Parameters
Type | Name | Description |
---|---|---|
String | name | The sensor name |
Vector3 | cellScale | The scale of each cell in the grid |
Vector3Int | gridSize | Number of cells on each side of the grid |
String[] | detectableTags | Tags to be detected by the sensor |
SensorCompressionType | compression | Compression type |
Properties
CompressionType
The compression type used by the sensor.
Declaration
public SensorCompressionType CompressionType { get; set; }
Property Value
Type | Description |
---|---|
SensorCompressionType |
DetectableTags
The tags which the sensor dectects.
Declaration
protected string[] DetectableTags { get; }
Property Value
Type | Description |
---|---|
String[] |
Methods
Dispose()
Clean up the internal objects.
Declaration
public void Dispose()
GetBuiltInSensorType()
Declaration
public BuiltInSensorType GetBuiltInSensorType()
Returns
Type | Description |
---|---|
BuiltInSensorType |
GetCellObservationSize()
Get the observation size for each cell. This will be the size of dataBuffer for GetObjectData(GameObject, Int32, Single[]). If overriding GetObjectData(GameObject, Int32, Single[]), override this method as well to the custom observation size.
Declaration
protected virtual int GetCellObservationSize()
Returns
Type | Description |
---|---|
Int32 | The observation size of each cell. |
GetCompressedObservation()
Return a compressed representation of the observation. For small observations, this should generally not be implemented. However, compressing large observations (such as visual results) can significantly improve model training time.
Declaration
public byte[] GetCompressedObservation()
Returns
Type | Description |
---|---|
Byte[] | Compressed observation. |
Implements
GetCompressionSpec()
Return information on the compression type being used. If no compression is used, return Default().
Declaration
public CompressionSpec GetCompressionSpec()
Returns
Type | Description |
---|---|
CompressionSpec | An object describing the compression used by the sensor. |
Implements
GetName()
Get the name of the sensor. This is used to ensure deterministic sorting of the sensors on an Agent, so the naming must be consistent across all sensors and agents.
Declaration
public string GetName()
Returns
Type | Description |
---|---|
String | The name of the sensor. |
Implements
GetObjectData(GameObject, Int32, Single[])
Get the observation values of the detected game object. Default is to record the detected tag index.
This method can be overridden to encode the observation differently or get custom data from the object. When overriding this method, GetCellObservationSize() and IsDataNormalized() might also need to change accordingly.
Declaration
protected virtual void GetObjectData(GameObject detectedObject, int tagIndex, float[] dataBuffer)
Parameters
Type | Name | Description |
---|---|---|
GameObject | detectedObject | The game object that was detected within a certain cell |
Int32 | tagIndex | The index of the detectedObject's tag in the DetectableObjects list |
Single[] | dataBuffer | The buffer to write the observation values. The buffer size is configured by GetCellObservationSize(). |
Examples
Here is an example of overriding GetObjectData to get the velocity of a potential Rigidbody:
protected override void GetObjectData(GameObject detectedObject, int tagIndex, float[] dataBuffer)
{
if (tagIndex == Array.IndexOf(DetectableTags, "RigidBodyObject"))
{
Rigidbody rigidbody = detectedObject.GetComponent<Rigidbody>();
dataBuffer[0] = rigidbody.velocity.x;
dataBuffer[1] = rigidbody.velocity.y;
dataBuffer[2] = rigidbody.velocity.z;
}
}
GetObservationSpec()
Returns a description of the observations that will be generated by the sensor. See ObservationSpec for more details, and helper methods to create one.
Declaration
public ObservationSpec GetObservationSpec()
Returns
Type | Description |
---|---|
ObservationSpec | An object describing the observation. |
Implements
GetProcessCollidersMethod()
Whether to process all detected colliders in a cell. Default to false and only use the one closest to the agent. If overriding GetObjectData(GameObject, Int32, Single[]), consider override this method when needed.
Declaration
protected virtual ProcessCollidersMethod GetProcessCollidersMethod()
Returns
Type | Description |
---|---|
ProcessCollidersMethod | Bool value indicating whether to process all detected colliders in a cell. |
IsDataNormalized()
Whether the data is normalized within [0, 1]. The sensor can only use PNG compression if the data is normailzed. If overriding GetObjectData(GameObject, Int32, Single[]), override this method as well according to the custom observation values.
Declaration
protected virtual bool IsDataNormalized()
Returns
Type | Description |
---|---|
Boolean | Bool value indicating whether data is normalized. |
Reset()
Resets the internal state of the sensor. This is called at the end of an Agent's episode. Most implementations can leave this empty.
Declaration
public void Reset()
Implements
ResetPerceptionBuffer()
Clears the perception buffer before loading in new data.
Declaration
public void ResetPerceptionBuffer()
Update()
Update any internal state of the sensor. This is called once per each agent step.
Declaration
public void Update()
Implements
Write(ObservationWriter)
Write the observation data directly to the ObservationWriter. Note that this (and GetCompressedObservation()) may be called multiple times per agent step, so should not mutate any internal state.
Declaration
public int Write(ObservationWriter writer)
Parameters
Type | Name | Description |
---|---|---|
ObservationWriter | writer | Where the observations will be written to. |
Returns
Type | Description |
---|---|
Int32 | The number of elements written. |