using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.MLAgents
{
///
/// The DecisionRequester component automatically request decisions for an
/// instance at regular intervals.
///
///
/// Attach a DecisionRequester component to the same [GameObject] as the
/// component.
///
/// The DecisionRequester component provides a convenient and flexible way to
/// trigger the agent decision making process. Without a DecisionRequester,
/// your implementation must manually call its
/// function.
///
[AddComponentMenu("ML Agents/Decision Requester", (int)MenuGroup.Default)]
[RequireComponent(typeof(Agent))]
public class DecisionRequester : MonoBehaviour
{
///
/// The frequency with which the agent requests a decision. A DecisionPeriod of 5 means
/// that the Agent will request a decision every 5 Academy steps. ///
[Range(1, 20)]
[Tooltip("The frequency with which the agent requests a decision. A DecisionPeriod " +
"of 5 means that the Agent will request a decision every 5 Academy steps.")]
public int DecisionPeriod = 5;
///
/// Indicates whether or not the agent will take an action during the Academy steps where
/// it does not request a decision. Has no effect when DecisionPeriod is set to 1.
///
[Tooltip("Indicates whether or not the agent will take an action during the Academy " +
"steps where it does not request a decision. Has no effect when DecisionPeriod " +
"is set to 1.")]
[FormerlySerializedAs("RepeatAction")]
public bool TakeActionsBetweenDecisions = true;
[NonSerialized]
Agent m_Agent;
///
/// Get the Agent attached to the DecisionRequester.
///
public Agent Agent
{
get => m_Agent;
}
internal void Awake()
{
m_Agent = gameObject.GetComponent();
Debug.Assert(m_Agent != null, "Agent component was not found on this gameObject and is required.");
Academy.Instance.AgentPreStep += MakeRequests;
}
void OnDestroy()
{
if (Academy.IsInitialized)
{
Academy.Instance.AgentPreStep -= MakeRequests;
}
}
///
/// Information about Academy step used to make decisions about whether to request a decision.
///
public struct DecisionRequestContext
{
///
/// The current step count of the Academy, equivalent to Academy.StepCount.
///
public int AcademyStepCount;
}
///
/// 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.
///
/// The current step count of the academy.
void MakeRequests(int academyStepCount)
{
var context = new DecisionRequestContext
{
AcademyStepCount = academyStepCount
};
if (ShouldRequestDecision(context))
{
m_Agent?.RequestDecision();
}
if (ShouldRequestAction(context))
{
m_Agent?.RequestAction();
}
}
///
/// Whether Agent.RequestDecision should be called on this update step.
///
///
///
protected virtual bool ShouldRequestDecision(DecisionRequestContext context)
{
return context.AcademyStepCount % DecisionPeriod == 0;
}
///
/// Whether Agent.RequestAction should be called on this update step.
///
///
///
protected virtual bool ShouldRequestAction(DecisionRequestContext context)
{
return TakeActionsBetweenDecisions;
}
}
}