Skip to content

Commit 1d23994

Browse files
author
Chris Elion
authored
Academy interface cleanup (#3376)
* Academy interface cleanup * fix tests * fix destroyImmediate use in test * use isEditor to switch between Destroy and DestroyImmediate
1 parent 98d25d0 commit 1d23994

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ public bool IsCommunicatorOn
148148
{
149149
Application.quitting += Dispose;
150150

151-
LazyInitialization();
151+
LazyInitialize();
152152
}
153153

154154
/// <summary>
155155
/// Initialize the Academy if it hasn't already been initialized.
156156
/// This method is always safe to call; it will have no effect if the Academy is already initialized.
157157
/// </summary>
158-
internal void LazyInitialization()
158+
internal void LazyInitialize()
159159
{
160160
if (!m_Initialized)
161161
{
@@ -168,7 +168,7 @@ internal void LazyInitialization()
168168
/// Enable stepping of the Academy during the FixedUpdate phase. This is done by creating a temporary
169169
/// GameObject with a MonoBehavior that calls Academy.EnvironmentStep().
170170
/// </summary>
171-
public void EnableAutomaticStepping()
171+
void EnableAutomaticStepping()
172172
{
173173
if (m_FixedUpdateStepper != null)
174174
{
@@ -185,15 +185,15 @@ public void EnableAutomaticStepping()
185185
/// Disable stepping of the Academy during the FixedUpdate phase. If this is called, the Academy must be
186186
/// stepped manually by the user by calling Academy.EnvironmentStep().
187187
/// </summary>
188-
public void DisableAutomaticStepping(bool destroyImmediate = false)
188+
void DisableAutomaticStepping()
189189
{
190190
if (m_FixedUpdateStepper == null)
191191
{
192192
return;
193193
}
194194

195195
m_FixedUpdateStepper = null;
196-
if (destroyImmediate)
196+
if (Application.isEditor)
197197
{
198198
UnityEngine.Object.DestroyImmediate(m_StepperObject);
199199
}
@@ -206,11 +206,21 @@ public void DisableAutomaticStepping(bool destroyImmediate = false)
206206
}
207207

208208
/// <summary>
209-
/// Returns whether or not the Academy is automatically stepped during the FixedUpdate phase.
209+
/// Determines whether or not the Academy is automatically stepped during the FixedUpdate phase.
210210
/// </summary>
211-
public bool IsAutomaticSteppingEnabled
211+
public bool AutomaticSteppingEnabled
212212
{
213213
get { return m_FixedUpdateStepper != null; }
214+
set {
215+
if (value)
216+
{
217+
EnableAutomaticStepping();
218+
}
219+
else
220+
{
221+
DisableAutomaticStepping();
222+
}
223+
}
214224
}
215225

216226
// Used to read Python-provided environment parameters
@@ -335,9 +345,9 @@ void OnResetCommand()
335345
/// <returns>
336346
/// Current episode number.
337347
/// </returns>
338-
public int GetEpisodeCount()
348+
public int EpisodeCount
339349
{
340-
return m_EpisodeCount;
350+
get { return m_EpisodeCount; }
341351
}
342352

343353
/// <summary>
@@ -346,9 +356,9 @@ public int GetEpisodeCount()
346356
/// <returns>
347357
/// Current step count.
348358
/// </returns>
349-
public int GetStepCount()
359+
public int StepCount
350360
{
351-
return m_StepCount;
361+
get { return m_StepCount; }
352362
}
353363

354364
/// <summary>
@@ -357,9 +367,9 @@ public int GetStepCount()
357367
/// <returns>
358368
/// Total step count.
359369
/// </returns>
360-
public int GetTotalStepCount()
370+
public int TotalStepCount
361371
{
362-
return m_TotalStepCount;
372+
get { return m_TotalStepCount; }
363373
}
364374

365375
/// <summary>
@@ -445,7 +455,7 @@ internal ModelRunner GetOrCreateModelRunner(
445455
/// </summary>
446456
public void Dispose()
447457
{
448-
DisableAutomaticStepping(true);
458+
DisableAutomaticStepping();
449459
// Signal to listeners that the academy is being destroyed now
450460
DestroyAction?.Invoke();
451461

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ public void TestAcademy()
128128
{
129129
var aca = Academy.Instance;
130130
Assert.AreNotEqual(null, aca);
131-
Assert.AreEqual(0, aca.GetEpisodeCount());
132-
Assert.AreEqual(0, aca.GetStepCount());
133-
Assert.AreEqual(0, aca.GetTotalStepCount());
131+
Assert.AreEqual(0, aca.EpisodeCount);
132+
Assert.AreEqual(0, aca.StepCount);
133+
Assert.AreEqual(0, aca.TotalStepCount);
134134
}
135135

136136
[Test]
@@ -164,12 +164,12 @@ public void TestAcademy()
164164
Assert.AreEqual(true, Academy.IsInitialized);
165165

166166
// Check that init is idempotent
167-
aca.LazyInitialization();
168-
aca.LazyInitialization();
167+
aca.LazyInitialize();
168+
aca.LazyInitialize();
169169

170-
Assert.AreEqual(0, aca.GetEpisodeCount());
171-
Assert.AreEqual(0, aca.GetStepCount());
172-
Assert.AreEqual(0, aca.GetTotalStepCount());
170+
Assert.AreEqual(0, aca.EpisodeCount);
171+
Assert.AreEqual(0, aca.StepCount);
172+
Assert.AreEqual(0, aca.TotalStepCount);
173173
Assert.AreNotEqual(null, aca.FloatProperties);
174174

175175
// Check that Dispose is idempotent
@@ -246,8 +246,8 @@ public void TestAcademy()
246246
var numberReset = 0;
247247
for (var i = 0; i < 10; i++)
248248
{
249-
Assert.AreEqual(numberReset, aca.GetEpisodeCount());
250-
Assert.AreEqual(i, aca.GetStepCount());
249+
Assert.AreEqual(numberReset, aca.EpisodeCount);
250+
Assert.AreEqual(i, aca.StepCount);
251251

252252
// The reset happens at the beginning of the first step
253253
if (i == 0)
@@ -262,11 +262,11 @@ public void TestAcademy()
262262
public void TestAcademyAutostep()
263263
{
264264
var aca = Academy.Instance;
265-
Assert.IsTrue(aca.IsAutomaticSteppingEnabled);
266-
aca.DisableAutomaticStepping(true);
267-
Assert.IsFalse(aca.IsAutomaticSteppingEnabled);
268-
aca.EnableAutomaticStepping();
269-
Assert.IsTrue(aca.IsAutomaticSteppingEnabled);
265+
Assert.IsTrue(aca.AutomaticSteppingEnabled);
266+
aca.AutomaticSteppingEnabled = false;
267+
Assert.IsFalse(aca.AutomaticSteppingEnabled);
268+
aca.AutomaticSteppingEnabled = true;
269+
Assert.IsTrue(aca.AutomaticSteppingEnabled);
270270
}
271271

272272
[Test]
@@ -357,9 +357,9 @@ public void TestAcademy()
357357
var stepsSinceReset = 0;
358358
for (var i = 0; i < 50; i++)
359359
{
360-
Assert.AreEqual(stepsSinceReset, aca.GetStepCount());
361-
Assert.AreEqual(numberReset, aca.GetEpisodeCount());
362-
Assert.AreEqual(i, aca.GetTotalStepCount());
360+
Assert.AreEqual(stepsSinceReset, aca.StepCount);
361+
Assert.AreEqual(numberReset, aca.EpisodeCount);
362+
Assert.AreEqual(i, aca.TotalStepCount);
363363
// Academy resets at the first step
364364
if (i == 0)
365365
{
@@ -395,10 +395,10 @@ public void TestAgent()
395395
var agent2StepSinceReset = 0;
396396
for (var i = 0; i < 5000; i++)
397397
{
398-
Assert.AreEqual(acaStepsSinceReset, aca.GetStepCount());
399-
Assert.AreEqual(numberAcaReset, aca.GetEpisodeCount());
398+
Assert.AreEqual(acaStepsSinceReset, aca.StepCount);
399+
Assert.AreEqual(numberAcaReset, aca.EpisodeCount);
400400

401-
Assert.AreEqual(i, aca.GetTotalStepCount());
401+
Assert.AreEqual(i, aca.TotalStepCount);
402402

403403
Assert.AreEqual(agent2StepSinceReset, agent2.GetStepCount());
404404
Assert.AreEqual(numberAgent1Reset, agent1.agentResetCalls);

0 commit comments

Comments
 (0)