Skip to content

A couple fixes for recording demonstrations #1999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class DemonstrationTests : MonoBehaviour
[Test]
public void TestSanitization()
{
const string dirtyString = "abc123&!@";
const string dirtyString = "abc1234567&!@";
const string knownCleanString = "abc123";
var cleanString = DemonstrationRecorder.SanitizeName(dirtyString);
var cleanString = DemonstrationRecorder.SanitizeName(dirtyString, 6);
Assert.AreNotEqual(dirtyString, cleanString);
Assert.AreEqual(cleanString, knownCleanString);
}
Expand Down
22 changes: 14 additions & 8 deletions UnitySDK/Assets/ML-Agents/Scripts/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,15 +531,21 @@ void ResetData()

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)
Expand Down
11 changes: 9 additions & 2 deletions UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class DemonstrationRecorder : MonoBehaviour
private Agent recordingAgent;
private string filePath;
private DemonstrationStore demoStore;
public const int MAX_NAME_LENGTH = 16;

/// <summary>
/// Initializes Demonstration store.
Expand All @@ -24,7 +25,7 @@ private void Start()
{
recordingAgent = GetComponent<Agent>();
demoStore = new DemonstrationStore();
demonstrationName = SanitizeName(demonstrationName);
demonstrationName = SanitizeName(demonstrationName, MAX_NAME_LENGTH);
demoStore.Initialize(
demonstrationName,
recordingAgent.brain.brainParameters,
Expand All @@ -35,11 +36,17 @@ private void Start()

/// <summary>
/// Removes all characters except alphanumerics from demonstration name.
/// Shorten name if it is longer than the maxNameLength.
/// </summary>
public static string SanitizeName(string demoName)
public static string SanitizeName(string demoName, int maxNameLength)
{
var rgx = new Regex("[^a-zA-Z0-9 -]");
demoName = rgx.Replace(demoName, "");
// If the string is too long, it will overflow the metadata.
if (demoName.Length > maxNameLength)
{
demoName = demoName.Substring(0, maxNameLength);
}
return demoName;
}

Expand Down