| using System; |
|
|
| namespace Unity.MLAgents.Sensors |
| { |
| |
| |
| |
| public class BufferSensor : ISensor, IBuiltInSensor |
| { |
| private string m_Name; |
| private int m_MaxNumObs; |
| private int m_ObsSize; |
| float[] m_ObservationBuffer; |
| int m_CurrentNumObservables; |
| ObservationSpec m_ObservationSpec; |
|
|
|
|
| |
| |
| |
| |
| |
| |
| public BufferSensor(int maxNumberObs, int obsSize, string name) |
| { |
| m_Name = name; |
| m_MaxNumObs = maxNumberObs; |
| m_ObsSize = obsSize; |
| m_ObservationBuffer = new float[m_ObsSize * m_MaxNumObs]; |
| m_CurrentNumObservables = 0; |
| m_ObservationSpec = ObservationSpec.VariableLength(m_MaxNumObs, m_ObsSize); |
| } |
|
|
| |
| public ObservationSpec GetObservationSpec() |
| { |
| return m_ObservationSpec; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| 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; |
| } |
| for (int i = 0; i < obs.Length; i++) |
| { |
| m_ObservationBuffer[m_CurrentNumObservables * m_ObsSize + i] = obs[i]; |
| } |
| m_CurrentNumObservables++; |
| } |
|
|
| |
| public int Write(ObservationWriter writer) |
| { |
| |
| |
| |
| |
|
|
| for (int i = 0; i < m_MaxNumObs; i++) |
| { |
| for (int j = 0; j < m_ObsSize; j++) |
| { |
| writer[i, j] = m_ObservationBuffer[i * m_ObsSize + j]; |
| } |
| } |
|
|
| return m_ObsSize * m_MaxNumObs; |
| } |
|
|
| |
| public virtual byte[] GetCompressedObservation() |
| { |
| return null; |
| } |
|
|
| |
| public void Update() |
| { |
| Reset(); |
| } |
|
|
| |
| public void Reset() |
| { |
| m_CurrentNumObservables = 0; |
| Array.Clear(m_ObservationBuffer, 0, m_ObservationBuffer.Length); |
| } |
|
|
| |
| public CompressionSpec GetCompressionSpec() |
| { |
| return CompressionSpec.Default(); |
| } |
|
|
| |
| public string GetName() |
| { |
| return m_Name; |
| } |
|
|
| |
| public BuiltInSensorType GetBuiltInSensorType() |
| { |
| return BuiltInSensorType.BufferSensor; |
| } |
| } |
| } |
|
|