using Barracuda; using System; using UnityEngine; using UnityEngine.Serialization; namespace MLAgents.Policies { /// /// Defines what type of behavior the Agent will be using /// [Serializable] public enum BehaviorType { /// /// The Agent will use the remote process for decision making. /// if unavailable, will use inference and if no model is provided, will use /// the heuristic. /// Default, /// /// The Agent will always use its heuristic /// HeuristicOnly, /// /// The Agent will always use inference with the provided /// neural network model. /// InferenceOnly } /// /// The Factory to generate policies. /// [AddComponentMenu("ML Agents/Behavior Parameters", (int)MenuGroup.Default)] public class BehaviorParameters : MonoBehaviour { [HideInInspector, SerializeField] BrainParameters m_BrainParameters = new BrainParameters(); /// /// The associated for this behavior. /// public BrainParameters brainParameters { get { return m_BrainParameters; } internal set { m_BrainParameters = value; } } [HideInInspector, SerializeField] NNModel m_Model; /// /// The neural network model used when in inference mode. /// This should not be set at runtime; use /// to set it instead. /// public NNModel model { get { return m_Model; } set { m_Model = value; UpdateAgentPolicy(); } } [HideInInspector, SerializeField] InferenceDevice m_InferenceDevice; /// /// How inference is performed for this Agent's model. /// This should not be set at runtime; use /// to set it instead. /// public InferenceDevice inferenceDevice { get { return m_InferenceDevice; } set { m_InferenceDevice = value; UpdateAgentPolicy();} } [HideInInspector, SerializeField] BehaviorType m_BehaviorType; /// /// The BehaviorType for the Agent. /// public BehaviorType behaviorType { get { return m_BehaviorType; } set { m_BehaviorType = value; UpdateAgentPolicy(); } } [HideInInspector, SerializeField] string m_BehaviorName = "My Behavior"; /// /// The name of this behavior, which is used as a base name. See /// for the full name. /// This should not be set at runtime; use /// to set it instead. /// public string behaviorName { get { return m_BehaviorName; } set { m_BehaviorName = value; UpdateAgentPolicy(); } } /// /// The team ID for this behavior. /// [HideInInspector, SerializeField, FormerlySerializedAs("m_TeamID")] public int TeamId; // TODO properties here instead of Agent [FormerlySerializedAs("m_useChildSensors")] [HideInInspector] [SerializeField] [Tooltip("Use all Sensor components attached to child GameObjects of this Agent.")] bool m_UseChildSensors = true; /// /// Whether or not to use all the sensor components attached to child GameObjects of the agent. /// Note that changing this after the Agent has been initialized will not have any effect. /// public bool useChildSensors { get { return m_UseChildSensors; } set { m_UseChildSensors = value; } } /// /// Returns the behavior name, concatenated with any other metadata (i.e. team id). /// public string fullyQualifiedBehaviorName { get { return m_BehaviorName + "?team=" + TeamId; } } internal IPolicy GeneratePolicy(Func heuristic) { switch (m_BehaviorType) { case BehaviorType.HeuristicOnly: return new HeuristicPolicy(heuristic); case BehaviorType.InferenceOnly: { if (m_Model == null) { var behaviorType = BehaviorType.InferenceOnly.ToString(); throw new UnityAgentsException( $"Can't use Behavior Type {behaviorType} without a model. " + "Either assign a model, or change to a different Behavior Type." ); } return new BarracudaPolicy(m_BrainParameters, m_Model, m_InferenceDevice); } case BehaviorType.Default: if (Academy.Instance.IsCommunicatorOn) { return new RemotePolicy(m_BrainParameters, fullyQualifiedBehaviorName); } if (m_Model != null) { return new BarracudaPolicy(m_BrainParameters, m_Model, m_InferenceDevice); } else { return new HeuristicPolicy(heuristic); } default: return new HeuristicPolicy(heuristic); } } internal void UpdateAgentPolicy() { var agent = GetComponent(); if (agent == null) { return; } agent.ReloadPolicy(); } } }