namespace Unity.MLAgents.Sensors
{
///
/// A description of the observations that an ISensor produces.
/// This includes the size of the observation, the properties of each dimension, and how the observation
/// should be used for training.
///
public struct ObservationSpec
{
internal readonly InplaceArray m_Shape;
///
/// The size of the observations that will be generated.
/// For example, a sensor that observes the velocity of a rigid body (in 3D) would use [3].
/// A sensor that returns an RGB image would use [Height, Width, 3].
///
public InplaceArray Shape
{
get => m_Shape;
}
internal readonly InplaceArray m_DimensionProperties;
///
/// The properties of each dimensions of the observation.
/// The length of the array must be equal to the rank of the observation tensor.
///
///
/// It is generally recommended to use default values provided by helper functions,
/// as not all combinations of DimensionProperty may be supported by the trainer.
///
public InplaceArray DimensionProperties
{
get => m_DimensionProperties;
}
internal ObservationType m_ObservationType;
///
/// The type of the observation, e.g. whether they are generic or
/// help determine the goal for the Agent.
///
public ObservationType ObservationType
{
get => m_ObservationType;
}
///
/// The number of dimensions of the observation.
///
public int Rank
{
get { return Shape.Length; }
}
///
/// Construct an ObservationSpec for 1-D observations of the requested length.
///
///
///
///
public static ObservationSpec Vector(int length, ObservationType obsType = ObservationType.Default)
{
return new ObservationSpec(
new InplaceArray(length),
new InplaceArray(DimensionProperty.None),
obsType
);
}
///
/// Construct an ObservationSpec for variable-length observations.
///
///
///
///
public static ObservationSpec VariableLength(int obsSize, int maxNumObs)
{
var dimProps = new InplaceArray(
DimensionProperty.VariableSize,
DimensionProperty.None
);
return new ObservationSpec(
new InplaceArray(obsSize, maxNumObs),
dimProps
);
}
///
/// Construct an ObservationSpec for visual-like observations, e.g. observations
/// with a height, width, and possible multiple channels.
///
///
///
///
///
///
public static ObservationSpec Visual(int height, int width, int channels, ObservationType obsType = ObservationType.Default)
{
var dimProps = new InplaceArray(
DimensionProperty.TranslationalEquivariance,
DimensionProperty.TranslationalEquivariance,
DimensionProperty.None
);
return new ObservationSpec(
new InplaceArray(height, width, channels),
dimProps,
obsType
);
}
///
/// Create a general ObservationSpec from the shape, dimension properties, and observation type.
///
///
/// Note that not all combinations of DimensionProperty may be supported by the trainer.
/// shape and dimensionProperties must have the same size.
///
///
///
///
///
public ObservationSpec(
InplaceArray shape,
InplaceArray dimensionProperties,
ObservationType observationType = ObservationType.Default
)
{
if (shape.Length != dimensionProperties.Length)
{
throw new UnityAgentsException("shape and dimensionProperties must have the same length.");
}
m_Shape = shape;
m_DimensionProperties = dimensionProperties;
m_ObservationType = observationType;
}
}
}