您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
134 行
4.4 KiB
134 行
4.4 KiB
using Barracuda;
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace MLAgents.Policies
|
|
{
|
|
/// <summary>
|
|
/// The Factory to generate policies.
|
|
/// </summary>
|
|
[AddComponentMenu("ML Agents/Behavior Parameters", (int)MenuGroup.Default)]
|
|
internal class BehaviorParameters : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
enum BehaviorType
|
|
{
|
|
Default,
|
|
HeuristicOnly,
|
|
InferenceOnly
|
|
}
|
|
|
|
[HideInInspector]
|
|
[SerializeField]
|
|
BrainParameters m_BrainParameters = new BrainParameters();
|
|
[HideInInspector]
|
|
[SerializeField]
|
|
NNModel m_Model;
|
|
[HideInInspector]
|
|
[SerializeField]
|
|
InferenceDevice m_InferenceDevice;
|
|
[HideInInspector]
|
|
[SerializeField]
|
|
|
|
// Disable warning /com.unity.ml-agents/Runtime/Policy/BehaviorParameters.cs(...):
|
|
// warning CS0649: Field 'BehaviorParameters.m_BehaviorType' is never assigned to,
|
|
// and will always have its default value
|
|
// This field is set in the custom editor.
|
|
#pragma warning disable 0649
|
|
BehaviorType m_BehaviorType;
|
|
#pragma warning restore 0649
|
|
[HideInInspector]
|
|
[SerializeField]
|
|
string m_BehaviorName = "My Behavior";
|
|
|
|
/// <summary>
|
|
/// The team ID for this behavior.
|
|
/// </summary>
|
|
[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;
|
|
|
|
/// <summary>
|
|
/// The associated <see cref="BrainParameters"/> for this behavior.
|
|
/// </summary>
|
|
internal BrainParameters brainParameters
|
|
{
|
|
get { return m_BrainParameters; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether or not to use all the sensor components attached to child GameObjects of the agent.
|
|
/// </summary>
|
|
public bool useChildSensors
|
|
{
|
|
get { return m_UseChildSensors; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The name of this behavior, which is used as a base name. See
|
|
/// <see cref="fullyQualifiedBehaviorName"/> for the full name.
|
|
/// </summary>
|
|
public string behaviorName
|
|
{
|
|
get { return m_BehaviorName; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the behavior name, concatenated with any other metadata (i.e. team id).
|
|
/// </summary>
|
|
public string fullyQualifiedBehaviorName
|
|
{
|
|
get { return m_BehaviorName + "?team=" + TeamId; }
|
|
}
|
|
|
|
public IPolicy GeneratePolicy(Func<float[]> heuristic)
|
|
{
|
|
switch (m_BehaviorType)
|
|
{
|
|
case BehaviorType.HeuristicOnly:
|
|
return new HeuristicPolicy(heuristic);
|
|
case BehaviorType.InferenceOnly:
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the model and related details for this behavior.
|
|
/// </summary>
|
|
/// <param name="newBehaviorName">New name for the behavior.</param>
|
|
/// <param name="model">New neural network model for this behavior.</param>
|
|
/// <param name="inferenceDevice">New inference device for this behavior.</param>
|
|
public void GiveModel(
|
|
string newBehaviorName,
|
|
NNModel model,
|
|
InferenceDevice inferenceDevice = InferenceDevice.CPU)
|
|
{
|
|
m_Model = model;
|
|
m_InferenceDevice = inferenceDevice;
|
|
m_BehaviorName = newBehaviorName;
|
|
}
|
|
}
|
|
}
|