浏览代码

added usim status fields

/usim-randomization
Steven Leal 4 年前
当前提交
e7cf2a16
共有 9 个文件被更改,包括 126 次插入116 次删除
  1. 101
      com.unity.perception/Editor/Randomization/RunInUSimWindow.cs
  2. 25
      com.unity.perception/Editor/Randomization/Uxml/RunInUSimWindow.uxml
  3. 33
      com.unity.perception/Runtime/Randomization/Scenarios/FixedLengthScenario.cs
  4. 11
      com.unity.perception/Runtime/Randomization/Scenarios/USimScenario.cs
  5. 29
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs
  6. 7
      com.unity.perception/Runtime/Randomization/Scenarios/USimScenarioBase.cs
  7. 3
      com.unity.perception/Runtime/Randomization/Scenarios/USimScenarioBase.cs.meta
  8. 30
      com.unity.perception/Runtime/Randomization/Scenarios/FixedFrameCountScenario.cs
  9. 3
      com.unity.perception/Runtime/Randomization/Scenarios/FixedFrameCountScenario.cs.meta

101
com.unity.perception/Editor/Randomization/RunInUSimWindow.cs


{
public class RunInUSimWindow : EditorWindow
{
SceneAsset m_MainScene;
USimScenario m_Scenario;
SysParamDefinition m_SysParam;
string m_RunName;
int m_TotalIterations;
int m_InstanceCount;
SysParamDefinition m_SysParam;
TextField m_RunNameField;
TextField m_RunExecutionIdField;
IntegerField m_TotalIterationsField;
IntegerField m_InstanceCountField;
ObjectField m_MainSceneField;
ObjectField m_ScenarioField;
TextElement m_NumNotRun;
TextElement m_NumFailures;
TextElement m_NumInProgress;
TextElement m_NumSuccess;
TextElement m_RunState;
[MenuItem("Window/Run in USim")]
public static void ShowWindow()

AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/RunInUSimWindow.uxml").CloneTree(root);
var runNameField = root.Q<TextField>("run-name");
runNameField.RegisterCallback<ChangeEvent<string>>(evt => { m_RunName = evt.newValue; });
SetViewDataKey(runNameField);
m_RunNameField = root.Q<TextField>("run-name");
SetViewDataKey(m_RunNameField);
m_TotalIterationsField = root.Q<IntegerField>("total-iterations");
SetViewDataKey(m_TotalIterationsField);
var totalIterationsField = root.Q<IntegerField>("total-iterations");
totalIterationsField.RegisterCallback<ChangeEvent<int>>(evt => { m_TotalIterations = evt.newValue; });
SetViewDataKey(totalIterationsField);
m_InstanceCountField = root.Q<IntegerField>("instance-count");
SetViewDataKey(m_InstanceCountField);
m_MainSceneField = root.Q<ObjectField>("main-scene");
m_MainSceneField.objectType = typeof(SceneAsset);
m_ScenarioField = root.Q<ObjectField>("scenario");
m_ScenarioField.objectType = typeof(ScenarioBase);
var instanceCountField = root.Q<IntegerField>("instance-count");
instanceCountField.RegisterCallback<ChangeEvent<int>>(evt => { m_InstanceCount = evt.newValue; });
instanceCountField.viewDataKey = "RunInUSim_instanceCount";
SetViewDataKey(instanceCountField);
m_RunExecutionIdField = root.Q<TextField>("run-execution-id");
SetViewDataKey(m_RunExecutionIdField);
var mainSceneField = root.Q<ObjectField>("main-scene");
mainSceneField.objectType = typeof(SceneAsset);
mainSceneField.RegisterCallback<ChangeEvent<Object>>(evt => { m_MainScene = (SceneAsset)evt.newValue; });
m_NumNotRun = root.Q<TextElement>("num-not-run");
m_NumFailures = root.Q<TextElement>("num-failures");
m_NumInProgress = root.Q<TextElement>("num-in-progress");
m_NumSuccess = root.Q<TextElement>("num-success");
m_RunState = root.Q<TextElement>("run-state");
var scenarioField = root.Q<ObjectField>("scenario");
scenarioField.objectType = typeof(ScenarioBase);
scenarioField.RegisterCallback<ChangeEvent<Object>>(evt => { m_Scenario = (USimScenario)evt.newValue; });
var summarizeExecutionButton = root.Q<Button>("summarize-execution");
summarizeExecutionButton.clicked += SummarizeExecution;
var sysParamDefinitions = API.GetSysParams();
var sysParamMenu = root.Q<ToolbarMenu>("sys-param");

runButton.clicked += RunInUSim;
}
void SummarizeExecution()
{
var summary = API.Summarize(m_RunExecutionIdField.value);
m_NumNotRun.text = summary.num_not_run.ToString();
m_NumFailures.text = summary.num_failures.ToString();
m_NumInProgress.text = summary.num_in_progress.ToString();
m_NumSuccess.text = summary.num_success.ToString();
m_RunState.text = summary.state.ToString();
}
void RunInUSim()
{
ValidateSettings();

void ValidateSettings()
{
if (string.IsNullOrEmpty(m_RunName))
if (string.IsNullOrEmpty(m_RunNameField.value))
if (m_Scenario == null)
if (m_ScenarioField.value == null)
if (m_MainScene == null)
if (m_MainSceneField.value == null)
throw new RankException("Null main scene");
}

var pathToProjectBuild = Application.dataPath + "/../" + "Build/";
if (!Directory.Exists(pathToProjectBuild + m_RunName))
Directory.CreateDirectory(pathToProjectBuild + m_RunName);
if (!Directory.Exists(pathToProjectBuild + m_RunNameField.value))
Directory.CreateDirectory(pathToProjectBuild + m_RunNameField.value);
pathToProjectBuild = pathToProjectBuild + m_RunName + "/";
pathToProjectBuild = pathToProjectBuild + m_RunNameField.value + "/";
scenes = new[] { AssetDatabase.GetAssetPath(m_MainScene) },
locationPathName = Path.Combine(pathToProjectBuild, m_RunName + ".x86_64"),
scenes = new[] { AssetDatabase.GetAssetPath(m_MainSceneField.value) },
locationPathName = Path.Combine(pathToProjectBuild, m_RunNameField.value + ".x86_64"),
target = BuildTarget.StandaloneLinux64
};
var report = BuildPipeline.BuildPlayer(buildPlayerOptions);

// Zip the build
Debug.Log("Starting to zip...");
var buildFolder = Application.dataPath + "/../" + "Build/" + m_RunName;
Zip.DirectoryContents(buildFolder, m_RunName);
var buildFolder = Application.dataPath + "/../" + "Build/" + m_RunNameField.value;
Zip.DirectoryContents(buildFolder, m_RunNameField.value);
m_BuildZipPath = buildFolder + ".zip";
Debug.Log("Created build zip");
}

