浏览代码

add and use Agent.ReloadPolicy()

/bug-failed-api-check
Chris Elion 5 年前
当前提交
fcfd0f7f
共有 5 个文件被更改,包括 32 次插入80 次删除
  1. 3
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs
  2. 4
      Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
  3. 29
      com.unity.ml-agents/Editor/BehaviorParametersEditor.cs
  4. 57
      com.unity.ml-agents/Runtime/Agent.cs
  5. 19
      com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs

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

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


{
var so = serializedObject;
so.Update();
bool needModelUpdate; // Whether the name, model, or inference device changed.
bool behaviorTypeChanged;
bool needPolicyUpdate; // Whether the name, model, inference device, or BehaviorType changed.
// Drawing the Behavior Parameters
EditorGUI.indentLevel++;

{
EditorGUILayout.PropertyField(so.FindProperty("m_BehaviorName"));
}
needModelUpdate = EditorGUI.EndChangeCheck();
needPolicyUpdate = EditorGUI.EndChangeCheck();
EditorGUI.BeginDisabledGroup(Application.isPlaying);
{

EditorGUILayout.PropertyField(so.FindProperty("m_InferenceDevice"), true);
EditorGUI.indentLevel--;
}
needModelUpdate = needModelUpdate || EditorGUI.EndChangeCheck();
needPolicyUpdate = needPolicyUpdate || EditorGUI.EndChangeCheck();
behaviorTypeChanged = EditorGUI.EndChangeCheck();
needPolicyUpdate = needPolicyUpdate || EditorGUI.EndChangeCheck();
EditorGUILayout.PropertyField(so.FindProperty("TeamId"));
EditorGUI.BeginDisabledGroup(Application.isPlaying);

DisplayFailedModelChecks();
so.ApplyModifiedProperties();
if (needModelUpdate || behaviorTypeChanged)
if (needPolicyUpdate)
UpdateAgentFromBehaviorParameters(needModelUpdate, behaviorTypeChanged);
UpdateAgentPolicy();
}
}

}
}
void UpdateAgentFromBehaviorParameters(bool needModelUpdate, bool behaviorTypeChanged)
void UpdateAgentPolicy()
{
if (Application.isPlaying)
{

return;
}
if (needModelUpdate)
{
agent.GiveModel(
behaviorParameters.behaviorName,
behaviorParameters.model,
behaviorParameters.inferenceDevice,
true
);
}
agent.ReloadPolicy();
if (behaviorTypeChanged)
{
agent.SetBehaviorType(behaviorParameters.behaviorType, true);
}
}
}
}

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


public int maxStep;
}
/// <summary>
/// The team ID for this Agent.
/// </summary>
public int TeamId
{
// TODO should we remove this and BehaviorName properties,
// and add properties in BehaviorParameters instead?
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]

/// 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, unless the force parameter is true.
/// remain unchanged.
/// </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,
bool force = false)
InferenceDevice inferenceDevice = InferenceDevice.CPU)
if (!force)
{
// If everything is the same, don't make any changes.
return;
}
// 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);
m_PolicyFactory.model = model;
m_PolicyFactory.inferenceDevice = inferenceDevice;
m_PolicyFactory.behaviorName = behaviorName;
ReloadPolicy();
}
/// <summary>

/// <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)
public void SetBehaviorType(BehaviorType behaviorType)
if (m_PolicyFactory.behaviorType == behaviorType && !force)
if (m_PolicyFactory.behaviorType == behaviorType)
ReloadPolicy();
}
internal void ReloadPolicy()
{
}
/// <summary>

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


public NNModel model
{
get { return m_Model; }
internal set { m_Model = value; }
}
[HideInInspector, SerializeField]

public InferenceDevice inferenceDevice
{
get { return m_InferenceDevice; }
internal set { m_InferenceDevice = value; }
}
[HideInInspector, SerializeField]

public string behaviorName
{
get { return m_BehaviorName; }
internal set { m_BehaviorName = value; }
}
/// <summary>

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>
internal void SetModel(
string newBehaviorName,
NNModel model,
InferenceDevice inferenceDevice = InferenceDevice.CPU)
{
m_Model = model;
m_InferenceDevice = inferenceDevice;
m_BehaviorName = newBehaviorName;
}
}
}
正在加载...
取消
保存