|
| 1 | +using NUnit.Framework; |
| 2 | +using Unity.MLAgents.Actuators; |
| 3 | +using Unity.MLAgents.Policies; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace Unity.MLAgents.Tests.Policies |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class HeuristicPolicyTest |
| 10 | + { |
| 11 | + [SetUp] |
| 12 | + public void SetUp() |
| 13 | + { |
| 14 | + if (Academy.IsInitialized) |
| 15 | + { |
| 16 | + Academy.Instance.Dispose(); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Assert that the action buffers are initialized to zero, and then set them to non-zero values. |
| 22 | + /// </summary> |
| 23 | + /// <param name="actionsOut"></param> |
| 24 | + static void CheckAndSetBuffer(in ActionBuffers actionsOut) |
| 25 | + { |
| 26 | + var continuousActions = actionsOut.ContinuousActions; |
| 27 | + for (var continuousIndex = 0; continuousIndex < continuousActions.Length; continuousIndex++) |
| 28 | + { |
| 29 | + Assert.AreEqual(continuousActions[continuousIndex], 0.0f); |
| 30 | + continuousActions[continuousIndex] = 1.0f; |
| 31 | + } |
| 32 | + |
| 33 | + var discreteActions = actionsOut.DiscreteActions; |
| 34 | + for (var discreteIndex = 0; discreteIndex < discreteActions.Length; discreteIndex++) |
| 35 | + { |
| 36 | + Assert.AreEqual(discreteActions[discreteIndex], 0); |
| 37 | + discreteActions[discreteIndex] = 1; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + class ActionClearedAgent : Agent |
| 43 | + { |
| 44 | + public int HeuristicCalls = 0; |
| 45 | + public override void Heuristic(in ActionBuffers actionsOut) |
| 46 | + { |
| 47 | + CheckAndSetBuffer(actionsOut); |
| 48 | + HeuristicCalls++; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + class ActionClearedActuator : IActuator |
| 53 | + { |
| 54 | + public int HeuristicCalls = 0; |
| 55 | + public ActionClearedActuator(ActionSpec actionSpec) |
| 56 | + { |
| 57 | + ActionSpec = actionSpec; |
| 58 | + Name = GetType().Name; |
| 59 | + } |
| 60 | + |
| 61 | + public void OnActionReceived(ActionBuffers actionBuffers) |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + public void WriteDiscreteActionMask(IDiscreteActionMask actionMask) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + public void Heuristic(in ActionBuffers actionBuffersOut) |
| 70 | + { |
| 71 | + CheckAndSetBuffer(actionBuffersOut); |
| 72 | + HeuristicCalls++; |
| 73 | + } |
| 74 | + |
| 75 | + public ActionSpec ActionSpec { get; } |
| 76 | + public string Name { get; } |
| 77 | + |
| 78 | + public void ResetData() |
| 79 | + { |
| 80 | + |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + class ActionClearedActuatorComponent : ActuatorComponent |
| 85 | + { |
| 86 | + public ActionClearedActuator ActionClearedActuator; |
| 87 | + public ActionClearedActuatorComponent() |
| 88 | + { |
| 89 | + ActionSpec = new ActionSpec(2, new[] { 3, 3 }); |
| 90 | + } |
| 91 | + |
| 92 | + public override IActuator[] CreateActuators() |
| 93 | + { |
| 94 | + ActionClearedActuator = new ActionClearedActuator(ActionSpec); |
| 95 | + return new IActuator[] { ActionClearedActuator }; |
| 96 | + } |
| 97 | + |
| 98 | + public override ActionSpec ActionSpec { get; } |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void TestActionsCleared() |
| 103 | + { |
| 104 | + var gameObj = new GameObject(); |
| 105 | + var agent = gameObj.AddComponent<ActionClearedAgent>(); |
| 106 | + var behaviorParameters = agent.GetComponent<BehaviorParameters>(); |
| 107 | + behaviorParameters.BrainParameters.ActionSpec = new ActionSpec(1, new[] { 4 }); |
| 108 | + behaviorParameters.BrainParameters.VectorObservationSize = 0; |
| 109 | + behaviorParameters.BehaviorType = BehaviorType.HeuristicOnly; |
| 110 | + |
| 111 | + var actuatorComponent = gameObj.AddComponent<ActionClearedActuatorComponent>(); |
| 112 | + agent.LazyInitialize(); |
| 113 | + |
| 114 | + const int k_NumSteps = 5; |
| 115 | + for (var i = 0; i < k_NumSteps; i++) |
| 116 | + { |
| 117 | + agent.RequestDecision(); |
| 118 | + Academy.Instance.EnvironmentStep(); |
| 119 | + } |
| 120 | + |
| 121 | + Assert.AreEqual(agent.HeuristicCalls, k_NumSteps); |
| 122 | + Assert.AreEqual(actuatorComponent.ActionClearedActuator.HeuristicCalls, k_NumSteps); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments