浏览代码

add maxstep to teammanager and hook to academy

/develop/superpush/int
Ruo-Ping Dong 3 年前
当前提交
ab4ec610
共有 4 个文件被更改,包括 164 次插入1 次删除
  1. 148
      com.unity.ml-agents.extensions/Runtime/Teams/BaseTeamManager.cs
  2. 3
      com.unity.ml-agents/Runtime/Academy.cs
  3. 2
      com.unity.ml-agents/Runtime/Actuators/ITeamManager.cs
  4. 12
      com.unity.ml-agents/Runtime/Agent.cs

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


using System.Collections.Generic;
int m_StepCount;
int m_TeamMaxStep;
List<Agent> m_Agents = new List<Agent> { };
public virtual void RegisterAgent(Agent agent) { }
public BaseTeamManager()
{
Academy.Instance.TeamManagerStep += _ManagerStep;
}
void _ManagerStep()
{
m_StepCount += 1;
if ((m_StepCount >= m_TeamMaxStep) && (m_TeamMaxStep > 0))
{
foreach (var agent in m_Agents)
{
// if (agent.gameObject.activeSelf)
if (agent.gameObject.activeInHierarchy)
{
agent.EpisodeInterrupted();
}
}
Reset();
}
}
/// <summary>
/// Register the agent to the TeamManager.
/// Registered agents will be able to receive team rewards from the TeamManager.
/// All agents in the same training area should be added to the same TeamManager.
/// </summary>
public virtual void RegisterAgent(Agent agent)
{
if (!m_Agents.Contains(agent))
{
m_Agents.Add(agent);
}
}
/// <summary>
/// Remove the agent from the TeamManager.
/// </summary>
public virtual void RemoveAgent(Agent agent)
{
if (m_Agents.Contains(agent))
{
m_Agents.Remove(agent);
}
}
/// <summary>
/// Get the ID of the TeamManager.
/// </summary>
/// <returns>
/// TeamManager ID.
/// </returns>
}
/// <summary>
/// Get list of all agents registered to this TeamManager.
/// </summary>
/// <returns>
/// List of agents belongs to the TeamManager.
/// </returns>
public List<Agent> GetTeammates()
{
return m_Agents;
}
/// <summary>
/// Add team reward for all agents under this Teammanager.
/// Disabled agent will not receive this reward.
/// </summary>
public void AddTeamReward(float reward)
{
foreach (var agent in m_Agents)
{
if (agent.gameObject.activeInHierarchy)
{
agent.AddTeamReward(reward);
}
}
}
/// <summary>
/// Set team reward for all agents under this Teammanager.
/// Disabled agent will not receive this reward.
/// </summary>
public void SetTeamReward(float reward)
{
foreach (var agent in m_Agents)
{
if (agent.gameObject.activeInHierarchy)
{
agent.SetTeamReward(reward);
}
}
}
/// <summary>
/// Returns the current step counter (within the current episode).
/// </summary>
/// <returns>
/// Current step count.
/// </returns>
public int StepCount
{
get { return m_StepCount; }
}
public int TeamMaxStep
{
get { return m_TeamMaxStep; }
}
public void SetTeamMaxStep(int maxStep)
{
m_TeamMaxStep = maxStep;
}
/// <summary>
/// End Episode for all agents under this TeamManager.
/// </summary>
public void EndTeamEpisode()
{
foreach (var agent in m_Agents)
{
if (agent.gameObject.activeInHierarchy)
{
agent.EndEpisode();
}
}
Reset();
}
/// <summary>
/// End Episode for all agents under this TeamManager.
/// </summary>
public virtual void OnTeamEpisodeBegin()
{
}
void Reset()
{
m_StepCount = 0;
OnTeamEpisodeBegin();
}
}
}

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


// This will mark the Agent as Done if it has reached its maxSteps.
internal event Action AgentIncrementStep;
internal event Action TeamManagerStep;
/// <summary>
/// Signals to all of the <see cref="Agent"/>s that their step is about to begin.

{
AgentAct?.Invoke();
}
TeamManagerStep?.Invoke();
}
}

2
com.unity.ml-agents/Runtime/Actuators/ITeamManager.cs


int GetId();
void RegisterAgent(Agent agent);
void RemoveAgent(Agent agent);
}
}

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


m_Initialized = false;
}
void OnDestroy()
{
if (m_TeamManager != null)
{
m_TeamManager.RemoveAgent(this);
}
}
void NotifyAgentDone(DoneReason doneReason)
{
if (m_Info.done)

public void SetTeamManager(ITeamManager teamManager)
{
if (m_TeamManager != null)
{
m_TeamManager.RemoveAgent(this);
}
m_TeamManager = teamManager;
teamManager?.RegisterAgent(this);
}
正在加载...
取消
保存