Skip to content

Commit fa7d381

Browse files
Develop side channels: migrate reset parameters (#2990)
* [WIP] Side Channel initial layout * Working prototype for raw bytes * fixing format mistake * Added some errors and some unit tests in C# * Added the side channel for the Engine Configuration. (#2958) * Added the side channel for the Engine Configuration. Note that this change does not require modifying a lot of files : - Adding a sender in Python - Adding a receiver in C# - subscribe the receiver to the communicator (here is a one liner in the Academy) - Add the side channel to the Python UnityEnvironment (not represented here) Adding the side channel to the environment would look like such : ```python from mlagents.envs.environment import UnityEnvironment from mlagents.envs.side_channel.raw_bytes_channel import RawBytesChannel from mlagents.envs.side_channel.engine_configuration_channel import EngineConfigurationChannel channel0 = RawBytesChannel() channel1 = EngineConfigurationChannel() env = UnityEnvironment(base_port = 5004, side_channels = [channel0, channel1]) ``` * renamings * addressing comments * Logging a message when an unknown side channel number has been received by Unity * Addressing comments * renamings * renamings * Adding FloatProperties to the side channels (#2968) * renaming m_SideChannelsDict to m_SideChannel * renaming and some comments * renaming and adding a GetAndClearReceivedMessages() in the RawBytesSideChannel * micro-optimization * more errors and some nit * addressing comments * Using little-endian format in Python * adding some comments * Code comments * some changes and added the unit tests on both Python and C# * removing default default in get default * Update UnitySDK/Assets/ML-Agents/Scripts/SideChannel/SideChannel.cs Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * Update ml-agents-envs/mlagents/envs/side_channel/raw_bytes_channel.py Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * addressing comments * fixing tests * removing the arguments to reset and the property reset_parameters on the UnityEnvironment * curriculum works but removed the check for reset parameters in the scene * processing side channels before the reset command * Removing engine configuration from C# * Engine configuration removed * fixing the tests * Update ml-agents-envs/mlagents/envs/subprocess_env_manager.py Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * Removing register callbacks with empty calls in FloarProperties * Clamp instead of min max * removing the brain names from the environment.py print * renaming reset_parameters to get properties * made a default engine config * bug fix * Empty commit * Docs changes for the Side Channels feature (#3011) * Docs changes for the Side Channels feature * replace deprecated with removed on the CustomResetPratmeters` * Update docs/Python-API.md Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * Update docs/Training-Generalized-Reinforcement-Learning-Agents.md Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * Removing the console outputs in the docs * Update docs/Training-ML-Agents.md Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * replace does not work with ignored * adding a note on side channels * adding some steps to migrate * addressing comments * adding more docs to the LL-API * added a blob on how to access the properties in C# * adding space between ResetParameters * fix typo * bug fixes * addressing comments
1 parent 51a2f07 commit fa7d381

File tree

70 files changed

+429
-1514
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+429
-1514
lines changed

UnitySDK/Assets/ML-Agents/Editor/ResetParameterDrawer.cs

Lines changed: 0 additions & 179 deletions
This file was deleted.

UnitySDK/Assets/ML-Agents/Editor/ResetParameterDrawer.cs.meta

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public void TestAgentWrite()
9393
var acaGo = new GameObject("TestAcademy");
9494
acaGo.AddComponent<TestAcademy>();
9595
var aca = acaGo.GetComponent<TestAcademy>();
96-
aca.resetParameters = new ResetParameters();
9796

9897
var academyInitializeMethod = typeof(Academy).GetMethod("InitializeEnvironment",
9998
BindingFlags.Instance | BindingFlags.NonPublic);

UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ static IEnumerable<Agent> GetFakeAgents()
1515
var acaGo = new GameObject("TestAcademy");
1616
acaGo.AddComponent<TestAcademy>();
1717
var aca = acaGo.GetComponent<TestAcademy>();
18-
aca.resetParameters = new ResetParameters();
1918

2019
var goA = new GameObject("goA");
2120
var bpA = goA.AddComponent<BehaviorParameters>();

UnitySDK/Assets/ML-Agents/Editor/Tests/MLAgentsEditModeTest.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public void TestAcademy()
143143
var acaGo = new GameObject("TestAcademy");
144144
acaGo.AddComponent<TestAcademy>();
145145
var aca = acaGo.GetComponent<TestAcademy>();
146-
aca.resetParameters = new ResetParameters();
147146
Assert.AreEqual(0, aca.initializeAcademyCalls);
148147
Assert.AreEqual(0, aca.GetStepCount());
149148
Assert.AreEqual(0, aca.GetEpisodeCount());
@@ -169,7 +168,6 @@ public void TestAgent()
169168
var acaGo = new GameObject("TestAcademy");
170169
acaGo.AddComponent<TestAcademy>();
171170
var aca = acaGo.GetComponent<TestAcademy>();
172-
aca.resetParameters = new ResetParameters();
173171

174172
Assert.AreEqual(false, agent1.IsDone());
175173
Assert.AreEqual(false, agent2.IsDone());
@@ -215,7 +213,6 @@ public void TestAcademy()
215213
var acaGo = new GameObject("TestAcademy");
216214
acaGo.AddComponent<TestAcademy>();
217215
var aca = acaGo.GetComponent<TestAcademy>();
218-
aca.resetParameters = new ResetParameters();
219216
var academyInitializeMethod = typeof(Academy).GetMethod("InitializeEnvironment",
220217
BindingFlags.Instance | BindingFlags.NonPublic);
221218
academyInitializeMethod?.Invoke(aca, new object[] { });
@@ -252,7 +249,6 @@ public void TestAgent()
252249
var acaGo = new GameObject("TestAcademy");
253250
acaGo.AddComponent<TestAcademy>();
254251
var aca = acaGo.GetComponent<TestAcademy>();
255-
aca.resetParameters = new ResetParameters();
256252

257253

258254
var agentEnableMethod = typeof(Agent).GetMethod(
@@ -330,7 +326,6 @@ public void TestAcademy()
330326
var acaGo = new GameObject("TestAcademy");
331327
acaGo.AddComponent<TestAcademy>();
332328
var aca = acaGo.GetComponent<TestAcademy>();
333-
aca.resetParameters = new ResetParameters();
334329
var academyInitializeMethod = typeof(Academy).GetMethod(
335330
"InitializeEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);
336331
academyInitializeMethod?.Invoke(aca, new object[] { });
@@ -369,7 +364,6 @@ public void TestAgent()
369364
var acaGo = new GameObject("TestAcademy");
370365
acaGo.AddComponent<TestAcademy>();
371366
var aca = acaGo.GetComponent<TestAcademy>();
372-
aca.resetParameters = new ResetParameters();
373367

374368

375369
var agentEnableMethod = typeof(Agent).GetMethod(
@@ -475,7 +469,6 @@ public void TestResetOnDone()
475469
var acaGo = new GameObject("TestAcademy");
476470
acaGo.AddComponent<TestAcademy>();
477471
var aca = acaGo.GetComponent<TestAcademy>();
478-
aca.resetParameters = new ResetParameters();
479472

480473

481474
var agentEnableMethod = typeof(Agent).GetMethod(
@@ -552,7 +545,6 @@ public void TestCumulativeReward()
552545
var acaGo = new GameObject("TestAcademy");
553546
acaGo.AddComponent<TestAcademy>();
554547
var aca = acaGo.GetComponent<TestAcademy>();
555-
aca.resetParameters = new ResetParameters();
556548

557549

558550
var agentEnableMethod = typeof(Agent).GetMethod(

UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAcademy.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
public class Ball3DAcademy : Academy
55
{
6-
public override void AcademyReset()
6+
public override void InitializeAcademy()
77
{
8-
Physics.gravity = new Vector3(0, -resetParameters["gravity"], 0);
8+
FloatProperties.RegisterCallback("gravity", f => { Physics.gravity = new Vector3(0, -f, 0); });
99
}
1010

11-
public override void AcademyStep()
12-
{
13-
}
1411
}

UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ public class Ball3DAgent : Agent
66
[Header("Specific to Ball3D")]
77
public GameObject ball;
88
Rigidbody m_BallRb;
9-
ResetParameters m_ResetParams;
9+
IFloatProperties m_ResetParams;
1010

1111
public override void InitializeAgent()
1212
{
1313
m_BallRb = ball.GetComponent<Rigidbody>();
1414
var academy = FindObjectOfType<Academy>();
15-
m_ResetParams = academy.resetParameters;
15+
m_ResetParams = academy.FloatProperties;
1616
SetResetParameters();
1717
}
1818

@@ -77,8 +77,8 @@ public override float[] Heuristic()
7777
public void SetBall()
7878
{
7979
//Set the attributes of the ball by fetching the information from the academy
80-
m_BallRb.mass = m_ResetParams["mass"];
81-
var scale = m_ResetParams["scale"];
80+
m_BallRb.mass = m_ResetParams.GetPropertyWithDefault("mass", 1.0f);
81+
var scale = m_ResetParams.GetPropertyWithDefault("scale", 1.0f);
8282
ball.transform.localScale = new Vector3(scale, scale, scale);
8383
}
8484

UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ public class Ball3DHardAgent : Agent
66
[Header("Specific to Ball3DHard")]
77
public GameObject ball;
88
Rigidbody m_BallRb;
9-
ResetParameters m_ResetParams;
9+
IFloatProperties m_ResetParams;
1010

1111
public override void InitializeAgent()
1212
{
1313
m_BallRb = ball.GetComponent<Rigidbody>();
1414
var academy = FindObjectOfType<Academy>();
15-
m_ResetParams = academy.resetParameters;
15+
m_ResetParams = academy.FloatProperties;
1616
SetResetParameters();
1717
}
1818

@@ -65,8 +65,8 @@ public override void AgentReset()
6565
public void SetBall()
6666
{
6767
//Set the attributes of the ball by fetching the information from the academy
68-
m_BallRb.mass = m_ResetParams["mass"];
69-
var scale = m_ResetParams["scale"];
68+
m_BallRb.mass = m_ResetParams.GetPropertyWithDefault("mass", 1.0f);
69+
var scale = m_ResetParams.GetPropertyWithDefault("scale", 1.0f);
7070
ball.transform.localScale = new Vector3(scale, scale, scale);
7171
}
7272

UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void FixedUpdate()
9797

9898
void WaitTimeInference()
9999
{
100-
if (!m_Academy.GetIsInference())
100+
if (!m_Academy.IsCommunicatorOn)
101101
{
102102
RequestDecision();
103103
}

UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public class BouncerAgent : Agent
1313
int m_NumberJumps = 20;
1414
int m_JumpLeft = 20;
1515

16-
ResetParameters m_ResetParams;
16+
IFloatProperties m_ResetParams;
1717

1818
public override void InitializeAgent()
1919
{
2020
m_Rb = gameObject.GetComponent<Rigidbody>();
2121
m_LookDir = Vector3.zero;
2222

2323
var academy = FindObjectOfType<Academy>();
24-
m_ResetParams = academy.resetParameters;
24+
m_ResetParams = academy.FloatProperties;
2525

2626
SetResetParameters();
2727
}
@@ -126,7 +126,7 @@ void Update()
126126

127127
public void SetTargetScale()
128128
{
129-
var targetScale = m_ResetParams["target_scale"];
129+
var targetScale = m_ResetParams.GetPropertyWithDefault("target_scale", 1.0f);
130130
target.transform.localScale = new Vector3(targetScale, targetScale, targetScale);
131131
}
132132

UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorAgent.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,12 @@ public override void AgentOnDone()
277277

278278
public void SetLaserLengths()
279279
{
280-
m_LaserLength = m_MyAcademy.resetParameters.TryGetValue("laser_length", out m_LaserLength) ? m_LaserLength : 1.0f;
280+
m_LaserLength = m_MyAcademy.FloatProperties.GetPropertyWithDefault("laser_length", 1.0f);
281281
}
282282

283283
public void SetAgentScale()
284284
{
285-
float agentScale;
286-
agentScale = m_MyAcademy.resetParameters.TryGetValue("agent_scale", out agentScale) ? agentScale : 1.0f;
285+
float agentScale = m_MyAcademy.FloatProperties.GetPropertyWithDefault("agent_scale", 1.0f);
287286
gameObject.transform.localScale = new Vector3(agentScale, agentScale, agentScale);
288287
}
289288

0 commit comments

Comments
 (0)