浏览代码

A couple fixes for recording demonstrations (#1999)

* Sanitize demo filenames so that they can't be too long, overflow the header, and corrupt demo files
* Fix issue where 1st demo of each episode is always recorded as 0 action
/develop-generalizationTraining-TrainerController
GitHub 6 年前
当前提交
9c6dcb1b
共有 3 个文件被更改,包括 25 次插入12 次删除
  1. 4
      UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs
  2. 22
      UnitySDK/Assets/ML-Agents/Scripts/Agent.cs
  3. 11
      UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs

4
UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs


[Test]
public void TestSanitization()
{
const string dirtyString = "abc123&!@";
const string dirtyString = "abc1234567&!@";
var cleanString = DemonstrationRecorder.SanitizeName(dirtyString);
var cleanString = DemonstrationRecorder.SanitizeName(dirtyString, 6);
Assert.AreNotEqual(dirtyString, cleanString);
Assert.AreEqual(cleanString, knownCleanString);
}

22
UnitySDK/Assets/ML-Agents/Scripts/Agent.cs


BrainParameters param = brain.brainParameters;
actionMasker = new ActionMasker(param);
if (param.vectorActionSpaceType == SpaceType.continuous)
// If we haven't initialized vectorActions, initialize to 0. This should only
// happen during the creation of the Agent. In subsequent episodes, vectorAction
// should stay the previous action before the Done(), so that it is properly recorded.
if (action.vectorActions == null)
action.vectorActions = new float[param.vectorActionSize[0]];
info.storedVectorActions = new float[param.vectorActionSize[0]];
}
else
{
action.vectorActions = new float[param.vectorActionSize.Length];
info.storedVectorActions = new float[param.vectorActionSize.Length];
if (param.vectorActionSpaceType == SpaceType.continuous)
{
action.vectorActions = new float[param.vectorActionSize[0]];
info.storedVectorActions = new float[param.vectorActionSize[0]];
}
else
{
action.vectorActions = new float[param.vectorActionSize.Length];
info.storedVectorActions = new float[param.vectorActionSize.Length];
}
}
if (info.textObservation == null)

11
UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs


private Agent recordingAgent;
private string filePath;
private DemonstrationStore demoStore;
public const int MAX_NAME_LENGTH = 16;
/// <summary>
/// Initializes Demonstration store.

{
recordingAgent = GetComponent<Agent>();
demoStore = new DemonstrationStore();
demonstrationName = SanitizeName(demonstrationName);
demonstrationName = SanitizeName(demonstrationName, MAX_NAME_LENGTH);
demoStore.Initialize(
demonstrationName,
recordingAgent.brain.brainParameters,

/// <summary>
/// Removes all characters except alphanumerics from demonstration name.
/// Shorten name if it is longer than the maxNameLength.
public static string SanitizeName(string demoName)
public static string SanitizeName(string demoName, int maxNameLength)
// If the string is too long, it will overflow the metadata.
if (demoName.Length > maxNameLength)
{
demoName = demoName.Substring(0, maxNameLength);
}
return demoName;
}

正在加载...
取消
保存