浏览代码

[MLA-1159] Add virtual methods to DecisionRequester (#5223)

/check-for-ModelOverriders
GitHub 4 年前
当前提交
723a8447
共有 3 个文件被更改,包括 70 次插入2 次删除
  1. 2
      com.unity.ml-agents/CHANGELOG.md
  2. 49
      com.unity.ml-agents/Runtime/DecisionRequester.cs
  3. 21
      com.unity.ml-agents/Tests/Editor/PublicAPI/PublicApiValidation.cs

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


depend on the previous behavior, you can explicitly set the Agent's `InferenceDevice` to `InferenceDevice.CPU`. (#5175)
- Added support for `Goal Signal` as a type of observation. Trainers can now use HyperNetworks to process `Goal Signal`. Trainers with HyperNetworks are more effective at solving multiple tasks. (#5142, #5159, #5149)
- Modified the [GridWorld environment](https://github.com/Unity-Technologies/ml-agents/blob/main/docs/Learning-Environment-Examples.md#gridworld) to use the new `Goal Signal` feature. (#5193)
- `DecisionRequester.ShouldRequestDecision()` and `ShouldRequestAction()`methods were added. These are used to
determine whether `Agent.RequestDecision()` and `Agent.RequestAction()` are called (respectively). (#5223)
- `RaycastPerceptionSensor` now caches its raycast results; they can be accessed via `RayPerceptionSensor.RayPerceptionOutput`. (#5222)
#### ml-agents / ml-agents-envs / gym-unity (Python)

49
com.unity.ml-agents/Runtime/DecisionRequester.cs


[NonSerialized]
Agent m_Agent;
/// <summary>
/// Get the Agent attached to the DecisionRequester.
/// </summary>
public Agent Agent
{
get => m_Agent;
}
internal void Awake()
{
m_Agent = gameObject.GetComponent<Agent>();

}
/// <summary>
/// Information about Academy step used to make decisions about whether to request a decision.
/// </summary>
public struct DecisionRequestContext
{
/// <summary>
/// The current step count of the Academy, equivalent to Academy.StepCount.
/// </summary>
public int AcademyStepCount;
}
/// <summary>
/// Method that hooks into the Academy in order inform the Agent on whether or not it should request a
/// decision, and whether or not it should take actions between decisions.
/// </summary>

if (academyStepCount % DecisionPeriod == 0)
var context = new DecisionRequestContext
{
AcademyStepCount = academyStepCount
};
if (ShouldRequestDecision(context))
if (TakeActionsBetweenDecisions)
if (ShouldRequestAction(context))
}
/// <summary>
/// Whether Agent.RequestDecision should be called on this update step.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected virtual bool ShouldRequestDecision(DecisionRequestContext context)
{
return context.AcademyStepCount % DecisionPeriod == 0;
}
/// <summary>
/// Whether Agent.RequestAction should be called on this update step.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected virtual bool ShouldRequestAction(DecisionRequestContext context)
{
return TakeActionsBetweenDecisions;
}
}
}

21
com.unity.ml-agents/Tests/Editor/PublicAPI/PublicApiValidation.cs


using System.Collections.Generic;
using Unity.MLAgents.Sensors;
using NUnit.Framework;
using Unity.MLAgents;
using UnityEngine;
namespace Unity.MLAgentsExamples

Assert.AreEqual(outputs.RayOutputs.Length, 2*sensorComponent.RaysPerDirection + 1);
}
#endif
/// <summary>
/// Make sure we can inherit from DecisionRequester and override some logic.
/// </summary>
class CustomDecisionRequester : DecisionRequester
{
/// <summary>
/// Example logic. If the killswitch flag is set, the Agent never requests a decision.
/// </summary>
public bool KillswitchEnabled;
public CustomDecisionRequester()
{
}
protected override bool ShouldRequestDecision(DecisionRequestContext context)
{
return !KillswitchEnabled && base.ShouldRequestDecision(context);
}
}
}
}
正在加载...
取消
保存