|
|
|
|
|
|
|
|
|
|
namespace Unity.MLAgents.Sensors |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// A Sensor that allows to observe a variable number of entities.
|
|
|
|
/// </summary>
|
|
|
|
public class BufferSensor : ISensor, IDimensionPropertiesSensor, IBuiltInSensor |
|
|
|
{ |
|
|
|
private int m_MaxNumObs; |
|
|
|
|
|
|
static DimensionProperty[] m_DimensionProperties = new DimensionProperty[]{ |
|
|
|
DimensionProperty.VariableSize, |
|
|
|
DimensionProperty.None |
|
|
|
}; |
|
|
|
public BufferSensor(int maxNumberObs, int obsSize) |
|
|
|
{ |
|
|
|
m_MaxNumObs = maxNumberObs; |
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public DimensionProperty[] GetDimensionProperties() |
|
|
|
{ |
|
|
|
return new DimensionProperty[]{ |
|
|
|
DimensionProperty.VariableSize, |
|
|
|
DimensionProperty.None |
|
|
|
}; |
|
|
|
return m_DimensionProperties; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
/// <param name="obs"> The float array observation</param>
|
|
|
|
public void AppendObservation(float[] obs) |
|
|
|
{ |
|
|
|
if (obs.Length != m_ObsSize) |
|
|
|
{ |
|
|
|
throw new UnityAgentsException( |
|
|
|
"The BufferSensor was expecting an observation of size " + |
|
|
|
$"{m_ObsSize} but received {obs.Length} observations instead." |
|
|
|
) |
|
|
|
} |
|
|
|
if (m_CurrentNumObservables >= m_MaxNumObs) |
|
|
|
{ |
|
|
|
return; |
|
|
|