浏览代码

Merge pull request #137 from Unity-Technologies/randomizer_tag_inheritence

Randomizer tag inheritence
/main
GitHub 4 年前
当前提交
93b7abef
共有 11 个文件被更改,包括 136 次插入27 次删除
  1. 2
      com.unity.perception/CHANGELOG.md
  2. 2
      com.unity.perception/Runtime/Randomization/Parameters/CategoricalParameter.cs
  3. 1
      com.unity.perception/Runtime/Randomization/Parameters/ParameterValidationException.cs
  4. 65
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs
  5. 8
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs
  6. 10
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs
  7. 10
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs
  8. 12
      com.unity.perception/Runtime/Randomization/Samplers/SamplerUtility.cs
  9. 40
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  10. 2
      com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTag.cs
  11. 11
      com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs

2
com.unity.perception/CHANGELOG.md


Added ScenarioBase.SerializeToConfigFile()
Randomizer tags now support inheritance
### Changed
Randomizers now access their parent scenario through the static activeScenario property

2
com.unity.perception/Runtime/Randomization/Parameters/CategoricalParameter.cs


public override void Validate()
{
base.Validate();
if (m_Categories.Count == 0)
throw new ParameterValidationException("No options added to categorical parameter");
if (!uniform)
{
if (probabilities.Count != m_Categories.Count)

1
com.unity.perception/Runtime/Randomization/Parameters/ParameterValidationException.cs


class ParameterValidationException : Exception
{
public ParameterValidationException(string msg) : base(msg) {}
public ParameterValidationException(string msg, Exception innerException) : base(msg, innerException) {}
}
}

65
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs


using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Entities;
namespace UnityEngine.Experimental.Perception.Randomization.Randomizers
{

public class RandomizerTagManager
{
Dictionary<Type, HashSet<Type>> m_TypeTree = new Dictionary<Type, HashSet<Type>>();
Dictionary<Type, HashSet<GameObject>> m_TagMap = new Dictionary<Type, HashSet<GameObject>>();
/// <summary>

/// <param name="returnSubclasses">Should this method retrieve all tags derived from the passed in tag also?</param>
public IEnumerable<GameObject> Query<T>() where T : RandomizerTag
public IEnumerable<GameObject> Query<T>(bool returnSubclasses = false) where T : RandomizerTag
var type = typeof(T);
if (!m_TagMap.ContainsKey(type))
var queriedTagType = typeof(T);
if (!m_TagMap.ContainsKey(queriedTagType))
foreach (var taggedObject in m_TagMap[type])
yield return taggedObject;
if (returnSubclasses)
{
var typeStack = new Stack<Type>();
typeStack.Push(queriedTagType);
while (typeStack.Count > 0)
{
var tagType = typeStack.Pop();
foreach (var derivedType in m_TypeTree[tagType])
typeStack.Push(derivedType);
foreach (var obj in m_TagMap[tagType])
yield return obj;
}
}
else
{
foreach (var obj in m_TagMap[queriedTagType])
yield return obj;
}
if (!m_TagMap.ContainsKey(tagType))
m_TagMap[tagType] = new HashSet<GameObject>();
AddTagTypeToTypeHierarchy(tagType);
void AddTagTypeToTypeHierarchy(Type tagType)
{
if (m_TypeTree.ContainsKey(tagType))
return;
if (tagType == null || !tagType.IsSubclassOf(typeof(RandomizerTag)))
throw new ArgumentException("Tag type is not a subclass of RandomizerTag");
m_TagMap.Add(tagType, new HashSet<GameObject>());
m_TypeTree.Add(tagType, new HashSet<Type>());
var baseType = tagType.BaseType;
while (baseType!= null && baseType != typeof(RandomizerTag))
{
if (!m_TypeTree.ContainsKey(baseType))
{
m_TagMap.Add(baseType, new HashSet<GameObject>());
m_TypeTree[baseType] = new HashSet<Type> { tagType };
}
else
{
m_TypeTree[baseType].Add(tagType);
break;
}
tagType = baseType;
baseType = tagType.BaseType;
}
}
if (m_TagMap.ContainsKey(tagType))
if (m_TagMap.ContainsKey(tagType) && m_TagMap[tagType].Contains(obj))
m_TagMap[tagType].Remove(obj);
}
}

8
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs


}
/// <summary>
/// Constructs a ConstantSampler
/// </summary>
public ConstantSampler()
{
value = 0f;
}
/// <summary>
/// Constructs a new ConstantSampler
/// </summary>
/// <param name="value">The value from which samples will be generated</param>

