Skip to content

Commit b4e8ba1

Browse files
author
Chris Elion
authored
Move Demonstration code to sub-folder (#3488)
1 parent 7fa914a commit b4e8ba1

File tree

11 files changed

+57
-45
lines changed

11 files changed

+57
-45
lines changed

com.unity.ml-agents/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222
- The interface for `RayPerceptionSensor.PerceiveStatic()` was changed to take an input class and write to an output class.
2323
- The checkpoint file suffix was changed from `.cptk` to `.ckpt` (#3470)
2424
- The command-line argument used to determine the port that an environment will listen on was changed from `--port` to `--mlagents-port`.
25+
- `DemonstrationRecorder` can now record observations outside of the editor.
26+
- `DemonstrationRecorder` now has an optional path for the demonstrations. This will default to `Application.dataPath` if not set.
27+
- `DemonstrationStore` was changed to accept a `Stream` for its constructor, and was renamed to `DemonstrationWriter`
2528
- The method `GetStepCount()` on the Agent class has been replaced with the property getter `StepCount`
2629

2730
### Bugfixes

com.unity.ml-agents/Editor/DemonstrationImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override void OnImportAsset(AssetImportContext ctx)
3131
var metaDataProto = DemonstrationMetaProto.Parser.ParseDelimitedFrom(reader);
3232
var metaData = metaDataProto.ToDemonstrationMetaData();
3333

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

com.unity.ml-agents/Runtime/Agent.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,12 @@ internal struct AgentParameters
175175
ActionMasker m_ActionMasker;
176176

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

184185
/// <summary>
185186
/// List of sensors used to generate observations.
@@ -257,7 +258,7 @@ public void LazyInitialize()
257258

258259
void OnDisable()
259260
{
260-
DemonstrationStores.Clear();
261+
DemonstrationWriters.Clear();
261262

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

286287
// We also have to write any to any DemonstationStores so that they get the "done" flag.
287-
foreach(var demoWriter in DemonstrationStores)
288+
foreach(var demoWriter in DemonstrationWriters)
288289
{
289290
demoWriter.Record(m_Info, sensors);
290291
}
@@ -532,8 +533,8 @@ void SendInfoToBrain()
532533

533534
m_Brain.RequestDecision(m_Info, sensors);
534535

535-
// If we have any DemonstrationStores, write the AgentInfo and sensors to them.
536-
foreach(var demoWriter in DemonstrationStores)
536+
// If we have any DemonstrationWriters, write the AgentInfo and sensors to them.
537+
foreach(var demoWriter in DemonstrationWriters)
537538
{
538539
demoWriter.Record(m_Info, sensors);
539540
}

com.unity.ml-agents/Runtime/Demonstrations.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Runtime/Demonstration.cs.meta renamed to com.unity.ml-agents/Runtime/Demonstrations/Demonstration.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Runtime/DemonstrationRecorder.cs renamed to com.unity.ml-agents/Runtime/Demonstrations/DemonstrationRecorder.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class DemonstrationRecorder : MonoBehaviour
2121
[Tooltip("Base directory to write the demo files. If null, will use {Application.dataPath}/Demonstrations.")]
2222
public string demonstrationDirectory;
2323

24-
DemonstrationStore m_DemoStore;
24+
DemonstrationWriter m_DemoWriter;
2525
internal const int MaxNameLength = 16;
2626

2727
const string k_ExtensionType = ".demo";
@@ -46,11 +46,11 @@ void Update()
4646
/// Creates demonstration store for use in recording.
4747
/// Has no effect if the demonstration store was already created.
4848
/// </summary>
49-
internal DemonstrationStore LazyInitialize(IFileSystem fileSystem = null)
49+
internal DemonstrationWriter LazyInitialize(IFileSystem fileSystem = null)
5050
{
51-
if (m_DemoStore != null)
51+
if (m_DemoWriter != null)
5252
{
53-
return m_DemoStore;
53+
return m_DemoWriter;
5454
}
5555

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

77-
m_DemoStore.Initialize(
77+
m_DemoWriter.Initialize(
7878
demonstrationName,
7979
behaviorParams.brainParameters,
8080
behaviorParams.fullyQualifiedBehaviorName
8181
);
8282

83-
AddDemonstrationStoreToAgent(m_DemoStore);
83+
AddDemonstrationWriterToAgent(m_DemoWriter);
8484

85-
return m_DemoStore;
85+
return m_DemoWriter;
8686
}
8787

8888
/// <summary>
@@ -134,46 +134,46 @@ internal static string MakeDemonstrationFilePath(
134134
}
135135

136136
/// <summary>
137-
/// Close the DemonstrationStore and remove it from the Agent.
138-
/// Has no effect if the DemonstrationStore is already closed (or wasn't opened)
137+
/// Close the DemonstrationWriter and remove it from the Agent.
138+
/// Has no effect if the DemonstrationWriter is already closed (or wasn't opened)
139139
/// </summary>
140140
public void Close()
141141
{
142-
if (m_DemoStore != null)
142+
if (m_DemoWriter != null)
143143
{
144-
RemoveDemonstrationStoreFromAgent(m_DemoStore);
144+
RemoveDemonstrationWriterFromAgent(m_DemoWriter);
145145

146-
m_DemoStore.Close();
147-
m_DemoStore = null;
146+
m_DemoWriter.Close();
147+
m_DemoWriter = null;
148148
}
149149
}
150150

151151
/// <summary>
152-
/// Clean up the DemonstrationStore when shutting down or destroying the Agent.
152+
/// Clean up the DemonstrationWriter when shutting down or destroying the Agent.
153153
/// </summary>
154154
void OnDestroy()
155155
{
156156
Close();
157157
}
158158

159159
/// <summary>
160-
/// Add additional DemonstrationStore to the Agent. It is still up to the user to Close this
161-
/// DemonstrationStores when recording is done.
160+
/// Add additional DemonstrationWriter to the Agent. It is still up to the user to Close this
161+
/// DemonstrationWriters when recording is done.
162162
/// </summary>
163-
/// <param name="demoStore"></param>
164-
public void AddDemonstrationStoreToAgent(DemonstrationStore demoStore)
163+
/// <param name="demoWriter"></param>
164+
public void AddDemonstrationWriterToAgent(DemonstrationWriter demoWriter)
165165
{
166-
m_Agent.DemonstrationStores.Add(demoStore);
166+
m_Agent.DemonstrationWriters.Add(demoWriter);
167167
}
168168

169169
/// <summary>
170-
/// Remove additional DemonstrationStore to the Agent. It is still up to the user to Close this
171-
/// DemonstrationStores when recording is done.
170+
/// Remove additional DemonstrationWriter to the Agent. It is still up to the user to Close this
171+
/// DemonstrationWriters when recording is done.
172172
/// </summary>
173-
/// <param name="demoStore"></param>
174-
public void RemoveDemonstrationStoreFromAgent(DemonstrationStore demoStore)
173+
/// <param name="demoWriter"></param>
174+
public void RemoveDemonstrationWriterFromAgent(DemonstrationWriter demoWriter)
175175
{
176-
m_Agent.DemonstrationStores.Remove(demoStore);
176+
m_Agent.DemonstrationWriters.Remove(demoWriter);
177177
}
178178
}
179179
}

com.unity.ml-agents/Runtime/DemonstrationRecorder.cs.meta renamed to com.unity.ml-agents/Runtime/Demonstrations/DemonstrationRecorder.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Runtime/DemonstrationStore.cs renamed to com.unity.ml-agents/Runtime/Demonstrations/DemonstrationWriter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace MLAgents
77
/// <summary>
88
/// Responsible for writing demonstration data to stream (usually a file stream).
99
/// </summary>
10-
public class DemonstrationStore
10+
public class DemonstrationWriter
1111
{
1212
public const int MetaDataBytes = 32; // Number of bytes allocated to metadata in demo file.
1313

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

1919
/// <summary>
20-
/// Create a DemonstrationStore that will write to the specified stream.
20+
/// Create a DemonstrationWriter that will write to the specified stream.
2121
/// The stream must support writes and seeking.
2222
/// </summary>
2323
/// <param name="stream"></param>
24-
public DemonstrationStore(Stream stream)
24+
public DemonstrationWriter(Stream stream)
2525
{
2626
m_Writer = stream;
2727
}

com.unity.ml-agents/Runtime/DemonstrationStore.cs.meta renamed to com.unity.ml-agents/Runtime/Demonstrations/DemonstrationWriter.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Tests/Editor/DemonstrationTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void TestStoreInitalize()
5454
demoRec.record = true;
5555
demoRec.demonstrationName = k_DemoName;
5656
demoRec.demonstrationDirectory = k_DemoDirectory;
57-
var demoStore = demoRec.LazyInitialize(fileSystem);
57+
var demoWriter = demoRec.LazyInitialize(fileSystem);
5858

5959
Assert.IsTrue(fileSystem.Directory.Exists(k_DemoDirectory));
6060
Assert.IsTrue(fileSystem.FileExists(k_DemoDirectory + k_DemoName + k_ExtensionType));
@@ -70,15 +70,15 @@ public void TestStoreInitalize()
7070
};
7171

7272

73-
demoStore.Record(agentInfo, new System.Collections.Generic.List<ISensor>());
73+
demoWriter.Record(agentInfo, new System.Collections.Generic.List<ISensor>());
7474
demoRec.Close();
7575

7676
// Make sure close can be called multiple times
77-
demoStore.Close();
77+
demoWriter.Close();
7878
demoRec.Close();
7979

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

8484
public class ObservationAgent : TestAgent
@@ -129,7 +129,7 @@ public void TestAgentWrite()
129129

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

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

0 commit comments

Comments
 (0)