浏览代码

Merge pull request #57 from Unity-Technologies/randomization-housekeeping

Randomization housekeeping
/main
GitHub 4 年前
当前提交
a68c66c4
共有 14 个文件被更改,包括 59 次插入53 次删除
  1. 2
      com.unity.perception/Documentation~/Randomization/Scenarios.md
  2. 4
      com.unity.perception/Editor/Randomization/ParameterElement.cs
  3. 14
      com.unity.perception/Editor/Randomization/SamplerElement.cs
  4. 3
      com.unity.perception/Runtime/Randomization/Parameters/NumericParameter.cs
  5. 9
      com.unity.perception/Runtime/Randomization/Parameters/Parameter.cs
  6. 19
      com.unity.perception/Runtime/Randomization/Samplers/ISampler.cs
  7. 29
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs
  8. 2
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs
  9. 2
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs
  10. 2
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs
  11. 23
      com.unity.perception/Runtime/Randomization/Samplers/IRandomRangedSampler.cs
  12. 3
      com.unity.perception/Runtime/Randomization/Samplers/IRandomRangedSampler.cs.meta
  13. 0
      /com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs.meta
  14. 0
      /com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs

2
com.unity.perception/Documentation~/Randomization/Scenarios.md


6. **OnComplete** - actions to complete after the scenario as completed
## Constants
Scenarios define constants from which to expose global simulation behaviors like a starting iteration value or a total iteration count. Users can serialize these scenario constants to JSON, modify them in an external program, and finally reimport the JSON constants at runtime to configure their simulation even after their project has been built. Below is an example of the constants used in the `FixedFrameLengthScenario` class:
Scenarios define constants from which to expose global simulation behaviors like a starting iteration value or a total iteration count. Users can serialize these scenario constants to JSON, modify them in an external program, and finally reimport the JSON constants at runtime to configure their simulation even after their project has been built. Below is an example of the constants used in the `FixedLengthScenario` class:
```
[Serializable]
public class Constants

4
com.unity.perception/Editor/Randomization/ParameterElement.cs


var fieldInfos = componentType.GetFields();
foreach (var fieldInfo in fieldInfos)
{
if (fieldInfo.FieldType == propertyType)
if (fieldInfo.FieldType == propertyType && fieldInfo.IsPublic && !fieldInfo.IsInitOnly)
options.Add(new ParameterTarget()
{
gameObject = obj,

var propertyInfos = componentType.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
if (propertyInfo.PropertyType == propertyType)
if (propertyInfo.PropertyType == propertyType && propertyInfo.GetSetMethod() != null)
options.Add(new ParameterTarget()
{
gameObject = obj,

14
com.unity.perception/Editor/Randomization/SamplerElement.cs


void CreateSampler(Type samplerType)
{
var newSampler = (ISampler)Activator.CreateInstance(samplerType);
if (newSampler is IRandomRangedSampler rangedSampler)
{
rangedSampler.baseSeed = SamplerUtility.GenerateRandomSeed();
newSampler.baseSeed = SamplerUtility.GenerateRandomSeed();
if (m_RangeProperty != null)
rangedSampler.range = new FloatRange(
m_RangeProperty.FindPropertyRelative("minimum").floatValue,
m_RangeProperty.FindPropertyRelative("maximum").floatValue);
}
if (m_RangeProperty != null)
newSampler.range = new FloatRange(
m_RangeProperty.FindPropertyRelative("minimum").floatValue,
m_RangeProperty.FindPropertyRelative("maximum").floatValue);
m_Sampler = newSampler;
m_Property.managedReferenceValue = newSampler;

3
com.unity.perception/Runtime/Randomization/Parameters/NumericParameter.cs


{
base.Validate();
foreach (var sampler in samplers)
if (sampler is IRandomRangedSampler rangedSampler)
rangedSampler.range.Validate();
sampler.range.Validate();
}
}
}

9
com.unity.perception/Runtime/Randomization/Parameters/Parameter.cs


var i = 0;
foreach (var sampler in samplers)
{
sampler.Rebase(SamplerUtility.IterateSeed((uint)i++, SamplerUtility.largePrime));
sampler.IterateState(i++);
sampler.ResetState();
}
}

foreach (var sampler in samplers)
{
sampler.Rebase(SamplerUtility.GenerateRandomSeed());
sampler.baseSeed = SamplerUtility.GenerateRandomSeed();
sampler.ResetState();
}
}

public void ResetState(int scenarioIteration)
{
foreach (var sampler in samplers)
sampler.ResetState(scenarioIteration);
{
sampler.ResetState();
sampler.IterateState(scenarioIteration);
}
}
/// <summary>

19
com.unity.perception/Runtime/Randomization/Samplers/ISampler.cs


public interface ISampler
{
/// <summary>
/// Resets a sampler's state to its base random seed
/// The base seed used to initialize this sampler's state
void ResetState();
uint baseSeed { get; set; }
/// Resets a sampler's state to its base random seed and then offsets said seed using an index value
/// The current random state of this sampler
/// </summary>
uint state { get; set; }
/// <summary>
/// A range bounding the values generated by this sampler
/// <param name="index">Often a the active scenario's currentIteration</param>
void ResetState(int index);
FloatRange range { get; set; }
/// Set the base seed value of this sampler
/// Resets a sampler's state to its base random seed
/// <param name="seed">The seed that will replace the sampler's current seed</param>
void Rebase(uint seed);
void ResetState();
/// <summary>
/// Deterministically offsets a sampler's state when generating values within a batched job

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


public float value;
/// <summary>
/// The base seed used to initialize this sampler's state.
/// Note that ConstantSamplers do not utilize a baseSeed.
/// </summary>
public uint baseSeed
{
get => SamplerUtility.largePrime;
set { }
}
/// <summary>
/// The current random state of this sampler.
/// Note that ConstantSamplers do not utilize a random state.
/// </summary>
public uint state
{
get => SamplerUtility.largePrime;
set { }
}
/// <summary>
/// A range bounding the values generated by this sampler
/// </summary>
public FloatRange range
{
get => new FloatRange(value, value);
set { }
}
/// <summary>
/// Constructs a new ConstantSampler
/// </summary>
/// <param name="value">The value from which samples will be generated</param>

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


/// https://en.wikipedia.org/wiki/Truncated_normal_distribution
/// </summary>
[Serializable]
public struct NormalSampler : ISampler, IRandomRangedSampler
public struct NormalSampler : ISampler
{
Unity.Mathematics.Random m_Random;

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


/// Returns uniformly distributed random values within a designated range.
/// </summary>
[Serializable]
public struct UniformSampler : ISampler, IRandomRangedSampler
public struct UniformSampler : ISampler
{
Unity.Mathematics.Random m_Random;

2
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs


namespace RandomizationTests.SamplerTests
{
public abstract class RangedSamplerTests<T> where T : struct, ISampler, IRandomRangedSampler
public abstract class RangedSamplerTests<T> where T : struct, ISampler
{
const int k_TestSampleCount = 30;
protected T m_BaseSampler;

23
com.unity.perception/Runtime/Randomization/Samplers/IRandomRangedSampler.cs


namespace UnityEngine.Perception.Randomization.Samplers
{
/// <summary>
/// An interface describing bounded random samplers
/// </summary>
public interface IRandomRangedSampler
{
/// <summary>
/// The base seed used to initialize this sampler's state
/// </summary>
uint baseSeed { get; set; }
/// <summary>
/// The current random state of this sampler
/// </summary>
uint state { get; set; }
/// <summary>
/// A range bounding the values generated by this sampler
/// </summary>
FloatRange range { get; set; }
}
}

3
com.unity.perception/Runtime/Randomization/Samplers/IRandomRangedSampler.cs.meta


fileFormatVersion: 2
guid: 0f406605b3cb4d55b84b5955196c4911
timeCreated: 1597419465

/com.unity.perception/Tests/Runtime/Randomization/SamplerTests/RangedSamplerTests.cs.meta → /com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs.meta

/com.unity.perception/Tests/Runtime/Randomization/SamplerTests/RangedSamplerTests.cs → /com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs

正在加载...
取消
保存