浏览代码

ObservableAttribute - use dictionary for sensor type too (#3978)

* use dictionary for sensortype too

* update comment
/docs-update
GitHub 4 年前
当前提交
e724a8ad
共有 8 个文件被更改,包括 31 次插入54 次删除
  1. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/BoolReflectionSensor.cs
  2. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/FloatReflectionSensor.cs
  3. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/IntReflectionSensor.cs
  4. 71
      com.unity.ml-agents/Runtime/Sensors/Reflection/ObservableAttribute.cs
  5. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/QuaternionReflectionSensor.cs
  6. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector2ReflectionSensor.cs
  7. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector3ReflectionSensor.cs
  8. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector4ReflectionSensor.cs

2
com.unity.ml-agents/Runtime/Sensors/Reflection/BoolReflectionSensor.cs


/// </summary>
internal class BoolReflectionSensor : ReflectionSensorBase
{
internal BoolReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
public BoolReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 1)
{}

2
com.unity.ml-agents/Runtime/Sensors/Reflection/FloatReflectionSensor.cs


/// </summary>
internal class FloatReflectionSensor : ReflectionSensorBase
{
internal FloatReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
public FloatReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 1)
{}

2
com.unity.ml-agents/Runtime/Sensors/Reflection/IntReflectionSensor.cs


/// </summary>
internal class IntReflectionSensor : ReflectionSensorBase
{
internal IntReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
public IntReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 1)
{}

71
com.unity.ml-agents/Runtime/Sensors/Reflection/ObservableAttribute.cs


const BindingFlags k_BindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
/// <summary>
/// Supported types and their observation sizes.
/// Supported types and their observation sizes and corresponding sensor type.
static Dictionary<Type, int> s_TypeSizes = new Dictionary<Type, int>()
static Dictionary<Type, (int, Type)> s_TypeToSensorInfo = new Dictionary<Type, (int, Type)>()
{typeof(int), 1},
{typeof(bool), 1},
{typeof(float), 1},
{typeof(Vector2), 2},
{typeof(Vector3), 3},
{typeof(Vector4), 4},
{typeof(Quaternion), 4},
{typeof(int), (1, typeof(IntReflectionSensor))},
{typeof(bool), (1, typeof(BoolReflectionSensor))},
{typeof(float), (1, typeof(FloatReflectionSensor))},
{typeof(Vector2), (2, typeof(Vector2ReflectionSensor))},
{typeof(Vector3), (3, typeof(Vector3ReflectionSensor))},
{typeof(Vector4), (4, typeof(Vector4ReflectionSensor))},
{typeof(Quaternion), (4, typeof(QuaternionReflectionSensor))},
};
/// <summary>

memberType = propertyInfo.PropertyType;
}
if (!s_TypeToSensorInfo.ContainsKey(memberType))
{
// For unsupported types, return null and we'll filter them out later.
return null;
}
string sensorName;
if (string.IsNullOrEmpty(observableAttribute.m_Name))
{

SensorName = sensorName
};
ISensor sensor = null;
if (memberType == typeof(Int32))
{
sensor = new IntReflectionSensor(reflectionSensorInfo);
}
else if (memberType == typeof(float))
{
sensor = new FloatReflectionSensor(reflectionSensorInfo);
}
else if (memberType == typeof(bool))
{
sensor = new BoolReflectionSensor(reflectionSensorInfo);
}
else if (memberType == typeof(Vector2))
{
sensor = new Vector2ReflectionSensor(reflectionSensorInfo);
}
else if (memberType == typeof(Vector3))
{
sensor = new Vector3ReflectionSensor(reflectionSensorInfo);
}
else if (memberType == typeof(Vector4))
{
sensor = new Vector4ReflectionSensor(reflectionSensorInfo);
}
else if (memberType == typeof(Quaternion))
{
sensor = new QuaternionReflectionSensor(reflectionSensorInfo);
}
else
{
// For unsupported types, return null and we'll filter them out later.
return null;
}
var (_, sensorType) = s_TypeToSensorInfo[memberType];
var sensor = (ISensor) Activator.CreateInstance(sensorType, reflectionSensorInfo);
// Wrap the base sensor in a StackingSensor if we're using stacking.
if (observableAttribute.m_NumStackedObservations > 1)

int sizeOut = 0;
foreach (var(field, attr) in GetObservableFields(o, excludeInherited))
{
if (s_TypeSizes.ContainsKey(field.FieldType))
if (s_TypeToSensorInfo.ContainsKey(field.FieldType))
sizeOut += s_TypeSizes[field.FieldType] * attr.m_NumStackedObservations;
var (obsSize, _) = s_TypeToSensorInfo[field.FieldType];
sizeOut += obsSize * attr.m_NumStackedObservations;
}
else
{

foreach (var(prop, attr) in GetObservableProperties(o, excludeInherited))
{
if (s_TypeSizes.ContainsKey(prop.PropertyType))
if (s_TypeToSensorInfo.ContainsKey(prop.PropertyType))
sizeOut += s_TypeSizes[prop.PropertyType] * attr.m_NumStackedObservations;
var (obsSize, _) = s_TypeToSensorInfo[prop.PropertyType];
sizeOut += obsSize * attr.m_NumStackedObservations;
}
else
{

2
com.unity.ml-agents/Runtime/Sensors/Reflection/QuaternionReflectionSensor.cs


/// </summary>
internal class QuaternionReflectionSensor : ReflectionSensorBase
{
internal QuaternionReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
public QuaternionReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 4)
{}

2
com.unity.ml-agents/Runtime/Sensors/Reflection/Vector2ReflectionSensor.cs


/// </summary>
internal class Vector2ReflectionSensor : ReflectionSensorBase
{
internal Vector2ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
public Vector2ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 2)
{}

2
com.unity.ml-agents/Runtime/Sensors/Reflection/Vector3ReflectionSensor.cs


/// </summary>
internal class Vector3ReflectionSensor : ReflectionSensorBase
{
internal Vector3ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
public Vector3ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 3)
{}

2
com.unity.ml-agents/Runtime/Sensors/Reflection/Vector4ReflectionSensor.cs


/// </summary>
internal class Vector4ReflectionSensor : ReflectionSensorBase
{
internal Vector4ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
public Vector4ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 4)
{}

正在加载...
取消
保存