|
4 | 4 | using MLAgents.Sensors;
|
5 | 5 | using NUnit.Framework;
|
6 | 6 | using UnityEngine;
|
| 7 | +using UnityEngine.TestTools; |
7 | 8 |
|
8 | 9 | namespace MLAgentsExamples
|
9 | 10 | {
|
@@ -71,106 +72,5 @@ public void CheckSetupRayPerceptionSensorComponent()
|
71 | 72 |
|
72 | 73 | sensorComponent.CreateSensor();
|
73 | 74 | }
|
74 |
| - |
75 |
| - class PublicApiAgent : Agent |
76 |
| - { |
77 |
| - public int numHeuristicCalls; |
78 |
| - |
79 |
| - public override float[] Heuristic() |
80 |
| - { |
81 |
| - numHeuristicCalls++; |
82 |
| - return base.Heuristic(); |
83 |
| - } |
84 |
| - } |
85 |
| - |
86 |
| - // Simple SensorComponent that sets up a StackingSensor |
87 |
| - class StackingComponent : SensorComponent |
88 |
| - { |
89 |
| - public SensorComponent wrappedComponent; |
90 |
| - public int numStacks; |
91 |
| - |
92 |
| - public override ISensor CreateSensor() |
93 |
| - { |
94 |
| - var wrappedSensor = wrappedComponent.CreateSensor(); |
95 |
| - return new StackingSensor(wrappedSensor, numStacks); |
96 |
| - } |
97 |
| - |
98 |
| - public override int[] GetObservationShape() |
99 |
| - { |
100 |
| - int[] shape = (int[]) wrappedComponent.GetObservationShape().Clone(); |
101 |
| - for (var i = 0; i < shape.Length; i++) |
102 |
| - { |
103 |
| - shape[i] *= numStacks; |
104 |
| - } |
105 |
| - |
106 |
| - return shape; |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - |
111 |
| - [Test] |
112 |
| - public void CheckSetupAgent() |
113 |
| - { |
114 |
| - var gameObject = new GameObject(); |
115 |
| - |
116 |
| - var behaviorParams = gameObject.AddComponent<BehaviorParameters>(); |
117 |
| - behaviorParams.brainParameters.vectorObservationSize = 3; |
118 |
| - behaviorParams.brainParameters.numStackedVectorObservations = 2; |
119 |
| - behaviorParams.brainParameters.vectorActionDescriptions = new[] { "TestActionA", "TestActionB" }; |
120 |
| - behaviorParams.brainParameters.vectorActionSize = new[] { 2, 2 }; |
121 |
| - behaviorParams.brainParameters.vectorActionSpaceType = SpaceType.Discrete; |
122 |
| - behaviorParams.behaviorName = "TestBehavior"; |
123 |
| - behaviorParams.TeamId = 42; |
124 |
| - behaviorParams.useChildSensors = true; |
125 |
| - |
126 |
| - var agent = gameObject.AddComponent<PublicApiAgent>(); |
127 |
| - // Make sure we can set the behavior type correctly after the agent is added |
128 |
| - behaviorParams.behaviorType = BehaviorType.InferenceOnly; |
129 |
| - // Can't actually create an Agent with InferenceOnly and no model, so change back |
130 |
| - behaviorParams.behaviorType = BehaviorType.Default; |
131 |
| - |
132 |
| - // TODO - not internal yet |
133 |
| - // var decisionRequester = gameObject.AddComponent<DecisionRequester>(); |
134 |
| - // decisionRequester.DecisionPeriod = 2; |
135 |
| - |
136 |
| - var sensorComponent = gameObject.AddComponent<RayPerceptionSensorComponent3D>(); |
137 |
| - sensorComponent.sensorName = "ray3d"; |
138 |
| - sensorComponent.detectableTags = new List<string> { "Player", "Respawn" }; |
139 |
| - sensorComponent.raysPerDirection = 3; |
140 |
| - |
141 |
| - // Make a StackingSensor that wraps the RayPerceptionSensorComponent3D |
142 |
| - // This isn't necessarily practical, just to ensure that it can be done |
143 |
| - var wrappingSensorComponent = gameObject.AddComponent<StackingComponent>(); |
144 |
| - wrappingSensorComponent.wrappedComponent = sensorComponent; |
145 |
| - wrappingSensorComponent.numStacks = 3; |
146 |
| - |
147 |
| - // ISensor isn't set up yet. |
148 |
| - Assert.IsNull(sensorComponent.raySensor); |
149 |
| - |
150 |
| - agent.LazyInitialize(); |
151 |
| - // Make sure we can set the behavior type correctly after the agent is initialized |
152 |
| - // (this creates a new policy). |
153 |
| - behaviorParams.behaviorType = BehaviorType.HeuristicOnly; |
154 |
| - |
155 |
| - // Initialization should set up the sensors |
156 |
| - Assert.IsNotNull(sensorComponent.raySensor); |
157 |
| - |
158 |
| - // Let's change the inference device |
159 |
| - var otherDevice = behaviorParams.inferenceDevice == InferenceDevice.CPU ? InferenceDevice.GPU : InferenceDevice.CPU; |
160 |
| - agent.SetModel(behaviorParams.behaviorName, behaviorParams.model, otherDevice); |
161 |
| - |
162 |
| - agent.AddReward(1.0f); |
163 |
| - |
164 |
| - agent.RequestAction(); |
165 |
| - agent.RequestDecision(); |
166 |
| - |
167 |
| - Academy.Instance.AutomaticSteppingEnabled = false; |
168 |
| - Academy.Instance.EnvironmentStep(); |
169 |
| - |
170 |
| - var actions = agent.GetAction(); |
171 |
| - // default Heuristic implementation should return zero actions. |
172 |
| - Assert.AreEqual(new[] {0.0f, 0.0f}, actions); |
173 |
| - Assert.AreEqual(1, agent.numHeuristicCalls); |
174 |
| - } |
175 | 75 | }
|
176 | 76 | }
|
0 commit comments