var appParamIds = new List<AppParam>();
for (var i = 0; i < m_InstanceCount; i++)
for (var i = 0; i < m_InstanceCountField.value; i++)
var appParamName = $"{m_RunName}_{i}";
var appParamName = $"{m_RunNameField.value}_{i}";
totalIterations = m_TotalIterations,
instanceCount = m_InstanceCount,
totalIterations = m_TotalIterationsField.value,
instanceCount = m_InstanceCountField.value,
instanceIndex = i
});
appParamIds.Add(new AppParam()

Debug.Log("Uploading build...");
var buildId = await API.UploadBuildAsync(
m_RunName,
m_RunNameField.value,
m_BuildZipPath,
cancellationTokenSource: cancellationTokenSource);

var runDefinitionId = API.UploadRunDefinition(new RunDefinition
{
app_params = appParams.ToArray(),
name = m_RunName,
name = m_RunNameField.value,
sys_param_id = m_SysParam.id,
build_id = buildId
});

run.Execute();
m_RunExecutionIdField.value = run.executionId;
cancellationTokenSource.Dispose();
Debug.Log("Executed run");
}

25
com.unity.perception/Editor/Randomization/Uxml/RunInUSimWindow.uxml


<VisualElement style="align-items: center;">
<Button name="run-button" text="Build and Run" style="margin-top: 20px; padding: 2 20; font-size: 13px;"/>
</VisualElement>
<TextField name="run-execution-id" label="Run Execution ID" style="margin-top: 40px;"/>
<VisualElement class="unity-base-field">
<Label text="Not Run" class="unity-base-field__label"/>
<TextElement name="num-not-run" text="0"/>
</VisualElement>
<VisualElement class="unity-base-field">
<Label text="Failed" class="unity-base-field__label"/>
<TextElement name="num-failures" text="0"/>
</VisualElement>
<VisualElement class="unity-base-field">
<Label text="In Progress" class="unity-base-field__label"/>
<TextElement name="num-in-progress" text="0"/>
</VisualElement>
<VisualElement class="unity-base-field">
<Label text="Succeeded" class="unity-base-field__label"/>
<TextElement name="num-success" text="0"/>
</VisualElement>
<VisualElement class="unity-base-field">
<Label text="State" class="unity-base-field__label"/>
<TextElement name="run-state" text="default"/>
</VisualElement>
<VisualElement style="align-items: center;">
<Button name="summarize-execution" text="Summarize Execution" style="margin-top: 20px; padding: 2 20; font-size: 13px;"/>
</VisualElement>
</VisualElement>
</UXML>

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


