|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using Unity.Barracuda; |
|
|
|
using Unity.MLAgents.Actuators; |
|
|
|
using Unity.MLAgents.Sensors; |
|
|
|
using Unity.MLAgents.Policies; |
|
|
|
|
|
|
|
|
|
|
/// The BrainParameters that are used verify the compatibility with the InferenceEngine
|
|
|
|
/// </param>
|
|
|
|
/// <param name="sensorComponents">Attached sensor components</param>
|
|
|
|
/// <param name="actuatorComponents">Attached actuator components</param>
|
|
|
|
SensorComponent[] sensorComponents, int observableAttributeTotalSize = 0, |
|
|
|
SensorComponent[] sensorComponents, ActuatorComponent[] actuatorComponents, |
|
|
|
int observableAttributeTotalSize = 0, |
|
|
|
BehaviorType behaviorType = BehaviorType.Default) |
|
|
|
{ |
|
|
|
List<string> failedModelChecks = new List<string>(); |
|
|
|
|
|
|
return failedModelChecks; |
|
|
|
} |
|
|
|
|
|
|
|
var modelDiscreteActionSize = isContinuous == ModelActionType.Discrete ? actionSize : 0; |
|
|
|
var modelContinuousActionSize = isContinuous == ModelActionType.Continuous ? actionSize : 0; |
|
|
|
|
|
|
|
failedModelChecks.AddRange( |
|
|
|
CheckIntScalarPresenceHelper(new Dictionary<string, int>() |
|
|
|
{ |
|
|
|
|
|
|
CheckInputTensorShape(model, brainParameters, sensorComponents, observableAttributeTotalSize) |
|
|
|
); |
|
|
|
failedModelChecks.AddRange( |
|
|
|
CheckOutputTensorShape(model, brainParameters, isContinuous, actionSize) |
|
|
|
CheckOutputTensorShape(model, brainParameters, actuatorComponents, isContinuous, modelContinuousActionSize, modelDiscreteActionSize) |
|
|
|
); |
|
|
|
return failedModelChecks; |
|
|
|
} |
|
|
|
|
|
|
/// <param name="brainParameters">
|
|
|
|
/// The BrainParameters that are used verify the compatibility with the InferenceEngine
|
|
|
|
/// </param>
|
|
|
|
/// <param name="actuatorComponents">Array of attached actuator components.</param>
|
|
|
|
/// <param name="modelActionSize">
|
|
|
|
/// The size of the action output that is expected by the model.
|
|
|
|
/// <param name="modelContinuousActionSize">
|
|
|
|
/// The size of the continuous action output that is expected by the model.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="modelSumDiscreteBranchSizes">
|
|
|
|
/// The size of the discrete action output that is expected by the model.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// A IEnumerable of string corresponding to the incompatible shapes between model
|
|
|
|
|
|
|
Model model, |
|
|
|
BrainParameters brainParameters, |
|
|
|
ActuatorComponent[] actuatorComponents, |
|
|
|
int modelActionSize) |
|
|
|
int modelContinuousActionSize, int modelSumDiscreteBranchSizes) |
|
|
|
{ |
|
|
|
var failedModelChecks = new List<string>(); |
|
|
|
if (isContinuous == ModelActionType.Unknown) |
|
|
|
|
|
|
"suggest Continuous Control."); |
|
|
|
return failedModelChecks; |
|
|
|
} |
|
|
|
var tensorTester = new Dictionary<string, Func<BrainParameters, TensorShape?, int, string>>(); |
|
|
|
if (brainParameters.VectorActionSpaceType == SpaceType.Continuous) |
|
|
|
var tensorTester = new Dictionary<string, Func<BrainParameters, ActuatorComponent[], TensorShape?, int, int, string>>(); |
|
|
|
|
|
|
|
// This will need to change a bit for hybrid action spaces.
|
|
|
|
if (isContinuous == ModelActionType.Continuous) |
|
|
|
{ |
|
|
|
tensorTester[TensorNames.ActionOutput] = CheckContinuousActionOutputShape; |
|
|
|
} |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Func<BrainParameters, TensorShape?, int, string> tester = tensorTester[name]; |
|
|
|
var error = tester.Invoke(brainParameters, model.GetShapeByName(name), modelActionSize); |
|
|
|
var tester = tensorTester[name]; |
|
|
|
var error = tester.Invoke(brainParameters, actuatorComponents, model.GetShapeByName(name), modelContinuousActionSize, modelSumDiscreteBranchSizes); |
|
|
|
if (error != null) |
|
|
|
{ |
|
|
|
failedModelChecks.Add(error); |
|
|
|
|
|
|
/// <param name="brainParameters">
|
|
|
|
/// The BrainParameters that are used verify the compatibility with the InferenceEngine
|
|
|
|
/// </param>
|
|
|
|
/// <param name="actuatorComponents">Array of attached actuator components.</param>
|
|
|
|
/// <param name="modelActionSize">
|
|
|
|
/// The size of the action output that is expected by the model.
|
|
|
|
/// <param name="modelContinuousActionSize">
|
|
|
|
/// The size of the continuous action output that is expected by the model.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="modelSumDiscreteBranchSizes">
|
|
|
|
/// The size of the discrete action output that is expected by the model.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// If the Check failed, returns a string containing information about why the
|
|
|
|
|
|
|
BrainParameters brainParameters, TensorShape? shape, int modelActionSize) |
|
|
|
BrainParameters brainParameters, ActuatorComponent[] actuatorComponents, TensorShape? shape, int modelContinuousActionSize, int modelSumDiscreteBranchSizes) |
|
|
|
var bpActionSize = brainParameters.VectorActionSize.Sum(); |
|
|
|
if (modelActionSize != bpActionSize) |
|
|
|
var sumOfDiscreteBranchSizes = 0; |
|
|
|
if (brainParameters.VectorActionSpaceType == SpaceType.Discrete) |
|
|
|
return "Action Size of the model does not match. The BrainParameters expect " + |
|
|
|
$"{bpActionSize} but the model contains {modelActionSize}."; |
|
|
|
sumOfDiscreteBranchSizes += brainParameters.VectorActionSize.Sum(); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var actuatorComponent in actuatorComponents) |
|
|
|
{ |
|
|
|
var actionSpec = actuatorComponent.ActionSpec; |
|
|
|
sumOfDiscreteBranchSizes += actionSpec.SumOfDiscreteBranchSizes; |
|
|
|
} |
|
|
|
|
|
|
|
if (modelSumDiscreteBranchSizes != sumOfDiscreteBranchSizes) |
|
|
|
{ |
|
|
|
return "Discrete Action Size of the model does not match. The BrainParameters expect " + |
|
|
|
$"{sumOfDiscreteBranchSizes} but the model contains {modelSumDiscreteBranchSizes}."; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
/// <param name="brainParameters">
|
|
|
|
/// The BrainParameters that are used verify the compatibility with the InferenceEngine
|
|
|
|
/// </param>
|
|
|
|
/// <param name="actuatorComponents">Array of attached actuator components.</param>
|
|
|
|
/// <param name="modelActionSize">
|
|
|
|
/// The size of the action output that is expected by the model.
|
|
|
|
/// <param name="modelContinuousActionSize">
|
|
|
|
/// The size of the continuous action output that is expected by the model.
|
|
|
|
/// </param>
|
|
|
|
/// <param name="modelSumDiscreteBranchSizes">
|
|
|
|
/// The size of the discrete action output that is expected by the model.
|
|
|
|
BrainParameters brainParameters, TensorShape? shape, int modelActionSize) |
|
|
|
BrainParameters brainParameters, ActuatorComponent[] actuatorComponents, TensorShape? shape, int modelContinuousActionSize, int modelSumDiscreteBranchSizes) |
|
|
|
var bpActionSize = brainParameters.VectorActionSize[0]; |
|
|
|
if (modelActionSize != bpActionSize) |
|
|
|
var numContinuousActions = 0; |
|
|
|
if (brainParameters.VectorActionSpaceType == SpaceType.Continuous) |
|
|
|
return "Action Size of the model does not match. The BrainParameters expect " + |
|
|
|
$"{bpActionSize} but the model contains {modelActionSize}."; |
|
|
|
numContinuousActions += brainParameters.NumActions; |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var actuatorComponent in actuatorComponents) |
|
|
|
{ |
|
|
|
var actionSpec = actuatorComponent.ActionSpec; |
|
|
|
numContinuousActions += actionSpec.NumContinuousActions; |
|
|
|
} |
|
|
|
|
|
|
|
if (modelContinuousActionSize != numContinuousActions) |
|
|
|
{ |
|
|
|
return "Continuous Action Size of the model does not match. The BrainParameters and ActuatorComponents expect " + |
|
|
|
$"{numContinuousActions} but the model contains {modelContinuousActionSize}."; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |