AgentInfoConvertor(AgentInfo info) { var agentInfoProto = new CommunicatorObjects.AgentInfoProto { StackedVectorObservation = { info.stackedVectorObservation }, StoredVectorActions = { info.storedVectorActions }, StoredTextActions = info.storedTextActions, TextObservation = info.textObservation, Reward = info.reward, MaxStepReached = info.maxStepReached, Done = info.done, Id = info.id, }; if (info.memories != null) { agentInfoProto.Memories.Add(info.memories); } if (info.actionMasks != null) { agentInfoProto.ActionMask.AddRange(info.actionMasks); } foreach (Texture2D obs in info.visualObservations) { agentInfoProto.VisualObservations.Add( ByteString.CopyFrom(obs.EncodeToPNG()) ); } return(agentInfoProto); }
/// <summary> /// Converts a AgentInfo to a protobuffer generated AgentInfoProto /// </summary> /// <returns>The protobuf verison of the AgentInfo.</returns> /// <param name="info">The AgentInfo to convert.</param> public CommunicatorObjects.AgentInfoProto ToProto() { var agentInfoProto = new CommunicatorObjects.AgentInfoProto { StackedVectorObservation = { stackedVectorObservation }, StoredVectorActions = { storedVectorActions }, StoredTextActions = storedTextActions, TextObservation = textObservation, Reward = reward, MaxStepReached = maxStepReached, Done = done, Id = id, CustomObservation = customObservation }; if (memories != null) { agentInfoProto.Memories.Add(memories); } if (actionMasks != null) { agentInfoProto.ActionMask.AddRange(actionMasks); } foreach (Texture2D obs in visualObservations) { agentInfoProto.VisualObservations.Add( ByteString.CopyFrom(obs.EncodeToPNG()) ); } return(agentInfoProto); }
/// <summary> /// Sends the brain info. If at least one brain has an agent in need of /// a decision or if the academy is done, the data is sent via /// Communicator. Else, a new step is realized. The data can only be /// sent once all the brains that subscribed to the batcher have tried /// to send information. /// </summary> /// <param name="brainKey">Brain key.</param> /// <param name="agentInfo">Agent info.</param> public void SendBrainInfo( string brainKey, Dictionary <Agent, AgentInfo> agentInfo) { // If no communicator is initialized, the Batcher will not transmit // BrainInfo if (m_communicator == null) { return; } // The brain tried called GiveBrainInfo, update m_hasQueried m_hasQueried[brainKey] = true; // Populate the currentAgents dictionary m_currentAgents[brainKey].Clear(); foreach (Agent agent in agentInfo.Keys) { m_currentAgents[brainKey].Add(agent); } // If at least one agent has data to send, then append data to // the message and update hasSentState if (m_currentAgents[brainKey].Count > 0) { foreach (Agent agent in m_currentAgents[brainKey]) { CommunicatorObjects.AgentInfoProto agentInfoProto = agentInfo[agent].ToProto(); m_currentUnityRLOutput.AgentInfos[brainKey].Value.Add(agentInfoProto); // Avoid visual obs memory leak. This should be called AFTER we are done with the visual obs. // e.g. after recording them to demo and using them for inference. agentInfo[agent].ClearVisualObs(); } m_hasData[brainKey] = true; } // If any agent needs to send data, then the whole message // must be sent if (m_hasQueried.Values.All(x => x)) { if (m_hasData.Values.Any(x => x) || m_academyDone) { m_currentUnityRLOutput.GlobalDone = m_academyDone; SendBatchedMessageHelper(); } // The message was just sent so we must reset hasSentState and // triedSendState foreach (string k in m_currentAgents.Keys) { m_hasData[k] = false; m_hasQueried[k] = false; } } }