浏览代码

Behavior and BrainParameters back to public

/bug-failed-api-check
Chris Elion 5 年前
当前提交
b4ce35a2
共有 6 个文件被更改,包括 156 次插入56 次删除
  1. 2
      com.unity.ml-agents/CHANGELOG.md
  2. 66
      com.unity.ml-agents/Editor/BehaviorParametersEditor.cs
  3. 31
      com.unity.ml-agents/Runtime/Agent.cs
  4. 102
      com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs
  5. 4
      com.unity.ml-agents/Runtime/Policies/BrainParameters.cs
  6. 7
      DevProject/Assets/placeholder.txt.meta

2
com.unity.ml-agents/CHANGELOG.md


- Multi-GPU training and the `--multi-gpu` option has been removed temporarily. (#3345)
- All Sensor related code has been moved to the namespace `MLAgents.Sensors`.
- All SideChannel related code has been moved to the namespace `MLAgents.SideChannels`.
- `BrainParameters` and `SpaceType` have been removed from the public API
- `BehaviorParameters` have been removed from the public API.
### Minor Changes
- Monitor.cs was moved to Examples. (#3372)

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


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

so.Update();
// Drawing the Behavior Parameters
EditorGUI.BeginChangeCheck();
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(so.FindProperty("m_BrainParameters"), true);
bool needModelUpdate = EditorGUI.EndChangeCheck();
EditorGUI.BeginChangeCheck();
EditorGUI.BeginDisabledGroup(Application.isPlaying);
{
EditorGUILayout.PropertyField(so.FindProperty("m_BrainParameters"), true);
}
EditorGUI.EndDisabledGroup();
var brainParamsChanged = EditorGUI.EndChangeCheck();
EditorGUI.BeginChangeCheck();
needModelUpdate = needModelUpdate || EditorGUI.EndChangeCheck();
EditorGUI.BeginChangeCheck();
var behaviorTypeChanged = EditorGUI.EndChangeCheck();
EditorGUI.BeginChangeCheck();
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();
var othersChanged = EditorGUI.EndChangeCheck();
EditorGUI.indentLevel--;
m_RequireReload = needModelUpdate || brainParamsChanged || behaviorTypeChanged || othersChanged;
if (needModelUpdate || behaviorTypeChanged)
{
UpdateAgent(needModelUpdate, behaviorTypeChanged);
}
}
/// <summary>

{
EditorGUILayout.HelpBox(check, MessageType.Warning);
}
}
}
}
void UpdateAgent(bool needModelUpdate, bool behaviorTypeChanged)
{
if (Application.isPlaying)
{
var behaviorParameters = (BehaviorParameters)target;
var agent = behaviorParameters.GetComponent<Agent>();
if (agent == null)
{
return;
}
if (needModelUpdate)
{
agent.GiveModel(
behaviorParameters.behaviorName,
behaviorParameters.model,
behaviorParameters.inferenceDevice,
true
);
}
if (behaviorTypeChanged)
{
agent.SetBehaviorType(behaviorParameters.behaviorType, true);
}
}
}

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


/// </summary>
public int TeamId
{
// TODO should we remove this and BehaviorName properties,
// and add properties in BehaviorParameters instead?
get
{
LazyInitialize();

/// Updates the Model for the agent. Any model currently assigned to the
/// agent will be replaced with the provided one. If the arguments are
/// identical to the current parameters of the agent, the model will
/// remain unchanged.
/// remain unchanged, unless the force parameter is true.
/// </summary>
/// <param name="behaviorName"> The identifier of the behavior. This
/// will categorize the agent when training.

/// will be run.</param>
/// <param name="force">Whether to update the Agent even if all the parameters are the same.</param>
InferenceDevice inferenceDevice = InferenceDevice.CPU)
InferenceDevice inferenceDevice = InferenceDevice.CPU,
bool force = false)
m_PolicyFactory.GiveModel(behaviorName, model, inferenceDevice);
if (behaviorName == m_PolicyFactory.behaviorName &&
model == m_PolicyFactory.model &&
inferenceDevice == m_PolicyFactory.inferenceDevice)
{
if (!force)
{
// If everything is the same, don't make any changes.
return;
}
}
m_PolicyFactory.SetModel(behaviorName, model, inferenceDevice);
m_Brain?.Dispose();
m_Brain = m_PolicyFactory.GeneratePolicy(Heuristic);
}

/// </summary>
/// <param name="behaviorType"> The new behaviorType for the Agent
/// </param>
public void SetBehaviorType(BehaviorType behaviorType)
/// <param name="behaviorType"> The new behaviorType for the Agent.</param>
/// <param name="force">Whether to update the Agent even if all the parameters are the same.</param>
public void SetBehaviorType(BehaviorType behaviorType, bool force = false)
if (m_PolicyFactory.m_BehaviorType == behaviorType)
if (m_PolicyFactory.behaviorType == behaviorType && !force)
m_PolicyFactory.m_BehaviorType = behaviorType;
m_PolicyFactory.behaviorType = behaviorType;
m_Brain?.Dispose();
m_Brain = m_PolicyFactory.GeneratePolicy(Heuristic);
}

102
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]
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
internal BehaviorType m_BehaviorType;
#pragma warning restore 0649
[HideInInspector]
[SerializeField]
string m_BehaviorName = "My Behavior";
/// The team ID for this behavior.
/// 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.
[HideInInspector, SerializeField, FormerlySerializedAs("m_TeamID")]
public int TeamId;
public NNModel model
{
get { return m_Model; }
}
[FormerlySerializedAs("m_useChildSensors")]
[HideInInspector]
[SerializeField]
[Tooltip("Use all Sensor components attached to child GameObjects of this Agent.")]
bool m_UseChildSensors = true;
[HideInInspector, SerializeField]
InferenceDevice m_InferenceDevice;
/// The associated <see cref="BrainParameters"/> for this behavior.
/// 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.
internal BrainParameters brainParameters
public InferenceDevice inferenceDevice
get { return m_BrainParameters; }
get { return m_InferenceDevice; }
[HideInInspector, SerializeField]
BehaviorType m_BehaviorType;
/// Whether or not to use all the sensor components attached to child GameObjects of the agent.
/// The BehaviorType for the Agent.
/// This cannot be set directly; use <see cref="Agent.SetBehaviorType(BehaviorType)"/>
/// to set it.
public bool useChildSensors
public BehaviorType behaviorType
get { return m_UseChildSensors; }
get { return m_BehaviorType; }
internal set { m_BehaviorType = value; }
[HideInInspector, SerializeField]
string m_BehaviorName = "My Behavior";
/// This cannot be set directly; use <see cref="Agent.GiveModel(string,NNModel,InferenceDevice)"/>
/// to set it.
/// </summary>
public string behaviorName
{

/// <summary>
/// The team ID for this behavior.
/// </summary>
[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;
/// <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; }
internal set { m_UseChildSensors = value; } // TODO make public, don't allow changes at runtime
}
/// <summary>
/// Returns the behavior name, concatenated with any other metadata (i.e. team id).
/// </summary>
public string fullyQualifiedBehaviorName

public IPolicy GeneratePolicy(Func<float[]> heuristic)
internal IPolicy GeneratePolicy(Func<float[]> heuristic)
{
switch (m_BehaviorType)
{

/// <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(
internal void SetModel(
string newBehaviorName,
NNModel model,
InferenceDevice inferenceDevice = InferenceDevice.CPU)

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.

7
DevProject/Assets/placeholder.txt.meta


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