/// A scenario that runs for a fixed number of frames during each iteration
/// </summary>
[AddComponentMenu("Perception/Randomization/Scenarios/Fixed Length Scenario")]
public class FixedLengthScenario: Scenario<FixedLengthScenario.Constants>
public class FixedLengthScenario : USimScenario
/// <summary>
/// Constants describing the execution of this scenario
/// </summary>
[Serializable]
public class Constants
{
/// <summary>
/// The number of frames to generate per iteration
/// </summary>
public int framesPerIteration = 1;
/// <summary>
/// The iteration index begin the simulation on
/// </summary>
public int startingIteration;
/// <summary>
/// The total number of iterations to complete before the simulation terminates
/// </summary>
public int totalIterations = 1000;
}
public int framesPerIteration = 1;
public int startingIteration;
public override bool isIterationComplete => currentIterationFrame >= constants.framesPerIteration;
public override bool isIterationComplete => currentIterationFrame >= framesPerIteration;
/// <summary>
/// Returns whether the scenario has completed

/// </summary>
public override void OnInitialize()
{
currentIteration = constants.startingIteration;
#if UNITY_EDITOR
currentIteration = startingIteration;
#else
base.OnInitialize();
#endif
}
}
}

11
com.unity.perception/Runtime/Randomization/Scenarios/USimScenario.cs


currentIteration += constants.instanceCount;
}
public override void OnFrameStart()
{
Debug.Log($"{currentIteration}: {currentIterationFrame}");
}
public override void Deserialize()
{
if (!string.IsNullOrEmpty(Unity.Simulation.Configuration.Instance.SimulationConfig.app_param_uri))

}
[Serializable]
public struct USimConstants
public class USimConstants
public int totalIterations;
public int instanceCount;
public int totalIterations = 100;
public int instanceCount = 1;
public int instanceIndex;
}
}

29
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs


m_Scenario = m_TestObject.AddComponent<FixedLengthScenario>();
m_Scenario.quitOnComplete = false;
m_Scenario.constants.totalIterations = totalIterations;
m_Scenario.constants.framesPerIteration = framesPerIteration;
m_Scenario.framesPerIteration = framesPerIteration;
yield return null; // Skip Start() frame
yield return null; // Skip first Update() frame
}

yield return CreateNewScenario(10, 10);
m_Scenario.serializedConstantsFileName = "perception_serialization_test";
var constants = new FixedLengthScenario.Constants
var constants = new USimConstants
framesPerIteration = 2,
startingIteration = 2,
totalIterations = 2
totalIterations = 2,
instanceCount = 2,
instanceIndex = 1
var changedConstants = new FixedLengthScenario.Constants
var changedConstants = new USimConstants
framesPerIteration = 0,
startingIteration = 0,
totalIterations = 0
totalIterations = 0,
instanceCount = 1,
instanceIndex = 0
};
// Serialize some values

// Change the values
m_Scenario.constants = changedConstants;
m_Scenario.Deserialize();
Assert.AreNotEqual(m_Scenario.constants.totalIterations, constants.totalIterations);
Assert.AreNotEqual(m_Scenario.constants.instanceCount, constants.instanceCount);
Assert.AreNotEqual(m_Scenario.constants.instanceIndex, constants.instanceIndex);
// Check if the values reverted correctly
Assert.AreEqual(m_Scenario.constants.framesPerIteration, constants.framesPerIteration);
Assert.AreEqual(m_Scenario.constants.startingIteration, constants.startingIteration);
// Check if the values deserialize correctly
m_Scenario.Deserialize();
Assert.AreEqual(m_Scenario.constants.instanceCount, constants.instanceCount);
Assert.AreEqual(m_Scenario.constants.instanceIndex, constants.instanceIndex);
// Clean up serialized constants
File.Delete(m_Scenario.serializedConstantsFilePath);

7
com.unity.perception/Runtime/Randomization/Scenarios/USimScenarioBase.cs


namespace UnityEngine.Perception.Randomization.Scenarios
{
public abstract class USimScenarioBase : ScenarioBase
{
}
}

3
com.unity.perception/Runtime/Randomization/Scenarios/USimScenarioBase.cs.meta


fileFormatVersion: 2
guid: 6b9735d0324e476999f22e4731058861
timeCreated: 1597687928

30
com.unity.perception/Runtime/Randomization/Scenarios/FixedFrameCountScenario.cs


using System;
namespace UnityEngine.Perception.Randomization.Scenarios
{
/// <summary>
/// An example scenario where each iteration runs for a fixed number of frames
/// </summary>
[AddComponentMenu("Randomization/Scenarios/Fixed Frame Count Scenario")]
public class FixedFrameCountScenario : USimScenario
{
public int framesPerIteration;
public override bool isIterationComplete => currentIterationFrame >= framesPerIteration;
public FixedFrameCountScenario()
{
constants = new USimConstants
{
instanceCount = 1,
instanceIndex = 0,
totalIterations = 1000
};
}
public override void OnInitialize()
{
currentIteration = constants.instanceIndex;
}
}
}

3
com.unity.perception/Runtime/Randomization/Scenarios/FixedFrameCountScenario.cs.meta


fileFormatVersion: 2
guid: a5fd9d0784580a747b3764afd636fc5f
timeCreated: 1589939921
正在加载...
取消
保存