浏览代码

optional ObsType in ObsSpec utils, clean up InplaceArray

/v2-staging-rebase
Chris Elion 4 年前
当前提交
280ecca6
共有 2 个文件被更改,包括 21 次插入24 次删除
  1. 27
      com.unity.ml-agents/Runtime/InplaceArray.cs
  2. 18
      com.unity.ml-agents/Runtime/Sensors/ObservationSpec.cs

27
com.unity.ml-agents/Runtime/InplaceArray.cs


/// This does not implement any interfaces such as IList, in order to avoid any accidental boxing allocations.
/// </remarks>
/// <typeparam name="T"></typeparam>
public struct InplaceArray<T> where T : struct
public struct InplaceArray<T> : IEquatable<InplaceArray<T>> where T : struct
{
private const int k_MaxLength = 4;
private readonly int m_Length;

/// <returns>Whether the arrays are equivalent.</returns>
public static bool operator ==(InplaceArray<T> lhs, InplaceArray<T> rhs)
{
if (lhs.Length != rhs.Length)
{
return false;
}
for (var i = 0; i < lhs.Length; i++)
{
// See https://stackoverflow.com/a/390974/224264
if (!EqualityComparer<T>.Default.Equals(lhs[i], rhs[i]))
{
return false;
}
}
return true;
return lhs.Equals(rhs);
}
/// <summary>

/// <param name="rhs"></param>
/// <returns>Whether the arrays are not equivalent</returns>
public static bool operator !=(InplaceArray<T> lhs, InplaceArray<T> rhs) => !(lhs == rhs);
public static bool operator !=(InplaceArray<T> lhs, InplaceArray<T> rhs) => !lhs.Equals(rhs);
/// <summary>
/// Check that the arrays are equivalent.

/// <returns>Whether the arrays are not equivalent</returns>
public bool Equals(InplaceArray<T> other)
{
return this == other;
// See https://montemagno.com/optimizing-c-struct-equality-with-iequatable/
var thisTuple = (m_Elem0, m_Elem1, m_Elem2, m_Elem3, Length);
var otherTuple = (other.m_Elem0, other.m_Elem1, other.m_Elem2, other.m_Elem3, other.Length);
return thisTuple.Equals(otherTuple);
}
/// <summary>

public override int GetHashCode()
{
return Tuple.Create(m_Elem0, m_Elem1, m_Elem2, m_Elem3, Length).GetHashCode();
return (m_Elem0, m_Elem1, m_Elem2, m_Elem3, Length).GetHashCode();
}
}
}

18
com.unity.ml-agents/Runtime/Sensors/ObservationSpec.cs


/// Construct an ObservationSpec for 1-D observations of the requested length.
/// </summary>
/// <param name="length"></param>
/// <param name="obsType"></param>
public static ObservationSpec Vector(int length)
public static ObservationSpec Vector(int length, ObservationType obsType = ObservationType.Default)
new InplaceArray<DimensionProperty>(DimensionProperty.None)
new InplaceArray<DimensionProperty>(DimensionProperty.None),
obsType
);
}

/// <param name="obsSize"></param>
/// <param name="maxNumObs"></param>
/// <param name="obsType"></param>
public static ObservationSpec VariableLength(int obsSize, int maxNumObs)
public static ObservationSpec VariableLength(int obsSize, int maxNumObs, ObservationType obsType = ObservationType.Default)
{
var dimProps = new InplaceArray<DimensionProperty>(
DimensionProperty.VariableSize,

new InplaceArray<int>(obsSize, maxNumObs),
dimProps
dimProps,
obsType
);
}

/// <param name="height"></param>
/// <param name="width"></param>
/// <param name="channels"></param>
/// <param name="obsType"></param>
public static ObservationSpec Visual(int height, int width, int channels)
public static ObservationSpec Visual(int height, int width, int channels, ObservationType obsType = ObservationType.Default)
{
var dimProps = new InplaceArray<DimensionProperty>(
DimensionProperty.TranslationalEquivariance,

return new ObservationSpec(
new InplaceArray<int>(height, width, channels),
dimProps
dimProps,
obsType
);
}

正在加载...
取消
保存