| | using System.Collections; |
| | using System.Collections.Generic; |
| | using Unity.MLAgents; |
| | using Unity.MLAgents.Policies; |
| | using Unity.MLAgents.Sensors; |
| | using Unity.MLAgents.Sensors.Reflection; |
| | using NUnit.Framework; |
| | using Unity.MLAgents.Actuators; |
| | using UnityEngine; |
| | using UnityEngine.TestTools; |
| |
|
| | namespace Tests |
| | { |
| | public class PublicApiAgent : Agent |
| | { |
| | public int numHeuristicCalls; |
| |
|
| | [Observable] |
| | public float ObservableFloat; |
| |
|
| | public override void Heuristic(in ActionBuffers actionsOut) |
| | { |
| | numHeuristicCalls++; |
| | base.Heuristic(actionsOut); |
| | } |
| | } |
| |
|
| | |
| | public class StackingComponent : SensorComponent |
| | { |
| | public SensorComponent wrappedComponent; |
| | public int numStacks; |
| |
|
| | public override ISensor[] CreateSensors() |
| | { |
| | var wrappedSensors = wrappedComponent.CreateSensors(); |
| | var sensorsOut = new ISensor[wrappedSensors.Length]; |
| | for (var i = 0; i < wrappedSensors.Length; i++) |
| | { |
| | sensorsOut[i] = new StackingSensor(wrappedSensors[i], numStacks); |
| | } |
| |
|
| | return sensorsOut; |
| | } |
| | } |
| |
|
| | public class RuntimeApiTest |
| | { |
| | [SetUp] |
| | public static void Setup() |
| | { |
| | if (Academy.IsInitialized) |
| | { |
| | Academy.Instance.Dispose(); |
| | } |
| | Academy.Instance.AutomaticSteppingEnabled = false; |
| | } |
| |
|
| | [UnityTest] |
| | public IEnumerator RuntimeApiTestWithEnumeratorPasses() |
| | { |
| | Academy.Instance.InferenceSeed = 1337; |
| | var gameObject = new GameObject(); |
| |
|
| | var behaviorParams = gameObject.AddComponent<BehaviorParameters>(); |
| | behaviorParams.BrainParameters.VectorObservationSize = 3; |
| | behaviorParams.BrainParameters.NumStackedVectorObservations = 2; |
| | behaviorParams.BrainParameters.VectorActionDescriptions = new[] { "Continuous1", "TestActionA", "TestActionB" }; |
| | behaviorParams.BrainParameters.ActionSpec = new ActionSpec(1, new[] { 2, 2 }); |
| | behaviorParams.BehaviorName = "TestBehavior"; |
| | behaviorParams.TeamId = 42; |
| | behaviorParams.UseChildSensors = true; |
| | behaviorParams.DeterministicInference = false; |
| | behaviorParams.ObservableAttributeHandling = ObservableAttributeOptions.ExamineAll; |
| |
|
| |
|
| | |
| | behaviorParams.BehaviorType = BehaviorType.Default; |
| | #if MLA_UNITY_PHYSICS_MODULE |
| | var sensorComponent = gameObject.AddComponent<RayPerceptionSensorComponent3D>(); |
| | sensorComponent.SensorName = "ray3d"; |
| | sensorComponent.DetectableTags = new List<string> { "Player", "Respawn" }; |
| | sensorComponent.RaysPerDirection = 3; |
| |
|
| | |
| | |
| | var wrappingSensorComponent = gameObject.AddComponent<StackingComponent>(); |
| | wrappingSensorComponent.wrappedComponent = sensorComponent; |
| | wrappingSensorComponent.numStacks = 3; |
| |
|
| | |
| | Assert.IsNull(sensorComponent.RaySensor); |
| | #endif |
| |
|
| |
|
| | |
| | |
| | behaviorParams.BehaviorType = BehaviorType.HeuristicOnly; |
| |
|
| | |
| | var agent = gameObject.AddComponent<PublicApiAgent>(); |
| |
|
| | |
| | var decisionRequester = gameObject.AddComponent<DecisionRequester>(); |
| | decisionRequester.DecisionPeriod = 2; |
| | decisionRequester.TakeActionsBetweenDecisions = true; |
| |
|
| | #if MLA_UNITY_PHYSICS_MODULE |
| | |
| | Assert.IsNotNull(sensorComponent.RaySensor); |
| | #endif |
| | |
| | var otherDevice = behaviorParams.InferenceDevice == InferenceDevice.CPU ? InferenceDevice.GPU : InferenceDevice.CPU; |
| | agent.SetModel(behaviorParams.BehaviorName, behaviorParams.Model, otherDevice); |
| |
|
| | agent.AddReward(1.0f); |
| |
|
| | |
| | yield return null; |
| |
|
| | Academy.Instance.EnvironmentStep(); |
| |
|
| | var actions = agent.GetStoredActionBuffers().DiscreteActions; |
| | |
| | Assert.AreEqual(new ActionSegment<int>(new[] { 0, 0 }), actions); |
| | Assert.AreEqual(1, agent.numHeuristicCalls); |
| |
|
| | Academy.Instance.EnvironmentStep(); |
| | Assert.AreEqual(1, agent.numHeuristicCalls); |
| |
|
| | Academy.Instance.EnvironmentStep(); |
| | Assert.AreEqual(2, agent.numHeuristicCalls); |
| | } |
| | } |
| | } |
| |
|