| using Unity.InferenceEngine; |
| using System; |
| using UnityEngine; |
| using UnityEngine.Serialization; |
| using Unity.MLAgents.Actuators; |
| using Unity.MLAgents.Sensors.Reflection; |
|
|
| namespace Unity.MLAgents.Policies |
| { |
| |
| |
| |
| [Serializable] |
| public enum BehaviorType |
| { |
| |
| |
| |
| |
| |
| Default, |
|
|
| |
| |
| |
| HeuristicOnly, |
|
|
| |
| |
| |
| |
| InferenceOnly |
| } |
|
|
| |
| |
| |
| public enum ObservableAttributeOptions |
| { |
| |
| |
| |
| |
| |
| Ignore, |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| ExcludeInherited, |
|
|
| |
| |
| |
| |
| ExamineAll |
| } |
|
|
| |
| |
| |
| |
| |
| |
| [AddComponentMenu("ML Agents/Behavior Parameters", (int)MenuGroup.Default)] |
| public class BehaviorParameters : MonoBehaviour |
| { |
| [HideInInspector, SerializeField] |
| BrainParameters m_BrainParameters = new BrainParameters(); |
|
|
| |
| |
| |
| |
| public delegate void PolicyUpdated(bool isInHeuristicMode); |
|
|
| |
| |
| |
| internal event PolicyUpdated OnPolicyUpdated; |
|
|
| |
| |
| |
| public BrainParameters BrainParameters |
| { |
| get { return m_BrainParameters; } |
| internal set { m_BrainParameters = value; } |
| } |
|
|
| [HideInInspector, SerializeField] |
| ModelAsset m_Model; |
|
|
| |
| |
| |
| |
| |
| public ModelAsset Model |
| { |
| get { return m_Model; } |
| set { m_Model = value; UpdateAgentPolicy(); } |
| } |
|
|
| [HideInInspector, SerializeField] |
| InferenceDevice m_InferenceDevice = InferenceDevice.Default; |
|
|
| |
| |
| |
| |
| |
| public InferenceDevice InferenceDevice |
| { |
| get { return m_InferenceDevice; } |
| set { m_InferenceDevice = value; UpdateAgentPolicy(); } |
| } |
|
|
| [HideInInspector, SerializeField] |
| BehaviorType m_BehaviorType; |
|
|
| |
| |
| |
| public BehaviorType BehaviorType |
| { |
| get { return m_BehaviorType; } |
| set { m_BehaviorType = value; UpdateAgentPolicy(); } |
| } |
|
|
| [HideInInspector, SerializeField] |
| string m_BehaviorName = "My Behavior"; |
|
|
| |
| |
| |
| |
| |
| |
| public string BehaviorName |
| { |
| get { return m_BehaviorName; } |
| set { m_BehaviorName = value; UpdateAgentPolicy(); } |
| } |
|
|
| |
| |
| |
| [HideInInspector, SerializeField, FormerlySerializedAs("m_TeamID")] |
| public int TeamId; |
| |
|
|
| [FormerlySerializedAs("m_useChildSensors")] |
| [HideInInspector] |
| [SerializeField] |
| [Tooltip("Use all Sensor components attached to child GameObjects of this Agent.")] |
| bool m_UseChildSensors = true; |
|
|
| [HideInInspector] |
| [SerializeField] |
| [Tooltip("Use all Actuator components attached to child GameObjects of this Agent.")] |
| bool m_UseChildActuators = true; |
|
|
| |
| |
| |
| |
| public bool UseChildSensors |
| { |
| get { return m_UseChildSensors; } |
| set { m_UseChildSensors = value; } |
| } |
|
|
| [HideInInspector] |
| [SerializeField] |
| [Tooltip("Set action selection to deterministic, Only applies to inference from within unity.")] |
| private bool m_DeterministicInference = false; |
|
|
| |
| |
| |
| public bool DeterministicInference |
| { |
| get { return m_DeterministicInference; } |
| set { m_DeterministicInference = value; } |
| } |
|
|
| |
| |
| |
| |
| public bool UseChildActuators |
| { |
| get { return m_UseChildActuators; } |
| set { m_UseChildActuators = value; } |
| } |
|
|
| [HideInInspector, SerializeField] |
| ObservableAttributeOptions m_ObservableAttributeHandling = ObservableAttributeOptions.Ignore; |
|
|
| |
| |
| |
| public ObservableAttributeOptions ObservableAttributeHandling |
| { |
| get { return m_ObservableAttributeHandling; } |
| set { m_ObservableAttributeHandling = value; } |
| } |
|
|
| |
| |
| |
| public string FullyQualifiedBehaviorName |
| { |
| get { return m_BehaviorName + "?team=" + TeamId; } |
| } |
|
|
| void Awake() |
| { |
| OnPolicyUpdated += mode => { }; |
| } |
|
|
| internal IPolicy GeneratePolicy(ActionSpec actionSpec, ActuatorManager actuatorManager) |
| { |
| switch (m_BehaviorType) |
| { |
| case BehaviorType.HeuristicOnly: |
| return new HeuristicPolicy(actuatorManager, actionSpec); |
| 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 SentisPolicy(actionSpec, actuatorManager, m_Model, m_InferenceDevice, m_BehaviorName, m_DeterministicInference); |
| } |
| case BehaviorType.Default: |
| if (Academy.Instance.IsCommunicatorOn) |
| { |
| return new RemotePolicy(actionSpec, actuatorManager, FullyQualifiedBehaviorName); |
| } |
| if (m_Model != null) |
| { |
| return new SentisPolicy(actionSpec, actuatorManager, m_Model, m_InferenceDevice, m_BehaviorName, m_DeterministicInference); |
| } |
| else |
| { |
| return new HeuristicPolicy(actuatorManager, actionSpec); |
| } |
| default: |
| return new HeuristicPolicy(actuatorManager, actionSpec); |
| } |
| } |
|
|
| |
| |
| |
| |
| public bool IsInHeuristicMode() |
| { |
| if (BehaviorType == BehaviorType.HeuristicOnly) |
| { |
| return true; |
| } |
|
|
| return BehaviorType == BehaviorType.Default && |
| ReferenceEquals(Model, null) && |
| (!Academy.IsInitialized || |
| Academy.IsInitialized && |
| !Academy.Instance.IsCommunicatorOn); |
| } |
|
|
| internal void UpdateAgentPolicy() |
| { |
| var agent = GetComponent<Agent>(); |
| if (agent == null) |
| { |
| return; |
| } |
| agent.ReloadPolicy(); |
| OnPolicyUpdated?.Invoke(IsInHeuristicMode()); |
| } |
| } |
| } |
|
|