浏览代码

Merge pull request #3595 from Unity-Technologies/develop-BehaviorParams-public

Make BehaviorParameters public again, update editor
/bug-failed-api-check
GitHub 5 年前
当前提交
ab99343a
共有 8 个文件被更改,包括 161 次插入102 次删除
  1. 3
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs
  2. 4
      Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
  3. 71
      com.unity.ml-agents/Editor/BehaviorParametersEditor.cs
  4. 53
      com.unity.ml-agents/Runtime/Agent.cs
  5. 119
      com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs
  6. 4
      com.unity.ml-agents/Runtime/Policies/BrainParameters.cs
  7. 2
      com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs
  8. 7
      DevProject/Assets/placeholder.txt.meta

3
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs


void OverrideModel()
{
m_Agent.LazyInitialize();
var name = m_Agent.BehaviorName;
var bp = m_Agent.GetComponent<BehaviorParameters>();
var name = bp.behaviorName;
var nnModel = GetModelForBehaviorName(name);
Debug.Log($"Overriding behavior {name} for agent with model {nnModel?.name}");

4
Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs


[HideInInspector]
public Rigidbody agentRb;
SoccerSettings m_SoccerSettings;
BehaviorParameters m_BehaviorParameters;
if (TeamId == (int)Team.Blue)
m_BehaviorParameters = gameObject.GetComponent<BehaviorParameters>();
if (m_BehaviorParameters.TeamId == (int)Team.Blue)
{
team = Team.Blue;
m_Transform = new Vector3(transform.position.x - 4f, .5f, transform.position.z);

71
com.unity.ml-agents/Editor/BehaviorParametersEditor.cs


using UnityEditor;
using Barracuda;
using MLAgents.Policies;
using UnityEngine;
namespace MLAgents.Editor
{

{
var so = serializedObject;
so.Update();
bool needPolicyUpdate; // Whether the name, model, inference device, or BehaviorType changed.
EditorGUI.indentLevel++;
EditorGUI.BeginChangeCheck(); // global
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(so.FindProperty("m_BehaviorName"));
EditorGUILayout.PropertyField(so.FindProperty("m_BrainParameters"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_Model"), true);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(so.FindProperty("m_InferenceDevice"), true);
EditorGUI.indentLevel--;
EditorGUILayout.PropertyField(so.FindProperty("m_BehaviorType"));
{
EditorGUILayout.PropertyField(so.FindProperty("m_BehaviorName"));
}
needPolicyUpdate = EditorGUI.EndChangeCheck();
EditorGUI.BeginDisabledGroup(Application.isPlaying);
{
EditorGUILayout.PropertyField(so.FindProperty("m_BrainParameters"), true);
}
EditorGUI.EndDisabledGroup();
EditorGUI.BeginChangeCheck();
{
EditorGUILayout.PropertyField(so.FindProperty("m_Model"), true);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(so.FindProperty("m_InferenceDevice"), true);
EditorGUI.indentLevel--;
}
needPolicyUpdate = needPolicyUpdate || EditorGUI.EndChangeCheck();
EditorGUI.BeginChangeCheck();
{
EditorGUILayout.PropertyField(so.FindProperty("m_BehaviorType"));
}
needPolicyUpdate = needPolicyUpdate || EditorGUI.EndChangeCheck();
EditorGUILayout.PropertyField(so.FindProperty("m_UseChildSensors"), true);
// EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Heuristic"), true);
EditorGUI.indentLevel--;
if (EditorGUI.EndChangeCheck())
EditorGUI.BeginDisabledGroup(Application.isPlaying);
m_RequireReload = true;
EditorGUILayout.PropertyField(so.FindProperty("m_UseChildSensors"), true);
EditorGUI.EndDisabledGroup();
EditorGUI.indentLevel--;
m_RequireReload = EditorGUI.EndChangeCheck();
if (needPolicyUpdate)
{
UpdateAgentPolicy();
}
}
/// <summary>

EditorGUILayout.HelpBox(check, MessageType.Warning);
}
}
}
}
void UpdateAgentPolicy()
{
if (Application.isPlaying)
{
var behaviorParameters = (BehaviorParameters)target;
var agent = behaviorParameters.GetComponent<Agent>();
if (agent == null)
{
return;
}
agent.ReloadPolicy();
}
}
}

53
com.unity.ml-agents/Runtime/Agent.cs


public int maxStep;
}
/// <summary>
/// The team ID for this Agent.
/// </summary>
public int TeamId
{
get
{
LazyInitialize();
return m_PolicyFactory.TeamId;
}
}
/// <summary>
/// The name of the behavior of the Agent.
/// </summary>
public string BehaviorName
{
get
{
LazyInitialize();
return m_PolicyFactory.behaviorName;
}
}
[SerializeField][HideInInspector]
internal AgentParameters agentParameters;
[SerializeField][HideInInspector]

