Skip to content

Commit a820650

Browse files
author
Chris Elion
authored
RayPerception sensor (#2874)
1 parent c85f944 commit a820650

29 files changed

+803
-156
lines changed

UnitySDK/Assets/ML-Agents/Editor/BehaviorParametersEditor.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using UnityEngine;
22
using UnityEditor;
33
using Barracuda;
4+
using MLAgents.Sensor;
45

56
namespace MLAgents
67
{
@@ -56,15 +57,17 @@ void DisplayFailedModelChecks()
5657
D.logEnabled = false;
5758
Model barracudaModel = null;
5859
var model = (NNModel)serializedObject.FindProperty("m_Model").objectReferenceValue;
59-
var brainParameters = ((BehaviorParameters)target).brainParameters;
60+
var behaviorParameters = (BehaviorParameters)target;
61+
var sensorComponents = behaviorParameters.GetComponents<SensorComponent>();
62+
var brainParameters = behaviorParameters.brainParameters;
6063
if (model != null)
6164
{
6265
barracudaModel = ModelLoader.Load(model.Value);
6366
}
6467
if (brainParameters != null)
6568
{
6669
var failedChecks = InferenceBrain.BarracudaModelParamLoader.CheckModel(
67-
barracudaModel, brainParameters);
70+
barracudaModel, brainParameters, sensorComponents);
6871
foreach (var check in failedChecks)
6972
{
7073
if (check != null)

UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System.Collections.Generic;
21
using NUnit.Framework;
32
using UnityEngine;
43
using System.IO.Abstractions.TestingHelpers;
54
using System.Reflection;
65
using MLAgents.CommunicatorObjects;
7-
using Google.Protobuf;
86

97
namespace MLAgents.Tests
108
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NUnit.Framework;
2+
using UnityEngine;
3+
using MLAgents.Sensor;
4+
5+
namespace MLAgents.Tests
6+
{
7+
public class RayPerceptionSensorTests
8+
{
9+
[Test]
10+
public void TestGetRayAngles()
11+
{
12+
var angles = RayPerceptionSensorComponentBase.GetRayAngles(3, 90f);
13+
var expectedAngles = new [] { 90f, 60f, 120f, 30f, 150f, 0f, 180f };
14+
Assert.AreEqual(expectedAngles.Length, angles.Length);
15+
for (var i = 0; i < angles.Length; i++)
16+
{
17+
Assert.AreEqual(expectedAngles[i], angles[i], .01);
18+
}
19+
}
20+
}
21+
}

UnitySDK/Assets/ML-Agents/Editor/Tests/Sensor/RayPerceptionSensorTests.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitySDK/Assets/ML-Agents/Editor/Tests/Sensor/WriterAdapterTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using Barracuda;
66
using MLAgents.InferenceBrain;
7-
using MLAgents.InferenceBrain.Utils;
87

98

109
namespace MLAgents.Tests

UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ public void MoveAgent(float[] act)
150150
{
151151
var myTransform = transform;
152152
myLaser.transform.localScale = new Vector3(1f, 1f, m_LaserLength);
153-
var position = myTransform.TransformDirection(RayPerception3D.PolarToCartesian(25f, 90f));
154-
Debug.DrawRay(myTransform.position, position, Color.red, 0f, true);
153+
var rayDir = 25.0f * myTransform.forward;
154+
Debug.DrawRay(myTransform.position, rayDir, Color.red, 0f, true);
155155
RaycastHit hit;
156-
if (Physics.SphereCast(transform.position, 2f, position, out hit, 25f))
156+
if (Physics.SphereCast(transform.position, 2f, rayDir, out hit, 25f))
157157
{
158158
if (hit.collider.gameObject.CompareTag("agent"))
159159
{
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using UnityEngine;
34

45
public abstract class RayPerception : MonoBehaviour
56
{
6-
protected List<float> m_PerceptionBuffer = new List<float>();
7+
protected float[] m_PerceptionBuffer;
78

8-
abstract public List<float> Perceive(float rayDistance,
9+
abstract public IList<float> Perceive(float rayDistance,
910
float[] rayAngles, string[] detectableObjects,
1011
float startOffset=0.0f, float endOffset=0.0f);
1112

12-
/// <summary>
13-
/// Converts degrees to radians.
14-
/// </summary>
15-
public static float DegreeToRadian(float degree)
16-
{
17-
return degree * Mathf.PI / 180f;
18-
}
13+
1914
}
Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using UnityEngine;
4+
using MLAgents.Sensor;
35

46
namespace MLAgents
57
{
68
/// <summary>
79
/// Ray 2D perception component. Attach this to agents to enable "local perception"
810
/// via the use of ray casts directed outward from the agent.
911
/// </summary>
12+
[Obsolete("The RayPerception MonoBehaviour is deprecated. Use the RayPerceptionSensorComponent instead")]
1013
public class RayPerception2D : RayPerception
1114
{
12-
Vector2 m_EndPosition;
1315
RaycastHit2D m_Hit;
1416

1517
/// <summary>
@@ -30,56 +32,25 @@ public class RayPerception2D : RayPerception
3032
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
3133
/// <param name="startOffset">Unused</param>
3234
/// <param name="endOffset">Unused</param>
33-
public override List<float> Perceive(float rayDistance,
35+
public override IList<float> Perceive(float rayDistance,
3436
float[] rayAngles, string[] detectableObjects,
3537
float startOffset=0.0f, float endOffset=0.0f)
3638
{
37-
m_PerceptionBuffer.Clear();
38-
// For each ray sublist stores categorical information on detected object
39-
// along with object distance.
40-
foreach (var angle in rayAngles)
39+
var perceptionSize = (detectableObjects.Length + 2) * rayAngles.Length;
40+
if (m_PerceptionBuffer == null || m_PerceptionBuffer.Length != perceptionSize)
4141
{
42-
m_EndPosition = transform.TransformDirection(
43-
PolarToCartesian(rayDistance, angle));
44-
if (Application.isEditor)
45-
{
46-
Debug.DrawRay(transform.position,
47-
m_EndPosition, Color.black, 0.01f, true);
48-
}
49-
50-
var subList = new float[detectableObjects.Length + 2];
51-
m_Hit = Physics2D.CircleCast(transform.position, 0.5f, m_EndPosition, rayDistance);
52-
if (m_Hit)
53-
{
54-
for (var i = 0; i < detectableObjects.Length; i++)
55-
{
56-
if (m_Hit.collider.gameObject.CompareTag(detectableObjects[i]))
57-
{
58-
subList[i] = 1;
59-
subList[detectableObjects.Length + 1] = m_Hit.distance / rayDistance;
60-
break;
61-
}
62-
}
63-
}
64-
else
65-
{
66-
subList[detectableObjects.Length] = 1f;
67-
}
68-
69-
m_PerceptionBuffer.AddRange(subList);
42+
m_PerceptionBuffer = new float[perceptionSize];
7043
}
7144

45+
const float castRadius = 0.5f;
46+
const bool legacyHitFractionBehavior = true;
47+
RayPerceptionSensor.PerceiveStatic(
48+
rayDistance, rayAngles, detectableObjects, startOffset, endOffset, castRadius,
49+
transform, RayPerceptionSensor.CastType.Cast3D, m_PerceptionBuffer, legacyHitFractionBehavior
50+
);
51+
7252
return m_PerceptionBuffer;
7353
}
7454

75-
/// <summary>
76-
/// Converts polar coordinate to cartesian coordinate.
77-
/// </summary>
78-
public static Vector2 PolarToCartesian(float radius, float angle)
79-
{
80-
var x = radius * Mathf.Cos(DegreeToRadian(angle));
81-
var y = radius * Mathf.Sin(DegreeToRadian(angle));
82-
return new Vector2(x, y);
83-
}
8455
}
8556
}
Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using UnityEngine;
4+
using MLAgents.Sensor;
45

56
namespace MLAgents
67
{
78
/// <summary>
89
/// Ray perception component. Attach this to agents to enable "local perception"
910
/// via the use of ray casts directed outward from the agent.
1011
/// </summary>
12+
[Obsolete("The RayPerception MonoBehaviour is deprecated. Use the RayPerceptionSensorComponent instead")]
1113
public class RayPerception3D : RayPerception
1214
{
13-
RaycastHit m_Hit;
14-
float[] m_SubList;
15-
1615
/// <summary>
1716
/// Creates perception vector to be used as part of an observation of an agent.
1817
/// Each ray in the rayAngles array adds a sublist of data to the observation.
@@ -31,66 +30,26 @@ public class RayPerception3D : RayPerception
3130
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
3231
/// <param name="startOffset">Starting height offset of ray from center of agent.</param>
3332
/// <param name="endOffset">Ending height offset of ray from center of agent.</param>
34-
public override List<float> Perceive(float rayDistance,
33+
public override IList<float> Perceive(float rayDistance,
3534
float[] rayAngles, string[] detectableObjects,
3635
float startOffset=0.0f, float endOffset=0.0f)
3736
{
38-
if (m_SubList == null || m_SubList.Length != detectableObjects.Length + 2)
39-
m_SubList = new float[detectableObjects.Length + 2];
40-
41-
m_PerceptionBuffer.Clear();
42-
m_PerceptionBuffer.Capacity = m_SubList.Length * rayAngles.Length;
43-
44-
// For each ray sublist stores categorical information on detected object
45-
// along with object distance.
46-
foreach (var angle in rayAngles)
37+
var perceptionSize = (detectableObjects.Length + 2) * rayAngles.Length;
38+
if (m_PerceptionBuffer == null || m_PerceptionBuffer.Length != perceptionSize)
4739
{
48-
Vector3 startPositionLocal = new Vector3(0, startOffset, 0);
49-
Vector3 endPositionLocal = PolarToCartesian(rayDistance, angle);
50-
endPositionLocal.y += endOffset;
51-
52-
var startPositionWorld = transform.TransformPoint(startPositionLocal);
53-
var endPositionWorld = transform.TransformPoint(endPositionLocal);
54-
55-
var rayDirection = endPositionWorld - startPositionWorld;
56-
if (Application.isEditor)
57-
{
58-
Debug.DrawRay(startPositionWorld,rayDirection, Color.black, 0.01f, true);
59-
}
60-
61-
Array.Clear(m_SubList, 0, m_SubList.Length);
62-
63-
if (Physics.SphereCast(startPositionWorld, 0.5f, rayDirection, out m_Hit, rayDistance))
64-
{
65-
for (var i = 0; i < detectableObjects.Length; i++)
66-
{
67-
if (m_Hit.collider.gameObject.CompareTag(detectableObjects[i]))
68-
{
69-
m_SubList[i] = 1;
70-
m_SubList[detectableObjects.Length + 1] = m_Hit.distance / rayDistance;
71-
break;
72-
}
73-
}
74-
}
75-
else
76-
{
77-
m_SubList[detectableObjects.Length] = 1f;
78-
}
79-
80-
Utilities.AddRangeNoAlloc(m_PerceptionBuffer, m_SubList);
40+
m_PerceptionBuffer = new float[perceptionSize];
8141
}
8242

43+
const float castRadius = 0.5f;
44+
const bool legacyHitFractionBehavior = true;
45+
RayPerceptionSensor.PerceiveStatic(
46+
rayDistance, rayAngles, detectableObjects, startOffset, endOffset, castRadius,
47+
transform, RayPerceptionSensor.CastType.Cast3D, m_PerceptionBuffer, legacyHitFractionBehavior
48+
);
49+
8350
return m_PerceptionBuffer;
8451
}
8552

86-
/// <summary>
87-
/// Converts polar coordinate to cartesian coordinate.
88-
/// </summary>
89-
public static Vector3 PolarToCartesian(float radius, float angle)
90-
{
91-
var x = radius * Mathf.Cos(DegreeToRadian(angle));
92-
var z = radius * Mathf.Sin(DegreeToRadian(angle));
93-
return new Vector3(x, 0f, z);
94-
}
53+
9554
}
9655
}

UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea.prefab

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ GameObject:
3939
- component: {fileID: 54678503543725326}
4040
- component: {fileID: 114898893333200490}
4141
- component: {fileID: 114925928594762506}
42-
- component: {fileID: 114092229367912210}
42+
- component: {fileID: 114458838850320084}
43+
- component: {fileID: 114227939525648256}
4344
m_Layer: 0
4445
m_Name: Agent
4546
m_TagString: agent
@@ -1052,17 +1053,58 @@ BoxCollider:
10521053
serializedVersion: 2
10531054
m_Size: {x: 1, y: 1, z: 1}
10541055
m_Center: {x: 0, y: 0, z: 0}
1055-
--- !u!114 &114092229367912210
1056+
--- !u!114 &114227939525648256
10561057
MonoBehaviour:
10571058
m_ObjectHideFlags: 1
10581059
m_PrefabParentObject: {fileID: 0}
10591060
m_PrefabInternal: {fileID: 100100000}
10601061
m_GameObject: {fileID: 1195095783991828}
10611062
m_Enabled: 1
10621063
m_EditorHideFlags: 0
1063-
m_Script: {fileID: 11500000, guid: bb172294dbbcc408286b156a2c4b553c, type: 3}
1064+
m_Script: {fileID: 11500000, guid: 6bb6b867a41448888c1cd4f99643ad71, type: 3}
10641065
m_Name:
10651066
m_EditorClassIdentifier:
1067+
sensorName: OffsetRayPerceptionSensor
1068+
detectableTags:
1069+
- wall
1070+
- goal
1071+
- block
1072+
raysPerDirection: 3
1073+
maxRayDegrees: 90
1074+
startVerticalOffset: 2.5
1075+
endVerticalOffset: 5
1076+
sphereCastRadius: 0.5
1077+
rayLength: 20
1078+
observationStacks: 6
1079+
rayHitColor: {r: 1, g: 0, b: 0, a: 1}
1080+
rayMissColor: {r: 1, g: 1, b: 1, a: 1}
1081+
useWorldPositions: 1
1082+
--- !u!114 &114458838850320084
1083+
MonoBehaviour:
1084+
m_ObjectHideFlags: 1
1085+
m_PrefabParentObject: {fileID: 0}
1086+
m_PrefabInternal: {fileID: 100100000}
1087+
m_GameObject: {fileID: 1195095783991828}
1088+
m_Enabled: 1
1089+
m_EditorHideFlags: 0
1090+
m_Script: {fileID: 11500000, guid: 6bb6b867a41448888c1cd4f99643ad71, type: 3}
1091+
m_Name:
1092+
m_EditorClassIdentifier:
1093+
sensorName: RayPerceptionSensor
1094+
detectableTags:
1095+
- wall
1096+
- goal
1097+
- block
1098+
raysPerDirection: 3
1099+
maxRayDegrees: 90
1100+
startVerticalOffset: 0
1101+
endVerticalOffset: 0
1102+
sphereCastRadius: 0.5
1103+
rayLength: 20
1104+
observationStacks: 6
1105+
rayHitColor: {r: 1, g: 0, b: 0, a: 1}
1106+
rayMissColor: {r: 1, g: 1, b: 1, a: 1}
1107+
useWorldPositions: 1
10661108
--- !u!114 &114898893333200490
10671109
MonoBehaviour:
10681110
m_ObjectHideFlags: 1
@@ -1075,12 +1117,12 @@ MonoBehaviour:
10751117
m_Name:
10761118
m_EditorClassIdentifier:
10771119
m_BrainParameters:
1078-
vectorObservationSize: 74
1120+
vectorObservationSize: 4
10791121
numStackedVectorObservations: 6
10801122
vectorActionSize: 03000000030000000300000002000000
10811123
vectorActionDescriptions: []
10821124
vectorActionSpaceType: 0
1083-
m_Model: {fileID: 11400000, guid: fb2ce36eb40b6480e94ea0b5d7573e47, type: 3}
1125+
m_Model: {fileID: 11400000, guid: 0468bf44b1efd4992b6bf22cadb50d89, type: 3}
10841126
m_InferenceDevice: 0
10851127
m_UseHeuristic: 0
10861128
m_BehaviorName: SmallWallJump

UnitySDK/Assets/ML-Agents/Examples/WallJump/Scenes/WallJump.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.44971442, g: 0.499779, b: 0.5756377, a: 1}
41+
m_IndirectSpecularColor: {r: 0.44971484, g: 0.49977958, b: 0.5756385, a: 1}
4242
--- !u!157 &3
4343
LightmapSettings:
4444
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)