Ruo-Ping Dong
4 年前
当前提交
918c2dcd
共有 21 个文件被更改,包括 290 次插入 和 133 次删除
-
52com.unity.ml-agents/Runtime/Agent.cs
-
4com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs
-
74com.unity.ml-agents/Runtime/Grpc/CommunicatorObjects/AgentInfo.cs
-
2com.unity.ml-agents/Runtime/IMultiAgentGroup.cs
-
2com.unity.ml-agents/Runtime/MultiAgentGroupIdCounter.cs.meta
-
44ml-agents-envs/mlagents_envs/base_env.py
-
8ml-agents-envs/mlagents_envs/communicator_objects/agent_info_pb2.py
-
12ml-agents-envs/mlagents_envs/communicator_objects/agent_info_pb2.pyi
-
28ml-agents-envs/mlagents_envs/rpc_utils.py
-
4protobuf-definitions/proto/mlagents_envs/communicator_objects/agent_info.proto
-
2com.unity.ml-agents.extensions/Runtime/MultiAgent/BaseMultiAgentGroup.cs.meta
-
13com.unity.ml-agents/Runtime/MultiAgentGroupIdCounter.cs
-
165com.unity.ml-agents.extensions/Runtime/MultiAgent/BaseMultiAgentGroup.cs
-
13com.unity.ml-agents/Runtime/TeamManagerIdCounter.cs
-
0/com.unity.ml-agents.extensions/Runtime/MultiAgent.meta
-
0/com.unity.ml-agents/Runtime/IMultiAgentGroup.cs
-
0/com.unity.ml-agents/Runtime/MultiAgentGroupIdCounter.cs.meta
-
0/com.unity.ml-agents.extensions/Runtime/MultiAgent/BaseMultiAgentGroup.cs.meta
-
0/com.unity.ml-agents/Runtime/IMultiAgentGroup.cs.meta
|
|||
using System.Threading; |
|||
|
|||
namespace Unity.MLAgents |
|||
{ |
|||
internal static class MultiAgentGroupIdCounter |
|||
{ |
|||
static int s_Counter; |
|||
public static int GetGroupId() |
|||
{ |
|||
return Interlocked.Increment(ref s_Counter); ; |
|||
} |
|||
} |
|||
} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
namespace Unity.MLAgents.Extensions.MultiAgent |
|||
{ |
|||
public class BaseMultiAgentGroup : IMultiAgentGroup, IDisposable |
|||
{ |
|||
int m_StepCount; |
|||
int m_GroupMaxStep; |
|||
readonly int m_Id = MultiAgentGroupIdCounter.GetGroupId(); |
|||
List<Agent> m_Agents = new List<Agent> { }; |
|||
|
|||
|
|||
public BaseMultiAgentGroup() |
|||
{ |
|||
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_GroupMaxStep) && (m_GroupMaxStep > 0)) |
|||
{ |
|||
foreach (var agent in m_Agents) |
|||
{ |
|||
if (agent.enabled) |
|||
{ |
|||
agent.EpisodeInterrupted(); |
|||
} |
|||
} |
|||
Reset(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Register the agent to the MultiAgentGroup.
|
|||
/// Registered agents will be able to receive group rewards from the MultiAgentGroup
|
|||
/// and share observations during training.
|
|||
/// </summary>
|
|||
public virtual void RegisterAgent(Agent agent) |
|||
{ |
|||
if (!m_Agents.Contains(agent)) |
|||
{ |
|||
agent.SetMultiAgentGroup(this); |
|||
m_Agents.Add(agent); |
|||
agent.UnregisterFromGroup += UnregisterAgent; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Remove the agent from the MultiAgentGroup.
|
|||
/// </summary>
|
|||
public virtual void UnregisterAgent(Agent agent) |
|||
{ |
|||
if (m_Agents.Contains(agent)) |
|||
{ |
|||
m_Agents.Remove(agent); |
|||
agent.UnregisterFromGroup -= UnregisterAgent; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get the ID of the MultiAgentGroup.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// MultiAgentGroup ID.
|
|||
/// </returns>
|
|||
public int GetId() |
|||
{ |
|||
return m_Id; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get list of all agents registered to this MultiAgentGroup.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// List of agents belongs to the MultiAgentGroup.
|
|||
/// </returns>
|
|||
public List<Agent> GetRegisteredAgents() |
|||
{ |
|||
return m_Agents; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Add group reward for all agents under this MultiAgentGroup.
|
|||
/// Disabled agent will not receive this reward.
|
|||
/// </summary>
|
|||
public void AddGroupReward(float reward) |
|||
{ |
|||
foreach (var agent in m_Agents) |
|||
{ |
|||
if (agent.enabled) |
|||
{ |
|||
agent.AddGroupReward(reward); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Set group reward for all agents under this MultiAgentGroup.
|
|||
/// Disabled agent will not receive this reward.
|
|||
/// </summary>
|
|||
public void SetGroupReward(float reward) |
|||
{ |
|||
foreach (var agent in m_Agents) |
|||
{ |
|||
if (agent.enabled) |
|||
{ |
|||
agent.SetGroupReward(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 GroupMaxStep |
|||
{ |
|||
get { return m_GroupMaxStep; } |
|||
} |
|||
|
|||
public void SetGroupMaxStep(int maxStep) |
|||
{ |
|||
m_GroupMaxStep = maxStep; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// End Episode for all agents under this MultiAgentGroup.
|
|||
/// </summary>
|
|||
public void EndGroupEpisode() |
|||
{ |
|||
foreach (var agent in m_Agents) |
|||
{ |
|||
if (agent.enabled) |
|||
{ |
|||
agent.EndEpisode(); |
|||
} |
|||
} |
|||
Reset(); |
|||
} |
|||
|
|||
void Reset() |
|||
{ |
|||
m_StepCount = 0; |
|||
} |
|||
} |
|||
} |
|
|||
using System.Threading; |
|||
|
|||
namespace Unity.MLAgents |
|||
{ |
|||
internal static class TeamManagerIdCounter |
|||
{ |
|||
static int s_Counter; |
|||
public static int GetTeamManagerId() |
|||
{ |
|||
return Interlocked.Increment(ref s_Counter); ; |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue