|
|
|
|
|
|
/// Responsible for communication with External using gRPC.
|
|
|
|
public class RpcCommunicator : ICommunicator |
|
|
|
{ |
|
|
|
public struct IdCallbackPair |
|
|
|
{ |
|
|
|
public int AgentId; |
|
|
|
public Action<AgentAction> Callback; |
|
|
|
} |
|
|
|
public event QuitCommandHandler QuitCommandReceived; |
|
|
|
public event ResetCommandHandler ResetCommandReceived; |
|
|
|
|
|
|
|
|
|
|
/// The default number of agents in the scene
|
|
|
|
const int k_NumAgents = 32; |
|
|
|
|
|
|
|
Dictionary<string, List<IdCallbackPair>> m_ActionCallbacks = new Dictionary<string, List<IdCallbackPair>>(); |
|
|
|
Dictionary<string, List<int>> m_OrderedAgentsRequestingDecisions = new Dictionary<string, List<int>>(); |
|
|
|
Dictionary<string, Dictionary<int, AgentAction>> m_LastActionsReceived = |
|
|
|
new Dictionary<string, Dictionary<int, AgentAction>>(); |
|
|
|
Dictionary<string, Dictionary<int, float[]>> m_LastActionsReceived = |
|
|
|
new Dictionary<string, Dictionary<int, float[]>>(); |
|
|
|
|
|
|
|
// Brains that we have sent over the communicator with agents.
|
|
|
|
HashSet<string> m_SentBrainKeys = new HashSet<string>(); |
|
|
|
|
|
|
} |
|
|
|
case CommandProto.Reset: |
|
|
|
{ |
|
|
|
foreach (var brainName in m_ActionCallbacks.Keys) |
|
|
|
foreach (var brainName in m_OrderedAgentsRequestingDecisions.Keys) |
|
|
|
m_ActionCallbacks[brainName].Clear(); |
|
|
|
m_OrderedAgentsRequestingDecisions[brainName].Clear(); |
|
|
|
} |
|
|
|
ResetCommandReceived?.Invoke(); |
|
|
|
return; |
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sends the observations of one Agent.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="brainKey">Batch Key.</param>
|
|
|
|
/// <param name="behaviorName">Batch Key.</param>
|
|
|
|
public void PutObservations(string brainKey, AgentInfo info, List<ISensor> sensors, Action<AgentAction> action) |
|
|
|
public void PutObservations(string behaviorName, AgentInfo info, List<ISensor> sensors) |
|
|
|
if (!m_SensorShapeValidators.ContainsKey(brainKey)) |
|
|
|
if (!m_SensorShapeValidators.ContainsKey(behaviorName)) |
|
|
|
m_SensorShapeValidators[brainKey] = new SensorShapeValidator(); |
|
|
|
m_SensorShapeValidators[behaviorName] = new SensorShapeValidator(); |
|
|
|
m_SensorShapeValidators[brainKey].ValidateSensors(sensors); |
|
|
|
m_SensorShapeValidators[behaviorName].ValidateSensors(sensors); |
|
|
|
#endif
|
|
|
|
|
|
|
|
using (TimerStack.Instance.Scoped("AgentInfo.ToProto")) |
|
|
|
|
|
|
agentInfoProto.Observations.Add(obsProto); |
|
|
|
} |
|
|
|
} |
|
|
|
m_CurrentUnityRlOutput.AgentInfos[brainKey].Value.Add(agentInfoProto); |
|
|
|
m_CurrentUnityRlOutput.AgentInfos[behaviorName].Value.Add(agentInfoProto); |
|
|
|
if (!m_ActionCallbacks.ContainsKey(brainKey)) |
|
|
|
if (!m_OrderedAgentsRequestingDecisions.ContainsKey(behaviorName)) |
|
|
|
{ |
|
|
|
m_OrderedAgentsRequestingDecisions[behaviorName] = new List<int>(); |
|
|
|
} |
|
|
|
m_OrderedAgentsRequestingDecisions[behaviorName].Add(info.episodeId); |
|
|
|
if (!m_LastActionsReceived.ContainsKey(behaviorName)) |
|
|
|
{ |
|
|
|
m_LastActionsReceived[behaviorName] = new Dictionary<int, float[]>(); |
|
|
|
} |
|
|
|
m_LastActionsReceived[behaviorName][info.episodeId] = null; |
|
|
|
if (info.done) |
|
|
|
m_ActionCallbacks[brainKey] = new List<IdCallbackPair>(); |
|
|
|
m_LastActionsReceived[behaviorName].Remove(info.episodeId); |
|
|
|
m_ActionCallbacks[brainKey].Add(new IdCallbackPair { AgentId = info.episodeId, Callback = action }); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
|
|
|
UpdateEnvironmentWithInput(rlInput); |
|
|
|
|
|
|
|
m_LastActionsReceived.Clear(); |
|
|
|
if (!m_ActionCallbacks[brainName].Any()) |
|
|
|
if (!m_OrderedAgentsRequestingDecisions[brainName].Any()) |
|
|
|
{ |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var agentActions = rlInput.AgentActions[brainName].ToAgentActionList(); |
|
|
|
var numAgents = m_ActionCallbacks[brainName].Count; |
|
|
|
var agentActionDict = new Dictionary<int, AgentAction>(numAgents); |
|
|
|
m_LastActionsReceived[brainName] = agentActionDict; |
|
|
|
var numAgents = m_OrderedAgentsRequestingDecisions[brainName].Count; |
|
|
|
var agentId = m_ActionCallbacks[brainName][i].AgentId; |
|
|
|
agentActionDict[agentId] = agentAction; |
|
|
|
m_ActionCallbacks[brainName][i].Callback.Invoke(agentAction); |
|
|
|
var agentId = m_OrderedAgentsRequestingDecisions[brainName][i]; |
|
|
|
if (m_LastActionsReceived[brainName].ContainsKey(agentId)) |
|
|
|
{ |
|
|
|
m_LastActionsReceived[brainName][agentId] = agentAction.vectorActions; |
|
|
|
} |
|
|
|
foreach (var brainName in m_ActionCallbacks.Keys) |
|
|
|
foreach (var brainName in m_OrderedAgentsRequestingDecisions.Keys) |
|
|
|
m_ActionCallbacks[brainName].Clear(); |
|
|
|
m_OrderedAgentsRequestingDecisions[brainName].Clear(); |
|
|
|
public Dictionary<int, AgentAction> GetActions(string key) |
|
|
|
public float[] GetActions(string behaviorName, int agentId) |
|
|
|
return m_LastActionsReceived[key]; |
|
|
|
if (m_LastActionsReceived.ContainsKey(behaviorName)) |
|
|
|
{ |
|
|
|
if (m_LastActionsReceived[behaviorName].ContainsKey(agentId)) |
|
|
|
{ |
|
|
|
return m_LastActionsReceived[behaviorName][agentId]; |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Wraps the UnityOuptut into a message with the appropriate status.
|
|
|
|
/// Wraps the UnityOutput into a message with the appropriate status.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The UnityMessage corresponding.</returns>
|
|
|
|
/// <param name="content">The UnityOutput to be wrapped.</param>
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
void CacheBrainParameters(string brainKey, BrainParameters brainParameters) |
|
|
|
void CacheBrainParameters(string behaviorName, BrainParameters brainParameters) |
|
|
|
if (m_SentBrainKeys.Contains(brainKey)) |
|
|
|
if (m_SentBrainKeys.Contains(behaviorName)) |
|
|
|
m_UnsentBrainKeys[brainKey] = brainParameters; |
|
|
|
m_UnsentBrainKeys[behaviorName] = brainParameters; |
|
|
|
foreach (var brainKey in m_UnsentBrainKeys.Keys) |
|
|
|
foreach (var behaviorName in m_UnsentBrainKeys.Keys) |
|
|
|
if (m_CurrentUnityRlOutput.AgentInfos.ContainsKey(brainKey)) |
|
|
|
if (m_CurrentUnityRlOutput.AgentInfos.ContainsKey(behaviorName)) |
|
|
|
{ |
|
|
|
if (output == null) |
|
|
|
{ |
|
|
|
|
|
|
var brainParameters = m_UnsentBrainKeys[brainKey]; |
|
|
|
output.BrainParameters.Add(brainParameters.ToProto(brainKey, true)); |
|
|
|
var brainParameters = m_UnsentBrainKeys[behaviorName]; |
|
|
|
output.BrainParameters.Add(brainParameters.ToProto(behaviorName, true)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|