浏览代码

Merge pull request #5020 from Unity-Technologies/fix-adding-name-field-to-var-len-obs-and-adding-to-changelog

Adding a SensorName field to BufferSensor
/develop/gail-srl-hack
GitHub 4 年前
当前提交
766c4e27
共有 7 个文件被更改,包括 84 次插入8 次删除
  1. 5
      Project/Assets/ML-Agents/Examples/Sorter/Prefabs/Area.prefab
  2. 1
      com.unity.ml-agents/CHANGELOG.md
  3. 8
      com.unity.ml-agents/Runtime/Sensors/BufferSensor.cs
  4. 31
      com.unity.ml-agents/Runtime/Sensors/BufferSensorComponent.cs
  5. 5
      com.unity.ml-agents/Tests/Editor/Sensor/BufferSensorTest.cs
  6. 31
      com.unity.ml-agents/Editor/BufferSensorComponentEditor.cs
  7. 11
      com.unity.ml-agents/Editor/BufferSensorComponentEditor.cs.meta

5
Project/Assets/ML-Agents/Examples/Sorter/Prefabs/Area.prefab


m_Script: {fileID: 11500000, guid: dd8012d5925524537b27131fef517017, type: 3}
m_Name:
m_EditorClassIdentifier:
ObservableSize: 23
MaxNumObservables: 20
m_SensorName: BufferSensor
m_ObservableSize: 23
m_MaxNumObservables: 20
--- !u!1 &6000518840957865293
GameObject:
m_ObjectHideFlags: 0

1
com.unity.ml-agents/CHANGELOG.md


## [Unreleased]
### Major Changes
#### com.unity.ml-agents (C#)
- The `BufferSensor` and `BufferSensorComponent` have been added. They allow the Agent to observe variable number of entities. (#4909)
#### ml-agents / ml-agents-envs / gym-unity (Python)
### Minor Changes

8
com.unity.ml-agents/Runtime/Sensors/BufferSensor.cs


/// </summary>
public class BufferSensor : ISensor, IDimensionPropertiesSensor, IBuiltInSensor
{
private string m_Name;
private int m_MaxNumObs;
private int m_ObsSize;
float[] m_ObservationBuffer;

DimensionProperty.None
};
public BufferSensor(int maxNumberObs, int obsSize)
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];

Array.Clear(m_ObservationBuffer, 0, m_ObservationBuffer.Length);
}
/// <inheritdoc/>
/// <inheritdoc/>
return "BufferSensor";
return m_Name;
}
/// <inheritdoc/>

31
com.unity.ml-agents/Runtime/Sensors/BufferSensorComponent.cs


[AddComponentMenu("ML Agents/Buffer Sensor", (int)MenuGroup.Sensors)]
public class BufferSensorComponent : SensorComponent
{
/// <summary>
/// Name of the generated <see cref="bufferSensor"/> object.
/// Note that changing this at runtime does not affect how the Agent sorts the sensors.
/// </summary>
public string SensorName
{
get { return m_SensorName; }
set { m_SensorName = value; }
}
[HideInInspector, SerializeField]
private string m_SensorName = "BufferSensor";
public int ObservableSize;
public int ObservableSize
{
get { return m_ObservableSize; }
set { m_ObservableSize = value; }
}
[HideInInspector, SerializeField]
private int m_ObservableSize;
public int MaxNumObservables;
public int MaxNumObservables
{
get { return m_MaxNumObservables; }
set { m_MaxNumObservables = value; }
}
[HideInInspector, SerializeField]
private int m_MaxNumObservables;
private BufferSensor m_Sensor;

m_Sensor = new BufferSensor(MaxNumObservables, ObservableSize);
m_Sensor = new BufferSensor(MaxNumObservables, ObservableSize, m_SensorName);
return m_Sensor;
}

5
com.unity.ml-agents/Tests/Editor/Sensor/BufferSensorTest.cs


public void TestBufferSensor()
{
var bufferSensor = new BufferSensor(20, 4);
var bufferSensor = new BufferSensor(20, 4, "testName");
var shape = bufferSensor.GetObservationShape();
var dimProp = bufferSensor.GetDimensionProperties();
Assert.AreEqual(shape[0], 20);

var bufferComponent = agentGameObj.AddComponent<BufferSensorComponent>();
bufferComponent.MaxNumObservables = 20;
bufferComponent.ObservableSize = 4;
bufferComponent.SensorName = "TestName";
var sensor = bufferComponent.CreateSensor();
var shape = bufferComponent.GetObservationShape();

Assert.AreEqual(shape, obs.Shape);
Assert.AreEqual(obs.DimensionProperties.Count, 2);
Assert.AreEqual(sensor.GetName(), "TestName");
for (int i = 0; i < 8; i++)
{

31
com.unity.ml-agents/Editor/BufferSensorComponentEditor.cs


using UnityEditor;
using Unity.MLAgents.Sensors;
namespace Unity.MLAgents.Editor
{
[CustomEditor(typeof(BufferSensorComponent))]
[CanEditMultipleObjects]
internal class BufferSensorComponentEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
var so = serializedObject;
so.Update();
// Drawing the BufferSensorComponent
EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties());
{
// These fields affect the sensor order or observation size,
// So can't be changed at runtime.
EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_ObservableSize"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_MaxNumObservables"), true);
}
EditorGUI.EndDisabledGroup();
so.ApplyModifiedProperties();
}
}
}

11
com.unity.ml-agents/Editor/BufferSensorComponentEditor.cs.meta


fileFormatVersion: 2
guid: b042fe65027f94c1eb38a2ee1362d38d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存