浏览代码
Enum reflection sensor (#4006)
Enum reflection sensor (#4006)
* WIP enum reflection sensor * handle unknown values * changelog * handle flags/test-sampler
GitHub
5 年前
当前提交
1577786f
共有 6 个文件被更改,包括 166 次插入 和 19 次删除
-
2com.unity.ml-agents/CHANGELOG.md
-
39com.unity.ml-agents/Runtime/Sensors/Reflection/ObservableAttribute.cs
-
6com.unity.ml-agents/Runtime/Sensors/Reflection/ReflectionSensorBase.cs
-
71com.unity.ml-agents/Tests/Editor/Sensor/ObservableAttributeTests.cs
-
5com.unity.ml-agents/Tests/Editor/Sensor/VectorSensorTests.cs
-
62com.unity.ml-agents/Runtime/Sensors/Reflection/EnumReflectionSensor.cs
|
|||
using System; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.MLAgents.Sensors.Reflection |
|||
{ |
|||
internal class EnumReflectionSensor: ReflectionSensorBase |
|||
{ |
|||
Array m_Values; |
|||
bool m_IsFlags; |
|||
|
|||
internal EnumReflectionSensor(ReflectionSensorInfo reflectionSensorInfo) |
|||
: base(reflectionSensorInfo, GetEnumObservationSize(reflectionSensorInfo.GetMemberType())) |
|||
{ |
|||
var memberType = reflectionSensorInfo.GetMemberType(); |
|||
m_Values = Enum.GetValues(memberType); |
|||
m_IsFlags = memberType.IsDefined(typeof(FlagsAttribute), false); |
|||
} |
|||
|
|||
internal override void WriteReflectedField(ObservationWriter writer) |
|||
{ |
|||
// Write the enum value as a one-hot encoding.
|
|||
// Note that unknown enum values will record all 0's.
|
|||
// Flags will get treated as a sequence of bools.
|
|||
var enumValue = (Enum)GetReflectedValue(); |
|||
|
|||
int i = 0; |
|||
foreach(var val in m_Values) |
|||
{ |
|||
if (m_IsFlags) |
|||
{ |
|||
if (enumValue.HasFlag((Enum)val)) |
|||
{ |
|||
writer[i] = 1.0f; |
|||
} |
|||
else |
|||
{ |
|||
writer[i] = 0.0f; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
if (val.Equals(enumValue)) |
|||
{ |
|||
writer[i] = 1.0f; |
|||
} |
|||
else |
|||
{ |
|||
writer[i] = 0.0f; |
|||
} |
|||
} |
|||
i++; |
|||
} |
|||
} |
|||
|
|||
internal static int GetEnumObservationSize(Type t) |
|||
{ |
|||
var values = Enum.GetValues(t); |
|||
// Account for all enum values
|
|||
return values.Length; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue