浏览代码

experimenting with ECS workflow

/parameter-bahaviours
Steven Leal 4 年前
当前提交
b7091e4e
共有 6 个文件被更改,包括 144 次插入120 次删除
  1. 54
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterList.cs
  2. 32
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterAsset.cs
  3. 67
      com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterBehaviour.cs
  4. 5
      com.unity.perception/Runtime/Randomization/Scenarios/FixedLengthScenario.cs
  5. 90
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  6. 16
      com.unity.perception/Tests/Runtime/Randomization/ParameterConfigurationTests.cs

54
com.unity.perception/Runtime/Randomization/ParameterBehaviours/Configuration/ParameterList.cs


{
[SerializeReference] internal List<ParameterListItem> configuredParameters = new List<ParameterListItem>();
/// <summary>
/// The parameters contained within this ParameterList
/// </summary>
public override IEnumerable<Parameter> parameters
{
get
{
foreach (var configParameter in configuredParameters)
yield return configParameter.parameter;
}
}
// /// <summary>
// /// The parameters contained within this ParameterList
// /// </summary>
// public override IEnumerable<Parameter> parameters
// {
// get
// {
// foreach (var configParameter in configuredParameters)
// yield return configParameter.parameter;
// }
// }
internal ParameterListItem AddParameter<T>(string parameterName) where T : Parameter, new()
{

configParameter.ApplyToTarget();
}
/// <summary>
/// Validates the settings of each parameter within this ParameterList
/// </summary>
/// <exception cref="ParameterListException"></exception>
public override void Validate()
{
var parameterNames = new HashSet<string>();
foreach (var configParameter in configuredParameters)
{
if (parameterNames.Contains(configParameter.name))
throw new ParameterListException(
$"Two or more parameters cannot share the same name (\"{configParameter.name}\")");
parameterNames.Add(configParameter.name);
configParameter.Validate();
}
}
// /// <summary>
// /// Validates the settings of each parameter within this ParameterList
// /// </summary>
// /// <exception cref="ParameterListException"></exception>
// public override void Validate()
// {
// var parameterNames = new HashSet<string>();
// foreach (var configParameter in configuredParameters)
// {
// if (parameterNames.Contains(configParameter.name))
// throw new ParameterListException(
// $"Two or more parameters cannot share the same name (\"{configParameter.name}\")");
// parameterNames.Add(configParameter.name);
// configParameter.Validate();
// }
// }
}
}

32
com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterAsset.cs


{
public abstract class ParameterAsset : ScriptableObject
{
public abstract IEnumerable<Parameter> parameters { get; }
internal IEnumerable<Parameter> parameters
{
get
{
var fields = GetType().GetFields();
foreach (var field in fields)
{
if (!field.IsPublic || !field.FieldType.IsSubclassOf(typeof(Parameter)))
continue;
var parameter = (Parameter)field.GetValue(this);
if (parameter == null)
{
parameter = (Parameter)Activator.CreateInstance(field.FieldType);
field.SetValue(this, parameter);
}
yield return parameter;
}
}
}
void Reset()
public virtual void Reset()
var fields = GetType().GetFields();
foreach (var field in fields)
{
if (!field.IsPublic || !field.FieldType.IsSubclassOf(typeof(Parameter)))
continue;
var parameter = (Parameter)field.GetValue(this);
if (parameter == null)
field.SetValue(this, Activator.CreateInstance(field.FieldType));
}
foreach (var parameter in parameters)
foreach (var sampler in parameter.samplers)
sampler.baseSeed = SamplerUtility.GenerateRandomSeed();

67
com.unity.perception/Runtime/Randomization/ParameterBehaviours/ParameterBehaviour.cs


using System;
using System.Collections.Generic;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Samplers;
using UnityEngine.Perception.Randomization.Scenarios;
namespace Randomization.ParameterBehaviours

/// </summary>
public abstract class ParameterBehaviour : MonoBehaviour
{
static ParameterBehaviour s_ActiveBehaviour;
internal static ParameterBehaviour activeBehaviour
{
get => s_ActiveBehaviour;
private set
{
if (value != null && s_ActiveBehaviour != null && value != s_ActiveBehaviour)
throw new Exception("There cannot be more than one active Scenario");
s_ActiveBehaviour = value;
}
}
/// <summary>
/// The parameters employed by this parameter behaviour
/// </summary>
public abstract IEnumerable<Parameter> parameters { get; }
public virtual int executionPriority => 1;
/// <summary>
/// This method is called when the ParameterBehaviour is enabled
/// </summary>
protected void OnEnable()
public void Awake()
activeBehaviour = this;
ResetParameterRandomStates();
ScenarioBase.activeScenario.AddBehaviour(this);
/// This method is called when the ParameterBehaviour is disabled
/// Included in the base ParameterBehaviour class to activate the enabled toggle in the inspector UI
protected void OnDisable()
{
activeBehaviour = null;
}
protected virtual void OnEnable() {}
/// <summary>
/// OnFrameStart is called at the start of every frame if the ParameterBehaviour is enabled

/// Run when the scenario completes
/// </summary>
public virtual void OnScenarioComplete() {}
/// <summary>
/// Validate all parameters employed by this ParameterBehaviour
/// </summary>
public virtual void Validate()
{
foreach (var parameter in parameters)
parameter.Validate();
}
/// <summary>
/// Reset to default values in the Editor
/// </summary>
protected virtual void Reset()
{
foreach (var parameter in parameters)
foreach (var sampler in parameter.samplers)
sampler.baseSeed = SamplerUtility.GenerateRandomSeed();
}
/// <summary>
/// Resets the state of each sampler on every parameter used by this ParameterBehaviour
/// </summary>
internal void ResetParameterRandomStates()
{
foreach (var parameter in parameters)
{
parameter.ResetState();
parameter.IterateState(ScenarioBase.ActiveScenario.currentIteration);
parameter.IterateState(GetInstanceID());
}
}
}
}

5
com.unity.perception/Runtime/Randomization/Scenarios/FixedLengthScenario.cs


/// </summary>
public override bool isScenarioComplete => currentIteration >= constants.totalIterations;
void Awake()
/// <summary>
/// Fast forwards the current scenario iteration to the starting iteration indicated in this scenario's constants
/// </summary>
protected override void OnAwake()
{
currentIteration = constants.startingIteration;
}

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


using System;
using System.Collections.Generic;
using UnityEngine.Perception.Randomization.Parameters;
namespace UnityEngine.Perception.Randomization.Scenarios
{

public abstract class ScenarioBase : MonoBehaviour
{
static ScenarioBase s_ActiveScenario;
List<ParameterBehaviour> m_Behaviours = new List<ParameterBehaviour>();
List<Parameter> m_Parameters = new List<Parameter>();
[SerializeReference] public ParameterAsset[] parameters;
IEnumerable<ParameterBehaviour> m_ActiveBehaviours
{
get
{
foreach (var behaviour in m_Behaviours)
if (behaviour.enabled)
yield return behaviour;
}
}
[SerializeReference] public ParameterAsset[] parameterAssets;
/// <summary>
/// If true, this scenario will quit the Unity application when it's finished executing

/// <summary>
/// Returns the active parameter scenario in the scene
/// </summary>
public static ScenarioBase ActiveScenario
public static ScenarioBase activeScenario
{
get => s_ActiveScenario;
private set

/// </summary>
public abstract void Deserialize();
/// <summary>
/// This method executed directly after this scenario has been registered and initialized
/// </summary>
protected virtual void OnAwake() { }
ActiveScenario = this;
activeScenario = this;
foreach (var asset in parameterAssets)
{
foreach (var parameter in asset.parameters)
{
parameter.Validate();
m_Parameters.Add(parameter);
}
}
OnAwake();
ActiveScenario = this;
activeScenario = this;
}
void OnDisable()

{
if (deserializeOnStart)
Deserialize();
ParameterBehaviour.activeBehaviour.Validate();
}
void Update()

{
currentIteration++;
currentIterationFrame = 0;
ParameterBehaviour.activeBehaviour.OnIterationEnd();
foreach (var behaviour in m_ActiveBehaviours)
behaviour.OnIterationEnd();
}
}

ParameterBehaviour.activeBehaviour.OnScenarioComplete();
foreach (var behaviour in m_ActiveBehaviours)
behaviour.OnScenarioComplete();
Manager.Instance.Shutdown();
DatasetCapture.ResetSimulation();
m_WaitingForFinalUploads = true;

if (currentIterationFrame == 0)
{
DatasetCapture.StartNewSequence();
ParameterBehaviour.activeBehaviour.ResetParameterRandomStates();
ParameterBehaviour.activeBehaviour.OnIterationStart();
foreach (var parameter in m_Parameters)
{
parameter.ResetState();
parameter.IterateState(currentIteration);
}
foreach (var behaviour in m_ActiveBehaviours)
behaviour.OnIterationStart();
ParameterBehaviour.activeBehaviour.OnFrameStart();
foreach (var behaviour in m_ActiveBehaviours)
behaviour.OnFrameStart();
}
public T GetParameterAsset<T>() where T : ParameterAsset
{
foreach (var asset in parameterAssets)
if (asset is T typedAsset)
return typedAsset;
throw new ScenarioException($"A ParameterAsset of type {typeof(T).Name} was not added to this scenario");
}
public T GetParameterBehaviour<T>() where T : ParameterBehaviour
{
foreach (var behaviour in m_Behaviours)
if (behaviour is T typedBehaviour)
return typedBehaviour;
throw new ScenarioException($"A ParameterBehaviour of type {typeof(T).Name} was not added to this scenario");
}
internal void AddBehaviour<T>(T newBehaviour) where T : ParameterBehaviour
{
foreach (var behaviour in m_Behaviours)
if (behaviour.GetType() == newBehaviour.GetType())
throw new ScenarioException(
$"Two ParameterBehaviours of the same type {typeof(T).Name} cannot both be active simultaneously");
m_Behaviours.Add(newBehaviour);
m_Behaviours.Sort((b1, b2) => b1.executionPriority.CompareTo(b2.executionPriority));
}
internal void RemoveBehaviour(ParameterBehaviour behaviour)
{
var removed = m_Behaviours.Remove(behaviour);
if (!removed)
throw new ScenarioException(
$"No active ParameterBehaviour of type {behaviour.GetType().Name} could be removed");
}
}
}

16
com.unity.perception/Tests/Runtime/Randomization/ParameterConfigurationTests.cs


Object.DestroyImmediate(m_TestObject);
}
[Test]
public void CheckForParametersWithSameNameTest()
{
var config = m_TestObject.AddComponent<ParameterList>();
config.AddParameter<FloatParameter>("SameName");
config.AddParameter<BooleanParameter>("SameName");
Assert.Throws<ParameterListException>(() => config.Validate());
}
// [Test]
// public void CheckForParametersWithSameNameTest()
// {
// var config = m_TestObject.AddComponent<ParameterList>();
// config.AddParameter<FloatParameter>("SameName");
// config.AddParameter<BooleanParameter>("SameName");
// Assert.Throws<ParameterListException>(() => config.Validate());
// }
[Test]
public void AddingNonParameterTypesTest()

正在加载...
取消
保存