浏览代码

[format] Format code whitespace with Unity Formatter. (#2550)

/develop-gpu-test
GitHub 5 年前
当前提交
88b917b3
共有 117 个文件被更改,包括 603 次插入737 次删除
  1. 9
      UnitySDK/Assets/ML-Agents/Editor/AgentEditor.cs
  2. 10
      UnitySDK/Assets/ML-Agents/Editor/BrainEditor.cs
  3. 58
      UnitySDK/Assets/ML-Agents/Editor/BrainParametersDrawer.cs
  4. 10
      UnitySDK/Assets/ML-Agents/Editor/BroadcastHubDrawer.cs
  5. 2
      UnitySDK/Assets/ML-Agents/Editor/Builder.cs
  6. 4
      UnitySDK/Assets/ML-Agents/Editor/BuilderUtils.cs
  7. 6
      UnitySDK/Assets/ML-Agents/Editor/DemonstrationDrawer.cs
  8. 2
      UnitySDK/Assets/ML-Agents/Editor/DemonstrationImporter.cs
  9. 6
      UnitySDK/Assets/ML-Agents/Editor/HeuristicBrainEditor.cs
  10. 14
      UnitySDK/Assets/ML-Agents/Editor/LearningBrainEditor.cs
  11. 20
      UnitySDK/Assets/ML-Agents/Editor/PlayerBrainEditor.cs
  12. 10
      UnitySDK/Assets/ML-Agents/Editor/ResetParameterDrawer.cs
  13. 22
      UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs
  14. 5
      UnitySDK/Assets/ML-Agents/Editor/Tests/DiscreteActionOutputApplierTest.cs
  15. 29
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestActionMasker.cs
  16. 16
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorApplier.cs
  17. 20
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorGenerator.cs
  18. 65
      UnitySDK/Assets/ML-Agents/Editor/Tests/MLAgentsEditModeTest.cs
  19. 2
      UnitySDK/Assets/ML-Agents/Editor/Tests/MultinomialTest.cs
  20. 2
      UnitySDK/Assets/ML-Agents/Editor/Tests/RandomNormalTest.cs
  21. 4
      UnitySDK/Assets/ML-Agents/Editor/Tests/RayPerceptionTests.cs
  22. 2
      UnitySDK/Assets/ML-Agents/Editor/Tests/TensorUtilsTest.cs
  23. 16
      UnitySDK/Assets/ML-Agents/Editor/Tests/UtilitiesTests.cs
  24. 3
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAcademy.cs
  25. 5
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs
  26. 16
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs
  27. 7
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
  28. 2
      UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAcademy.cs
  29. 12
      UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAgent.cs
  30. 10
      UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaArea.cs
  31. 29
      UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaLogic.cs
  32. 9
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAcademy.cs
  33. 30
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
  34. 2
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicDecision.cs
  35. 13
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAcademy.cs
  36. 33
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
  37. 20
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerBanana.cs
  38. 2
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAcademy.cs
  39. 16
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs
  40. 11
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAcademy.cs
  41. 16
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  42. 11
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs
  43. 9
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs
  44. 8
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/GoalDetect.cs
  45. 29
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs
  46. 20
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockAcademy.cs
  47. 5
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAcademy.cs
  48. 7
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs
  49. 24
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidArea.cs
  50. 3
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidSwitch.cs
  51. 7
      UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAcademy.cs
  52. 5
      UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs
  53. 15
      UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherDecision.cs
  54. 18
      UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherGoal.cs
  55. 10
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/AdjustTrainingTimescale.cs
  56. 7
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/Area.cs
  57. 4
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/CameraFollow.cs
  58. 11
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/FlyCamera.cs
  59. 2
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/GroundContact.cs
  60. 12
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/JointDriveController.cs
  61. 6
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RandomDecision.cs
  62. 35
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs
  63. 10
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception2D.cs
  64. 14
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs
  65. 2
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/TargetContact.cs
  66. 26
      UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
  67. 6
      UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerAcademy.cs
  68. 5
      UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerBallController.cs
  69. 13
      UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerFieldArea.cs
  70. 11
      UnitySDK/Assets/ML-Agents/Examples/Template/Scripts/TemplateAcademy.cs
  71. 14
      UnitySDK/Assets/ML-Agents/Examples/Template/Scripts/TemplateAgent.cs
  72. 3
      UnitySDK/Assets/ML-Agents/Examples/Template/Scripts/TemplateDecision.cs
  73. 24
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/HitWall.cs
  74. 4
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAcademy.cs
  75. 15
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs
  76. 14
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisArea.cs
  77. 2
      UnitySDK/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAcademy.cs
  78. 6
      UnitySDK/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs
  79. 18
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAcademy.cs
  80. 76
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs
  81. 8
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/Editor/BarracudaEditor/NNModelImporter.cs
  82. 12
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/OSX/MacBLAS.cs
  83. 10
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/iOS/iOSBLAS.cs
  84. 34
      UnitySDK/Assets/ML-Agents/Scripts/Academy.cs
  85. 21
      UnitySDK/Assets/ML-Agents/Scripts/ActionMasker.cs
  86. 15
      UnitySDK/Assets/ML-Agents/Scripts/Agent.cs
  87. 10
      UnitySDK/Assets/ML-Agents/Scripts/BCTeacherHelper.cs
  88. 2
      UnitySDK/Assets/ML-Agents/Scripts/Batcher.cs
  89. 9
      UnitySDK/Assets/ML-Agents/Scripts/Brain.cs
  90. 18
      UnitySDK/Assets/ML-Agents/Scripts/BrainParameters.cs
  91. 10
      UnitySDK/Assets/ML-Agents/Scripts/BroadcastHub.cs
  92. 15
      UnitySDK/Assets/ML-Agents/Scripts/Communicator.cs
  93. 10
      UnitySDK/Assets/ML-Agents/Scripts/Decision.cs
  94. 8
      UnitySDK/Assets/ML-Agents/Scripts/Demonstration.cs
  95. 8
      UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs
  96. 2
      UnitySDK/Assets/ML-Agents/Scripts/DemonstrationStore.cs
  97. 5
      UnitySDK/Assets/ML-Agents/Scripts/HeuristicBrain.cs
  98. 12
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ApplierImpl.cs
  99. 44
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs
  100. 6
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/GeneratorImpl.cs

9
UnitySDK/Assets/ML-Agents/Editor/AgentEditor.cs


using UnityEngine;
using UnityEngine;
using UnityEditor;
namespace MLAgents

[CanEditMultipleObjects]
public class AgentEditor : Editor
{
public override void OnInspectorGUI()
{
SerializedObject serializedAgent = serializedObject;

if (cameras.arraySize > 0 && renderTextures.arraySize > 0)
{
EditorGUILayout.HelpBox("Brain visual observations created by first getting all cameras then all render textures.", MessageType.Info);
EditorGUILayout.HelpBox("Brain visual observations created by first getting all cameras then all render textures.", MessageType.Info);
EditorGUILayout.LabelField("Agent Cameras");
for (int i = 0; i < cameras.arraySize; i++)
{

}
EditorGUILayout.EndHorizontal();
EditorGUILayout.LabelField("Agent RenderTextures");
for (int i = 0; i < renderTextures.arraySize; i++)
{

10
UnitySDK/Assets/ML-Agents/Editor/BrainEditor.cs


using UnityEngine;
using UnityEngine;
using UnityEditor;

/// </summary>
[CustomEditor(typeof(Brain))]
public class BrainEditor : Editor
{
{
var brain = (Brain) target;
var brain = (Brain)target;
var brainToCopy = EditorGUILayout.ObjectField(
"Copy Brain Parameters from : ", null, typeof(Brain), false) as Brain;
if (brainToCopy != null)

return;
}
var serializedBrain = serializedObject;
serializedBrain.Update();
serializedBrain.Update();
// Draws a horizontal thick line
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
}

58
UnitySDK/Assets/ML-Agents/Editor/BrainParametersDrawer.cs


using UnityEngine;
using UnityEngine;
using UnityEditor;
namespace MLAgents

private const string ActionTypePropName = "vectorActionSpaceType";
private const string ActionDescriptionPropName = "vectorActionDescriptions";
private const string VecObsPropName = "vectorObservationSize";
private const string NumVecObsPropName ="numStackedVectorObservations";
private const string NumVecObsPropName = "numStackedVectorObservations";
private const string CamWidthPropName = "width";
private const string CamHeightPropName = "height";
private const string CamGrayPropName = "blackAndWhite";

return LineHeight +
GetHeightDrawVectorObservation() +
GetHeightDrawVisualObservation(property) +
GetHeightDrawVectorAction(property) +
GetHeightDrawVectorActionDescriptions(property);
return LineHeight +
GetHeightDrawVectorObservation() +
GetHeightDrawVisualObservation(property) +
GetHeightDrawVectorAction(property) +
GetHeightDrawVectorActionDescriptions(property);
}
return LineHeight;
}

{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.indentLevel++;
// Vector Action Descriptions
DrawVectorActionDescriptions(position, property);
position.y += GetHeightDrawVectorActionDescriptions(property);

"vector for brain (In Continuous state space)." +
"Or number of possible values (in Discrete state space)."));
position.y += LineHeight;
EditorGUI.PropertyField(position,
property.FindPropertyRelative(NumVecObsPropName),
new GUIContent("Stacked Vectors",

EditorGUI.indentLevel--;
EditorGUI.indentLevel--;
}
/// <summary>

/// <param name="property">The SerializedProperty of the BrainParameters
/// to make the custom GUI for.</param>
private static void DrawVisualObservations(Rect position, SerializedProperty property)
{
{
EditorGUI.LabelField(position, "Visual Observations");
position.y += LineHeight;
var quarter = position.width / 4;

var heightRect = new Rect(position.x + 2*quarter, position.y, quarter, position.height);
var bwRect = new Rect(position.x + 3*quarter, position.y, quarter, position.height);
var heightRect = new Rect(position.x + 2 * quarter, position.y, quarter, position.height);
var bwRect = new Rect(position.x + 3 * quarter, position.y, quarter, position.height);
EditorGUI.indentLevel++;
if (resolutions.arraySize > 0)
{

private static void DrawVisualObsButtons(Rect position, SerializedProperty resolutions)
{
var widthEighth = position.width / 8;
var addButtonRect = new Rect(position.x + widthEighth, position.y,
var addButtonRect = new Rect(position.x + widthEighth, position.y,
3 * widthEighth, position.height);
var removeButtonRect = new Rect(position.x + 4 * widthEighth, position.y,
3 * widthEighth, position.height);

newRes.FindPropertyRelative(CamWidthPropName).intValue = DefaultCameraWidth;
newRes.FindPropertyRelative(CamHeightPropName).intValue = DefaultCameraHeight;
newRes.FindPropertyRelative(CamGrayPropName).boolValue = DefaultCameraGray;
}
if (resolutions.arraySize > 0)
{

}
}
}
/// <summary>
/// The Height required to draw the Visual Observations parameters
/// </summary>

/// to make the custom GUI for.</param>
private static void DrawVectorAction(Rect position, SerializedProperty property)
{
EditorGUI.LabelField(position, "Vector Action");
EditorGUI.LabelField(position, "Vector Action");
position.y += LineHeight;
EditorGUI.indentLevel++;
var bpVectorActionType = property.FindPropertyRelative(ActionTypePropName);

continuousActionSize,
new GUIContent("Space Size", "Length of continuous action vector."));
}
/// <summary>
/// Draws the Discrete Vector Actions parameters for the Brain Parameters
/// </summary>

position.x += 20;
position.width -= 20;
for (var branchIndex = 0;
branchIndex < vecActionSize.arraySize;
branchIndex++)
branchIndex < vecActionSize.arraySize;
branchIndex++)
EditorGUI.PropertyField(
position,
branchActionSize,

}
}
/// <summary>
/// The Height required to draw the Vector Action parameters
/// </summary>

position,
vecActionDescriptions,
new GUIContent("Action Descriptions",
"A list of strings used to name the available actionsm for the Brain."),
"A list of strings used to name the available actionsm for the Brain."),
true);
position.y += LineHeight;
}