NNModel model,
InferenceDevice inferenceDevice = InferenceDevice.CPU)
{
m_PolicyFactory.GiveModel(behaviorName, model, inferenceDevice);
m_Brain?.Dispose();
m_Brain = m_PolicyFactory.GeneratePolicy(Heuristic);
if (behaviorName == m_PolicyFactory.behaviorName &&
model == m_PolicyFactory.model &&
inferenceDevice == m_PolicyFactory.inferenceDevice)
{
// If everything is the same, don't make any changes.
return;
}
m_PolicyFactory.model = model;
m_PolicyFactory.inferenceDevice = inferenceDevice;
m_PolicyFactory.behaviorName = behaviorName;
ReloadPolicy();
/// <param name="behaviorType"> The new behaviorType for the Agent
/// </param>
/// <param name="behaviorType"> The new behaviorType for the Agent.</param>
if (m_PolicyFactory.m_BehaviorType == behaviorType)
if (m_PolicyFactory.behaviorType == behaviorType)
m_PolicyFactory.m_BehaviorType = behaviorType;
m_PolicyFactory.behaviorType = behaviorType;
ReloadPolicy();
}
internal void ReloadPolicy()
{
}
/// <summary>

{
throw new UnityAgentsException("Call to SendInfoToBrain when Agent hasn't been initialized." +
"Please ensure that you are calling 'base.OnEnable()' if you have overridden OnEnable.");
}
if (m_Brain == null)

119
com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs


/// The Factory to generate policies.
/// </summary>
[AddComponentMenu("ML Agents/Behavior Parameters", (int)MenuGroup.Default)]
internal class BehaviorParameters : MonoBehaviour
public class BehaviorParameters : MonoBehaviour
[HideInInspector]
[SerializeField]
[HideInInspector, SerializeField]
[HideInInspector]
[SerializeField]
/// <summary>
/// The associated <see cref="BrainParameters"/> for this behavior.
/// </summary>
public BrainParameters brainParameters
{
get { return m_BrainParameters; }
internal set { m_BrainParameters = value; }
}
[HideInInspector, SerializeField]
[HideInInspector]
[SerializeField]
/// <summary>
/// The neural network model used when in inference mode.
/// This cannot be set directly; use <see cref="Agent.GiveModel(string,NNModel,InferenceDevice)"/>
/// to set it.
/// </summary>
public NNModel model
{
get { return m_Model; }
internal set { m_Model = value; }
}
[HideInInspector, SerializeField]
[HideInInspector]
[SerializeField]
/// <summary>
/// How inference is performed for this Agent's model.
/// This cannot be set directly; use <see cref="Agent.GiveModel(string,NNModel,InferenceDevice)"/>
/// to set it.
/// </summary>
public InferenceDevice inferenceDevice
{
get { return m_InferenceDevice; }
internal set { m_InferenceDevice = value; }
}
[HideInInspector, SerializeField]
BehaviorType m_BehaviorType;
/// <summary>
/// The BehaviorType for the Agent.
/// This cannot be set directly; use <see cref="Agent.SetBehaviorType(BehaviorType)"/>
/// to set it.
/// </summary>
public BehaviorType behaviorType
{
get { return m_BehaviorType; }
internal set { m_BehaviorType = value; }
}
// 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
internal BehaviorType m_BehaviorType;
#pragma warning restore 0649
[HideInInspector]
[SerializeField]
[HideInInspector, SerializeField]
/// The name of this behavior, which is used as a base name. See
/// <see cref="fullyQualifiedBehaviorName"/> for the full name.
/// This cannot be set directly; use <see cref="Agent.GiveModel(string,NNModel,InferenceDevice)"/>
/// to set it.
/// </summary>
public string behaviorName
{
get { return m_BehaviorName; }
internal set { m_BehaviorName = value; }
}
/// <summary>
// TODO properties here instead of Agent
[FormerlySerializedAs("m_useChildSensors")]
[HideInInspector]

/// <summary>
/// The associated <see cref="BrainParameters"/> for this behavior.
/// </summary>
internal BrainParameters brainParameters
{
get { return m_BrainParameters; }
}
/// <summary>
}
/// <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; }
internal set { m_UseChildSensors = value; } // TODO make public, don't allow changes at runtime
}
/// <summary>

get { return m_BehaviorName + "?team=" + TeamId; }
}
public IPolicy GeneratePolicy(Func<float[]> heuristic)
internal IPolicy GeneratePolicy(Func<float[]> heuristic)
{
switch (m_BehaviorType)
{

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;
}
}
}

4
com.unity.ml-agents/Runtime/Policies/BrainParameters.cs


/// <summary>
/// Whether the action space is discrete or continuous.
/// </summary>
internal enum SpaceType
public enum SpaceType
{
/// <summary>
/// Discrete action space: a fixed number of options are available.

/// decision process.
/// </summary>
[Serializable]
internal class BrainParameters
public class BrainParameters
{
/// <summary>
/// If continuous : The length of the float vector that represents the state.

2
com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs


internal IPolicy GetPolicy()
{
return (IPolicy) typeof(Agent).GetField("m_Brain", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
return (IPolicy)typeof(Agent).GetField("m_Brain", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this);
}
public int initializeAgentCalls;

7
DevProject/Assets/placeholder.txt.meta


fileFormatVersion: 2
guid: 702d33154db334c689920b2b91b7f5b9
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存