|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create an <see cref="ActionBuffers"/> instance with ActionSpec and all actions stored as a float array.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="actionSpec"><see cref="ActionSpec"/> of the <see cref="ActionBuffers"/></param>
|
|
|
|
/// <param name="actions">The float array of all actions, including discrete and continuous actions.</param>
|
|
|
|
/// <returns>An <see cref="ActionBuffers"/> instance initialized with a <see cref="ActionSpec"/> and a float array.
|
|
|
|
internal static ActionBuffers FromActionSpec(ActionSpec actionSpec, float[] actions) |
|
|
|
{ |
|
|
|
if (actions == null) |
|
|
|
{ |
|
|
|
return new ActionBuffers(ActionSegment<float>.Empty, ActionSegment<int>.Empty); |
|
|
|
} |
|
|
|
|
|
|
|
Debug.Assert(actions.Length == actionSpec.NumContinuousActions + actionSpec.NumDiscreteActions, |
|
|
|
$"The length of '{nameof(actions)}' does not match the total size of ActionSpec.\n" + |
|
|
|
$"{nameof(actions)}.Length: {actions.Length}\n" + |
|
|
|
$"{nameof(actionSpec)}: {actionSpec.NumContinuousActions + actionSpec.NumDiscreteActions}"); |
|
|
|
|
|
|
|
ActionSegment<float> continuousActionSegment = ActionSegment<float>.Empty; |
|
|
|
ActionSegment<int> discreteActionSegment = ActionSegment<int>.Empty; |
|
|
|
int offset = 0; |
|
|
|
if (actionSpec.NumContinuousActions > 0) |
|
|
|
{ |
|
|
|
continuousActionSegment = new ActionSegment<float>(actions, 0, actionSpec.NumContinuousActions); |
|
|
|
offset += actionSpec.NumContinuousActions; |
|
|
|
} |
|
|
|
if (actionSpec.NumDiscreteActions > 0) |
|
|
|
{ |
|
|
|
int[] discreteActions = new int[actionSpec.NumDiscreteActions]; |
|
|
|
for (var i = 0; i < actionSpec.NumDiscreteActions; i++) |
|
|
|
{ |
|
|
|
discreteActions[i] = (int)actions[i + offset]; |
|
|
|
|
|
|
|
} |
|
|
|
discreteActionSegment = new ActionSegment<int>(discreteActions); |
|
|
|
} |
|
|
|
|
|
|
|
return new ActionBuffers(continuousActionSegment, discreteActionSegment); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Clear the <see cref="ContinuousActions"/> and <see cref="DiscreteActions"/> segments to be all zeros.
|
|
|
|
/// </summary>
|
|
|
|
public void Clear() |
|
|
|