10
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs


/// <summary>
/// Constructs a normal distribution sampler
/// </summary>
public NormalSampler()
{
range = new FloatRange(-1f, 1f);
mean = 0;
standardDeviation = 1;
}
/// <summary>
/// Constructs a normal distribution sampler
/// </summary>
/// <param name="min">The smallest value contained within the range</param>
/// <param name="max">The largest value contained within the range</param>
/// <param name="mean">The mean of the normal distribution to sample from</param>

10
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs


public FloatRange range { get; set; }
/// <summary>
/// Constructs a UniformSampler
/// </summary>
public UniformSampler()
{
range = new FloatRange(0f, 1f);
}
/// <summary>
/// Constructs a new uniform distribution sampler
/// </summary>
/// <param name="min">The smallest value contained within the range</param>

public float Sample()
{
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
return math.lerp(range.minimum, range.maximum, rng.NextFloat());
return rng.NextFloat(range.minimum, range.maximum);
}
/// <summary>

12
com.unity.perception/Runtime/Randomization/Samplers/SamplerUtility.cs


}
/// <summary>
/// Generates a 32-bit non-zero hash using an unsigned integer seed
/// </summary>
/// <param name="seed">The unsigned integer to hash</param>
/// <returns>The calculated hash value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Hash32NonZero(uint seed)
{
var hash = Hash32(seed);
return hash == 0u ? largePrime : hash;
}
/// <summary>
/// Based on splitmix64: http://xorshift.di.unimi.it/splitmix64.c
/// </summary>
/// <param name="x">64-bit value to hash</param>

40
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


using System.IO;
using Unity.Simulation;
using UnityEngine;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
using UnityEngine.Experimental.Perception.Randomization.Randomizers;
using UnityEngine.Experimental.Perception.Randomization.Samplers;
using UnityEngine.Perception.GroundTruth;

/// </summary>
public static ScenarioBase activeScenario
{
get => s_ActiveScenario;
get
{
#if UNITY_EDITOR
// This compiler define is required to allow samplers to
// iterate the scenario's random state in edit-mode
if (s_ActiveScenario == null)
s_ActiveScenario = FindObjectOfType<ScenarioBase>();
#endif
return s_ActiveScenario;
}
private set
{
if (value != null && s_ActiveScenario != null && value != s_ActiveScenario)

m_SkipFrame = false;
}
void OnEnable()
{
activeScenario = this;
}
void OnDisable()
{
s_ActiveScenario = null;
}
void Reset()
{
activeScenario = this;
}
void Start()
{
var randomSeedMetricDefinition = DatasetCapture.RegisterMetricDefinition(

/// <returns>The newly generated random state</returns>
public uint NextRandomState()
{
m_RandomState = SamplerUtility.Hash32(m_RandomState);
m_RandomState = SamplerUtility.Hash32NonZero(m_RandomState);
return m_RandomState;
}

foreach (var parameter in randomizer.parameters)
parameter.Validate();
{
try
{
parameter.Validate();
}
catch (ParameterValidationException exception)
{
Debug.LogException(exception, this);
}
}
}
}
}

2
com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/ExampleTag.cs


namespace RandomizationTests.RandomizerTests
{
public class ExampleTag : RandomizerTag { }
public class ExampleTag2 : ExampleTag { }
}

11
com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs


for (var i = 0; i < copyCount - 1; i++)
Object.Instantiate(gameObject);
var gameObject2 = new GameObject();
gameObject2.AddComponent<ExampleTag2>();
for (var i = 0; i < copyCount - 1; i++)
Object.Instantiate(gameObject2);
queriedObjects = m_Scenario.tagManager.Query<ExampleTag2>().ToArray();
Assert.AreEqual(queriedObjects.Length, copyCount);
queriedObjects = m_Scenario.tagManager.Query<ExampleTag>(true).ToArray();
Assert.AreEqual(queriedObjects.Length, copyCount * 2);
}
}
}
正在加载...
取消
保存