Skip to content

Fix stacked grid sensor #5335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions com.unity.ml-agents/Runtime/Sensors/GridSensorComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Unity.MLAgents.Sensors
Expand All @@ -11,7 +12,7 @@ public class GridSensorComponent : SensorComponent
{
// dummy sensor only used for debug gizmo
GridSensorBase m_DebugSensor;
List<ISensor> m_Sensors;
List<GridSensorBase> m_Sensors;
internal BoxOverlapChecker m_BoxOverlapChecker;

[HideInInspector, SerializeField]
Expand Down Expand Up @@ -196,7 +197,6 @@ public int ObservationStacks
/// <inheritdoc/>
public override ISensor[] CreateSensors()
{
m_Sensors = new List<ISensor>();
m_BoxOverlapChecker = new BoxOverlapChecker(
m_CellScale,
m_GridSize,
Expand All @@ -213,29 +213,33 @@ public override ISensor[] CreateSensors()
m_DebugSensor = new GridSensorBase("DebugGridSensor", m_CellScale, m_GridSize, m_DetectableTags, SensorCompressionType.None);
m_BoxOverlapChecker.RegisterDebugSensor(m_DebugSensor);

var gridSensors = GetGridSensors();
if (gridSensors == null || gridSensors.Length < 1)
m_Sensors = GetGridSensors().ToList();
if (m_Sensors == null || m_Sensors.Count < 1)
{
throw new UnityAgentsException("GridSensorComponent received no sensors. Specify at least one observation type (OneHot/Counting) to use grid sensors." +
"If you're overriding GridSensorComponent.GetGridSensors(), return at least one grid sensor.");
}

foreach (var sensor in gridSensors)
// Only one sensor needs to reference the boxOverlapChecker, so that it gets updated exactly once
m_Sensors[0].m_BoxOverlapChecker = m_BoxOverlapChecker;
foreach (var sensor in m_Sensors)
{
if (ObservationStacks != 1)
{
m_Sensors.Add(new StackingSensor(sensor, ObservationStacks));
}
else
{
m_Sensors.Add(sensor);
}
m_BoxOverlapChecker.RegisterSensor(sensor);
}

// Only one sensor needs to reference the boxOverlapChecker, so that it gets updated exactly once
((GridSensorBase)m_Sensors[0]).m_BoxOverlapChecker = m_BoxOverlapChecker;
return m_Sensors.ToArray();
if (ObservationStacks != 1)
{
var sensors = new ISensor[m_Sensors.Count];
for (var i = 0; i < m_Sensors.Count; i++)
{
sensors[i] = new StackingSensor(m_Sensors[i], ObservationStacks);
}
return sensors;
}
else
{
return m_Sensors.ToArray();
}
}

/// <summary>
Expand All @@ -262,7 +266,7 @@ internal void UpdateSensor()
m_BoxOverlapChecker.ColliderMask = m_ColliderMask;
foreach (var sensor in m_Sensors)
{
((GridSensorBase)sensor).CompressionType = m_CompressionType;
sensor.CompressionType = m_CompressionType;
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion com.unity.ml-agents/Tests/Runtime/Sensor/GridSensorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void TestCreateSensor()
gridSensorComponent.SetComponentParameters(tags, useGridSensorBase: true);

gridSensorComponent.CreateSensors();
var componentSensor = (List<ISensor>)typeof(GridSensorComponent).GetField("m_Sensors",
var componentSensor = (List<GridSensorBase>)typeof(GridSensorComponent).GetField("m_Sensors",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(gridSensorComponent);
Assert.AreEqual(componentSensor.Count, 1);
}
Expand Down Expand Up @@ -191,6 +191,17 @@ public void TestNoSensors()
gridSensorComponent.CreateSensors();
});
}

[Test]
public void TestStackedSensors()
{
testGo.tag = k_Tag2;
string[] tags = { k_Tag1, k_Tag2 };
gridSensorComponent.SetComponentParameters(tags, useGridSensorBase: true);
gridSensorComponent.ObservationStacks = 3;
var sensors = gridSensorComponent.CreateSensors();
Assert.IsInstanceOf(typeof(StackingSensor), sensors[0]);
}
}
}
#endif