浏览代码

remove deprecated RayPerceptionMB, legacy codepath: (#3304)

/asymm-envs
GitHub 5 年前
当前提交
be0e5635
共有 7 个文件被更改,包括 4 次插入176 次删除
  1. 21
      com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs
  2. 11
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs.meta
  3. 11
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception2D.cs.meta
  4. 11
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs.meta
  5. 15
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs
  6. 56
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception2D.cs
  7. 55
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs

21
com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs


{
PerceiveStatic(
m_RayDistance, m_Angles, m_DetectableObjects, m_StartOffset, m_EndOffset,
m_CastRadius, m_Transform, m_CastType, m_Observations, false, m_LayerMask,
m_CastRadius, m_Transform, m_CastType, m_Observations, m_LayerMask,
m_DebugDisplayInfo
);
adapter.AddRange(m_Observations);

/// 3. The 'length+1' element of the sublist will contain the normalised distance to the object hit, or 1 if
/// nothing was hit.
///
/// The legacyHitFractionBehavior changes the behavior to be backwards compatible but has some
/// counter-intuitive behavior:
/// * if the cast hits a object that's not in the detectableObjects list, all results are 0
/// * if the cast doesn't hit, the hit fraction field is 0
/// </summary>
/// <param name="rayLength"></param>
/// <param name="rayAngles">List of angles (in degrees) used to define the rays. 90 degrees is considered

/// <param name="transform">Transform of the GameObject</param>
/// <param name="castType">Whether to perform the casts in 2D or 3D.</param>
/// <param name="perceptionBuffer">Output array of floats. Must be (num rays) * (num tags + 2) in size.</param>
/// <param name="legacyHitFractionBehavior">Whether to use the legacy behavior for hit fractions.</param>
/// <param name="debugInfo">Optional debug information output, only used by RayPerceptionSensor.</param>
///
public static void PerceiveStatic(float rayLength,

bool legacyHitFractionBehavior = false,
int layerMask = Physics.DefaultRaycastLayers,
DebugDisplayInfo debugInfo = null)
{

// sublist[numObjects-1] <- did hit detectableObjects[numObjects-1]
// sublist[numObjects ] <- 1 if missed else 0
// sublist[numObjects+1] <- hit fraction (or 1 if no hit)
// The legacyHitFractionBehavior changes the behavior to be backwards compatible but has some
// counter-intuitive behavior:
// * if the cast hits a object that's not in the detectableObjects list, all results are 0
// * if the cast doesn't hit, the hit fraction field is 0
bool castHit;
float hitFraction;

}
}
if (!hitTaggedObject && !legacyHitFractionBehavior)
if (!hitTaggedObject)
{
// Something was hit but not on the list. Still set the hit fraction.
perceptionBuffer[bufferOffset + detectableObjects.Count + 1] = hitFraction;

{
perceptionBuffer[bufferOffset + detectableObjects.Count] = 1f;
if (!legacyHitFractionBehavior)
{
// Nothing was hit, so there's full clearance in front of the agent.
perceptionBuffer[bufferOffset + detectableObjects.Count + 1] = 1.0f;
}
// Nothing was hit, so there's full clearance in front of the agent.
perceptionBuffer[bufferOffset + detectableObjects.Count + 1] = 1.0f;
}
bufferOffset += detectableObjects.Count + 2;

11
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs.meta


fileFormatVersion: 2
guid: a14e2e238ae844231bc2c88e17bae5a5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception2D.cs.meta


fileFormatVersion: 2
guid: 59a9c4378bb2a40c49eb7a248c52fbf5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs.meta


fileFormatVersion: 2
guid: bb172294dbbcc408286b156a2c4b553c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

15
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs


using System;
using System.Collections.Generic;
using UnityEngine;
[Obsolete]
public abstract class RayPerception : MonoBehaviour
{
protected float[] m_PerceptionBuffer;
abstract public IList<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects,
float startOffset=0.0f, float endOffset=0.0f);
}

56
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception2D.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using MLAgents.Sensor;
namespace MLAgents
{
/// <summary>
/// Ray 2D perception component. Attach this to agents to enable "local perception"
/// via the use of ray casts directed outward from the agent.
/// </summary>
[Obsolete("The RayPerception MonoBehaviour is deprecated. Use the RayPerceptionSensorComponent instead")]
public class RayPerception2D : RayPerception
{
RaycastHit2D m_Hit;
/// <summary>
/// Creates perception vector to be used as part of an observation of an agent.
/// Each ray in the rayAngles array adds a sublist of data to the observation.
/// The sublist contains the observation data for a single ray. The list is composed of the following:
/// 1. A one-hot encoding for detectable objects. For example, if detectableObjects.Length = n, the
/// first n elements of the sublist will be a one-hot encoding of the detectableObject that was hit, or
/// all zeroes otherwise.
/// 2. The 'length' element of the sublist will be 1 if the ray missed everything, or 0 if it hit
/// something (detectable or not).
/// 3. The 'length+1' element of the sublist will contain the normalised distance to the object hit.
/// NOTE: Only objects with tags in the detectableObjects array will have a distance set.
/// </summary>
/// <returns>The partial vector observation corresponding to the set of rays</returns>
/// <param name="rayDistance">Radius of rays</param>
/// <param name="rayAngles">Angles of rays (starting from (1,0) on unit circle).</param>
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
/// <param name="startOffset">Unused</param>
/// <param name="endOffset">Unused</param>
public override IList<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects,
float startOffset=0.0f, float endOffset=0.0f)
{
var perceptionSize = (detectableObjects.Length + 2) * rayAngles.Length;
if (m_PerceptionBuffer == null || m_PerceptionBuffer.Length != perceptionSize)
{
m_PerceptionBuffer = new float[perceptionSize];
}
const float castRadius = 0.5f;
const bool legacyHitFractionBehavior = true;
RayPerceptionSensor.PerceiveStatic(
rayDistance, rayAngles, detectableObjects, startOffset, endOffset, castRadius,
transform, RayPerceptionSensor.CastType.Cast3D, m_PerceptionBuffer, legacyHitFractionBehavior
);
return m_PerceptionBuffer;
}
}
}

