using System; using System.Collections.Generic; namespace Unity.MLAgents.Extensions.Teams { public class BaseTeamManager : ITeamManager, IDisposable { int m_StepCount; int m_TeamMaxStep; readonly int m_Id = TeamManagerIdCounter.GetTeamManagerId(); List m_Agents = new List { }; public BaseTeamManager() { Academy.Instance.PostAgentAct += _ManagerStep; } public void Dispose() { Academy.Instance.PostAgentAct -= _ManagerStep; while (m_Agents.Count > 0) { UnregisterAgent(m_Agents[0]); } } void _ManagerStep() { m_StepCount += 1; if ((m_StepCount >= m_TeamMaxStep) && (m_TeamMaxStep > 0)) { foreach (var agent in m_Agents) { if (agent.enabled) { agent.EpisodeInterrupted(); } } Reset(); } } /// /// Register the agent to the TeamManager. /// Registered agents will be able to receive team rewards from the TeamManager /// and share observations during training. /// public virtual void RegisterAgent(Agent agent) { if (!m_Agents.Contains(agent)) { agent.SetTeamManager(this); m_Agents.Add(agent); agent.UnregisterFromTeamManager += UnregisterAgent; } } /// /// Remove the agent from the TeamManager. /// public virtual void UnregisterAgent(Agent agent) { if (m_Agents.Contains(agent)) { m_Agents.Remove(agent); agent.UnregisterFromTeamManager -= UnregisterAgent; } } /// /// Get the ID of the TeamManager. /// /// /// TeamManager ID. /// public int GetId() { return m_Id; } /// /// Get list of all agents registered to this TeamManager. /// /// /// List of agents belongs to the TeamManager. /// public List GetRegisteredAgents() { return m_Agents; } /// /// Add team reward for all agents under this Teammanager. /// Disabled agent will not receive this reward. /// public void AddTeamReward(float reward) { foreach (var agent in m_Agents) { if (agent.enabled) { agent.AddTeamReward(reward); } } } /// /// Set team reward for all agents under this Teammanager. /// Disabled agent will not receive this reward. /// public void SetTeamReward(float reward) { foreach (var agent in m_Agents) { if (agent.enabled) { agent.SetTeamReward(reward); } } } /// /// Returns the current step counter (within the current episode). /// /// /// Current step count. /// public int StepCount { get { return m_StepCount; } } public int TeamMaxStep { get { return m_TeamMaxStep; } } public void SetTeamMaxStep(int maxStep) { m_TeamMaxStep = maxStep; } /// /// End Episode for all agents under this TeamManager. /// public void EndTeamEpisode() { foreach (var agent in m_Agents) { if (agent.enabled) { agent.EndEpisode(); } } Reset(); } /// /// End Episode for all agents under this TeamManager. /// public virtual void OnTeamEpisodeBegin() { } void Reset() { m_StepCount = 0; OnTeamEpisodeBegin(); } } }