using NUnit.Framework; using Unity.Barracuda; using Unity.MLAgents.Actuators; using UnityEngine; using Unity.MLAgents.Policies; using UnityEditor; using UnityEngine.TestTools; namespace Unity.MLAgents.Tests { [TestFixture] public class BehaviorParameterTests : IHeuristicProvider { const string k_continuousONNXPath = "Packages/com.unity.ml-agents/Tests/Editor/TestModels/continuous2vis8vec2action.onnx"; public void Heuristic(in ActionBuffers actionsOut) { // No-op } [Test] public void TestNoModelInferenceOnlyThrows() { var gameObj = new GameObject(); var bp = gameObj.AddComponent(); bp.BehaviorType = BehaviorType.InferenceOnly; var actionSpec = new ActionSpec(); Assert.Throws(() => { bp.GeneratePolicy(actionSpec, new ActuatorManager()); }); } [Test] public void TestIsInHeuristicMode() { var gameObj = new GameObject(); var bp = gameObj.AddComponent(); bp.Model = null; gameObj.AddComponent(); bp.BehaviorType = BehaviorType.HeuristicOnly; Assert.IsTrue(bp.IsInHeuristicMode()); bp.BehaviorType = BehaviorType.Default; Assert.IsTrue(bp.IsInHeuristicMode()); bp.Model = ScriptableObject.CreateInstance(); Assert.IsFalse(bp.IsInHeuristicMode()); } [Test] public void TestPolicyUpdateEventFired() { var gameObj = new GameObject(); var bp = gameObj.AddComponent(); gameObj.AddComponent().LazyInitialize(); bp.OnPolicyUpdated += delegate (bool isInHeuristicMode) { Debug.Log($"OnPolicyChanged:{isInHeuristicMode}"); }; bp.BehaviorType = BehaviorType.HeuristicOnly; LogAssert.Expect(LogType.Log, $"OnPolicyChanged:{true}"); bp.BehaviorType = BehaviorType.Default; LogAssert.Expect(LogType.Log, $"OnPolicyChanged:{true}"); Assert.Throws(() => { bp.BehaviorType = BehaviorType.InferenceOnly; }); bp.Model = AssetDatabase.LoadAssetAtPath(k_continuousONNXPath); LogAssert.Expect(LogType.Log, $"OnPolicyChanged:{false}"); bp.BehaviorType = BehaviorType.HeuristicOnly; LogAssert.Expect(LogType.Log, $"OnPolicyChanged:{true}"); } } }