Skip to content

Commit dd8b5fb

Browse files
Ruo-Ping DongChris Elion
andauthored
Team manager prototype (#4850)
* remove group id * very rough sketch for TeamManager interface * add team manager id to proto * team manager for hallway * add manager to hallway * send and process team manager id * remove print * small cleanup Co-authored-by: Chris Elion <[email protected]>
1 parent f391b35 commit dd8b5fb

File tree

20 files changed

+253
-35
lines changed

20 files changed

+253
-35
lines changed

Project/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayCollabAgent.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ public class HallwayCollabAgent : HallwayAgent
1515

1616
[HideInInspector]
1717
public int selection = 0;
18+
19+
public override void Initialize()
20+
{
21+
base.Initialize();
22+
if (isSpotter)
23+
{
24+
var teamManager = new HallwayTeamManager();
25+
SetTeamManager(teamManager);
26+
teammate.SetTeamManager(teamManager);
27+
}
28+
}
1829
public override void OnEpisodeBegin()
1930
{
2031
m_Message = -1;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using Unity.MLAgents;
3+
using Unity.MLAgents.Extensions.Teams;
4+
using Unity.MLAgents.Sensors;
5+
6+
public class HallwayTeamManager : BaseTeamManager
7+
{
8+
List<Agent> m_AgentList = new List<Agent> { };
9+
10+
11+
public override void RegisterAgent(Agent agent)
12+
{
13+
m_AgentList.Add(agent);
14+
}
15+
16+
public override void OnAgentDone(Agent agent, Agent.DoneReason doneReason, List<ISensor> sensors)
17+
{
18+
agent.SendDoneToTrainer();
19+
}
20+
21+
public override void AddTeamReward(float reward)
22+
{
23+
24+
}
25+
}

Project/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayTeamManager.cs.meta

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

Project/Assets/ML-Agents/Examples/Hallway/TFModels/HallwayCollab.onnx.meta

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

com.unity.ml-agents.extensions/Runtime/Teams.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using Unity.MLAgents;
3+
using Unity.MLAgents.Sensors;
4+
5+
namespace Unity.MLAgents.Extensions.Teams
6+
{
7+
public class BaseTeamManager : ITeamManager
8+
{
9+
readonly string m_Id = System.Guid.NewGuid().ToString();
10+
11+
public virtual void RegisterAgent(Agent agent)
12+
{
13+
throw new System.NotImplementedException();
14+
}
15+
16+
public virtual void OnAgentDone(Agent agent, Agent.DoneReason doneReason, List<ISensor> sensors)
17+
{
18+
// Possible implementation - save reference to Agent's IPolicy so that we can repeatedly
19+
// call IPolicy.RequestDecision on behalf of the Agent after it's dead
20+
// If so, we'll need dummy sensor impls with the same shape as the originals.
21+
throw new System.NotImplementedException();
22+
}
23+
24+
public virtual void AddTeamReward(float reward)
25+
{
26+
27+
}
28+
29+
public string GetId()
30+
{
31+
return m_Id;
32+
}
33+
34+
}
35+
}

com.unity.ml-agents.extensions/Runtime/Teams/BaseTeamManager.cs.meta

Lines changed: 3 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/Editor/BehaviorParametersEditor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ internal class BehaviorParametersEditor : UnityEditor.Editor
2626
const string k_InferenceDeviceName = "m_InferenceDevice";
2727
const string k_BehaviorTypeName = "m_BehaviorType";
2828
const string k_TeamIdName = "TeamId";
29-
const string k_GroupIdName = "GroupId";
3029
const string k_UseChildSensorsName = "m_UseChildSensors";
3130
const string k_ObservableAttributeHandlingName = "m_ObservableAttributeHandling";
3231

@@ -68,7 +67,6 @@ public override void OnInspectorGUI()
6867
}
6968
needPolicyUpdate = needPolicyUpdate || EditorGUI.EndChangeCheck();
7069

71-
EditorGUILayout.PropertyField(so.FindProperty(k_GroupIdName));
7270
EditorGUILayout.PropertyField(so.FindProperty(k_TeamIdName));
7371
EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties());
7472
{

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ internal struct AgentInfo
5050
/// </summary>
5151
public int episodeId;
5252

53+
/// <summary>
54+
/// Team Manager identifier.
55+
/// </summary>
56+
public string teamManagerId;
57+
5358
public void ClearActions()
5459
{
5560
storedActions.Clear();
@@ -312,6 +317,8 @@ internal struct AgentParameters
312317
/// </summary>
313318
float[] m_LegacyActionCache;
314319

320+
private ITeamManager m_TeamManager;
321+
315322
/// <summary>
316323
/// Called when the attached [GameObject] becomes enabled and active.
317324
/// [GameObject]: https://docs.unity3d.com/Manual/GameObjects.html
@@ -443,6 +450,11 @@ public void LazyInitialize()
443450
new int[m_ActuatorManager.NumDiscreteActions]
444451
);
445452

453+
if (m_TeamManager != null)
454+
{
455+
m_Info.teamManagerId = m_TeamManager.GetId();
456+
}
457+
446458
// The first time the Academy resets, all Agents in the scene will be
447459
// forced to reset through the <see cref="AgentForceReset"/> event.
448460
// To avoid the Agent resetting twice, the Agents will not begin their
@@ -459,7 +471,7 @@ public void LazyInitialize()
459471
/// <summary>
460472
/// The reason that the Agent has been set to "done".
461473
/// </summary>
462-
enum DoneReason
474+
public enum DoneReason
463475
{
464476
/// <summary>
465477
/// The episode was ended manually by calling <see cref="EndEpisode"/>.
@@ -535,9 +547,17 @@ void NotifyAgentDone(DoneReason doneReason)
535547
}
536548
}
537549
// Request the last decision with no callbacks
538-
// We request a decision so Python knows the Agent is done immediately
539-
m_Brain?.RequestDecision(m_Info, sensors);
540-
ResetSensors();
550+
if (m_TeamManager != null)
551+
{
552+
// Send final observations to TeamManager if it exists.
553+
// The TeamManager is responsible to keeping track of the Agent after it's
554+
// done, including propagating any "posthumous" rewards.
555+
m_TeamManager.OnAgentDone(this, doneReason, sensors);
556+
}
557+
else
558+
{
559+
SendDoneToTrainer();
560+
}
541561

