Skip to content

Move Demonstration code to sub-folder #3488

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 2 commits into from
Feb 25, 2020
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
3 changes: 3 additions & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- The interface for `RayPerceptionSensor.PerceiveStatic()` was changed to take an input class and write to an output class.
- The checkpoint file suffix was changed from `.cptk` to `.ckpt` (#3470)
- The command-line argument used to determine the port that an environment will listen on was changed from `--port` to `--mlagents-port`.
- `DemonstrationRecorder` can now record observations outside of the editor.
- `DemonstrationRecorder` now has an optional path for the demonstrations. This will default to `Application.dataPath` if not set.
- `DemonstrationStore` was changed to accept a `Stream` for its constructor, and was renamed to `DemonstrationWriter`
- The method `GetStepCount()` on the Agent class has been replaced with the property getter `StepCount`

### Bugfixes
Expand Down
2 changes: 1 addition & 1 deletion com.unity.ml-agents/Editor/DemonstrationImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void OnImportAsset(AssetImportContext ctx)
var metaDataProto = DemonstrationMetaProto.Parser.ParseDelimitedFrom(reader);
var metaData = metaDataProto.ToDemonstrationMetaData();

reader.Seek(DemonstrationStore.MetaDataBytes + 1, 0);
reader.Seek(DemonstrationWriter.MetaDataBytes + 1, 0);
var brainParamsProto = BrainParametersProto.Parser.ParseDelimitedFrom(reader);
var brainParameters = brainParamsProto.ToBrainParameters();

Expand Down
17 changes: 9 additions & 8 deletions com.unity.ml-agents/Runtime/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ internal struct AgentParameters
ActionMasker m_ActionMasker;

/// <summary>
/// Set of DemonstrationStores that the Agent will write its step information to.
/// If you use a DemonstrationRecorder component, this will automatically register its DemonstrationStore.
/// You can also add your own DemonstrationStore by calling DemonstrationRecorder.AddDemonstrationStoreToAgent()
/// Set of DemonstrationWriters that the Agent will write its step information to.
/// If you use a DemonstrationRecorder component, this will automatically register its DemonstrationWriter.
/// You can also add your own DemonstrationWriter by calling
/// DemonstrationRecorder.AddDemonstrationWriterToAgent()
/// </summary>
internal ISet<DemonstrationStore> DemonstrationStores = new HashSet<DemonstrationStore>();
internal ISet<DemonstrationWriter> DemonstrationWriters = new HashSet<DemonstrationWriter>();

/// <summary>
/// List of sensors used to generate observations.
Expand Down Expand Up @@ -252,7 +253,7 @@ public void LazyInitialize()
/// becomes disabled or inactive.
void OnDisable()
{
DemonstrationStores.Clear();
DemonstrationWriters.Clear();

// If Academy.Dispose has already been called, we don't need to unregister with it.
// We don't want to even try, because this will lazily create a new Academy!
Expand All @@ -279,7 +280,7 @@ void NotifyAgentDone(bool maxStepReached = false)
m_Brain?.RequestDecision(m_Info, sensors);

// We also have to write any to any DemonstationStores so that they get the "done" flag.
foreach(var demoWriter in DemonstrationStores)
foreach(var demoWriter in DemonstrationWriters)
{
demoWriter.Record(m_Info, sensors);
}
Expand Down Expand Up @@ -524,8 +525,8 @@ void SendInfoToBrain()

m_Brain.RequestDecision(m_Info, sensors);

// If we have any DemonstrationStores, write the AgentInfo and sensors to them.
foreach(var demoWriter in DemonstrationStores)
// If we have any DemonstrationWriters, write the AgentInfo and sensors to them.
foreach(var demoWriter in DemonstrationWriters)
{
demoWriter.Record(m_Info, sensors);
}
Expand Down
8 changes: 8 additions & 0 deletions com.unity.ml-agents/Runtime/Demonstrations.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DemonstrationRecorder : MonoBehaviour
[Tooltip("Base directory to write the demo files. If null, will use {Application.dataPath}/Demonstrations.")]
public string demonstrationDirectory;

DemonstrationStore m_DemoStore;
DemonstrationWriter m_DemoWriter;
internal const int MaxNameLength = 16;

const string k_ExtensionType = ".demo";
Expand All @@ -46,11 +46,11 @@ void Update()
/// Creates demonstration store for use in recording.
/// Has no effect if the demonstration store was already created.
/// </summary>
internal DemonstrationStore LazyInitialize(IFileSystem fileSystem = null)
internal DemonstrationWriter LazyInitialize(IFileSystem fileSystem = null)
{
if (m_DemoStore != null)
if (m_DemoWriter != null)
{
return m_DemoStore;
return m_DemoWriter;
}

if (m_Agent == null)
Expand All @@ -72,17 +72,17 @@ internal DemonstrationStore LazyInitialize(IFileSystem fileSystem = null)
demonstrationName = SanitizeName(demonstrationName, MaxNameLength);
var filePath = MakeDemonstrationFilePath(m_FileSystem, demonstrationDirectory, demonstrationName);
var stream = m_FileSystem.File.Create(filePath);
m_DemoStore = new DemonstrationStore(stream);
m_DemoWriter = new DemonstrationWriter(stream);

m_DemoStore.Initialize(
m_DemoWriter.Initialize(
demonstrationName,
behaviorParams.brainParameters,
behaviorParams.fullyQualifiedBehaviorName
);

AddDemonstrationStoreToAgent(m_DemoStore);
AddDemonstrationWriterToAgent(m_DemoWriter);

return m_DemoStore;
return m_DemoWriter;
}

/// <summary>
Expand Down Expand Up @@ -134,46 +134,46 @@ internal static string MakeDemonstrationFilePath(
}

/// <summary>
/// Close the DemonstrationStore and remove it from the Agent.
/// Has no effect if the DemonstrationStore is already closed (or wasn't opened)
/// Close the DemonstrationWriter and remove it from the Agent.
/// Has no effect if the DemonstrationWriter is already closed (or wasn't opened)
/// </summary>
public void Close()
{
if (m_DemoStore != null)
if (m_DemoWriter != null)
{
RemoveDemonstrationStoreFromAgent(m_DemoStore);
RemoveDemonstrationWriterFromAgent(m_DemoWriter);

m_DemoStore.Close();
m_DemoStore = null;
m_DemoWriter.Close();
m_DemoWriter = null;
}
}

/// <summary>
/// Clean up the DemonstrationStore when shutting down or destroying the Agent.
/// Clean up the DemonstrationWriter when shutting down or destroying the Agent.
/// </summary>
void OnDestroy()
{
Close();
}

/// <summary>
/// Add additional DemonstrationStore to the Agent. It is still up to the user to Close this
/// DemonstrationStores when recording is done.
/// Add additional DemonstrationWriter to the Agent. It is still up to the user to Close this
/// DemonstrationWriters when recording is done.
/// </summary>
/// <param name="demoStore"></param>
public void AddDemonstrationStoreToAgent(DemonstrationStore demoStore)
/// <param name="demoWriter"></param>
public void AddDemonstrationWriterToAgent(DemonstrationWriter demoWriter)
{
m_Agent.DemonstrationStores.Add(demoStore);
m_Agent.DemonstrationWriters.Add(demoWriter);
}

/// <summary>
/// Remove additional DemonstrationStore to the Agent. It is still up to the user to Close this
/// DemonstrationStores when recording is done.
/// Remove additional DemonstrationWriter to the Agent. It is still up to the user to Close this
/// DemonstrationWriters when recording is done.
/// </summary>
/// <param name="demoStore"></param>
public void RemoveDemonstrationStoreFromAgent(DemonstrationStore demoStore)
/// <param name="demoWriter"></param>
public void RemoveDemonstrationWriterFromAgent(DemonstrationWriter demoWriter)
{
m_Agent.DemonstrationStores.Remove(demoStore);
m_Agent.DemonstrationWriters.Remove(demoWriter);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MLAgents
/// <summary>
/// Responsible for writing demonstration data to stream (usually a file stream).
/// </summary>
public class DemonstrationStore
public class DemonstrationWriter
{
public const int MetaDataBytes = 32; // Number of bytes allocated to metadata in demo file.

Expand All @@ -17,11 +17,11 @@ public class DemonstrationStore
WriteAdapter m_WriteAdapter = new WriteAdapter();

/// <summary>
/// Create a DemonstrationStore that will write to the specified stream.
/// Create a DemonstrationWriter that will write to the specified stream.
/// The stream must support writes and seeking.
/// </summary>
/// <param name="stream"></param>
public DemonstrationStore(Stream stream)
public DemonstrationWriter(Stream stream)
{
m_Writer = stream;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions com.unity.ml-agents/Tests/Editor/DemonstrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void TestStoreInitalize()
demoRec.record = true;
demoRec.demonstrationName = k_DemoName;
demoRec.demonstrationDirectory = k_DemoDirectory;
var demoStore = demoRec.LazyInitialize(fileSystem);
var demoWriter = demoRec.LazyInitialize(fileSystem);

Assert.IsTrue(fileSystem.Directory.Exists(k_DemoDirectory));
Assert.IsTrue(fileSystem.FileExists(k_DemoDirectory + k_DemoName + k_ExtensionType));
Expand All @@ -70,15 +70,15 @@ public void TestStoreInitalize()
};


demoStore.Record(agentInfo, new System.Collections.Generic.List<ISensor>());
demoWriter.Record(agentInfo, new System.Collections.Generic.List<ISensor>());
demoRec.Close();

// Make sure close can be called multiple times
demoStore.Close();
demoWriter.Close();
demoRec.Close();

// Make sure trying to write after closing doesn't raise an error.
demoStore.Record(agentInfo, new System.Collections.Generic.List<ISensor>());
demoWriter.Record(agentInfo, new System.Collections.Generic.List<ISensor>());
}

public class ObservationAgent : TestAgent
Expand Down Expand Up @@ -129,7 +129,7 @@ public void TestAgentWrite()

// Read back the demo file and make sure observations were written
var reader = fileSystem.File.OpenRead("Assets/Demonstrations/TestBrain.demo");
reader.Seek(DemonstrationStore.MetaDataBytes + 1, 0);
reader.Seek(DemonstrationWriter.MetaDataBytes + 1, 0);
BrainParametersProto.Parser.ParseDelimitedFrom(reader);

var agentInfoProto = AgentInfoActionPairProto.Parser.ParseDelimitedFrom(reader).AgentInfo;
Expand Down