position,
vecActionDescriptions,
new GUIContent("Branch Descriptions",
"A list of strings used to name the available branches for the Brain."),
"A list of strings used to name the available branches for the Brain."),
/// <summary>
/// The Height required to draw the Action Descriptions
/// </summary>

10
UnitySDK/Assets/ML-Agents/Editor/BroadcastHubDrawer.cs


using UnityEngine;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;

{
LazyInitializeHub(property, label);
position.height = LineHeight;
EditorGUI.LabelField(position, new GUIContent(label.text,
EditorGUI.LabelField(position, new GUIContent(label.text,
"The Broadcast Hub helps you define which Brains you want to expose to " +
"the external process"));
position.y += LineHeight;

EditorGUI.indentLevel++;
DrawAddRemoveButtons(position);
position.y += LineHeight;
// This is the labels for each columns
var brainWidth = position.width - ControlSize;
var brainRect = new Rect(

EditorGUI.indentLevel--;
EditorGUI.EndProperty();
}
/// <summary>
/// Draws the Add and Remove buttons.
/// </summary>

fieldInfo.SetValue(target, _hub);
}
}
/// <summary>
/// Signals that the property has been modified and requires the scene to be saved for
/// the changes to persist. Only works when the Editor is not playing.

2
UnitySDK/Assets/ML-Agents/Editor/Builder.cs


#if UNITY_CLOUD_BUILD
#if UNITY_CLOUD_BUILD
namespace MLAgents
{

4
UnitySDK/Assets/ML-Agents/Editor/BuilderUtils.cs


#if UNITY_CLOUD_BUILD
#if UNITY_CLOUD_BUILD
using System.Linq;
using UnityEditor;

}
}
}
#endif

6
UnitySDK/Assets/ML-Agents/Editor/DemonstrationDrawer.cs


using System.Text;
using System.Text;
using MLAgents;
using UnityEditor;

/// <summary>
/// Constructs complex label for each CameraResolution object.
/// An example of this could be `[ 84 X 84 ]`
/// for a single camera with 84 pixels height and width.
/// for a single camera with 84 pixels height and width.
/// </summary>
private static string BuildCameraResolutionLabel(SerializedProperty cameraArray)
{

actSizeProperty.displayName + ": " + BuildActionArrayLabel(actSizeProperty);
var camResLabel = camResProp.displayName + ": " + BuildCameraResolutionLabel(camResProp);
var actSpaceTypeLabel = actSpaceTypeProp.displayName + ": " +
(SpaceType) actSpaceTypeProp.enumValueIndex;
(SpaceType)actSpaceTypeProp.enumValueIndex;
EditorGUILayout.LabelField(vecObsSizeLabel);
EditorGUILayout.LabelField(numStackedLabel);

2
UnitySDK/Assets/ML-Agents/Editor/DemonstrationImporter.cs


using System;
using System;
using System.IO;
using MLAgents.CommunicatorObjects;
using UnityEditor;

6
UnitySDK/Assets/ML-Agents/Editor/HeuristicBrainEditor.cs


using UnityEngine;
using UnityEngine;
using UnityEditor;
namespace MLAgents

public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Heuristic Brain", EditorStyles.boldLabel);
var brain = (HeuristicBrain) target;
var brain = (HeuristicBrain)target;
// Expose the Heuristic Brain's Monoscript for decision in a drag and drop box.
brain.decisionScript = EditorGUILayout.ObjectField(
"Decision Script", brain.decisionScript, typeof(MonoScript), true) as MonoScript;

14
UnitySDK/Assets/ML-Agents/Editor/LearningBrainEditor.cs


using UnityEngine;
using UnityEngine;
using UnityEditor;
namespace MLAgents

/// LearningBrain.
/// Shows the BrainParameters of the Brain and expose a tool to deep copy BrainParameters
/// between brains. Also exposes a drag box for the Model that will be used by the
/// LearningBrain.
/// LearningBrain.
/// </summary>
[CustomEditor(typeof(LearningBrain))]
public class LearningBrainEditor : BrainEditor

private float _timeSinceModelReload;
// Whether or not the model needs to be reloaded
private bool _requireReload;
/// <summary>
/// Called when the user opens the Inspector for the LearningBrain
/// </summary>

EditorApplication.update += IncreaseTimeSinceLastModelReload;
}
/// <summary>
/// Called when the user leaves the Inspector for the LearningBrain
/// </summary>

}
var brain = (LearningBrain) target;
var brain = (LearningBrain)target;
serializedBrain.Update();
serializedBrain.Update();
var tfGraphModel = serializedBrain.FindProperty(ModelPropName);
EditorGUILayout.ObjectField(tfGraphModel);
var inferenceDevice = serializedBrain.FindProperty(InferenceDevicePropName);

20
UnitySDK/Assets/ML-Agents/Editor/PlayerBrainEditor.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

private const string KeyContinuousPropName = "keyContinuousPlayerActions";
private const string KeyDiscretePropName = "discretePlayerActions";
private const string AxisContinuousPropName = "axisContinuousPlayerActions";
var brain = (PlayerBrain) target;
var brain = (PlayerBrain)target;
serializedBrain.Update();
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
{

DrawDiscreteKeyMapping(serializedBrain);
}
serializedBrain.ApplyModifiedProperties();
}
}
/// <summary>
/// Draws the UI for continuous control key mapping to actions.

EditorGUILayout.HelpBox(
$"Key {action.key.ToString()} is assigned to index " +
$"{action.index.ToString()} but the action size is only of size " +
$"{brainParams.vectorActionSize.ToString()}",
$"{brainParams.vectorActionSize.ToString()}",
MessageType.Error);
}
}

{
EditorGUILayout.HelpBox(
$"Axis {action.axis} is assigned to index {action.index.ToString()} " +
$"but the action size is only of size {brainParams.vectorActionSize}",
$"but the action size is only of size {brainParams.vectorActionSize}",
GUILayout.Label("You can change axis settings from Edit->Project Settings->Input",
EditorStyles.helpBox );
GUILayout.Label("You can change axis settings from Edit->Project Settings->Input",
EditorStyles.helpBox);
}
/// <summary>

private static void DrawDiscreteKeyMapping(SerializedObject serializedBrain)
{
GUILayout.Label("Edit the discrete inputs for your actions",
GUILayout.Label("Edit the discrete inputs for your actions",
EditorStyles.boldLabel);
var dhas = serializedBrain.FindProperty(KeyDiscretePropName);
serializedBrain.Update();

10
UnitySDK/Assets/ML-Agents/Editor/ResetParameterDrawer.cs


using UnityEngine;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;

addRect.width *= 2;
}
if (GUI.Button(addRect,
new GUIContent("Add New", "Add a new item to the default reset parameters"),
new GUIContent("Add New", "Add a new item to the default reset parameters"),
// If there are no items in the ResetParameters, Hide the Remove button
if (_parameters.Count == 0)
{

if (GUI.Button(removeRect,
if (GUI.Button(removeRect,
"Remove Last", "Remove the last item from the default reset parameters"),
"Remove Last", "Remove the last item from the default reset parameters"),
EditorStyles.miniButton))
{
MarkSceneAsDirty();

22
UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs


using System.Collections.Generic;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using System.IO.Abstractions.TestingHelpers;

private const string DemoDirecory = "Assets/Demonstrations/";
private const string ExtensionType = ".demo";
private const string DemoName = "Test";
[Test]
public void TestSanitization()
{

var demoStore = new DemonstrationStore(fileSystem);
Assert.IsFalse(fileSystem.Directory.Exists(DemoDirecory));
cameraResolutions = new [] {new Resolution()},
vectorActionDescriptions = new [] {"TestActionA", "TestActionB"},
vectorActionSize = new [] {2, 2},
cameraResolutions = new[] {new Resolution()},
vectorActionDescriptions = new[] {"TestActionA", "TestActionB"},
vectorActionSize = new[] {2, 2},
Assert.IsTrue(fileSystem.Directory.Exists(DemoDirecory));
Assert.IsTrue(fileSystem.FileExists(DemoDirecory + DemoName + ExtensionType));

visualObservations = new List<Texture2D>(),
actionMasks = new []{false, true},
actionMasks = new[] {false, true},
done = true,
id = 5,
maxStepReached = true,

storedVectorActions = new [] {0f, 1f},
storedVectorActions = new[] {0f, 1f},
demoStore.Record(agentInfo);
demoStore.Close();
}

5
UnitySDK/Assets/ML-Agents/Editor/Tests/DiscreteActionOutputApplierTest.cs


var src = new TensorProxy
{
data = new Tensor(2, 3, new []
data = new Tensor(2, 3, new[]
}),
valueType = TensorProxy.TensorType.FloatingPoint
};

var src = new TensorProxy
{
valueType = TensorProxy.TensorType.FloatingPoint,
data = new Tensor(0,1)
data = new Tensor(0, 1)
};
var dst = new TensorProxy

29
UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestActionMasker.cs


using NUnit.Framework;
using NUnit.Framework;
namespace MLAgents.Tests
{

var masker = new ActionMasker(bp);
masker.SetActionMask(0, new int[1] {0});
Assert.Catch<UnityAgentsException>(() => masker.GetMask());
}
[Test]

var mask = masker.GetMask();
Assert.IsNull(mask);
}
[Test]
public void FirstBranchMask()
{

var masker = new ActionMasker(bp);
var mask = masker.GetMask();
Assert.IsNull(mask);
masker.SetActionMask(0, new int[]{1,2,3});
masker.SetActionMask(0, new int[] {1, 2, 3});
mask = masker.GetMask();
Assert.IsFalse(mask[0]);
Assert.IsTrue(mask[1]);

Assert.AreEqual(mask.Length, 15);
}
[Test]
public void SecondBranchMask()
{

var masker = new ActionMasker(bp);
bool[] mask = masker.GetMask();
masker.SetActionMask(1, new int[]{1,2,3});
masker.SetActionMask(1, new int[] {1, 2, 3});
mask = masker.GetMask();
Assert.IsFalse(mask[0]);
Assert.IsFalse(mask[4]);

Assert.IsFalse(mask[8]);
Assert.IsFalse(mask[9]);
}
[Test]
public void MaskReset()
{

var masker = new ActionMasker(bp);
var mask = masker.GetMask();
masker.SetActionMask(1, new int[3]{1,2,3});
masker.SetActionMask(1, new int[3] {1, 2, 3});
mask = masker.GetMask();
masker.ResetMask();
mask = masker.GetMask();

bp.vectorActionSpaceType = SpaceType.discrete;
bp.vectorActionSize = new int[3] {4, 5, 6};
var masker = new ActionMasker(bp);
() => masker.SetActionMask(0, new int[1]{5}));
() => masker.SetActionMask(0, new int[1] {5}));
() => masker.SetActionMask(1, new int[1]{5}));
() => masker.SetActionMask(1, new int[1] {5}));
() => masker.SetActionMask(3, new int[1]{1}));
() => masker.SetActionMask(3, new int[1] {1}));
masker.SetActionMask(0, new int[4] {0, 1, 2, 3});
masker.SetActionMask(0, new int[4] {0, 1, 2, 3});
[Test]
public void MultipleMaskEdit()
{

var mask = masker.GetMask();
for (var i = 0; i < 15; i++)
{
if ((i == 0) || (i == 1) || (i == 3)|| (i == 10))
if ((i == 0) || (i == 1) || (i == 3) || (i == 10))
{
Assert.IsTrue(mask[i]);
}

16
UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorApplier.cs


using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEngine;

{
var f = typeof(Agent).GetField(
"action", BindingFlags.Instance | BindingFlags.NonPublic);
return (AgentAction) f.GetValue(this);
return (AgentAction)f.GetValue(this);
}
}

var agentB = goB.AddComponent<TestAgent>();
var infoB = new AgentInfo();
return new Dictionary<Agent, AgentInfo>(){{agentA, infoA},{agentB, infoB}};
return new Dictionary<Agent, AgentInfo>(){{agentA, infoA}, {agentB, infoB}};
}
[Test]

var inputTensor = new TensorProxy()
{
shape = new long[] {2, 3},
data = new Tensor (2, 3, new float[] {1, 2, 3, 4, 5, 6})
data = new Tensor(2, 3, new float[] {1, 2, 3, 4, 5, 6})
};
var agentInfos = GetFakeAgentInfos();