55
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using MLAgents.Sensor;
namespace MLAgents
{
/// <summary>
/// Ray perception component. Attach this to agents to enable "local perception"
/// via the use of ray casts directed outward from the agent.
/// </summary>
[Obsolete("The RayPerception MonoBehaviour is deprecated. Use the RayPerceptionSensorComponent instead")]
public class RayPerception3D : RayPerception
{
/// <summary>
/// Creates perception vector to be used as part of an observation of an agent.
/// Each ray in the rayAngles array adds a sublist of data to the observation.
/// The sublist contains the observation data for a single ray. The list is composed of the following:
/// 1. A one-hot encoding for detectable objects. For example, if detectableObjects.Length = n, the
/// first n elements of the sublist will be a one-hot encoding of the detectableObject that was hit, or
/// all zeroes otherwise.
/// 2. The 'length' element of the sublist will be 1 if the ray missed everything, or 0 if it hit
/// something (detectable or not).
/// 3. The 'length+1' element of the sublist will contain the normalised distance to the object hit.
/// NOTE: Only objects with tags in the detectableObjects array will have a distance set.
/// </summary>
/// <returns>The partial vector observation corresponding to the set of rays</returns>
/// <param name="rayDistance">Radius of rays</param>
/// <param name="rayAngles">Angles of rays (starting from (1,0) on unit circle).</param>
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
/// <param name="startOffset">Starting height offset of ray from center of agent.</param>
/// <param name="endOffset">Ending height offset of ray from center of agent.</param>
public override IList<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects,
float startOffset=0.0f, float endOffset=0.0f)
{
var perceptionSize = (detectableObjects.Length + 2) * rayAngles.Length;
if (m_PerceptionBuffer == null || m_PerceptionBuffer.Length != perceptionSize)
{
m_PerceptionBuffer = new float[perceptionSize];
}
const float castRadius = 0.5f;
const bool legacyHitFractionBehavior = true;
RayPerceptionSensor.PerceiveStatic(
rayDistance, rayAngles, detectableObjects, startOffset, endOffset, castRadius,
transform, RayPerceptionSensor.CastType.Cast3D, m_PerceptionBuffer, legacyHitFractionBehavior
);
return m_PerceptionBuffer;
}
}
}
正在加载...
取消
保存