Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public override int GetHashCode()
/// <param name="destination">A float array to pack actions into whose length is greater than or
/// equal to the addition of the Lengths of this objects <see cref="ContinuousActions"/> and
/// <see cref="DiscreteActions"/> segments.</param>
/// [Obsolete("PackActions has been deprecated.")]
[Obsolete("PackActions has been deprecated.")]
public void PackActions(in float[] destination)
{
Debug.Assert(destination.Length >= ContinuousActions.Length + DiscreteActions.Length,
Expand Down
14 changes: 13 additions & 1 deletion com.unity.ml-agents/Runtime/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,11 @@ public virtual void Initialize() { }
/// <seealso cref="IActionReceiver.OnActionReceived"/>
public virtual void Heuristic(in ActionBuffers actionsOut)
{
// For backward compatibility
// Disable deprecation warnings so we can call the legacy overload.
#pragma warning disable CS0618

// The default implementation of Heuristic calls the
// obsolete version for backward compatibility
switch (m_PolicyFactory.BrainParameters.VectorActionSpaceType)
{
case SpaceType.Continuous:
Expand All @@ -904,6 +908,8 @@ public virtual void Heuristic(in ActionBuffers actionsOut)
actionsOut.ContinuousActions.Clear();
break;
}
#pragma warning restore CS0618

}

/// <summary>
Expand Down Expand Up @@ -1151,7 +1157,10 @@ public virtual void WriteDiscreteActionMask(IDiscreteActionMask actionMask)
{
m_ActionMasker = new DiscreteActionMasker(actionMask);
}
// Disable deprecation warnings so we can call the legacy overload.
#pragma warning disable CS0618
CollectDiscreteActionMasks(m_ActionMasker);
#pragma warning restore CS0618
}

/// <summary>
Expand Down Expand Up @@ -1232,7 +1241,10 @@ public virtual void OnActionReceived(ActionBuffers actions)
{
m_LegacyActionCache = Array.ConvertAll(actions.DiscreteActions.Array, x => (float)x);
}
// Disable deprecation warnings so we can call the legacy overload.
#pragma warning disable CS0618
OnActionReceived(m_LegacyActionCache);
#pragma warning restore CS0618
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion com.unity.ml-agents/Runtime/Agent.deprecated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public partial class Agent
/// Deprecated, use <see cref="WriteDiscreteActionMask"/> instead.
/// </summary>
/// <param name="actionMasker"></param>
[Obsolete("CollectDiscreteActionMasks has been deprecated, please use WriteDiscreteActionMask.")]
public virtual void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
{
}
Expand All @@ -17,6 +18,7 @@ public virtual void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker
/// Deprecated, use <see cref="Heuristic(in ActionBuffers)"/> instead.
/// </summary>
/// <param name="actionsOut"></param>
[Obsolete("The float[] version of Heuristic has been deprecated, please use the ActionBuffers version instead.")]
public virtual void Heuristic(float[] actionsOut)
{
Debug.LogWarning("Heuristic method called but not implemented. Returning placeholder actions.");
Expand All @@ -27,6 +29,7 @@ public virtual void Heuristic(float[] actionsOut)
/// Deprecated, use <see cref="OnActionReceived(ActionBuffers)"/> instead.
/// </summary>
/// <param name="vectorAction"></param>
[Obsolete("The float[] version of OnActionReceived has been deprecated, please use the ActionBuffers version instead.")]
public virtual void OnActionReceived(float[] vectorAction) { }

/// <summary>
Expand All @@ -36,7 +39,7 @@ public virtual void OnActionReceived(float[] vectorAction) { }
/// The last action that was decided by the Agent (or null if no decision has been made).
/// </returns>
/// <seealso cref="OnActionReceived(ActionBuffers)"/>
// [Obsolete("GetAction has been deprecated, please use GetStoredActionBuffers, Or GetStoredDiscreteActions.")]
[Obsolete("GetAction has been deprecated, please use GetStoredActionBuffers instead.")]
public float[] GetAction()
{
var storedAction = m_Info.storedVectorActions;
Expand Down
3 changes: 3 additions & 0 deletions com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public static List<ObservationSummary> GetObservationSummaries(this AgentInfoAct
/// <param name="isTraining">Whether or not the Brain is training.</param>
public static BrainParametersProto ToProto(this BrainParameters bp, string name, bool isTraining)
{
// Disable deprecation warnings so we can set legacy fields
#pragma warning disable CS0618
var brainParametersProto = new BrainParametersProto
{
VectorActionSpaceTypeDeprecated = (SpaceTypeProto)bp.VectorActionSpaceType,
Expand All @@ -116,6 +118,7 @@ public static BrainParametersProto ToProto(this BrainParameters bp, string name,
{
brainParametersProto.VectorActionDescriptionsDeprecated.AddRange(bp.VectorActionDescriptions);
}
#pragma warning restore CS0618
return brainParametersProto;
}

Expand Down
16 changes: 13 additions & 3 deletions com.unity.ml-agents/Runtime/Policies/BrainParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ActionSpec ActionSpec
/// the action.
/// For the discrete action space: the number of branches in the action space.
/// </value>
/// [Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")]
[Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")]
[FormerlySerializedAs("vectorActionSize")]
public int[] VectorActionSize = new[] { 1 };

Expand All @@ -91,7 +91,7 @@ public ActionSpec ActionSpec
/// <summary>
/// (Deprecated) Defines if the action is discrete or continuous.
/// </summary>
/// [Obsolete("VectorActionSpaceType has been deprecated, please use ActionSpec instead.")]
[Obsolete("VectorActionSpaceType has been deprecated, please use ActionSpec instead.")]
[FormerlySerializedAs("vectorActionSpaceType")]
public SpaceType VectorActionSpaceType = SpaceType.Discrete;

Expand All @@ -102,7 +102,7 @@ public ActionSpec ActionSpec
/// <summary>
/// (Deprecated) The number of actions specified by this Brain.
/// </summary>
/// [Obsolete("NumActions has been deprecated, please use ActionSpec instead.")]
[Obsolete("NumActions has been deprecated, please use ActionSpec instead.")]
public int NumActions
{
get
Expand All @@ -117,6 +117,8 @@ public int NumActions
/// <returns> A new BrainParameter object with the same values as the original.</returns>
public BrainParameters Clone()
{
// Disable deprecation warnings so we can read/write the old fields.
#pragma warning disable CS0618
return new BrainParameters
{
VectorObservationSize = VectorObservationSize,
Expand All @@ -126,13 +128,16 @@ public BrainParameters Clone()
VectorActionSize = (int[])VectorActionSize.Clone(),
VectorActionSpaceType = VectorActionSpaceType,
};
#pragma warning restore CS0618
}

/// <summary>
/// Propogate ActionSpec fields from deprecated fields
/// </summary>
private void UpdateToActionSpec()
{
// Disable deprecation warnings so we can read the old fields.
#pragma warning disable CS0618
if (!hasUpgradedBrainParametersWithActionSpec
&& m_ActionSpec.NumContinuousActions == 0
&& m_ActionSpec.BranchSizes == null)
Expand All @@ -149,13 +154,17 @@ private void UpdateToActionSpec()
}
}
hasUpgradedBrainParametersWithActionSpec = true;
#pragma warning restore CS0618
}

/// <summary>
/// Sync values in ActionSpec fields to deprecated fields
/// </summary>
private void SyncDeprecatedActionFields()
{
// Disable deprecation warnings so we can read the old fields.
#pragma warning disable CS0618

if (m_ActionSpec.NumContinuousActions == 0)
{
VectorActionSize = (int[])ActionSpec.BranchSizes.Clone();
Expand All @@ -170,6 +179,7 @@ private void SyncDeprecatedActionFields()
{
VectorActionSize = null;
}
#pragma warning restore CS0618
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,16 @@ public void TestHeuristicPolicyStepsSensors()
Assert.AreEqual(numSteps, agent1.sensor1.numWriteCalls);
Assert.AreEqual(numSteps, agent1.sensor2.numCompressedCalls);

// Disable deprecation warnings so we can read/write the old fields.
#pragma warning disable CS0618

// Make sure the Heuristic method read the observation and set the action
Assert.AreEqual(agent1.collectObservationsCallsForEpisode, agent1.GetAction()[0]);
Assert.AreEqual(
agent1.collectObservationsCallsForEpisode,
agent1.GetStoredActionBuffers().ContinuousActions[0]
);
#pragma warning restore CS0618
}
}

Expand Down