浏览代码

use delegate to avoid agent-manager cyclic reference

/develop/superpush/int
Ruo-Ping Dong 4 年前
当前提交
596a540c
共有 2 个文件被更改,包括 22 次插入15 次删除
  1. 7
      com.unity.ml-agents.extensions/Runtime/Teams/BaseTeamManager.cs
  2. 30
      com.unity.ml-agents/Runtime/Agent.cs

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


public void Dispose()
{
Academy.Instance.PostAgentAct -= _ManagerStep;
foreach (var agent in m_Agents)
{
agent.UnregisterFromTeamManager -= UnregisterAgent;
}
}
void _ManagerStep()

if (!m_Agents.Contains(agent))
{
m_Agents.Add(agent);
agent.UnregisterFromTeamManager += UnregisterAgent;
agent.SetTeamManager(this);
}
}

if (m_Agents.Contains(agent))
{
m_Agents.Remove(agent);
agent.UnregisterFromTeamManager -= UnregisterAgent;
}
}

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


/// </summary>
float[] m_LegacyActionCache;
ITeamManager m_TeamManager;
/// <summary>
/// This is used to avoid allocation of a float array during legacy calls to Heuristic.
/// </summary>
float[] m_LegacyHeuristicCache;
int m_TeamManagerID;
internal event Action<Agent> UnregisterFromTeamManager;
/// <summary>
/// Called when the attached [GameObject] becomes enabled and active.

new int[m_ActuatorManager.NumDiscreteActions]
);
m_Info.teamManagerId = m_TeamManager == null ? 0 : m_TeamManager.GetId();
m_Info.teamManagerId = m_TeamManagerID;
// The first time the Academy resets, all Agents in the scene will be
// forced to reset through the <see cref="AgentForceReset"/> event.

void OnDestroy()
{
if (m_TeamManager != null)
{
m_TeamManager.UnregisterAgent(this);
}
UnregisterFromTeamManager?.Invoke(this);
}
void NotifyAgentDone(DoneReason doneReason)

m_Info.teamReward = m_TeamReward;
m_Info.done = true;
m_Info.maxStepReached = doneReason == DoneReason.MaxStepReached;
m_Info.teamManagerId = m_TeamManager == null ? 0 : m_TeamManager.GetId();
m_Info.teamManagerId = m_TeamManagerID;
if (collectObservationsSensor != null)
{
// Make sure the latest observations are being passed to training.

m_Info.done = false;
m_Info.maxStepReached = false;
m_Info.episodeId = m_EpisodeId;
m_Info.teamManagerId = m_TeamManager == null ? 0 : m_TeamManager.GetId();
m_Info.teamManagerId = m_TeamManagerID;
using (TimerStack.Instance.Scoped("RequestDecision"))
{

m_ActuatorManager.UpdateActions(actions);
}
public void SetTeamManager(ITeamManager teamManager)
internal void SetTeamManager(ITeamManager teamManager)
if (m_TeamManager != null)
{
m_TeamManager.UnregisterAgent(this);
}
m_TeamManager = teamManager;
teamManager?.RegisterAgent(this);
UnregisterFromTeamManager?.Invoke(this);
m_TeamManagerID = teamManager.GetId();
}
}
}
正在加载...
取消
保存