var inputTensor = new TensorProxy()
{
shape = new long[] {2, 5},
data = new Tensor (
data = new Tensor(
2,
5,
new[] {0.5f, 22.5f, 0.1f, 5f, 1f, 4f, 5f, 6f, 7f, 8f})

var applier = new DiscreteActionOutputApplier(new []{2, 3}, 0, alloc);
var applier = new DiscreteActionOutputApplier(new[] {2, 3}, 0, alloc);
applier.Apply(inputTensor, agentInfos);
var agents = agentInfos.Keys.ToList();

var inputTensor = new TensorProxy()
{
shape = new long[] {2, 5},
data = new Tensor (
data = new Tensor(
2,
5,
new[] {0.5f, 22.5f, 0.1f, 5f, 1f, 4f, 5f, 6f, 7f, 8f})

var inputTensor = new TensorProxy()
{
shape = new long[] {2, 1},
data = new Tensor (2, 1, new[]{0.5f, 8f})
data = new Tensor(2, 1, new[] {0.5f, 8f})
};
var agentInfos = GetFakeAgentInfos();

20
UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorGenerator.cs


using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Barracuda;
using NUnit.Framework;

{
private class TestAgent : Agent
{
}
private Dictionary<Agent, AgentInfo> GetFakeAgentInfos()

var infoA = new AgentInfo()
{
stackedVectorObservation = (new [] {1f, 2f, 3f}).ToList(),
stackedVectorObservation = (new[] {1f, 2f, 3f}).ToList(),
storedVectorActions = new [] {1f, 2f},
storedVectorActions = new[] {1f, 2f},
stackedVectorObservation = (new [] {4f, 5f, 6f}).ToList(),
memories = (new [] {1f, 1f, 1f}).ToList(),
storedVectorActions = new [] {3f, 4f},
actionMasks = new [] {true, false, false, false, false},
stackedVectorObservation = (new[] {4f, 5f, 6f}).ToList(),
memories = (new[] {1f, 1f, 1f}).ToList(),
storedVectorActions = new[] {3f, 4f},
actionMasks = new[] {true, false, false, false, false},
return new Dictionary<Agent, AgentInfo>(){{agentA, infoA},{agentB, infoB}};
return new Dictionary<Agent, AgentInfo>(){{agentA, infoA}, {agentB, infoB}};
}
[Test]

{
shape = new long[] {2, 2},
valueType = TensorProxy.TensorType.Integer
};
const int batchSize = 4;
var agentInfos = GetFakeAgentInfos();

{
shape = new long[] {2, 5},
valueType = TensorProxy.TensorType.FloatingPoint
};
const int batchSize = 4;
var agentInfos = GetFakeAgentInfos();

65
UnitySDK/Assets/ML-Agents/Editor/Tests/MLAgentsEditModeTest.cs


{
initializeAcademyCalls += 1;
}
public override void AcademyReset()
{
academyResetCalls += 1;

{
initializeAgentCalls += 1;
}
public override void CollectObservations()
{
collectObservationsCalls += 1;

public class EditModeTestInitialization
{
}
}
[Test]
public void TestAcademy()
{

Assert.AreEqual(false, aca.IsDone());
//This will call the method even though it is private
MethodInfo AcademyInitializeMethod = typeof(Academy).GetMethod("InitializeEnvironment",
BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] { });
BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] {});
Assert.AreEqual(1, aca.initializeAcademyCalls);
Assert.AreEqual(0, aca.GetEpisodeCount());
Assert.AreEqual(0, aca.GetStepCount());

Assert.AreEqual(0, agent2.agentActionCalls);
MethodInfo AgentEnableMethod = typeof(Agent).GetMethod("OnEnableHelper",
BindingFlags.Instance | BindingFlags.NonPublic);
BindingFlags.Instance | BindingFlags.NonPublic);
BindingFlags.Instance | BindingFlags.NonPublic);
BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
AgentEnableMethod.Invoke(agent1, new object[] { aca });
Assert.AreEqual(false, agent1.IsDone());

Assert.AreEqual(0, agent1.agentActionCalls);
Assert.AreEqual(0, agent2.agentActionCalls);
}
}
public class EditModeTestStep

acaGO.AddComponent<TestAcademy>();
TestAcademy aca = acaGO.GetComponent<TestAcademy>();
MethodInfo AcademyInitializeMethod = typeof(Academy).GetMethod("InitializeEnvironment",
BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] { });
BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] {});
BindingFlags.Instance | BindingFlags.NonPublic);
BindingFlags.Instance | BindingFlags.NonPublic);
int numberReset = 0;
for (int i = 0; i < 10; i++)

{
numberReset += 1;
}
AcademyStepMethod.Invoke((object)aca, new object[] { });
AcademyStepMethod.Invoke((object)aca, new object[] {});
}
}

agent2.GiveBrain(brain);
AgentEnableMethod.Invoke(agent1, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
MethodInfo AcademyStepMethod = typeof(Academy).GetMethod(
"EnvironmentStep", BindingFlags.Instance | BindingFlags.NonPublic);

requestAction += 1;
agent2.RequestAction();
}
AcademyStepMethod.Invoke(aca, new object[] { });
AcademyStepMethod.Invoke(aca, new object[] {});
}
}
}

TestAcademy aca = acaGO.GetComponent<TestAcademy>();
MethodInfo AcademyInitializeMethod = typeof(Academy).GetMethod(
"InitializeEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
MethodInfo AcademyStepMethod = typeof(Academy).GetMethod(
"EnvironmentStep", BindingFlags.Instance | BindingFlags.NonPublic);

for (int i = 0; i < 50; i++)
{
Assert.AreEqual(stepsSinceReset, aca.GetStepCount());
Assert.AreEqual(1, aca.initializeAcademyCalls);
Assert.AreEqual(numberReset, aca.GetEpisodeCount());

stepsSinceReset = 1;
Assert.AreEqual(true, aca.IsDone());
}
AcademyStepMethod.Invoke((object)aca, new object[] { });
AcademyStepMethod.Invoke((object)aca, new object[] {});
}
}

agent2.GiveBrain(brain);
AgentEnableMethod.Invoke(agent2, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
int numberAgent1Reset = 0;
int numberAgent2Reset = 0;

if (i == 2)
{
AgentEnableMethod.Invoke(agent1, new object[] { aca });
}
// Reset Academy every 100 steps
if (i % 100 == 3)

{
agent1StepSinceReset = 0;
}
AcademyStepMethod.Invoke((object)aca, new object[] { });
AcademyStepMethod.Invoke((object)aca, new object[] {});
}
}
}

TestAcademy aca = acaGO.GetComponent<TestAcademy>();
MethodInfo AcademyInitializeMethod = typeof(Academy).GetMethod(
"InitializeEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
MethodInfo AcademyStepMethod = typeof(Academy).GetMethod(
"EnvironmentStep", BindingFlags.Instance | BindingFlags.NonPublic);

{
numberReset += 1;
stepsSinceReset = 1;
AcademyStepMethod.Invoke((object)aca, new object[] { });
AcademyStepMethod.Invoke((object)aca, new object[] {});
}
}

agent2.GiveBrain(brain);
AgentEnableMethod.Invoke(agent2, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
int numberAgent1Reset = 0;

agent1StepSinceReset = 0;
}
AcademyStepMethod.Invoke((object)aca, new object[] { });
AcademyStepMethod.Invoke((object)aca, new object[] {});
}
}

agent2.GiveBrain(brain);
AgentEnableMethod.Invoke(agent2, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
AgentEnableMethod.Invoke(agent1, new object[] { aca });
int agent1ResetOnDone = 0;

}
AcademyStepMethod.Invoke((object)aca, new object[] { });
AcademyStepMethod.Invoke((object)aca, new object[] {});
}
[Test]

agent2.GiveBrain(brain);
AgentEnableMethod.Invoke(agent2, new object[] { aca });
AcademyInitializeMethod.Invoke(aca, new object[] { });
AcademyInitializeMethod.Invoke(aca, new object[] {});
AgentEnableMethod.Invoke(agent1, new object[] { aca });

Assert.LessOrEqual(Mathf.Abs(i * 0.1f - agent2.GetCumulativeReward()), 0.05f);
AcademyStepMethod.Invoke((object)aca, new object[] { });
AcademyStepMethod.Invoke((object)aca, new object[] {});
agent1.AddReward(10f);
if ((i % 21 == 0) && (i > 0))

}
}
}
}

2
UnitySDK/Assets/ML-Agents/Editor/Tests/MultinomialTest.cs


using NUnit.Framework;
using NUnit.Framework;
using MLAgents.InferenceBrain.Utils;
namespace MLAgents.Tests

2
UnitySDK/Assets/ML-Agents/Editor/Tests/RandomNormalTest.cs


