using System.Collections.Generic; using System; using UnityEngine; namespace Unity.MLAgents.SideChannels { internal class AgentParametersChannel : SideChannel { Dictionary> m_Parameters = new Dictionary>(); const string k_EnvParamsId = "534c891e-810f-11ea-a9d0-822485860401"; /// /// Initializes the side channel. The constructor is internal because only one instance is /// supported at a time, and is created by the Academy. /// internal AgentParametersChannel() { ChannelId = new Guid(k_EnvParamsId); } /// protected override void OnMessageReceived(IncomingMessage msg) { var episodeId = msg.ReadInt32(); var key = msg.ReadString(); var value = msg.ReadFloat32(); if(!m_Parameters.ContainsKey(episodeId)) { m_Parameters[episodeId] = new Dictionary(); } m_Parameters[episodeId][key] = value; } /// /// Returns the parameter value associated with the provided key. Returns the default /// value if one doesn't exist. /// /// Parameter key. /// Default value to return. /// public float GetWithDefault(int episodeId, string key, float defaultValue) { float value = defaultValue; bool hasKey = false; Dictionary agent_dict; if(m_Parameters.TryGetValue(episodeId, out agent_dict)) { agent_dict.TryGetValue(key, out value); } return value; } /// /// Returns all parameter keys that have a registered value. /// /// public IList ListParameters(int episodeId) { Dictionary agent_dict; m_Parameters.TryGetValue(episodeId, out agent_dict); return new List(agent_dict.Keys); } } }