From a13f595c05ce40dd26204e99bf4bf351cad4b20f Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 11 Mar 2020 17:34:16 -0700 Subject: [PATCH 1/3] Improve warnings and exception if using unsupported combo --- .../Editor/BehaviorParametersEditor.cs | 3 +- .../Inference/BarracudaModelParamLoader.cs | 16 +++++++--- .../Runtime/Policies/BehaviorParameters.cs | 10 +++++++ .../Tests/Editor/BehaviorParameterTests.cs | 29 +++++++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs diff --git a/com.unity.ml-agents/Editor/BehaviorParametersEditor.cs b/com.unity.ml-agents/Editor/BehaviorParametersEditor.cs index e5069a5102..90a6f8a5b9 100644 --- a/com.unity.ml-agents/Editor/BehaviorParametersEditor.cs +++ b/com.unity.ml-agents/Editor/BehaviorParametersEditor.cs @@ -106,7 +106,8 @@ void DisplayFailedModelChecks() if (brainParameters != null) { var failedChecks = Inference.BarracudaModelParamLoader.CheckModel( - barracudaModel, brainParameters, sensorComponents); + barracudaModel, brainParameters, sensorComponents, behaviorParameters.behaviorType + ); foreach (var check in failedChecks) { if (check != null) diff --git a/com.unity.ml-agents/Runtime/Inference/BarracudaModelParamLoader.cs b/com.unity.ml-agents/Runtime/Inference/BarracudaModelParamLoader.cs index 019378c7df..54cdbcbaaa 100644 --- a/com.unity.ml-agents/Runtime/Inference/BarracudaModelParamLoader.cs +++ b/com.unity.ml-agents/Runtime/Inference/BarracudaModelParamLoader.cs @@ -127,14 +127,22 @@ public static string[] GetOutputNames(Model model) /// /// Attached sensor components /// The list the error messages of the checks that failed - public static IEnumerable CheckModel(Model model, BrainParameters brainParameters, SensorComponent[] sensorComponents) + public static IEnumerable CheckModel(Model model, BrainParameters brainParameters, + SensorComponent[] sensorComponents, BehaviorType behaviorType = BehaviorType.Default) { List failedModelChecks = new List(); if (model == null) { - failedModelChecks.Add( - "There is no model for this Brain, cannot run inference. " + - "(But can still train)"); + var errorMsg = "There is no model for this Brain; cannot run inference. "; + if (behaviorType == BehaviorType.InferenceOnly) + { + errorMsg += "Either assign a model, or change to a different Behavior Type."; + } + else + { + errorMsg += "(But can still train)"; + } + failedModelChecks.Add(errorMsg); return failedModelChecks; } diff --git a/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs b/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs index 9f9b8a08ad..20d01362e6 100644 --- a/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs +++ b/com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs @@ -142,7 +142,17 @@ internal IPolicy GeneratePolicy(Func heuristic) case BehaviorType.HeuristicOnly: return new HeuristicPolicy(heuristic); case BehaviorType.InferenceOnly: + { + if (m_Model == null) + { + var behaviorType = BehaviorType.InferenceOnly.ToString(); + throw new UnityAgentsException( + $"Can't use Behavior Type {behaviorType} without a model. " + + "Either assign a model, or change to a different Behavior Type." + ); + } return new BarracudaPolicy(m_BrainParameters, m_Model, m_InferenceDevice); + } case BehaviorType.Default: if (Academy.Instance.IsCommunicatorOn) { diff --git a/com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs b/com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs new file mode 100644 index 0000000000..136c7ce964 --- /dev/null +++ b/com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs @@ -0,0 +1,29 @@ +using NUnit.Framework; +using UnityEngine; +using MLAgents; +using MLAgents.Policies; + +namespace MLAgents.Tests +{ + [TestFixture] + public class BehaviorParameterTests + { + static float[] DummyHeuristic() + { + return null; + } + + [Test] + public void TestNoModelInferenceOnlyThrows() + { + var gameObj = new GameObject(); + var bp = gameObj.AddComponent(); + bp.behaviorType = BehaviorType.InferenceOnly; + + Assert.Throws(() => + { + bp.GeneratePolicy(DummyHeuristic); + }); + } + } +} From 1cec3207bf933a7a11e3303b547f089cf4a3d07e Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Wed, 11 Mar 2020 17:34:44 -0700 Subject: [PATCH 2/3] add meta file --- .../Tests/Editor/BehaviorParameterTests.cs.meta | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs.meta diff --git a/com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs.meta b/com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs.meta new file mode 100644 index 0000000000..0656104d8c --- /dev/null +++ b/com.unity.ml-agents/Tests/Editor/BehaviorParameterTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 877266b9e1bfe4330a68ab5f2da1836b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 69e1684c89700453aac64634b44bfa28fee56dad Mon Sep 17 00:00:00 2001 From: Chris Elion Date: Thu, 12 Mar 2020 10:39:42 -0700 Subject: [PATCH 3/3] fix unit test --- .../Tests/Editor/PublicAPI/PublicApiValidation.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/com.unity.ml-agents/Tests/Editor/PublicAPI/PublicApiValidation.cs b/com.unity.ml-agents/Tests/Editor/PublicAPI/PublicApiValidation.cs index 3f1102b2f6..1608a9d93e 100644 --- a/com.unity.ml-agents/Tests/Editor/PublicAPI/PublicApiValidation.cs +++ b/com.unity.ml-agents/Tests/Editor/PublicAPI/PublicApiValidation.cs @@ -102,6 +102,8 @@ public void CheckSetupAgent() var agent = gameObject.AddComponent(); // Make sure we can set the behavior type correctly after the agent is added behaviorParams.behaviorType = BehaviorType.InferenceOnly; + // Can't actually create an Agent with InferenceOnly and no model, so change back + behaviorParams.behaviorType = BehaviorType.Default; // TODO - not internal yet // var decisionRequester = gameObject.AddComponent();