Assert.AreEqual(FirstValue * stddev + mean, rn.NextDouble(), Epsilon);
Assert.AreEqual(SecondValue * stddev + mean, rn.NextDouble(), Epsilon);
}
[Test]
public void RandomNormalTestDistribution()
{

4
UnitySDK/Assets/ML-Agents/Editor/Tests/RayPerceptionTests.cs


{
var angles = new[] {0f, 90f, 180f};
var tags = new[] {"test", "test_1"};
var go = new GameObject("MyGameObject");
RayPerception3D rayPer3D = go.AddComponent<RayPerception3D>();
var result = rayPer3D.Perceive(1f, angles ,

{
var angles = new[] {0f, 90f, 180f};
var tags = new[] {"test", "test_1"};
var go = new GameObject("MyGameObject");
RayPerception2D rayPer2D = go.AddComponent<RayPerception2D>();
var result = rayPer2D.Perceive(1f, angles,

2
UnitySDK/Assets/ML-Agents/Editor/Tests/TensorUtilsTest.cs


TensorUtils.FillTensorWithRandomNormal(t, rn);
var reference = new []
var reference = new[]
{
-0.4315872f,
-1.11074f,

16
UnitySDK/Assets/ML-Agents/Editor/Tests/UtilitiesTests.cs


using NUnit.Framework;
using NUnit.Framework;
namespace MLAgents.Tests
{

public void TestCumSum()
{
var output = Utilities.CumSum(new []{1, 2, 3, 10});
CollectionAssert.AreEqual(output, new [] {0, 1, 3, 6, 16});
var output = Utilities.CumSum(new[] {1, 2, 3, 10});
CollectionAssert.AreEqual(output, new[] {0, 1, 3, 6, 16});
CollectionAssert.AreEqual(output, new []{0});
CollectionAssert.AreEqual(output, new[] {0});
output = Utilities.CumSum(new []{100});
CollectionAssert.AreEqual(output, new []{0, 100});
output = Utilities.CumSum(new[] {100});
CollectionAssert.AreEqual(output, new[] {0, 100});
output = Utilities.CumSum(new []{-1, 10});
CollectionAssert.AreEqual(output, new []{0, -1, 9});
output = Utilities.CumSum(new[] {-1, 10});
CollectionAssert.AreEqual(output, new[] {0, -1, 9});
}
}
}

3
UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

public override void AcademyStep()
{
}
}

5
UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

public override void AgentAction(float[] vectorAction, string textAction)
{
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
{
var actionZ = 2f * Mathf.Clamp(vectorAction[0], -1f, 1f);

gameObject.transform.Rotate(new Vector3(0, 0, 1), Random.Range(-10f, 10f));
ballRb.velocity = new Vector3(0f, 0f, 0f);
ball.transform.position = new Vector3(Random.Range(-1.5f, 1.5f), 4f, Random.Range(-1.5f, 1.5f))
+ gameObject.transform.position;
+ gameObject.transform.position;
//Reset the parameters when the Agent is reset.
SetResetParameters();
}

16
UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

{
List<float> act = new List<float>();
// state[5] is the velocity of the ball in the x orientation.
// We use this number to control the Platform's z axis rotation speed,
// so that the Platform is tilted in the x orientation correspondingly.
// state[5] is the velocity of the ball in the x orientation.
// We use this number to control the Platform's z axis rotation speed,
// so that the Platform is tilted in the x orientation correspondingly.
// state[7] is the velocity of the ball in the z orientation.
// We use this number to control the Platform's x axis rotation speed,
// so that the Platform is tilted in the z orientation correspondingly.
// state[7] is the velocity of the ball in the z orientation.
// We use this number to control the Platform's x axis rotation speed,
// so that the Platform is tilted in the z orientation correspondingly.
// If the vector action space type is discrete, then we don't do anything.
// If the vector action space type is discrete, then we don't do anything.
return new float[1] { 1f };
}

7
UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

public override void AgentAction(float[] vectorAction, string textAction)
{
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
{
var actionZ = 2f * Mathf.Clamp(vectorAction[0], -1f, 1f);

gameObject.transform.Rotate(new Vector3(0, 0, 1), Random.Range(-10f, 10f));
ballRb.velocity = new Vector3(0f, 0f, 0f);
ball.transform.position = new Vector3(Random.Range(-1.5f, 1.5f), 4f, Random.Range(-1.5f, 1.5f))
+ gameObject.transform.position;
+ gameObject.transform.position;
public void SetBall()
{

2
UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

12
UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAgent.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

else
{
myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
void Freeze()
{

gameObject.GetComponent<Renderer>().material = frozenMaterial;
}
void Unfreeze()
{

satiated = false;
gameObject.GetComponent<Renderer>().material = normalMaterial;
}
public override void AgentAction(float[] vectorAction, string textAction)
{

bananas = 0;
myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
transform.position = new Vector3(Random.Range(-myArea.range, myArea.range),
2f, Random.Range(-myArea.range, myArea.range))
2f, Random.Range(-myArea.range, myArea.range))
+ area.transform.position;
transform.rotation = Quaternion.Euler(new Vector3(0f, Random.Range(0, 360)));

public override void AgentOnDone()
{
laser_length = myAcademy.resetParameters.TryGetValue("laser_length", out laser_length) ? laser_length: 1.0f;
laser_length = myAcademy.resetParameters.TryGetValue("laser_length", out laser_length) ? laser_length : 1.0f;
}
public void SetAgentScale()

10
UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaArea.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

// Use this for initialization
void Start()
{
}
void CreateBanana(int numBana, GameObject bananaType)

GameObject bana = Instantiate(bananaType, new Vector3(Random.Range(-range, range), 1f,
Random.Range(-range, range)) + transform.position,
Quaternion.Euler(new Vector3(0f, Random.Range(0f, 360f), 90f)));
Random.Range(-range, range)) + transform.position,
Quaternion.Euler(new Vector3(0f, Random.Range(0f, 360f), 90f)));
bana.GetComponent<BananaLogic>().respawn = respawnBananas;
bana.GetComponent<BananaLogic>().myArea = this;
}

if (agent.transform.parent == gameObject.transform)
{
agent.transform.position = new Vector3(Random.Range(-range, range), 2f,
Random.Range(-range, range))
Random.Range(-range, range))
+ transform.position;
agent.transform.rotation = Quaternion.Euler(new Vector3(0f, Random.Range(0, 360)));
}

29
UnitySDK/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaLogic.cs


using System.Collections;
using System.Collections;
public class BananaLogic : MonoBehaviour {
public class BananaLogic : MonoBehaviour
{
void Start () {
void Start()
{
void Update () {
void Update()
{
public void OnEaten() {
if (respawn)
public void OnEaten()
{
if (respawn)
transform.position = new Vector3(Random.Range(-myArea.range, myArea.range),
3f,
Random.Range(-myArea.range, myArea.range)) + myArea.transform.position;
transform.position = new Vector3(Random.Range(-myArea.range, myArea.range),
3f,
Random.Range(-myArea.range, myArea.range)) + myArea.transform.position;
else
else
{
Destroy(gameObject);
}

9
UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAcademy.cs


using System.Collections;
using System.Collections;
public class BasicAcademy : Academy {
public class BasicAcademy : Academy
{
}

30
UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs


}
public override void AgentAction(float[] vectorAction, string textAction)
{
{
int direction = 0;
switch (movement)
{
case 1:
direction = -1;
break;
case 2:
direction = 1;
break;
}
position += direction;
int direction = 0;
switch (movement)
{
case 1:
direction = -1;
break;
case 2:
direction = 1;
break;
}
position += direction;
if (position < minPosition) { position = minPosition; }
if (position > maxPosition) { position = maxPosition; }

public override void AgentOnDone()
{
}
public void FixedUpdate()

}
}
}
}

2
UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicDecision.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

13
UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAcademy.cs


using System.Collections;
using System.Collections;
public class BouncerAcademy : Academy {
public class BouncerAcademy : Academy
{
Physics.gravity = new Vector3(0,-9.8f*gravityMultiplier,0);
Physics.gravity = new Vector3(0, -9.8f * gravityMultiplier, 0);
}

33
UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs


using System.Collections;
using System.Collections;
public class BouncerAgent : Agent {
public class BouncerAgent : Agent
{
[Header("Bouncer Specific")]
public GameObject banana;
public GameObject bodyObject;

}
public override void AgentAction(float[] vectorAction, string textAction)
{
{
{
vectorAction[i] = Mathf.Clamp(vectorAction[i], -1f, 1f);
}
{
vectorAction[i] = Mathf.Clamp(vectorAction[i], -1f, 1f);
}
rb.AddForce( new Vector3(x, y+1, z) * strength);
rb.AddForce(new Vector3(x, y + 1, z) * strength);
AddReward(-0.05f * (
vectorAction[0] * vectorAction[0] +

public override void AgentReset()
{
(1 - 2 * Random.value) *5, 2, (1 - 2 * Random.value)*5);
(1 - 2 * Random.value) * 5, 2, (1 - 2 * Random.value) * 5);
BouncerBanana[] bananas =
BouncerBanana[] bananas =
environment.GetComponentsInChildren<BouncerBanana>();
foreach (BouncerBanana bb in bananas)
{

public override void AgentOnDone()
{
if (Physics.Raycast(transform.position, new Vector3(0f,-1f,0f), 0.51f) && jumpCooldown <= 0f)
if (Physics.Raycast(transform.position, new Vector3(0f, -1f, 0f), 0.51f) && jumpCooldown <= 0f)
{
RequestDecision();
jumpLeft -= 1;

if (gameObject.transform.position.y < -1)
{
AddReward(-1);

if (gameObject.transform.localPosition.x < -19 || gameObject.transform.localPosition.x >19
if (gameObject.transform.localPosition.x < -19 || gameObject.transform.localPosition.x > 19
|| gameObject.transform.localPosition.z < -19 || gameObject.transform.localPosition.z > 19)
{
AddReward(-1);

{
Done();
}
}
private void Update()

20
UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerBanana.cs


using System.Collections;
using System.Collections;
public class BouncerBanana : MonoBehaviour {
public class BouncerBanana : MonoBehaviour
{
void FixedUpdate () {
void FixedUpdate()
{
gameObject.transform.Rotate(new Vector3(1, 0, 0), 0.5f);
}

agent.AddReward(1f);
Respawn();
}
public void Respawn(){
gameObject.transform.localPosition =
public void Respawn()
{
gameObject.transform.localPosition =
(1 - 2 * Random.value) * 5f,
2f+ Random.value * 5f,
(1 - 2 * Random.value) * 5f,
2f + Random.value * 5f,
}

2
UnitySDK/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

16
UnitySDK/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

{
[Header("Target To Walk Towards")] [Space(10)]
[Header("Target To Walk Towards")][Space(10)]
public Transform target;
public Transform ground;

[Header("Body Parts")] [Space(10)] public Transform body;
[Header("Body Parts")][Space(10)] public Transform body;
public Transform leg0Upper;
public Transform leg0Lower;
public Transform leg1Upper;

public Transform leg3Upper;
public Transform leg3Lower;
[Header("Joint Settings")] [Space(10)] JointDriveController jdController;
[Header("Joint Settings")][Space(10)] JointDriveController jdController;
[Header("Reward Functions To Use")] [Space(10)]
[Header("Reward Functions To Use")][Space(10)]
[Header("Foot Grounded Visualization")] [Space(10)]
[Header("Foot Grounded Visualization")][Space(10)]
public bool useFootGroundedVisualization;
public MeshRenderer foot0;

jdController.SetupBodyPart(leg2Lower);
jdController.SetupBodyPart(leg3Upper);
jdController.SetupBodyPart(leg3Lower);
}
}
/// <summary>
/// We only need to change the joint settings based on decision freq.

{
transform.rotation = Quaternion.LookRotation(dirToTarget);
}
transform.Rotate(Vector3.up,Random.Range(0.0f, 360.0f));
transform.Rotate(Vector3.up, Random.Range(0.0f, 360.0f));
foreach (var bodyPart in jdController.bodyPartsDict.Values)
{

11
UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAcademy.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using MLAgents;

public void SetEnvironment()
{
cam.transform.position = new Vector3(-((int)resetParameters["gridSize"] - 1) / 2f,
(int)resetParameters["gridSize"] * 1.25f,
-((int)resetParameters["gridSize"] - 1) / 2f);
cam.transform.position = new Vector3(-((int)resetParameters["gridSize"] - 1) / 2f,
(int)resetParameters["gridSize"] * 1.25f,
-((int)resetParameters["gridSize"] - 1) / 2f);
cam.orthographicSize = ((int)resetParameters["gridSize"] + 5f) / 2f;
List<int> playersList = new List<int>();

agentCam.orthographicSize = (gridSize) / 2f;
agentCam.transform.position = new Vector3((gridSize - 1) / 2f, gridSize + 1f, (gridSize - 1) / 2f);
}
public override void AcademyReset()

int x_a = (numbersA[players.Length]) / gridSize;
int y_a = (numbersA[players.Length]) % gridSize;
trueAgent.transform.position = new Vector3(x_a, -0.25f, y_a);
}
}

16
UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs


using System;
using System;
using UnityEngine;
using System.Linq;
using MLAgents;

public float timeBetweenDecisionsAtInference;
private float timeSinceDecision;
[Tooltip("Because we want an observation right before making a decision, we can force " +
"a camera to render before making a decision. Place the agentCam here if using " +
"RenderTexture as observations.")]
[Tooltip("Because we want an observation right before making a decision, we can force " +
"a camera to render before making a decision. Place the agentCam here if using " +
"RenderTexture as observations.")]
"masking turned on may not behave optimally when action masking is turned off.")]
"masking turned on may not behave optimally when action masking is turned off.")]
public bool maskActions = true;
private const int NoAction = 0; // do nothing!

private void SetMask()
{
// Prevents the agent from picking an action that would make it collide with a wall
var positionX = (int) transform.position.x;
var positionZ = (int) transform.position.z;
var positionX = (int)transform.position.x;
var positionZ = (int)transform.position.z;
var maxPosition = academy.gridSize - 1;
if (positionX == 0)

private void WaitTimeInference()
{
if(renderCamera != null)
if (renderCamera != null)
{
renderCamera.Render();
}

11
UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs


using System.Collections;
using System.Collections;
public class HallwayAcademy : Academy {
public float agentRunSpeed;
public class HallwayAcademy : Academy
{
public float agentRunSpeed;
public Material failMaterial; // when fail, the ground will use this material for a few seconds.
public Material failMaterial; // when fail, the ground will use this material for a few seconds.
public float gravityMultiplier; // use ~3 to make things less floaty
public override void InitializeAcademy()

public override void AcademyReset()
{
}
}

9
UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

public void MoveAgent(float[] act)
{
Vector3 dirToGo = Vector3.zero;
Vector3 rotateDir = Vector3.zero;

else
{
orangeBlock.transform.position =
new Vector3(0f, -1000f, blockOffset + Random.Range(-5f, 5f))
+ ground.transform.position;
new Vector3(0f, -1000f, blockOffset + Random.Range(-5f, 5f))
+ ground.transform.position;
redBlock.transform.position =
new Vector3(0f, 2f, blockOffset + Random.Range(-5f, 5f))
+ ground.transform.position;

1f, agentOffset + Random.Range(-5f, 5f))
1f, agentOffset + Random.Range(-5f, 5f))
+ ground.transform.position;
transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
agentRB.velocity *= 0f;

8
UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/GoalDetect.cs


//Detect when the orange block has touched the goal.
//Detect when the orange block has touched an obstacle.
//Detect when the orange block has touched the goal.
//Detect when the orange block has touched an obstacle.
//Put this script onto the orange block. There's nothing you need to set in the editor.
//Make sure the goal is tagged with "goal" in the editor.

[HideInInspector]
/// <summary>
/// The associated agent.
/// This will be set by the agent script on Initialization.
/// This will be set by the agent script on Initialization.
public PushAgentBasic agent; //
public PushAgentBasic agent; //
void OnCollisionEnter(Collision col)
{

29
UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs


//Put this script on your blue cube.
//Put this script on your blue cube.
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// The ground. The bounds are used to spawn the elements.
/// </summary>
public GameObject ground;
public GameObject ground;
public GameObject area;

[HideInInspector]
[HideInInspector]
public Bounds areaBounds;
PushBlockAcademy academy;

/// <summary>
/// Detects when the block touches the goal.
/// </summary>
[HideInInspector]
[HideInInspector]
public GoalDetect goalDetect;
public bool useVectorObs;

Material groundMaterial; //cached on Awake()
RayPerception rayPer;
float[] rayAngles = { 0f, 45f, 90f, 135f, 180f, 110f, 70f };
string[] detectableObjects = { "block", "goal", "wall" };

if (useVectorObs)
{
var rayDistance = 12f;
AddVectorObs(rayPer.Perceive(rayDistance, rayAngles, detectableObjects, 0f, 0f));
AddVectorObs(rayPer.Perceive(rayDistance, rayAngles, detectableObjects, 1.5f, 0f));
}

while (foundNewSpawnLocation == false)
{
float randomPosX = Random.Range(-areaBounds.extents.x * academy.spawnAreaMarginMultiplier,
areaBounds.extents.x * academy.spawnAreaMarginMultiplier);
areaBounds.extents.x * academy.spawnAreaMarginMultiplier);
areaBounds.extents.z * academy.spawnAreaMarginMultiplier);
areaBounds.extents.z * academy.spawnAreaMarginMultiplier);
randomSpawnPos = ground.transform.position + new Vector3(randomPosX, 1f, randomPosZ);
if (Physics.CheckBox(randomSpawnPos, new Vector3(2.5f, 0.01f, 2.5f)) == false)
{

/// <summary>
/// Moves the agent according to the selected action.
/// </summary>
public void MoveAgent(float[] act)
public void MoveAgent(float[] act)
Vector3 dirToGo = Vector3.zero;
Vector3 rotateDir = Vector3.zero;

}
transform.Rotate(rotateDir, Time.fixedDeltaTime * 200f);
agentRB.AddForce(dirToGo * academy.agentRunSpeed,
ForceMode.VelocityChange);
ForceMode.VelocityChange);
public override void AgentAction(float[] vectorAction, string textAction)
public override void AgentAction(float[] vectorAction, string textAction)
{
// Move the agent using the action.
MoveAgent(vectorAction);

blockRB.angularVelocity = Vector3.zero;
}
/// In the editor, if "Reset On Done" is checked then AgentReset() will be
/// In the editor, if "Reset On Done" is checked then AgentReset() will be
public override void AgentReset()
public override void AgentReset()
{
int rotation = Random.Range(0, 4);
float rotationAngle = rotation * 90f;

20
UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockAcademy.cs


//Every scene needs an academy script.
//Every scene needs an academy script.
//Create an empty gameObject and attach this script.
//The brain needs to be a child of the Academy gameObject.

public class PushBlockAcademy : Academy
{
/// The "walking speed" of the agents in the scene.
/// The "walking speed" of the agents in the scene.
public float agentRunSpeed;
public float agentRunSpeed;
public float agentRotationSpeed;
public float agentRotationSpeed;
/// ex: .9 means 90% of spawn area will be used.
/// .1 margin will be left (so players don't spawn off of the edge).
/// ex: .9 means 90% of spawn area will be used.
/// .1 margin will be left (so players don't spawn off of the edge).
public float spawnAreaMarginMultiplier;
public float spawnAreaMarginMultiplier;
/// When a goal is scored the ground will switch to this
/// When a goal is scored the ground will switch to this
/// material for a few seconds.
/// </summary>
public Material goalScoredMaterial;

/// The gravity multiplier.
/// Use ~3 to make things less floaty
/// </summary>
public float gravityMultiplier;
public float gravityMultiplier;
}
}

5
UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

{
}
}

7
UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs


using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

{
var enumerable = Enumerable.Range(0, 9).OrderBy(x => Guid.NewGuid()).Take(9);
var items = enumerable.ToArray();
agentRb.velocity = Vector3.zero;
myArea.PlaceObject(gameObject, items[0]);
transform.rotation = Quaternion.Euler(new Vector3(0f, Random.Range(0, 360)));

public override void AgentOnDone()
{
}
}

24
UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidArea.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

{
CreateObject(numObjects, pyramid, spawnAreaIndex);
}
var newObject = Instantiate(desiredObject, Vector3.zero,
var newObject = Instantiate(desiredObject, Vector3.zero,
Quaternion.Euler(0f, 0f, 0f), transform);
PlaceObject(newObject, spawnAreaIndex);
}

var spawnTransform = spawnAreas[spawnAreaIndex].transform;
var xRange = spawnTransform.localScale.x / 2.1f;
var zRange = spawnTransform.localScale.z / 2.1f;
objectToPlace.transform.position = new Vector3(Random.Range(-xRange, xRange), 2f, Random.Range(-zRange, zRange))
+ spawnTransform.position;
objectToPlace.transform.position = new Vector3(Random.Range(-xRange, xRange), 2f, Random.Range(-zRange, zRange))
+ spawnTransform.position;
foreach (Transform child in transform) if (child.CompareTag("pyramid"))
{
Destroy(child.gameObject);
}
foreach (Transform child in transform)
if (child.CompareTag("pyramid"))
{
Destroy(child.gameObject);
}
}
}

3
UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidSwitch.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

tag = "switchOn";
}
}
}

7
UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

public override void AcademyReset()
{
Physics.gravity = new Vector3(0, -resetParameters["gravity"], 0);

{
}

5
UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs


using UnityEngine;
using UnityEngine;
public GameObject pendulumA;
public GameObject pendulumB;
public GameObject hand;

goal.transform.localScale = new Vector3(goalSize, goalSize, goalSize);
}
public void SetResetParameters()
{
goalSize = myAcademy.resetParameters["goal_size"];

}
}

15
UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherDecision.cs


using System.Collections;
using System.Collections;
public class ReacherDecision : Decision {
public override float[] Decide (List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
public class ReacherDecision : Decision
{
public override float[] Decide(List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 4; i++)
{
public override List<float> MakeMemory (List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
public override List<float> MakeMemory(List<float> state, List<Texture2D> observation, float reward, bool done, List<float> memory)
}
}

18
UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherGoal.cs


using System.Collections;
using System.Collections;
public class ReacherGoal : MonoBehaviour {
public class ReacherGoal : MonoBehaviour
{
void Start () {
void Start()
{
void Update () {
void Update()
{
}
private void OnTriggerEnter(Collider other)

}
}
private void OnTriggerStay(Collider other)
private void OnTriggerStay(Collider other)
{
if (other.gameObject == hand)
{

10
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/AdjustTrainingTimescale.cs


//This script lets you change time scale during training. It is not a required script for this demo to function
//This script lets you change time scale during training. It is not a required script for this demo to function
using System.Collections;
using System.Collections.Generic;

{
public class AdjustTrainingTimescale : MonoBehaviour {
public class AdjustTrainingTimescale : MonoBehaviour
{
void Update ()
void Update()
if (Input.GetKeyDown(KeyCode.Alpha1))
{
Time.timeScale = 1f;

7
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/Area.cs


using System.Collections;
using System.Collections;
}
}
}

4
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/CameraFollow.cs


using System.Collections;
using System.Collections;
public Transform target;
Vector3 offset;

11
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/FlyCamera.cs


namespace MLAgents
{
/*
wasd : basic movement
shift : Makes camera accelerate

private Vector3
lastMouse =
new Vector3(255, 255,
255); // kind of in the middle of the screen, rather than at the top (play)
new Vector3(255, 255,
255); // kind of in the middle of the screen, rather than at the top (play)
private float totalRun = 1.0f;

transform.rotation = Quaternion.Euler(25, 0, 0);
}
if (Input.GetMouseButtonDown(1))
{
lastMouse = Input.mousePosition; // $CTK reset when we begin

transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
// Mouse camera angle done.
// Mouse camera angle done.
}
// Keyboard commands

{
transform.Translate(p);
}
}
private Vector3 GetBaseInput()

2
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/GroundContact.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

12
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/JointDriveController.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class BodyPart
{
[Header("Body Part Info")] [Space(10)] public ConfigurableJoint joint;
[Header("Body Part Info")][Space(10)] public ConfigurableJoint joint;
[Header("Ground & Target Contact")] [Space(10)]
[Header("Ground & Target Contact")][Space(10)]
public GroundContact groundContact;
public TargetContact targetContact;

[Header("Current Joint Settings")] [Space(10)]
[Header("Current Joint Settings")][Space(10)]
public Vector3 currentEularJointRotation;
[HideInInspector] public float currentStrength;

[Header("Other Debug Info")] [Space(10)]
[Header("Other Debug Info")][Space(10)]
public Vector3 currentJointForce;
public float currentJointForceSqrMag;

public class JointDriveController : MonoBehaviour
{
[Header("Joint Drive Settings")] [Space(10)]
[Header("Joint Drive Settings")][Space(10)]
public float maxJointSpring;
public float jointDampen;

6
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RandomDecision.cs


using System.Collections.Generic;
using System.Collections.Generic;
public override float[] Decide(
List<float> vectorObs,
List<Texture2D> visualObs,

float[] act = new float[brainParameters.vectorActionSize.Length];
for (int i = 0; i < brainParameters.vectorActionSize.Length; i++)
{
act[i]=Random.Range(0, brainParameters.vectorActionSize[i]);
act[i] = Random.Range(0, brainParameters.vectorActionSize[i]);
}
return act;
}

35
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs


using System.Collections;
using System.Collections;
public abstract class RayPerception : MonoBehaviour {
public abstract class RayPerception : MonoBehaviour
{
protected List<float> perceptionBuffer = new List<float>();
protected List<float> perceptionBuffer = new List<float>();
public virtual List<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects,
float startOffset, float endOffset)
{
return perceptionBuffer;
}
public virtual List<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects,
float startOffset, float endOffset)
{
return perceptionBuffer;
}
/// <summary>
/// Converts degrees to radians.
/// </summary>
public static float DegreeToRadian(float degree)
{
return degree * Mathf.PI / 180f;
}
/// <summary>
/// Converts degrees to radians.
/// </summary>
public static float DegreeToRadian(float degree)
{
return degree * Mathf.PI / 180f;
}
}

10
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception2D.cs


using System.Collections.Generic;
using System.Collections.Generic;
/// via the use of ray casts directed outward from the agent.
/// via the use of ray casts directed outward from the agent.
/// </summary>
public class RayPerception2D : RayPerception
{

/// <param name="rayAngles">Angles of rays (starting from (1,0) on unit circle).</param>
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
public List<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects)
float[] rayAngles, string[] detectableObjects)
{
perceptionBuffer.Clear();
// For each ray sublist stores categorical information on detected object

endPosition = transform.TransformDirection(
PolarToCartesian(rayDistance, angle));
PolarToCartesian(rayDistance, angle));
if (Application.isEditor)
{
Debug.DrawRay(transform.position,

float y = radius * Mathf.Sin(DegreeToRadian(angle));
return new Vector2(x, y);
}
}
}

14
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs


using System;
using System;
/// via the use of ray casts directed outward from the agent.
/// via the use of ray casts directed outward from the agent.
/// </summary>
public class RayPerception3D : RayPerception
{

{
if (subList == null || subList.Length != detectableObjects.Length + 2)
subList = new float[detectableObjects.Length + 2];
// For each ray sublist stores categorical information on detected object
// along with object distance.
foreach (float angle in rayAngles)

}
Array.Clear(subList, 0, subList.Length);
new Vector3(0f, startOffset, 0f), 0.5f,
new Vector3(0f, startOffset, 0f), 0.5f,
endPosition, out hit, rayDistance))
{
for (int i = 0; i < detectableObjects.Length; i++)

float z = radius * Mathf.Sin(DegreeToRadian(angle));
return new Vector3(x, 0f, z);
}
}
}

2
UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/TargetContact.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

26
UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs


using UnityEngine;
using UnityEngine;
Red,
Red,
Striker,
Striker,
"wall", "redAgent", "blueAgent" };
"wall", "redAgent", "blueAgent" };
"wall", "blueAgent", "redAgent" };
"wall", "blueAgent", "redAgent" };
public void ChooseRandomTeam()
{

var playerState = new PlayerState
{
agentRB = agentRb,
startingPos = transform.position,
agentRB = agentRb,
startingPos = transform.position,
agentScript = this,
};
area.playerStates.Add(playerState);

}
transform.Rotate(rotateDir, Time.deltaTime * 100f);
agentRb.AddForce(dirToGo * academy.agentRunSpeed,
ForceMode.VelocityChange);
ForceMode.VelocityChange);
public override void AgentAction(float[] vectorAction, string textAction)
{

AddReward(1f / 3000f);
}
MoveAgent(vectorAction);
}
/// <summary>

6
UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

public Brain brainStriker;
public Brain brainGoalie;
public Material redMaterial;

{
Physics.gravity *= gravityMultiplier; //for soccer a multiplier of 3 looks good
}
public override void AcademyReset()
{
Physics.gravity = new Vector3(0, -resetParameters["gravity"], 0);

{
}

5
UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerBallController.cs


using System.Collections;
using System.Collections;
[HideInInspector]
public SoccerFieldArea area;
public AgentSoccer lastTouchedBy; //who was the last to touch the ball

// Use this for initialization
void Start()
{
}
void OnCollisionEnter(Collision col)

13
UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerFieldArea.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

groundRenderer.material = groundMaterial;
}
void Awake()
{
academy = FindObjectOfType<SoccerAcademy>();

}
ps.agentScript.Done();
}
}
}

ps.agentScript.Done(); //all agents need to be reset
}
public Vector3 GetRandomSpawnPos(AgentSoccer.AgentRole role, AgentSoccer.Team team)
{
float xOffset = 0f;

xOffset = xOffset * -1f;
}
var randomSpawnPos = ground.transform.position +
new Vector3(xOffset, 0f, 0f)
+ (Random.insideUnitSphere * 2);
new Vector3(xOffset, 0f, 0f)
+ (Random.insideUnitSphere * 2);
randomSpawnPos.y = ground.transform.position.y + 2;
return randomSpawnPos;
}

var randomSpawnPos = ground.transform.position +
new Vector3(0f, 0f, 0f)
+ (Random.insideUnitSphere * 2);
new Vector3(0f, 0f, 0f)
+ (Random.insideUnitSphere * 2);
randomSpawnPos.y = ground.transform.position.y + 2;
return randomSpawnPos;
}

11
UnitySDK/Assets/ML-Agents/Examples/Template/Scripts/TemplateAcademy.cs


using System.Collections;
using System.Collections;
public class TemplateAcademy : Academy {
public class TemplateAcademy : Academy
{
}

14
UnitySDK/Assets/ML-Agents/Examples/Template/Scripts/TemplateAgent.cs


using System.Collections;
using System.Collections;
public class TemplateAgent : Agent {
public class TemplateAgent : Agent
{
{
{
}
}

3
UnitySDK/Assets/ML-Agents/Examples/Template/Scripts/TemplateDecision.cs


using System.Collections.Generic;
using System.Collections.Generic;
public override float[] Decide(
List<float> vectorObs,
List<Texture2D> visualObs,

24
UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/HitWall.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

{
if (lastAgentHit == 0)
{
agentA.AddReward( 0.1f);
agentA.AddReward(0.1f);
}
else
{

}
}

{
if (lastAgentHit == 0)
{
agentA.AddReward( -0.01f);
agentA.AddReward(-0.01f);
agentB.SetReward(0);
agentB.score += 1;
}

{
if (lastAgentHit == 0)
{
agentA.AddReward( -0.01f);
agentA.AddReward(-0.01f);
agentB.SetReward(0);
agentB.score += 1;
}

agentB.AddReward( -0.01f);
agentB.AddReward(-0.01f);
agentA.score += 1;
}
}

{
agentA.AddReward( -0.01f);
agentA.AddReward(-0.01f);
agentA.AddReward( -0.01f);
agentA.AddReward(-0.01f);
}
}
else if (collision.gameObject.name == "floorB")

agentA.SetReward(0);
agentB.AddReward( -0.01f);
agentB.AddReward(-0.01f);
agentB.AddReward( -0.01f);
agentB.AddReward(-0.01f);
agentA.score += 1;
}
}

{
agentA.AddReward( -0.01f);
agentA.AddReward(-0.01f);
agentB.SetReward(0);
agentB.score += 1;
}

agentB.AddReward( -0.01f);
agentB.AddReward(-0.01f);
agentA.score += 1;
}
}

4
UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

{
public override void AcademyReset()
{
Physics.gravity = new Vector3(0, -resetParameters["gravity"], 0);

{
}
}

15
UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

AddVectorObs(ballRb.velocity.y);
}
public override void AgentAction(float[] vectorAction, string textAction)
{
var moveX = Mathf.Clamp(vectorAction[0], -1f, 1f) * invertMult;

!invertX && transform.position.x - transform.parent.transform.position.x > -invertMult)
{
transform.position = new Vector3(-invertMult + transform.parent.transform.position.x,
transform.position.y,
transform.position.z);
transform.position.y,
transform.position.z);
}
textComponent.text = score.ToString();

{
angle = resetParams["angle"];
gameObject.transform.eulerAngles = new Vector3(
gameObject.transform.eulerAngles.x,
gameObject.transform.eulerAngles.y,
invertMult * angle
);
gameObject.transform.eulerAngles.x,
gameObject.transform.eulerAngles.y,
invertMult * angle
);
}
public void SetBall()

14
UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisArea.cs


using System.Collections;
using System.Collections;
public class TennisArea : MonoBehaviour {
public class TennisArea : MonoBehaviour
{
public GameObject ball;
public GameObject agentA;
public GameObject agentB;

void Start ()
void Start()
public void MatchReset()
public void MatchReset()
{
float ballOut = Random.Range(6f, 8f);
int flip = Random.Range(0, 2);

ball.GetComponent<HitWall>().lastAgentHit = -1;
}
void FixedUpdate()
void FixedUpdate()
{
Vector3 rgV = ballRb.velocity;
ballRb.velocity = new Vector3(Mathf.Clamp(rgV.x, -9f, 9f), Mathf.Clamp(rgV.y, -9f, 9f), rgV.z);

2
UnitySDK/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAcademy.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

6
UnitySDK/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;

[Header("Specific to Walker")] [Header("Target To Walk Towards")] [Space(10)]
[Header("Specific to Walker")][Header("Target To Walk Towards")][Space(10)]
public Transform target;
Vector3 dirToTarget;

{
dirToTarget = target.position - jdController.bodyPartsDict[hips].rb.position;
// Apply action to all relevant body parts.
// Apply action to all relevant body parts.
if (isNewDecisionStep)
{
var bpDict = jdController.bodyPartsDict;

18
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAcademy.cs


using System.Collections;
using System.Collections;
public class WallJumpAcademy : Academy {
public class WallJumpAcademy : Academy
{
public float agentRunSpeed;
public float agentRunSpeed;
public Material goalScoredMaterial;
//when fail, the ground will use this material for a few seconds.
public Material goalScoredMaterial;
//when fail, the ground will use this material for a few seconds.
public float gravityMultiplier = 2.5f;
public float gravityMultiplier = 2.5f;
public float agentJumpVelocity = 777;
public float agentJumpVelocity = 777;
public float agentJumpVelocityMaxChange = 10;
public float agentJumpVelocityMaxChange = 10;
// Use this for initialization
public override void InitializeAcademy()

76
UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs


//Put this script on your blue cube.
//Put this script on your blue cube.
using System.Collections;
using System.Collections.Generic;

spawnArea.SetActive(false);
}
jumpingTime = 0.2f;
jumpStartingPos = agentRB.position;
}

/// </summary>
/// <returns><c>true</c>, if the agent is on the ground,
/// <returns><c>true</c>, if the agent is on the ground,
/// <param name="boxWidth">The width of the box used to perform
/// <param name="boxWidth">The width of the box used to perform
/// the ground check. </param>
public bool DoGroundCheck(bool smallCheck)
{

bool grounded = false;
foreach (Collider col in hitGroundColliders)
{
if (col != null && col.transform != this.transform &&
(col.CompareTag("walkableSurface") ||
col.CompareTag("block") ||

}
else
{
RaycastHit hit;
Physics.Raycast(transform.position + new Vector3(0, -0.05f, 0), -Vector3.up, out hit,
1f);

}
}
/// <summary>
/// Moves a rigidbody towards a position smoothly.
/// </summary>

public override void CollectObservations()
{
float rayDistance = 20f;
float[] rayAngles = { 0f, 45f, 90f, 135f, 180f, 110f, 70f };
AddVectorObs(rayPer.Perceive(
rayDistance, rayAngles, detectableObjects, 0f, 0f));
AddVectorObs(rayPer.Perceive(
rayDistance, rayAngles, detectableObjects, 2.5f, 2.5f));
Vector3 agentPos = agentRB.position - ground.transform.position;
float rayDistance = 20f;
float[] rayAngles = { 0f, 45f, 90f, 135f, 180f, 110f, 70f };
AddVectorObs(rayPer.Perceive(
rayDistance, rayAngles, detectableObjects, 0f, 0f));
AddVectorObs(rayPer.Perceive(
rayDistance, rayAngles, detectableObjects, 2.5f, 2.5f));
Vector3 agentPos = agentRB.position - ground.transform.position;
AddVectorObs(agentPos / 20f);
AddVectorObs(DoGroundCheck(true) ? 1 : 0);
AddVectorObs(agentPos / 20f);
AddVectorObs(DoGroundCheck(true) ? 1 : 0);
}
/// <summary>

{
Vector3 randomSpawnPos = Vector3.zero;
float randomPosX = Random.Range(-spawnAreaBounds.extents.x,
spawnAreaBounds.extents.x);
spawnAreaBounds.extents.x);
spawnAreaBounds.extents.z);
spawnAreaBounds.extents.z);
new Vector3(randomPosX, 0.45f, randomPosZ);
new Vector3(randomPosX, 0.45f, randomPosZ);
return randomSpawnPos;
}

groundRenderer.material = groundMaterial;
}
AddReward(-0.0005f);
bool smallGrounded = DoGroundCheck(true);
bool largeGrounded = DoGroundCheck(false);

int dirToGoForwardAction = (int) act[0];
int rotateDirAction = (int) act[1];
int dirToGoSideAction = (int) act[2];
int jumpAction = (int) act[3];
int dirToGoForwardAction = (int)act[0];
int rotateDirAction = (int)act[1];
int dirToGoSideAction = (int)act[2];
int jumpAction = (int)act[3];
if (dirToGoForwardAction==1)
if (dirToGoForwardAction == 1)
else if (dirToGoForwardAction==2)
else if (dirToGoForwardAction == 2)
if (rotateDirAction==1)
if (rotateDirAction == 1)
else if (rotateDirAction==2)
else if (rotateDirAction == 2)
if (dirToGoSideAction==1)
if (dirToGoSideAction == 1)
else if (dirToGoSideAction==2)
else if (dirToGoSideAction == 2)
dirToGo = transform.right * 0.6f * (largeGrounded ? 1f : 0.5f);
if (jumpAction == 1)
if ((jumpingTime <= 0f) && smallGrounded)

transform.Rotate(rotateDir, Time.fixedDeltaTime * 300f);
agentRB.AddForce(dirToGo * academy.agentRunSpeed,
ForceMode.VelocityChange);
ForceMode.VelocityChange);
new Vector3(agentRB.position.x,
jumpStartingPos.y + academy.agentJumpHeight,
agentRB.position.z) + dirToGo;
new Vector3(agentRB.position.x,
jumpStartingPos.y + academy.agentJumpHeight,
agentRB.position.z) + dirToGo;
academy.agentJumpVelocityMaxChange);
academy.agentJumpVelocityMaxChange);
Vector3.down * fallingForce, ForceMode.Acceleration);
Vector3.down * fallingForce, ForceMode.Acceleration);
}
jumpingTime -= Time.fixedDeltaTime;
}

18 * (Random.value - 0.5f), 1, -12);
configuration = Random.Range(0, 5);
agentRB.velocity = default(Vector3);
}
private void FixedUpdate()

/// Configures the agent. Given an integer config, the wall will have
/// different height and a different brain will be assigned to the agent.
/// </summary>
/// <param name="config">Config.
/// <param name="config">Config.
/// If 0 : No wall and noWallBrain.
/// If 1: Small wall and smallWallBrain.
/// Other : Tall wall and BigWallBrain. </param>

float height =
academy.resetParameters["big_wall_min_height"] +
Random.value * (academy.resetParameters["big_wall_max_height"] -
academy.resetParameters["big_wall_min_height"]);
academy.resetParameters["big_wall_min_height"]);
wall.transform.localScale = new Vector3(
wall.transform.localScale.x,
height,

}
}

8
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/Editor/BarracudaEditor/NNModelImporter.cs


using System.IO;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEditor.Experimental.AssetImporters;

/// Asset Importer of barracuda models.
/// </summary>
[ScriptedImporter(1, new[] {"nn"})]
public class NNModelImporter : ScriptedImporter {
public class NNModelImporter : ScriptedImporter
{
private const string iconName = "NNModelIcon";
private Texture2D iconTexture;

var model = File.ReadAllBytes(ctx.assetPath);
var asset = ScriptableObject.CreateInstance<NNModel>();
asset.Value = model;
ctx.AddObjectToAsset("main obj", asset, LoadIconTexture());
ctx.SetMainObject(asset);
}

}
return iconTexture;
}
}
}

12
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/OSX/MacBLAS.cs


public class MacBLAS : BLASPlugin
{
[DllImport("macblas")]
static extern unsafe void macsgemm(float* Ap, int AN, int AM,
float* Bp, int BN, int BM,
float* Cp, int CN, int CM,
int bs, bool transposeA, bool transposeB);
static extern unsafe void macsgemm(float* Ap, int AN, int AM,
float* Bp, int BN, int BM,
float* Cp, int CN, int CM,
int bs, bool transposeA, bool transposeB);
return Application.platform == RuntimePlatform.OSXEditor ||
Application.platform == RuntimePlatform.OSXPlayer;
return Application.platform == RuntimePlatform.OSXEditor ||
Application.platform == RuntimePlatform.OSXPlayer;
}
public unsafe void SGEMM(float* Ap, int AN, int AM, float* Bp, int BN, int BM, float* Cp, int CN, int CM, int bs,

10
UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/iOS/iOSBLAS.cs


public class iOSBLAS : BLASPlugin
{
[DllImport("__Internal")]
static extern unsafe void iossgemm(float* Ap, int AN, int AM,
float* Bp, int BN, int BM,
float* Cp, int CN, int CM,
int bs, bool transposeA, bool transposeB);
static extern unsafe void iossgemm(float* Ap, int AN, int AM,
float* Bp, int BN, int BM,
float* Cp, int CN, int CM,
int bs, bool transposeA, bool transposeB);
public bool IsCurrentPlatformSupported()
{

iossgemm(Ap, AN, AM, Bp, BN, BM, Cp, CN, CM, bs, transposeA, transposeB);
}
}
#endif // UNITY_IOS
#endif // UNITY_IOS

34
UnitySDK/Assets/ML-Agents/Scripts/Academy.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;

[Tooltip("Height of the environment window in pixels.")]
public int height;
[Tooltip("Rendering quality of environment. (Higher is better quality.)")] [Range(0, 5)]
[Tooltip("Rendering quality of environment. (Higher is better quality.)")][Range(0, 5)]
[Tooltip("Speed at which environment is run. (Higher is faster.)")] [Range(1f, 100f)]
[Tooltip("Speed at which environment is run. (Higher is faster.)")][Range(1f, 100f)]
public float timeScale;
[Tooltip("Frames per second (FPS) engine attempts to maintain.")]

/// attached to it (which may be internal, heuristic or player).
/// </remarks>
[HelpURL("https://github.com/Unity-Technologies/ml-agents/blob/master/" +
"docs/Learning-Environment-Design-Academy.md")]
"docs/Learning-Environment-Design-Academy.md")]
public abstract class Academy : MonoBehaviour
{
[SerializeField]

[SerializeField]
[Tooltip("Total number of steps per global episode.\nNon-positive " +
"values correspond to episodes without a maximum number of \n" +
"steps. Once the step counter reaches this maximum value, the " +
"environment will reset.")]
"values correspond to episodes without a maximum number of \n" +
"steps. Once the step counter reaches this maximum value, the " +
"environment will reset.")]
"quality and engine speed during Training.")]
"quality and engine speed during Training.")]
"quality and engine speed during Inference.")]
"quality and engine speed during Inference.")]
EnvironmentConfiguration inferenceConfiguration =
new EnvironmentConfiguration(1280, 720, 5, 1.0f, 60);

/// </remarks>
[SerializeField]
[Tooltip("List of custom parameters that can be changed in the " +
"environment when it resets.")]
"environment when it resets.")]
public ResetParameters resetParameters;
public CommunicatorObjects.CustomResetParameters customResetParameters;

// in inference mode.
isInference = !isCommunicatorOn;
BrainDecideAction += () => { };
DestroyAction += () => { };
AgentSetStatus += (m, d, i) => { };
AgentResetIfDone += () => { };
AgentSendState += () => { };
AgentAct += () => { };
AgentForceReset += () => { };
BrainDecideAction += () => {};
DestroyAction += () => {};
AgentSetStatus += (m, d, i) => {};
AgentResetIfDone += () => {};
AgentSendState += () => {};
AgentAct += () => {};
AgentForceReset += () => {};
// Configure the environment using the configurations provided by
// the developer in the Editor.

21
UnitySDK/Assets/ML-Agents/Scripts/ActionMasker.cs


using System;
using System;
using System.Collections.Generic;
using System.Linq;

{
this._brainParameters = brainParameters;
}
/// <summary>
/// Modifies an action mask for discrete control agents. When used, the agent will not be
/// able to perform the action passed as argument at the next decision. If no branch is

/// <param name="branch">The branch for which the actions will be masked</param>
/// <param name="actionIndices">The indices of the masked actions</param>
public void SetActionMask(int branch, IEnumerable<int> actionIndices)
{
{
if (branch >= _brainParameters.vectorActionSize.Length )
if (branch >= _brainParameters.vectorActionSize.Length)
"Invalid Action Masking : Branch "+branch+" does not exist.");
"Invalid Action Masking : Branch " + branch + " does not exist.");
// By default, the masks are null. If we want to specify a new mask, we initialize
// the actionMasks with trues.
if (_currentMask == null)

{
_startingActionIndices = Utilities.CumSum(_brainParameters.vectorActionSize);
}
// Perform the masking
foreach (var actionIndex in actionIndices)
{

"Invalid Action Masking: Action Mask is too large for specified branch.");
}
_currentMask[actionIndex + _startingActionIndices[branch]] = true;
}
}
}
/// <summary>

throw new UnityAgentsException(
"Invalid Action Masking : Can only set action mask for Discrete Control.");
}
for (var branchIndex = 0 ; branchIndex < numBranches; branchIndex++ )
for (var branchIndex = 0; branchIndex < numBranches; branchIndex++)
{
if (AreAllActionsMasked(branchIndex))
{

}
}
return true;
}
}
}

15
UnitySDK/Assets/ML-Agents/Scripts/Agent.cs


/// see the Examples/ directory within this Unity project.
/// </remarks>
[HelpURL("https://github.com/Unity-Technologies/ml-agents/blob/master/" +
"docs/Learning-Environment-Design-Agents.md")]
"docs/Learning-Environment-Design-Agents.md")]
[System.Serializable]
public abstract class Agent : MonoBehaviour
{

new List<float>(param.vectorObservationSize);
info.stackedVectorObservation =
new List<float>(param.vectorObservationSize
* brain.brainParameters.numStackedVectorObservations);
* brain.brainParameters.numStackedVectorObservations);
info.stackedVectorObservation.AddRange(
new float[param.vectorObservationSize
* param.numStackedVectorObservations]);

Utilities.ShiftLeft(info.stackedVectorObservation, param.vectorObservationSize);
Utilities.ReplaceRange(info.stackedVectorObservation, info.vectorObservation,
info.stackedVectorObservation.Count - info.vectorObservation.Count);
info.stackedVectorObservation.Count - info.vectorObservation.Count);
var visualObservationCount = agentParameters.agentCameras.Count+agentParameters.agentRenderTextures.Count;
var visualObservationCount = agentParameters.agentCameras.Count + agentParameters.agentRenderTextures.Count;
if (param.cameraResolutions.Length > visualObservationCount)
{
throw new UnityAgentsException(string.Format(

{
var obsTexture = ObservationToTexture(
agentParameters.agentRenderTextures[i],
param.cameraResolutions[camCount+i].width,
param.cameraResolutions[camCount+i].height);
param.cameraResolutions[camCount + i].width,
param.cameraResolutions[camCount + i].height);
info.visualObservations.Add(obsTexture);
}

{
actionMasker.SetActionMask(branch, actionIndices);
}
/// <summary>
/// Adds a float observation to the vector observations of the agent.

texture2D.Resize(width, height);
}
if(width != obsTexture.width || height != obsTexture.height)
if (width != obsTexture.width || height != obsTexture.height)
{
throw new UnityAgentsException(string.Format(
"RenderTexture {0} : width/height is {1}/{2} brain is expecting {3}/{4}.",

10
UnitySDK/Assets/ML-Agents/Scripts/BCTeacherHelper.cs


using System.Collections;
using System.Collections;
/// Behavioral Cloning Helper script. Attach to teacher agent to enable
/// Behavioral Cloning Helper script. Attach to teacher agent to enable
bool recordExperiences;
bool resetBuffer;
Agent myAgent;

Monitor.Log("Recording experiences " + recordKey, recordExperiences.ToString());
float timeSinceBufferReset = Time.time - bufferResetTime;
Monitor.Log("Seconds since buffer reset " + resetKey,
Monitor.Log("Seconds since buffer reset " + resetKey,
Mathf.FloorToInt(timeSinceBufferReset).ToString());
}

// assumption that this structure is preserved.
// assumption that this structure is preserved.
myAgent.SetTextObs(recordExperiences + "," + resetBuffer);
}
}

2
UnitySDK/Assets/ML-Agents/Scripts/Batcher.cs


using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Google.Protobuf;

9
UnitySDK/Assets/ML-Agents/Scripts/Brain.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
namespace MLAgents

}
LazyInitialize();
}
/// <summary>
/// Adds the data of an agent to the current batch so it will be processed in DecideAction.
/// </summary>

{
LazyInitialize();
agentInfos.Add(agent, info);
}
/// <summary>

}
}
}
/// <summary>
/// Called by the Academy when it shuts down. This ensures that the Brain cleans up properly
/// after scene changes.

if (_isInitialized)
{
agentInfos.Clear();
_isInitialized = false;
}
}

18
UnitySDK/Assets/ML-Agents/Scripts/BrainParameters.cs


using UnityEngine;
using UnityEngine;
using System.Linq;
namespace MLAgents

/**< \brief The height of the observation in pixels */
public bool blackAndWhite;
/**< \brief If true, the image will be in black and white.
/**< \brief If true, the image will be in black and white.
* If false, it will be in colors RGB */
}

public class BrainParameters
{
public int vectorObservationSize = 1;
/**< \brief If continuous : The length of the float vector that represents
/**< \brief If continuous : The length of the float vector that represents
* the state
* <br> If discrete : The number of possible values the state can take*/

/// <param name="name">The name of the brain.</param>
/// <param name="isTraining">Whether or not the Brain is training.</param>
public CommunicatorObjects.BrainParametersProto
ToProto(string name, bool isTraining)
ToProto(string name, bool isTraining)
{
var brainParametersProto = new CommunicatorObjects.BrainParametersProto
{

VectorActionSpaceType =
(CommunicatorObjects.SpaceTypeProto) vectorActionSpaceType,
(CommunicatorObjects.SpaceTypeProto)vectorActionSpaceType,
BrainName = name,
IsTraining = isTraining
};

numStackedVectorObservations = brainParametersProto.NumStackedVectorObservations;
vectorActionSize = brainParametersProto.VectorActionSize.ToArray();
vectorActionDescriptions = brainParametersProto.VectorActionDescriptions.ToArray();
vectorActionSpaceType = (SpaceType) brainParametersProto.VectorActionSpaceType;
vectorActionSpaceType = (SpaceType)brainParametersProto.VectorActionSpaceType;
}
/// <summary>

{
vectorObservationSize = this.vectorObservationSize,
numStackedVectorObservations = this.numStackedVectorObservations,
vectorActionSize = (int[]) this.vectorActionSize.Clone(),
cameraResolutions = (Resolution[]) this.cameraResolutions.Clone(),
vectorActionDescriptions = (string[]) this.vectorActionDescriptions.Clone(),
vectorActionSize = (int[])this.vectorActionSize.Clone(),
cameraResolutions = (Resolution[])this.cameraResolutions.Clone(),
vectorActionDescriptions = (string[])this.vectorActionDescriptions.Clone(),
vectorActionSpaceType = this.vectorActionSpaceType
};
}

10
UnitySDK/Assets/ML-Agents/Scripts/BroadcastHub.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
namespace MLAgents

public int Count
{
get { return broadcastingBrains.Count; }
}
}
/// Checks that a given Brain is set to be remote controlled.
/// Checks that a given Brain is set to be remote controlled.
/// </summary>
/// <param name="brain"> The Brain that is beeing checked</param>
/// <returns>true if the Brain is set to Controlled and false otherwise. Will return

return _brainsToControl.Contains(brain);
}
/// <summary>
/// Sets a brain to controlled.
/// </summary>

}
}
}
/// <summary>
/// Removes all the Brains of the BroadcastHub
/// </summary>

15
UnitySDK/Assets/ML-Agents/Scripts/Communicator.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents.CommunicatorObjects;

}
/**
This is the interface of the Communicators.
This is the interface of the Communicators.
This does not need to be modified nor implemented to create a Unity environment.
When the Unity Communicator is initialized, it will wait for the External Communicator

External. Inputs and outputs are relative to Unity.
By convention, when the Unity Communicator and External Communicator call exchange, the
exchange is NOT simultaneous but sequential. This means that when a side of the
communication calls exchange, the other will receive the result of its previous
exchange is NOT simultaneous but sequential. This means that when a side of the
communication calls exchange, the other will receive the result of its previous
Since the messages are sent back and forth with exchange and simultaneously when calling
Since the messages are sent back and forth with exchange and simultaneously when calling
initialize, External sends two messages at initialization.
The structure of the messages is as follows:

public interface Communicator
{
/// <summary>
/// Initialize the communicator by sending the first UnityOutput and receiving the
/// Initialize the communicator by sending the first UnityOutput and receiving the
/// first UnityInput. The second UnityInput is stored in the unityInput argument.
/// </summary>
/// <returns>The first Unity Input.</returns>

out UnityInput unityInput);
out UnityInput unityInput);
/// <summary>
/// Send a UnityOutput and receives a UnityInput.

/// Close the communicator gracefully on both sides of the communication.
/// </summary>
void Close();
}
}

10
UnitySDK/Assets/ML-Agents/Scripts/Decision.cs


using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
namespace MLAgents

public abstract class Decision : ScriptableObject
{
public BrainParameters brainParameters;
/// Defines the decision-making logic of the agent. Given the information
/// Defines the decision-making logic of the agent. Given the information
/// about the agent, returns a vector of actions.
/// </summary>
/// <returns>Vector action vector.</returns>

/// <param name="done">Whether or not the agent is done.</param>
/// <param name="memory">
/// The memories stored from the previous step with
/// The memories stored from the previous step with
vectorObs,
vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,

8
UnitySDK/Assets/ML-Agents/Scripts/Demonstration.cs


using System;
using System;
using MLAgents.CommunicatorObjects;
using UnityEngine;

public DemonstrationMetaData metaData;
public BrainParameters brainParameters;
public void Initialize(BrainParameters brainParameters,
public void Initialize(BrainParameters brainParameters,
DemonstrationMetaData demonstrationMetaData)
{
this.brainParameters = brainParameters;

/// <summary>
/// Demonstration meta-data.
/// Kept in a struct for easy serialization and deserialization.

}
/// <summary>
/// Initialize metadata values based on proto object.
/// Initialize metadata values based on proto object.
/// </summary>
public DemonstrationMetaData(DemonstrationMetaProto demoProto)
{

8
UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs


demoStore = new DemonstrationStore();
demonstrationName = SanitizeName(demonstrationName, MaxNameLength);
demoStore.Initialize(
demonstrationName,
recordingAgent.brain.brainParameters,
recordingAgent.brain.name);
demonstrationName,
recordingAgent.brain.brainParameters,
recordingAgent.brain.name);
Monitor.Log("Recording Demonstration of Agent: ", recordingAgent.name);
}

{
var rgx = new Regex("[^a-zA-Z0-9 -]");
demoName = rgx.Replace(demoName, "");
// If the string is too long, it will overflow the metadata.
// If the string is too long, it will overflow the metadata.
if (demoName.Length > maxNameLength)
{
demoName = demoName.Substring(0, maxNameLength);

2
UnitySDK/Assets/ML-Agents/Scripts/DemonstrationStore.cs


using System.IO;
using System.IO;
using System.IO.Abstractions;
using Google.Protobuf;
using MLAgents.CommunicatorObjects;

5
UnitySDK/Assets/ML-Agents/Scripts/HeuristicBrain.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

}
#endif
}
/// <inheritdoc/>
protected override void Initialize()
{

agentInfos[agent].reward,
agentInfos[agent].done,
agentInfos[agent].memories));
}
foreach (Agent agent in agentInfos.Keys)
{

12
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ApplierImpl.cs


using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Barracuda;

var batchSize = agentInfo.Keys.Count;
var actions = new float[batchSize, _actionSize.Length];
var startActionIndices = Utilities.CumSum(_actionSize);
for (var actionIndex=0; actionIndex < _actionSize.Length; actionIndex++)
for (var actionIndex = 0; actionIndex < _actionSize.Length; actionIndex++)
shape = new long[]{batchSize, nBranchAction},
shape = new long[] {batchSize, nBranchAction},
data = _allocator.Alloc(new TensorShape(batchSize, nBranchAction))
};

branchActionIndex < nBranchAction;
branchActionIndex++)
branchActionIndex < nBranchAction;
branchActionIndex++)
{
actionProbs.data[batchIndex, branchActionIndex] =
tensorProxy.data[batchIndex, startActionIndices[actionIndex] + branchActionIndex];

var outputTensor = new TensorProxy()
{
valueType = TensorProxy.TensorType.FloatingPoint,
shape = new long[]{batchSize, 1},
shape = new long[] {batchSize, 1},
data = _allocator.Alloc(new TensorShape(batchSize, 1))
};

44
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs


using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Barracuda;

// If there are not enough Visual Observation Input compared to what the
// Brain Parameters expect.
for (var visObsIndex = 0;
visObsIndex < _brainParameters.cameraResolutions.Length;
visObsIndex++)
visObsIndex < _brainParameters.cameraResolutions.Length;
visObsIndex++)
{
if (!tensorsNames.Contains(
TensorNames.VisualObservationPlaceholderPrefix + visObsIndex))

"for visual observation "+visObsIndex+".");
"for visual observation " + visObsIndex + ".");
}
}

{
var tensorTester =
new Dictionary<string, Func<TensorProxy, string>>()
{
{TensorNames.VectorObservationPlacholder, CheckVectorObsShape},
{TensorNames.PreviousActionPlaceholder, CheckPreviousActionShape},
{TensorNames.RandomNormalEpsilonPlaceholder, ((tensor) => null)},
{TensorNames.ActionMaskPlaceholder, ((tensor) => null)},
{TensorNames.SequenceLengthPlaceholder, ((tensor) => null)},
{TensorNames.RecurrentInPlaceholder, ((tensor) => null)},
};
{
{TensorNames.VectorObservationPlacholder, CheckVectorObsShape},
{TensorNames.PreviousActionPlaceholder, CheckPreviousActionShape},
{TensorNames.RandomNormalEpsilonPlaceholder, ((tensor) => null)},
{TensorNames.ActionMaskPlaceholder, ((tensor) => null)},
{TensorNames.SequenceLengthPlaceholder, ((tensor) => null)},
{TensorNames.RecurrentInPlaceholder, ((tensor) => null)},
};
foreach (var mem in _model.memories)
{

if (vecObsSizeBp * numStackedVector != totalVecObsSizeT)
{
return "Vector Observation Size of the model does not match. Received " +
$"{vecObsSizeBp} x {numStackedVector} but was expecting {totalVecObsSizeT}.";
$"{vecObsSizeBp} x {numStackedVector} but was expecting {totalVecObsSizeT}.";
}
return null;
}

{
var numberActionsBp = _brainParameters.vectorActionSize.Length;
var numberActionsT = tensorProxy.shape[tensorProxy.shape.Length - 1];
if (numberActionsBp != numberActionsT)
if (numberActionsBp != numberActionsT)
$"Received {numberActionsBp} but was expecting {numberActionsT}.";
$"Received {numberActionsBp} but was expecting {numberActionsT}.";
}
return null;
}

var heightT = tensorProxy.shape[1];
var widthT = tensorProxy.shape[2];
var pixelT = tensorProxy.shape[3];
if ((widthBp != widthT) || (heightBp != heightT) || (pixelBp != pixelT))
if ((widthBp != widthT) || (heightBp != heightT) || (pixelBp != pixelT))
$"Received TensorProxy of shape [?x{widthBp}x{heightBp}x{pixelBp}] but " +
$"was expecting [?x{widthT}x{heightT}x{pixelT}].";
$"Received TensorProxy of shape [?x{widthBp}x{heightBp}x{pixelBp}] but " +
$"was expecting [?x{widthT}x{heightT}x{pixelT}].";
}
return null;
}

private string CheckDiscreteActionOutputShape(TensorShape shape, int modelActionSize)
{
var bpActionSize = _brainParameters.vectorActionSize.Sum();
if (modelActionSize != bpActionSize)
if (modelActionSize != bpActionSize)
$"{bpActionSize} but the model contains {modelActionSize}.";
$"{bpActionSize} but the model contains {modelActionSize}.";
}
return null;
}

private string CheckContinuousActionOutputShape(TensorShape shape, int modelActionSize)
{
var bpActionSize = _brainParameters.vectorActionSize[0];
if (modelActionSize != bpActionSize)
if (modelActionSize != bpActionSize)
$"{bpActionSize} but the model contains {modelActionSize}.";
$"{bpActionSize} but the model contains {modelActionSize}.";
}
return null;
}

6
UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/GeneratorImpl.cs


using System.Collections.Generic;
using System.Collections.Generic;
using System;
using System.Linq;
using Barracuda;

public void Generate(TensorProxy tensorProxy, int batchSize, Dictionary<Agent, AgentInfo> agentInfo)
{
tensorProxy.data?.Dispose();
tensorProxy.data = _allocator.Alloc(new TensorShape(1,1));
tensorProxy.data = _allocator.Alloc(new TensorShape(1, 1));
tensorProxy.data[0] = batchSize;
}
}

{
tensorProxy.shape = new long[0];
tensorProxy.data?.Dispose();
tensorProxy.data = _allocator.Alloc(new TensorShape(1,1));
tensorProxy.data = _allocator.Alloc(new TensorShape(1, 1));
tensorProxy.data[0] = 1;
}
}

部分文件因为文件数量过多而无法显示

正在加载...
取消
保存