using UnityEngine.Profiling;
namespace Unity.MLAgents.Actuators
{
///
/// IActuator implementation that forwards calls to an and an .
///
internal class VectorActuator : IActuator, IHeuristicProvider, IBuiltInActuator
{
IActionReceiver m_ActionReceiver;
IHeuristicProvider m_HeuristicProvider;
ActionBuffers m_ActionBuffers;
internal ActionBuffers ActionBuffers
{
get => m_ActionBuffers;
private set => m_ActionBuffers = value;
}
///
/// Create a VectorActuator that forwards to the provided IActionReceiver.
///
/// The used for OnActionReceived and WriteDiscreteActionMask.
/// If this parameter also implements it will be cast and used to forward calls to
/// .
///
///
public VectorActuator(IActionReceiver actionReceiver,
ActionSpec actionSpec,
string name = "VectorActuator")
: this(actionReceiver, actionReceiver as IHeuristicProvider, actionSpec, name) { }
///
/// Create a VectorActuator that forwards to the provided IActionReceiver.
///
/// The used for OnActionReceived and WriteDiscreteActionMask.
/// The used to fill the
/// for Heuristic Policies.
///
///
public VectorActuator(IActionReceiver actionReceiver,
IHeuristicProvider heuristicProvider,
ActionSpec actionSpec,
string name = "VectorActuator")
{
m_ActionReceiver = actionReceiver;
m_HeuristicProvider = heuristicProvider;
ActionSpec = actionSpec;
string suffix;
if (actionSpec.NumContinuousActions == 0)
{
suffix = "-Discrete";
}
else if (actionSpec.NumDiscreteActions == 0)
{
suffix = "-Continuous";
}
else
{
suffix = $"-Continuous-{actionSpec.NumContinuousActions}-Discrete-{actionSpec.NumDiscreteActions}";
}
Name = name + suffix;
}
///
public void ResetData()
{
m_ActionBuffers = ActionBuffers.Empty;
}
///
public void OnActionReceived(ActionBuffers actionBuffers)
{
Profiler.BeginSample("VectorActuator.OnActionReceived");
m_ActionBuffers = actionBuffers;
m_ActionReceiver.OnActionReceived(m_ActionBuffers);
Profiler.EndSample();
}
public void Heuristic(in ActionBuffers actionBuffersOut)
{
Profiler.BeginSample("VectorActuator.Heuristic");
m_HeuristicProvider?.Heuristic(actionBuffersOut);
Profiler.EndSample();
}
///
public void WriteDiscreteActionMask(IDiscreteActionMask actionMask)
{
m_ActionReceiver.WriteDiscreteActionMask(actionMask);
}
///
public ActionSpec ActionSpec { get; }
///
public string Name { get; }
///
public virtual BuiltInActuatorType GetBuiltInActuatorType()
{
return BuiltInActuatorType.VectorActuator;
}
}
}