542562
// We also have to write any to any DemonstationStores so that they get the "done" flag.
543563
foreach (var demoWriter in DemonstrationWriters)
@@ -560,6 +580,13 @@ void NotifyAgentDone(DoneReason doneReason)
560580
m_Info.storedActions.Clear();
561581
}
562582

583+
public void SendDoneToTrainer()
584+
{
585+
// We request a decision so Python knows the Agent is done immediately
586+
m_Brain?.RequestDecision(m_Info, sensors);
587+
ResetSensors();
588+
}
589+
563590
/// <summary>
564591
/// Updates the Model assigned to this Agent instance.
565592
/// </summary>
@@ -1344,5 +1371,12 @@ void DecideAction()
13441371
m_Info.CopyActions(actions);
13451372
m_ActuatorManager.UpdateActions(actions);
13461373
}
1374+
1375+
public void SetTeamManager(ITeamManager teamManager)
1376+
{
1377+
m_TeamManager = teamManager;
1378+
m_Info.teamManagerId = teamManager?.GetId();
1379+
teamManager?.RegisterAgent(this);
1380+
}
13471381
}
13481382
}

com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public static AgentInfoProto ToAgentInfoProto(this AgentInfo ai)
6767
agentInfoProto.ActionMask.AddRange(ai.discreteActionMasks);
6868
}
6969

70+
if (ai.teamManagerId != null)
71+
{
72+
agentInfoProto.TeamManagerId = ai.teamManagerId;
73+
}
74+
7075
return agentInfoProto;
7176
}
7277

0 commit comments

Comments
 (0)