浏览代码

add some utility methods to ObservationWriter (#4113)

/MLA-1734-demo-provider
GitHub 5 年前
当前提交
63195e42
共有 4 个文件被更改,包括 74 次插入13 次删除
  1. 73
      com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs
  2. 5
      com.unity.ml-agents/Runtime/Sensors/Reflection/QuaternionReflectionSensor.cs
  3. 4
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector3ReflectionSensor.cs
  4. 5
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector4ReflectionSensor.cs

73
com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs


using System.Collections.Generic;
using Unity.Barracuda;
using Unity.MLAgents.Inference;
using UnityEngine;
namespace Unity.MLAgents.Sensors
{

TensorShape m_TensorShape;
internal ObservationWriter() { }
internal ObservationWriter() {}
/// <summary>
/// Set the writer to write to an IList at the given channelOffset.

}
/// <summary>
/// 3D write access at the specified height, width, and channel. Only usable with a TensorProxy target.
/// 3D write access at the specified height, width, and channel.
/// </summary>
/// <param name="h"></param>
/// <param name="w"></param>

m_Proxy.data[m_Batch, index + m_Offset + writeOffset] = val;
index++;
}
}
}
/// <summary>
/// Write the Vector3 components.
/// </summary>
/// <param name="vec">The Vector3 to be written.</param>
/// <param name="writeOffset">Optional write offset.</param>
public void Add(Vector3 vec, int writeOffset = 0)
{
if (m_Data != null)
{
m_Data[m_Offset + writeOffset + 0] = vec.x;
m_Data[m_Offset + writeOffset + 1] = vec.y;
m_Data[m_Offset + writeOffset + 2] = vec.z;
}
else
{
m_Proxy.data[m_Batch, m_Offset + writeOffset + 0] = vec.x;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 1] = vec.y;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 2] = vec.z;
}
}
/// <summary>
/// Write the Vector4 components.
/// </summary>
/// <param name="vec">The Vector4 to be written.</param>
/// <param name="writeOffset">Optional write offset.</param>
public void Add(Vector4 vec, int writeOffset = 0)
{
if (m_Data != null)
{
m_Data[m_Offset + writeOffset + 0] = vec.x;
m_Data[m_Offset + writeOffset + 1] = vec.y;
m_Data[m_Offset + writeOffset + 2] = vec.z;
m_Data[m_Offset + writeOffset + 3] = vec.w;
}
else
{
m_Proxy.data[m_Batch, m_Offset + writeOffset + 0] = vec.x;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 1] = vec.y;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 2] = vec.z;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 3] = vec.w;
}
}
/// <summary>
/// Write the Quaternion components.
/// </summary>
/// <param name="quat">The Quaternion to be written.</param>
/// <param name="writeOffset">Optional write offset.</param>
public void Add(Quaternion quat, int writeOffset = 0)
{
if (m_Data != null)
{
m_Data[m_Offset + writeOffset + 0] = quat.x;
m_Data[m_Offset + writeOffset + 1] = quat.y;
m_Data[m_Offset + writeOffset + 2] = quat.z;
m_Data[m_Offset + writeOffset + 3] = quat.w;
}
else
{
m_Proxy.data[m_Batch, m_Offset + writeOffset + 0] = quat.x;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 1] = quat.y;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 2] = quat.z;
m_Proxy.data[m_Batch, m_Offset + writeOffset + 3] = quat.w;
}
}
}

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


internal override void WriteReflectedField(ObservationWriter writer)
{
var quatVal = (UnityEngine.Quaternion)GetReflectedValue();
writer[0] = quatVal.x;
writer[1] = quatVal.y;
writer[2] = quatVal.z;
writer[3] = quatVal.w;
writer.Add(quatVal);
}
}
}

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


internal override void WriteReflectedField(ObservationWriter writer)
{
var vecVal = (UnityEngine.Vector3)GetReflectedValue();
writer[0] = vecVal.x;
writer[1] = vecVal.y;
writer[2] = vecVal.z;
writer.Add(vecVal);
}
}
}

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


internal override void WriteReflectedField(ObservationWriter writer)
{
var vecVal = (UnityEngine.Vector4)GetReflectedValue();
writer[0] = vecVal.x;
writer[1] = vecVal.y;
writer[2] = vecVal.z;
writer[3] = vecVal.w;
writer.Add(vecVal);
}
}
}
正在加载...
取消
保存