Skip to content

Commit ea36321

Browse files
committed
feat: add DistanceNoiseStd and FlipHitProbability to RayPerceptionSensor
1 parent b6abc45 commit ea36321

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

com.unity.ml-agents/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ and this project adheres to
77
[Semantic Versioning](http://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
10+
11+
### Added
12+
- **Sensors** – Two new noise parameters are now available on RayPerceptionSensorComponent:
13+
14+
| Parameter | Default | Description |
15+
|-----------|---------|-------------|
16+
| `DistanceNoiseStd` | `0` | Standard deviation (σ) of Gaussian noise added to HitFraction, which represents the normalized hit distance (ranging from 0 to 1). |
17+
| `FlipHitProbability` | `0` | The probability of randomly flipping the HasHit boolean value (0 → 1 or 1 → 0). A value of 0 means no flipping occurs. |
18+
19+
Leaving both parameters at `0` reproduces the exact behaviour of previous releases.
20+
21+
### Fixed
22+
- n/a
23+
1024
### Major Changes
1125
#### com.unity.ml-agents / com.unity.ml-agents.extensions (C#)
1226
- Upgraded to Inference Engine 2.2.1 (#6212)

com.unity.ml-agents/Editor/RayPerceptionSensorComponentBaseEditor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ protected void OnRayPerceptionInspectorGUI(bool is3d)
7272
EditorGUILayout.PropertyField(so.FindProperty("rayHitColor"), true);
7373
EditorGUILayout.PropertyField(so.FindProperty("rayMissColor"), true);
7474

75+
// ───────── Parameters Noise ─────────
76+
EditorGUILayout.PropertyField(so.FindProperty("m_DistanceNoiseStd"), true);
77+
EditorGUILayout.PropertyField(so.FindProperty("m_FlipHitProbability"), true);
78+
7579
EditorGUI.indentLevel--;
7680
if (EditorGUI.EndChangeCheck())
7781
{

com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ public class RayPerceptionSensor : ISensor, IBuiltInSensor
254254

255255
bool m_UseBatchedRaycasts;
256256

257+
// ───────── Parameters Noise ─────────
258+
float m_DistanceNoiseStd;
259+
float m_FlipHitProb;
260+
257261
/// <summary>
258262
/// Time.frameCount at the last time Update() was called. This is only used for display in gizmos.
259263
/// </summary>
@@ -269,12 +273,16 @@ internal int DebugLastFrameCount
269273
/// </summary>
270274
/// <param name="name">The name of the sensor.</param>
271275
/// <param name="rayInput">The inputs for the sensor.</param>
272-
public RayPerceptionSensor(string name, RayPerceptionInput rayInput)
276+
public RayPerceptionSensor(string name, RayPerceptionInput rayInput, float distanceNoiseStd, float flipHitProb)
273277
{
274278
m_Name = name;
275279
m_RayPerceptionInput = rayInput;
276280
m_UseBatchedRaycasts = rayInput.UseBatchedRaycasts;
277281

282+
// ───────── Parameters Noise ─────────
283+
m_DistanceNoiseStd = distanceNoiseStd;
284+
m_FlipHitProb = flipHitProb;
285+
278286
SetNumObservations(rayInput.OutputSize());
279287

280288
m_DebugLastFrameCount = Time.frameCount;
@@ -329,7 +337,20 @@ public int Write(ObservationWriter writer)
329337
// For each ray, write the information to the observation buffer
330338
for (var rayIndex = 0; rayIndex < numRays; rayIndex++)
331339
{
332-
m_RayPerceptionOutput.RayOutputs?[rayIndex].ToFloatArray(numDetectableTags, rayIndex, m_Observations);
340+
var ro = m_RayPerceptionOutput.RayOutputs[rayIndex];
341+
342+
// Used to debug values
343+
float before = ro.HitFraction;
344+
bool hit0 = ro.HasHit;
345+
346+
// Aplly noise
347+
ApplyNoise(ref ro);
348+
349+
// Debug Value
350+
//if (before != ro.HitFraction) Debug.Log($"Ray {rayIndex} | dist {before} → {ro.HitFraction} | hit {hit0}→{ro.HasHit}");
351+
352+
m_RayPerceptionOutput.RayOutputs[rayIndex] = ro;
353+
ro.ToFloatArray(numDetectableTags, rayIndex, m_Observations);
333354
}
334355

335356
// Finally, add the observations to the ObservationWriter
@@ -666,5 +687,30 @@ int rayIndex
666687

667688
return rayOutput;
668689
}
690+
691+
/// <summary>
692+
/// Applies simulated noise to ray hit data to mimic real-world sensor or environmental conditions.
693+
/// Includes Gaussian-like noise on hit distance and random flipping of the hit detection state.
694+
/// </summary>
695+
/// <param name="ro">Reference to the RayOutput object to modify with applied noise.</param>
696+
void ApplyNoise(ref RayPerceptionOutput.RayOutput ro)
697+
{
698+
// Apply noise to the normalized hit distance if m_DistanceNoiseStd > 0
699+
if (m_DistanceNoiseStd > 0f)
700+
{
701+
// Generate a random value in the range [-m_DistanceNoiseStd, m_DistanceNoiseStd]
702+
// Add it to the original HitFraction and clamp the result between 0 and 1
703+
ro.HitFraction = Mathf.Clamp01(
704+
ro.HitFraction +
705+
UnityEngine.Random.Range(-m_DistanceNoiseStd, m_DistanceNoiseStd));
706+
}
707+
708+
// Randomly flip the hit detection state based on m_FlipHitProb
709+
if (m_FlipHitProb > 0f && UnityEngine.Random.value < m_FlipHitProb)
710+
{
711+
// Invert the boolean HasHit value: true becomes false, and vice versa
712+
ro.HasHit = !ro.HasHit;
713+
}
714+
}
669715
}
670716
}

com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ public bool UseBatchedRaycasts
180180
[SerializeField]
181181
internal Color rayMissColor = Color.white;
182182

183+
// ───────── Parametes Noise ─────────
184+
/// <summary>
185+
/// Standard deviation of Gaussian noise added to the normalized ray distance.
186+
/// A value of 0 means no noise is applied.
187+
/// </summary>
188+
[HideInInspector]
189+
[SerializeField, Range(0f, 0.5f)]
190+
[Header("Noise", order = 999)]
191+
[Tooltip("Standard deviation of Gaussian noise added to the normalized ray distance (HitFraction varies from 0 to 1)")]
192+
internal float m_DistanceNoiseStd;
193+
194+
/// <summary>
195+
/// Probability of randomly flipping the HasHit bit (0 → 1 or 1 → 0).
196+
/// This simulates sensor or detection errors in raycasting.
197+
/// A value of 0 means no flipping occurs.
198+
/// </summary>
199+
[HideInInspector]
200+
[SerializeField, Range(0f, 0.5f)]
201+
[Tooltip("Probability of flipping the HasHit bit (0 → 1 or 1 → 0).")]
202+
float m_FlipHitProbability;
203+
204+
183205
[NonSerialized]
184206
RayPerceptionSensor m_RaySensor;
185207

@@ -223,7 +245,12 @@ public override ISensor[] CreateSensors()
223245
{
224246
var rayPerceptionInput = GetRayPerceptionInput();
225247

226-
m_RaySensor = new RayPerceptionSensor(m_SensorName, rayPerceptionInput);
248+
m_RaySensor = new RayPerceptionSensor(
249+
m_SensorName,
250+
rayPerceptionInput,
251+
m_DistanceNoiseStd,
252+
m_FlipHitProbability
253+
);
227254

228255
if (ObservationStacks != 1)
229256
{

docs/Learning-Environment-Design-Agents.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ Both sensor components have several settings:
509509
but if using custom models the left-to-right layout that matches the spatial
510510
structuring can be preferred (e.g. for processing with conv nets).
511511
- _Use Batched Raycasts_ (3D only) Whether to use batched raycasts. Enable to use batched raycasts and the jobs system.
512+
- _Distance Noise Std_ This parameter controls the standard deviation (σ) of Gaussian noise added to HitFraction, a value representing the normalized hit distance along the ray. A value of 0 means no noise is applied. HitFraction ranges from 0 (no hit) to 1 (hit at maximum ray distance).
513+
- _FlipHitProbability_ Sets the probability that the HasHit result will be flipped (e.g., hit becomes no hit, or vice versa). Useful for simulating sensor noise or unreliable detection..
512514

513515
In the example image above, the Agent has two `RayPerceptionSensorComponent3D`s.
514516
Both use 3 Rays Per Direction and 90 Max Ray Degrees. One of the components had

0 commit comments

Comments
 (0)