浏览代码

add debugging option to ModelOverrider, handle scene resets (#3997)

/docs-update
GitHub 4 年前
当前提交
4cb7bfe7
共有 1 个文件被更改,包括 46 次插入2 次删除
  1. 48
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs

48
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs


using System.IO;
using Unity.MLAgents;
using Unity.MLAgents.Policies;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Unity.MLAgentsExamples
{

int m_MaxEpisodes;
int m_NumSteps;
int m_PreviousNumSteps;
int m_PreviousAgentCompletedEpisodes;
[Tooltip("Debug values to be used in place of the command line for overriding models.")]
public string debugCommandLineOverride;
// Static values to keep track of completed episodes and steps across resets
// These are updated in OnDisable.
static int s_PreviousAgentCompletedEpisodes;
static int s_PreviousNumSteps;
int TotalCompletedEpisodes
{
get { return m_PreviousAgentCompletedEpisodes + (m_Agent == null ? 0 : m_Agent.CompletedEpisodes); }
}
int TotalNumSteps
{
get { return m_PreviousNumSteps + m_NumSteps; }
}
/// <summary>
/// Get the asset path to use from the commandline arguments.

m_BehaviorNameOverrides.Clear();
var maxEpisodes = 0;
string[] commandLineArgsOverride = null;
if (!string.IsNullOrEmpty(debugCommandLineOverride) && Application.isEditor)
{
commandLineArgsOverride = debugCommandLineOverride.Split(' ');
}
var args = Environment.GetCommandLineArgs();
var args = commandLineArgsOverride ?? Environment.GetCommandLineArgs();
for (var i = 0; i < args.Length; i++)
{
if (args[i] == k_CommandLineModelOverrideFlag && i < args.Length-2)

void OnEnable()
{
// Start with these initialized to previous values in the case where we're resetting scenes.
m_PreviousNumSteps = s_PreviousNumSteps;
m_PreviousAgentCompletedEpisodes = s_PreviousAgentCompletedEpisodes;
m_Agent = GetComponent<Agent>();
GetAssetPathFromCommandLine();

}
}
void OnDisable()
{
// Update the static episode and step counts.
// For a single agent in the scene, this will be a straightforward increment.
// If there are multiple agents, we'll increment the count by the Agent that completed the most episodes.
s_PreviousAgentCompletedEpisodes = Mathf.Max(s_PreviousAgentCompletedEpisodes, TotalCompletedEpisodes);
s_PreviousNumSteps = Mathf.Max(s_PreviousNumSteps, TotalNumSteps);
}
void FixedUpdate()
{
if (m_MaxEpisodes > 0)

// Since we exit as soon as *any* Agent hits its target, the maxSteps condition keeps us running
// a bit longer in case there's an early failure.
if (m_Agent.CompletedEpisodes >= m_MaxEpisodes && m_NumSteps > m_MaxEpisodes * m_Agent.MaxStep)
if (TotalCompletedEpisodes >= m_MaxEpisodes && TotalNumSteps > m_MaxEpisodes * m_Agent.MaxStep)
Debug.Log($"ModelOverride reached {TotalCompletedEpisodes} episodes and {TotalNumSteps} steps. Exiting.");
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#endif
}
}
m_NumSteps++;

正在加载...
取消
保存