浏览代码

Merge remote-tracking branch 'origin/develop' into try-tf2-support

/develop-gpu-test
Chris Elion 5 年前
当前提交
3d8a70fb
共有 321 个文件被更改,包括 3716 次插入2690 次删除
  1. 67
      .circleci/config.yml
  2. 19
      .pre-commit-config.yaml
  3. 4
      SURVEY.md
  4. 61
      UnitySDK/Assets/ML-Agents/Editor/AgentEditor.cs
  5. 214
      UnitySDK/Assets/ML-Agents/Editor/BrainParametersDrawer.cs
  6. 30
      UnitySDK/Assets/ML-Agents/Editor/DemonstrationDrawer.cs
  7. 2
      UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs
  8. 26
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorGenerator.cs
  9. 113
      UnitySDK/Assets/ML-Agents/Editor/Tests/MLAgentsEditModeTest.cs
  10. 29
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Prefabs/3DBall.prefab
  11. 24
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Prefabs/3DBallHardNew.prefab
  12. 47
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scenes/3DBall.unity
  13. 34
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs
  14. 25
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
  15. 24
      UnitySDK/Assets/ML-Agents/Examples/Basic/Prefabs/Basic.prefab
  16. 13
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
  17. 24
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Prefabs/Environment.prefab
  18. 10
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
  19. 25
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Prefabs/DynamicPlatform.prefab
  20. 24
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Prefabs/FixedPlatform.prefab
  21. 128
      UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Prefabs/FoodCollectorArea.prefab
  22. 868
      UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Prefabs/VisualFoodCollectorArea.prefab
  23. 92
      UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Scenes/VisualFoodCollector.unity
  24. 102
      UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorAgent.cs
  25. 849
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scenes/GridWorld.unity
  26. 113
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAcademy.cs
  27. 32
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  28. 24
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Prefabs/SymbolFinderArea.prefab
  29. 43
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Prefabs/VisualSymbolFinderArea.prefab
  30. 79
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scenes/VisualHallway.unity
  31. 57
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs
  32. 24
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockArea.prefab
  33. 37
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockVisualArea.prefab
  34. 59
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scenes/VisualPushBlock.unity
  35. 21
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs
  36. 24
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Prefabs/AreaPB.prefab
  37. 55
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Prefabs/VisualAreaPyramids.prefab
  38. 73
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scenes/VisualPyramids.unity
  39. 65
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs
  40. 24
      UnitySDK/Assets/ML-Agents/Examples/Reacher/Prefabs/Agent.prefab
  41. 9
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs
  42. 9
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception2D.cs
  43. 4
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs
  44. 96
      UnitySDK/Assets/ML-Agents/Examples/Soccer/Prefabs/SoccerFieldTwos.prefab
  45. 48
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Prefabs/TennisArea.prefab
  46. 9
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs
  47. 24
      UnitySDK/Assets/ML-Agents/Examples/Walker/Prefabs/WalkerPair.prefab
  48. 30
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Prefabs/WallJumpArea.prefab
  49. 8
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scenes/WallJump.unity
  50. 37
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs
  51. 2
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda.md
  52. 2
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/LICENSE.md
  53. 10
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/ReleaseNotes.md
  54. 120
      UnitySDK/Assets/ML-Agents/Scripts/Academy.cs
  55. 316
      UnitySDK/Assets/ML-Agents/Scripts/Agent.cs
  56. 4
      UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs
  57. 69
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/CommunicatorObjects/AgentInfo.cs
  58. 44
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/CommunicatorObjects/BrainParameters.cs
  59. 9
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/CommunicatorObjects/SpaceType.cs
  60. 2
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/CommunicatorObjects/CompressedObservation.cs.meta
  61. 56
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/GrpcExtensions.cs
  62. 181
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/RpcCommunicator.cs
  63. 22
      UnitySDK/Assets/ML-Agents/Scripts/ICommunicator.cs
  64. 354
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs
  65. 16
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/GeneratorImpl.cs
  66. 23
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorGenerator.cs
  67. 35
      UnitySDK/Assets/ML-Agents/Scripts/Timer.cs
  68. 69
      UnitySDK/Assets/ML-Agents/Scripts/Utilities.cs
  69. 4
      UnitySDK/README.md
  70. 8
      config/gail_config.yaml
  71. 2
      config/offline_bc_config.yaml
  72. 53
      config/sac_trainer_config.yaml
  73. 42
      config/trainer_config.yaml
  74. 2
      docs/Background-TensorFlow.md
  75. 75
      docs/Basic-Guide.md
  76. 20
      docs/Creating-Custom-Protobuf-Messages.md
  77. 2
      docs/FAQ.md
  78. 4
      docs/Feature-Memory.md
  79. 118
      docs/Getting-Started-with-Balance-Ball.md
  80. 12
      docs/Glossary.md
  81. 16
      docs/Installation.md
  82. 7
      docs/Learning-Environment-Best-Practices.md
  83. 212
      docs/Learning-Environment-Create-New.md
  84. 2
      docs/Learning-Environment-Design-Academy.md
  85. 206
      docs/Learning-Environment-Design-Agents.md
  86. 66
      docs/Learning-Environment-Design.md
  87. 76
      docs/Learning-Environment-Examples.md
  88. 17
      docs/Learning-Environment-Executable.md
  89. 97
      docs/ML-Agents-Overview.md
  90. 9
      docs/Migrating.md
  91. 6
      docs/Readme.md
  92. 2
      docs/Reward-Signals.md
  93. 35
      docs/Training-Behavioral-Cloning.md
  94. 14
      docs/Training-Curriculum-Learning.md
  95. 70
      docs/Training-Generalized-Reinforcement-Learning-Agents.md
  96. 26
      docs/Training-ML-Agents.md
  97. 2
      docs/Training-PPO.md
  98. 2
      docs/Training-SAC.md
  99. 2
      docs/Training-Using-Concurrent-Unity-Instances.md
  100. 5
      docs/Training-on-Amazon-Web-Service.md

67
.circleci/config.yml


. venv/bin/activate
pip install --upgrade pip
pip install grpcio-tools==1.13.0 --progress-bar=off
pip install mypy-protobuf --progress-bar=off
pip install mypy-protobuf==1.16.0 --progress-bar=off
- save_cache:
paths:
- ./venv

path: /tmp/proto.patch
destination: proto.patch
deploy:
parameters:
directory:
type: string
description: Local directory to use for publishing (e.g. ml-agents)
docker:
- image: circleci/python:3.6
steps:
- checkout
- run:
name: install python dependencies
command: |
python3 -m venv venv
. venv/bin/activate
pip install --upgrade pip
pip install setuptools wheel twine
- run:
name: verify git tag vs. version
command: |
python3 -m venv venv
. venv/bin/activate
cd << parameters.directory >>
python setup.py verify
- run:
name: create packages
command: |
. venv/bin/activate
cd << parameters.directory >>
python setup.py sdist
python setup.py bdist_wheel
- run:
name: upload to pypi
# To upload to test, just add the following flag to twine upload:
# --repository-url https://test.pypi.org/legacy/
# and change the username to "mlagents-test"
command: |
. venv/bin/activate
cd << parameters.directory >>
twine upload -u mlagents -p $PYPI_PASSWORD dist/*
version: 2
workflow:
jobs:
- build_python:

pip_constraints: test_constraints_max_tf2_version.txt
- markdown_link_check
- protobuf_generation_check
- deploy:
name: deploy ml-agents-envs
directory: ml-agents-envs
filters:
tags:
only: /[0-9]+(\.[0-9]+)*(\.dev[0-9]+)*/
branches:
ignore: /.*/
- deploy:
name: deploy ml-agents
directory: ml-agents
filters:
tags:
only: /[0-9]+(\.[0-9]+)*(\.dev[0-9]+)*/
branches:
ignore: /.*/
- deploy:
name: deploy gym-unity
directory: gym-unity
filters:
tags:
only: /[0-9]+(\.[0-9]+)*(\.dev[0-9]+)*/
branches:
ignore: /.*/

19
.pre-commit-config.yaml


.*_pb2.py|
.*_pb2_grpc.py
)$
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.720
hooks:

name: mypy-gym-unity
files: "gym-unity/.*"
args: [--ignore-missing-imports, --disallow-incomplete-defs]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:

.*_pb2.py|
.*_pb2_grpc.py
)$
additional_dependencies: [flake8-tidy-imports]
# flake8-tidy-imports is used for banned-modules, not actually tidying
additional_dependencies: [flake8-comprehensions, flake8-tidy-imports]
- id: trailing-whitespace
name: trailing-whitespace-markdown
types: [markdown]
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.4.1 # Use the ref you want to point at
hooks:
- id: python-check-mock-methods
# "Local" hooks, see https://pre-commit.com/#repository-local-hooks
- repo: local
hooks:

exclude: ".*localized.*"
# Only run manually, e.g. pre-commit run --hook-stage manual markdown-link-check
stages: [manual]
- id: validate-versions
name: validate library versions
language: script
entry: utils/validate_versions.py
files: ".*/setup.py"

4
SURVEY.md


# Unity ML-Agents Toolkit Survey
Your opinion matters a great deal to us. Only by hearing your thoughts on the Unity ML-Agents Toolkit can we continue to improve and grow. Please take a few minutes to let us know about it.
Your opinion matters a great deal to us. Only by hearing your thoughts on the Unity ML-Agents Toolkit can we continue to improve and grow. Please take a few minutes to let us know about it.
[Fill out the survey](https://goo.gl/forms/qFMYSYr5TlINvG6f1)
[Fill out the survey](https://goo.gl/forms/qFMYSYr5TlINvG6f1)

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


using UnityEngine;
using UnityEditor;
using Barracuda;
/*
This code is meant to modify the behavior of the inspector on Brain Components.
Depending on the type of brain that is used, the available fields will be modified in the inspector accordingly.
*/
/*
This code is meant to modify the behavior of the inspector on Agent Components.
*/
[CustomEditor(typeof(Agent), true)]
[CanEditMultipleObjects]
public class AgentEditor : Editor

var serializedAgent = serializedObject;
serializedAgent.Update();
var brain = serializedAgent.FindProperty("brain");
var actionsPerDecision = serializedAgent.FindProperty(
"agentParameters.numberOfActionsBetweenDecisions");
var maxSteps = serializedAgent.FindProperty(

var isOdd = serializedAgent.FindProperty(
"agentParameters.onDemandDecision");
var cameras = serializedAgent.FindProperty(
"agentParameters.agentCameras");
var renderTextures = serializedAgent.FindProperty(
"agentParameters.agentRenderTextures");
EditorGUILayout.PropertyField(brain);
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.LabelField("Agent Cameras");
for (var i = 0; i < cameras.arraySize; i++)
{
EditorGUILayout.PropertyField(
cameras.GetArrayElementAtIndex(i),
new GUIContent("Camera " + (i + 1) + ": "));
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Add Camera", EditorStyles.miniButton))
{
cameras.arraySize++;
}
if (GUILayout.Button("Remove Camera", EditorStyles.miniButton))
{
cameras.arraySize--;
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.LabelField("Agent RenderTextures");
for (var i = 0; i < renderTextures.arraySize; i++)
{
EditorGUILayout.PropertyField(
renderTextures.GetArrayElementAtIndex(i),
new GUIContent("RenderTexture " + (i + 1) + ": "));
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Add RenderTextures", EditorStyles.miniButton))
{
renderTextures.arraySize++;
}
if (GUILayout.Button("Remove RenderTextures", EditorStyles.miniButton))
{
renderTextures.arraySize--;
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(

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


// The height of a line in the Unity Inspectors
private const float k_LineHeight = 17f;
private const int k_VecObsNumLine = 3;
private const string k_CamResPropName = "cameraResolutions";
private const string k_CamWidthPropName = "width";
private const string k_CamHeightPropName = "height";
private const string k_CamGrayPropName = "blackAndWhite";
private const int k_DefaultCameraWidth = 84;
private const int k_DefaultCameraHeight = 84;
private const bool k_DefaultCameraGray = false;
if (property.isExpanded)
{
return k_LineHeight +
GetHeightDrawVectorObservation() +
GetHeightDrawVisualObservation(property) +
GetHeightDrawVectorAction(property) +
GetHeightDrawVectorActionDescriptions(property);
}
return k_LineHeight;
return GetHeightDrawVectorObservation() +
GetHeightDrawVectorAction(property);
property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, label);
position.y += k_LineHeight;
if (property.isExpanded)
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.indentLevel++;
// Vector Observations
DrawVectorObservation(position, property);
position.y += GetHeightDrawVectorObservation();
EditorGUI.BeginProperty(position, label, property);
EditorGUI.indentLevel++;
//Visual Observations
DrawVisualObservations(position, property);
position.y += GetHeightDrawVisualObservation(property);
// Vector Observations
DrawVectorObservation(position, property);
position.y += GetHeightDrawVectorObservation();
// Vector Action
DrawVectorAction(position, property);
position.y += GetHeightDrawVectorAction(property);
// Vector Action
DrawVectorAction(position, property);
position.y += GetHeightDrawVectorAction(property);
// Vector Action Descriptions
DrawVectorActionDescriptions(position, property);
position.y += GetHeightDrawVectorActionDescriptions(property);
EditorGUI.EndProperty();
}
EditorGUI.EndProperty();
EditorGUI.indentLevel = indent;
}

}
/// <summary>
/// Draws the Visual Observations parameters for the Brain Parameters
/// </summary>
/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
/// <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 += k_LineHeight;
var quarter = position.width / 4;
var resolutions = property.FindPropertyRelative(k_CamResPropName);
DrawVisualObsButtons(position, resolutions);
position.y += k_LineHeight;
// Display the labels for the columns : Index, Width, Height and Gray
var indexRect = new Rect(position.x, position.y, quarter, position.height);
var widthRect = new Rect(position.x + 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)
{
EditorGUI.LabelField(indexRect, "Index");
indexRect.y += k_LineHeight;
EditorGUI.LabelField(widthRect, "Width");
widthRect.y += k_LineHeight;
EditorGUI.LabelField(heightRect, "Height");
heightRect.y += k_LineHeight;
EditorGUI.LabelField(bwRect, "Gray");
bwRect.y += k_LineHeight;
}
// Iterate over the resolutions
for (var i = 0; i < resolutions.arraySize; i++)
{
EditorGUI.LabelField(indexRect, "Obs " + i);
indexRect.y += k_LineHeight;
var res = resolutions.GetArrayElementAtIndex(i);
var w = res.FindPropertyRelative("width");
w.intValue = EditorGUI.IntField(widthRect, w.intValue);
widthRect.y += k_LineHeight;
var h = res.FindPropertyRelative("height");
h.intValue = EditorGUI.IntField(heightRect, h.intValue);
heightRect.y += k_LineHeight;
var bw = res.FindPropertyRelative("blackAndWhite");
bw.boolValue = EditorGUI.Toggle(bwRect, bw.boolValue);
bwRect.y += k_LineHeight;
}
EditorGUI.indentLevel--;
}
/// <summary>
/// Draws the buttons to add and remove the visual observations parameters
/// </summary>
/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
/// <param name="resolutions">The SerializedProperty of the resolution array
/// to make the custom GUI for.</param>
private static void DrawVisualObsButtons(Rect position, SerializedProperty resolutions)
{
var widthEighth = position.width / 8;
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);
if (resolutions.arraySize == 0)
{
addButtonRect.width *= 2;
}
// Display the buttons
if (GUI.Button(addButtonRect, "Add New", EditorStyles.miniButton))
{
resolutions.arraySize += 1;
var newRes = resolutions.GetArrayElementAtIndex(resolutions.arraySize - 1);
newRes.FindPropertyRelative(k_CamWidthPropName).intValue = k_DefaultCameraWidth;
newRes.FindPropertyRelative(k_CamHeightPropName).intValue = k_DefaultCameraHeight;
newRes.FindPropertyRelative(k_CamGrayPropName).boolValue = k_DefaultCameraGray;
}
if (resolutions.arraySize > 0)
{
if (GUI.Button(removeButtonRect, "Remove Last", EditorStyles.miniButton))
{
resolutions.arraySize -= 1;
}
}
}
/// <summary>
/// The Height required to draw the Visual Observations parameters
/// </summary>
/// <returns>The height of the drawer of the Visual Observations </returns>
private static float GetHeightDrawVisualObservation(SerializedProperty property)
{
var visObsSize = property.FindPropertyRelative(k_CamResPropName).arraySize + 2;
if (property.FindPropertyRelative(k_CamResPropName).arraySize > 0)
{
visObsSize += 1;
}
return k_LineHeight * visObsSize;
}
/// <summary>
/// Draws the Vector Actions parameters for the Brain Parameters
/// </summary>
/// <param name="position">Rectangle on the screen to use for the property GUI.</param>

actionSize += 1;
}
return actionSize * k_LineHeight;
}
/// <summary>
/// Draws the Vector Actions descriptions for the Brain Parameters
/// </summary>
/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
/// <param name="property">The SerializedProperty of the BrainParameters
/// to make the custom GUI for.</param>
private static void DrawVectorActionDescriptions(Rect position, SerializedProperty property)
{
var bpVectorActionType = property.FindPropertyRelative(k_ActionTypePropName);
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
var numberOfDescriptions = 0;
if (bpVectorActionType.enumValueIndex == 1)
{
numberOfDescriptions = vecActionSize.GetArrayElementAtIndex(0).intValue;
}
else
{
numberOfDescriptions = vecActionSize.arraySize;
}
EditorGUI.indentLevel++;
var vecActionDescriptions =
property.FindPropertyRelative(k_ActionDescriptionPropName);
vecActionDescriptions.arraySize = numberOfDescriptions;
if (bpVectorActionType.enumValueIndex == 1)
{
//Continuous case :
EditorGUI.PropertyField(
position,
vecActionDescriptions,
new GUIContent("Action Descriptions",
"A list of strings used to name the available actionsm for the Brain."),
true);
position.y += k_LineHeight;
}
else
{
// Discrete case :
EditorGUI.PropertyField(
position,
vecActionDescriptions,
new GUIContent("Branch Descriptions",
"A list of strings used to name the available branches for the Brain."),
true);
position.y += k_LineHeight;
}
}
/// <summary>
/// The Height required to draw the Action Descriptions
/// </summary>
/// <returns>The height of the drawer of the Action Descriptions </returns>
private static float GetHeightDrawVectorActionDescriptions(SerializedProperty property)
{
var descriptionSize = 1;
if (property.FindPropertyRelative(k_ActionDescriptionPropName).isExpanded)
{
var descriptions = property.FindPropertyRelative(k_ActionDescriptionPropName);
descriptionSize += descriptions.arraySize + 1;
}
return descriptionSize * k_LineHeight;
}
}
}

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


return actionLabel.ToString();
}
/// <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.
/// </summary>
private static string BuildCameraResolutionLabel(SerializedProperty cameraArray)
{
var numCameras = cameraArray.arraySize;
var cameraLabel = new StringBuilder("[ ");
for (var i = 0; i < numCameras; i++)
{
var camHeightPropName =
cameraArray.GetArrayElementAtIndex(i).FindPropertyRelative("height");
cameraLabel.Append(camHeightPropName.intValue);
cameraLabel.Append(" X ");
var camWidthPropName =
cameraArray.GetArrayElementAtIndex(i).FindPropertyRelative("width");
cameraLabel.Append(camWidthPropName.intValue);
if (i < numCameras - 1)
{
cameraLabel.Append(", ");
}
}
cameraLabel.Append(" ]");
return cameraLabel.ToString();
}
/// <summary>
/// Renders Inspector UI for Brain Parameters of Demonstration.

var vecObsSizeProp = property.FindPropertyRelative("vectorObservationSize");
var numStackedProp = property.FindPropertyRelative("numStackedVectorObservations");
var actSizeProperty = property.FindPropertyRelative("vectorActionSize");
var camResProp = property.FindPropertyRelative("cameraResolutions");
var actSpaceTypeProp = property.FindPropertyRelative("vectorActionSpaceType");
var vecObsSizeLabel = vecObsSizeProp.displayName + ": " + vecObsSizeProp.intValue;

var camResLabel = camResProp.displayName + ": " + BuildCameraResolutionLabel(camResProp);
var actSpaceTypeLabel = actSpaceTypeProp.displayName + ": " +
(SpaceType)actSpaceTypeProp.enumValueIndex;

EditorGUILayout.LabelField(camResLabel);
EditorGUILayout.LabelField(actSpaceTypeLabel);
}

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


{
vectorObservationSize = 3,
numStackedVectorObservations = 2,
cameraResolutions = new[] {new Resolution()},
vectorActionDescriptions = new[] {"TestActionA", "TestActionB"},
vectorActionSize = new[] {2, 2},
vectorActionSpaceType = SpaceType.Discrete

var agentInfo = new AgentInfo
{
reward = 1f,
visualObservations = new List<Texture2D>(),
actionMasks = new[] {false, true},
done = true,
id = 5,

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


{
public class EditModeTestInternalBrainTensorGenerator
{
private class TestAgent : Agent
{
}
private static IEnumerable<Agent> GetFakeAgentInfos()
{
var goA = new GameObject("goA");

stackedVectorObservation = new[] {1f, 2f, 3f}.ToList(),
stackedVectorObservation = new[] { 1f, 2f, 3f }.ToList(),
storedVectorActions = new[] {1f, 2f},
storedVectorActions = new[] { 1f, 2f },
actionMasks = null
};
var goB = new GameObject("goB");

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 List<Agent> {agentA, agentB};
return new List<Agent> { agentA, agentB };
}
[Test]

{
var inputTensor = new TensorProxy
{
shape = new long[] {2, 3}
shape = new long[] { 2, 3 }
};
const int batchSize = 4;
var agentInfos = GetFakeAgentInfos();

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

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

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

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


using UnityEngine;
using NUnit.Framework;
using System.Reflection;
using MLAgents.Sensor;
using MLAgents.InferenceBrain;
namespace MLAgents.Tests
{

public override void InitializeAgent()
{
initializeAgentCalls += 1;
// Add in some custom sensors so we can confirm they get sorted as expected.
var sensor1 = new TestSensor("testsensor1");
var sensor2 = new TestSensor("testsensor2");
m_Sensors.Add(sensor2);
m_Sensors.Add(sensor1);
AddVectorObs(0f);
}
public override void AgentAction(float[] vectorAction, string textAction)

public override void AgentOnDone()
{
agentOnDoneCalls += 1;
}
public override float[] Heuristic()
{
return new float[0];
// This is an empty class for testing the behavior of agents and academy
// It is left empty because we are not testing any brain behavior
public class TestBrain : Brain
public class TestSensor : ISensor
public int numberOfCallsToInitialize;
public int numberOfCallsToDecideAction;
public static TestBrain Instantiate()
public string sensorName;
public TestSensor(string n)
{
sensorName = n;
}
public int[] GetFloatObservationShape()
{
return new[] { 1 };
}
public void WriteToTensor(TensorProxy tensorProxy, int agentIndex) { }
public byte[] GetCompressedObservation()
return CreateInstance<TestBrain>();
return null;
protected override void Initialize()
public CompressionType GetCompressionType()
numberOfCallsToInitialize++;
return CompressionType.None;
protected override void DecideAction()
public string GetName()
numberOfCallsToDecideAction++;
m_Agents.Clear();
return sensorName;
public class EditModeTestGeneration
{

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

acaGo.AddComponent<TestAcademy>();
var aca = acaGo.GetComponent<TestAcademy>();
aca.resetParameters = new ResetParameters();
var brain = TestBrain.Instantiate();
brain.brainParameters = new BrainParameters();
brain.brainParameters.vectorObservationSize = 0;
agent1.GiveBrain(brain);
agent2.GiveBrain(brain);
Assert.AreEqual(false, agent1.IsDone());
Assert.AreEqual(false, agent2.IsDone());

agentEnableMethod?.Invoke(agent2, new object[] { aca });
academyInitializeMethod?.Invoke(aca, new object[] {});
academyInitializeMethod?.Invoke(aca, new object[] { });
agentEnableMethod?.Invoke(agent1, new object[] { aca });
Assert.AreEqual(false, agent1.IsDone());

Assert.AreEqual(1, agent2.initializeAgentCalls);
Assert.AreEqual(0, agent1.agentActionCalls);
Assert.AreEqual(0, agent2.agentActionCalls);
// Make sure the sensors were sorted
Assert.AreEqual(agent1.m_Sensors[0].GetName(), "testsensor1");
Assert.AreEqual(agent1.m_Sensors[1].GetName(), "testsensor2");
}
}

aca.resetParameters = new ResetParameters();
var academyInitializeMethod = typeof(Academy).GetMethod("InitializeEnvironment",
BindingFlags.Instance | BindingFlags.NonPublic);
academyInitializeMethod?.Invoke(aca, new object[] {});
academyInitializeMethod?.Invoke(aca, new object[] { });
var academyStepMethod = typeof(Academy).GetMethod("EnvironmentStep",
BindingFlags.Instance | BindingFlags.NonPublic);

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

acaGo.AddComponent<TestAcademy>();
var aca = acaGo.GetComponent<TestAcademy>();
aca.resetParameters = new ResetParameters();
var brain = TestBrain.Instantiate();
var agentEnableMethod = typeof(Agent).GetMethod(

agent1.agentParameters = new AgentParameters();
agent2.agentParameters = new AgentParameters();
brain.brainParameters = new BrainParameters();
// We use event based so the agent will now try to send anything to the brain
agent1.agentParameters.onDemandDecision = false;
agent1.agentParameters.numberOfActionsBetweenDecisions = 2;

brain.brainParameters.vectorObservationSize = 0;
brain.brainParameters.cameraResolutions = new Resolution[0];
agent1.GiveBrain(brain);
agent2.GiveBrain(brain);
academyInitializeMethod?.Invoke(aca, new object[] {});
academyInitializeMethod?.Invoke(aca, new object[] { });
var academyStepMethod = typeof(Academy).GetMethod(
"EnvironmentStep", BindingFlags.Instance | BindingFlags.NonPublic);

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

aca.resetParameters = new ResetParameters();
var academyInitializeMethod = typeof(Academy).GetMethod(
"InitializeEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);
academyInitializeMethod?.Invoke(aca, new object[] {});
academyInitializeMethod?.Invoke(aca, new object[] { });
var academyStepMethod = typeof(Academy).GetMethod(
"EnvironmentStep", BindingFlags.Instance | BindingFlags.NonPublic);

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

acaGo.AddComponent<TestAcademy>();
var aca = acaGo.GetComponent<TestAcademy>();
aca.resetParameters = new ResetParameters();
var brain = TestBrain.Instantiate();
var agentEnableMethod = typeof(Agent).GetMethod(

agent1.agentParameters = new AgentParameters();
agent2.agentParameters = new AgentParameters();
brain.brainParameters = new BrainParameters();
// agent2 will request decisions only when RequestDecision is called
brain.brainParameters.vectorObservationSize = 0;
brain.brainParameters.cameraResolutions = new Resolution[0];
agent1.GiveBrain(brain);
agent2.GiveBrain(brain);
academyInitializeMethod?.Invoke(aca, new object[] {});
academyInitializeMethod?.Invoke(aca, new object[] { });
var numberAgent1Reset = 0;
var numberAgent2Reset = 0;

agent2StepSinceReset += 1;
//Agent 1 is only initialized at step 2
if (i < 2)
{}
academyStepMethod?.Invoke(aca, new object[] {});
{ }
academyStepMethod?.Invoke(aca, new object[] { });
}
}
}

acaGo.AddComponent<TestAcademy>();
var aca = acaGo.GetComponent<TestAcademy>();
aca.resetParameters = new ResetParameters();
var brain = TestBrain.Instantiate();
var agentEnableMethod = typeof(Agent).GetMethod(

agent1.agentParameters = new AgentParameters();
agent2.agentParameters = new AgentParameters();
brain.brainParameters = new BrainParameters();
// We use event based so the agent will now try to send anything to the brain
agent1.agentParameters.onDemandDecision = false;
// agent1 will take an action at every step and request a decision every steps

//Here we specify that the agent does not reset when done
agent1.agentParameters.resetOnDone = false;
agent2.agentParameters.resetOnDone = false;
brain.brainParameters.vectorObservationSize = 0;
brain.brainParameters.cameraResolutions = new Resolution[0];
agent1.GiveBrain(brain);
agent2.GiveBrain(brain);
academyInitializeMethod?.Invoke(aca, new object[] {});
academyInitializeMethod?.Invoke(aca, new object[] { });
agentEnableMethod?.Invoke(agent1, new object[] { aca });
var agent1ResetOnDone = 0;

}
academyStepMethod?.Invoke(aca, new object[] {});
academyStepMethod?.Invoke(aca, new object[] { });
}
}

acaGo.AddComponent<TestAcademy>();
var aca = acaGo.GetComponent<TestAcademy>();
aca.resetParameters = new ResetParameters();
var brain = TestBrain.Instantiate();
var agentEnableMethod = typeof(Agent).GetMethod(

agent1.agentParameters = new AgentParameters();
agent2.agentParameters = new AgentParameters();
brain.brainParameters = new BrainParameters();
// We use event based so the agent will now try to send anything to the brain
agent1.agentParameters.onDemandDecision = false;
agent1.agentParameters.numberOfActionsBetweenDecisions = 3;

agent1.agentParameters.maxStep = 20;
brain.brainParameters.vectorObservationSize = 0;
brain.brainParameters.cameraResolutions = new Resolution[0];
agent1.GiveBrain(brain);
agent2.GiveBrain(brain);
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(aca, new object[] {});
academyStepMethod?.Invoke(aca, new object[] { });
agent1.AddReward(10f);
if ((i % 21 == 0) && (i > 0))

29
UnitySDK/Assets/ML-Agents/Examples/3DBall/Prefabs/3DBall.prefab


m_Component:
- component: {fileID: 4780098186595842}
- component: {fileID: 65010516625723872}
- component: {fileID: 114259948429386406}
- component: {fileID: 114368073295828880}
- component: {fileID: 114715123104194396}
m_Layer: 0
m_Name: Agent
m_TagString: Untagged

serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &114259948429386406
--- !u!114 &114368073295828880
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1424713891854676}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 8
numStackedVectorObservations: 1
vectorActionSize: 02000000
vectorActionDescriptions: []
vectorActionSpaceType: 1
m_Model: {fileID: 11400000, guid: a0e8d1fda5a6f41be955d2b30479c2a1, type: 3}
m_InferenceDevice: 0
m_UseHeuristic: 0
m_BehaviorName: 3DBall
--- !u!114 &114715123104194396
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}

m_Script: {fileID: 11500000, guid: aaba48bf82bee4751aa7b89569e57f73, type: 3}
m_Name:
m_EditorClassIdentifier:
brain: {fileID: 11400000, guid: 383c589e8bb76464eadc2525b5b0f2c1, type: 2}
agentCameras: []
agentRenderTextures: []
maxStep: 5000
resetOnDone: 1
onDemandDecision: 0

24
UnitySDK/Assets/ML-Agents/Examples/3DBall/Prefabs/3DBallHardNew.prefab


m_Component:
- component: {fileID: 4895942152145390}
- component: {fileID: 65170961617201804}
- component: {fileID: 114284317994838100}
- component: {fileID: 114466000339026140}
m_Layer: 0
m_Name: Agent

serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &114284317994838100
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1829721031899636}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 5
numStackedVectorObservations: 9
vectorActionSize: 02000000
cameraResolutions: []
vectorActionDescriptions: []
vectorActionSpaceType: 1
m_Model: {fileID: 11400000, guid: cee7d20369b814d549573de7e76c4a81, type: 3}
m_InferenceDevice: 0
m_UseHeuristic: 0
m_BehaviorName: 3DBallHard
--- !u!114 &114466000339026140
MonoBehaviour:
m_ObjectHideFlags: 1

m_Script: {fileID: 11500000, guid: edf26e11cf4ed42eaa3ffb7b91bb4676, type: 3}
m_Name:
m_EditorClassIdentifier:
brain: {fileID: 11400000, guid: 4f74e089fbb75455ebf6f0495e30be6e, type: 2}
agentParameters:
agentCameras: []
agentRenderTextures: []

47
UnitySDK/Assets/ML-Agents/Examples/3DBall/Scenes/3DBall.unity


propertyPath: m_Name
value: 3DBall (7)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
m_IsPrefabParent: 0

propertyPath: m_Name
value: 3DBall (5)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
m_IsPrefabParent: 0

- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_Name
value: 3DBall (6)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}

propertyPath: m_Name
value: 3DBall (3)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
m_IsPrefabParent: 0

propertyPath: m_Name
value: 3DBall (8)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
m_IsPrefabParent: 0

- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_Name
value: 3DBall (9)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}

propertyPath: m_Name
value: 3DBall (10)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
m_IsPrefabParent: 0

propertyPath: m_Name
value: 3DBall (1)
objectReference: {fileID: 0}
- target: {fileID: 0}
propertyPath: m_BrainParameters.numStackedVectorObservations
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
m_IsPrefabParent: 0

- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_Name
value: 3DBall (11)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}

propertyPath: m_Name
value: 3DBall (4)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
m_IsPrefabParent: 0

m_Script: {fileID: 11500000, guid: eb15e3c3d55e54abaafb74c635b6a458, type: 3}
m_Name:
m_EditorClassIdentifier:
broadcastHub:
brainsToControl:
- {fileID: 11400000, guid: 383c589e8bb76464eadc2525b5b0f2c1, type: 2}
m_TrainingConfiguration:
width: 300
height: 200

- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_Name
value: 3DBall (2)
objectReference: {fileID: 0}
- target: {fileID: 1321468028730240, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: cfa81c019162c4e3caf6e2999c6fdf48, type: 2}

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


public override void AgentAction(float[] vectorAction, string textAction)
{
if (brain.brainParameters.vectorActionSpaceType == SpaceType.Continuous)
var actionZ = 2f * Mathf.Clamp(vectorAction[0], -1f, 1f);
var actionX = 2f * Mathf.Clamp(vectorAction[1], -1f, 1f);
if ((gameObject.transform.rotation.z < 0.25f && actionZ > 0f) ||
(gameObject.transform.rotation.z > -0.25f && actionZ < 0f))
var actionZ = 2f * Mathf.Clamp(vectorAction[0], -1f, 1f);
var actionX = 2f * Mathf.Clamp(vectorAction[1], -1f, 1f);
gameObject.transform.Rotate(new Vector3(0, 0, 1), actionZ);
}
if ((gameObject.transform.rotation.z < 0.25f && actionZ > 0f) ||
(gameObject.transform.rotation.z > -0.25f && actionZ < 0f))
{
gameObject.transform.Rotate(new Vector3(0, 0, 1), actionZ);
}
if ((gameObject.transform.rotation.x < 0.25f && actionX > 0f) ||
(gameObject.transform.rotation.x > -0.25f && actionX < 0f))
{
gameObject.transform.Rotate(new Vector3(1, 0, 0), actionX);
}
if ((gameObject.transform.rotation.x < 0.25f && actionX > 0f) ||
(gameObject.transform.rotation.x > -0.25f && actionX < 0f))
{
gameObject.transform.Rotate(new Vector3(1, 0, 0), actionX);
}
if ((ball.transform.position.y - gameObject.transform.position.y) < -2f ||
Mathf.Abs(ball.transform.position.x - gameObject.transform.position.x) > 3f ||

+ gameObject.transform.position;
//Reset the parameters when the Agent is reset.
SetResetParameters();
}
public override float[] Heuristic()
{
var action = new float[2];
action[0] = -Input.GetAxis("Horizontal");
action[1] = Input.GetAxis("Vertical");
return action;
}
public void SetBall()

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


public override void AgentAction(float[] vectorAction, string textAction)
{
if (brain.brainParameters.vectorActionSpaceType == SpaceType.Continuous)
var actionZ = 2f * Mathf.Clamp(vectorAction[0], -1f, 1f);
var actionX = 2f * Mathf.Clamp(vectorAction[1], -1f, 1f);
if ((gameObject.transform.rotation.z < 0.25f && actionZ > 0f) ||
(gameObject.transform.rotation.z > -0.25f && actionZ < 0f))
var actionZ = 2f * Mathf.Clamp(vectorAction[0], -1f, 1f);
var actionX = 2f * Mathf.Clamp(vectorAction[1], -1f, 1f);
gameObject.transform.Rotate(new Vector3(0, 0, 1), actionZ);
}
if ((gameObject.transform.rotation.z < 0.25f && actionZ > 0f) ||
(gameObject.transform.rotation.z > -0.25f && actionZ < 0f))
{
gameObject.transform.Rotate(new Vector3(0, 0, 1), actionZ);
}
if ((gameObject.transform.rotation.x < 0.25f && actionX > 0f) ||
(gameObject.transform.rotation.x > -0.25f && actionX < 0f))
{
gameObject.transform.Rotate(new Vector3(1, 0, 0), actionX);
}
if ((gameObject.transform.rotation.x < 0.25f && actionX > 0f) ||
(gameObject.transform.rotation.x > -0.25f && actionX < 0f))
{
gameObject.transform.Rotate(new Vector3(1, 0, 0), actionX);
}
if ((ball.transform.position.y - gameObject.transform.position.y) < -2f ||
Mathf.Abs(ball.transform.position.x - gameObject.transform.position.x) > 3f ||

24
UnitySDK/Assets/ML-Agents/Examples/Basic/Prefabs/Basic.prefab


m_Component:
- component: {fileID: 4170723581433160}
- component: {fileID: 65968285873374238}
- component: {fileID: 114502619508238574}
- component: {fileID: 114827551040495112}
m_Layer: 0
m_Name: BasicAgent

serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &114502619508238574
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1263463520136984}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 20
numStackedVectorObservations: 1
vectorActionSize: 03000000
cameraResolutions: []
vectorActionDescriptions: []
vectorActionSpaceType: 0
m_Model: {fileID: 11400000, guid: 53fa7c392ce3c492281be273668f6aaf, type: 3}
m_InferenceDevice: 0
m_UseHeuristic: 0
m_BehaviorName: Basic
--- !u!114 &114827551040495112
MonoBehaviour:
m_ObjectHideFlags: 1

m_Script: {fileID: 11500000, guid: 624480a72e46148118ab2e2d89b537de, type: 3}
m_Name:
m_EditorClassIdentifier:
brain: {fileID: 11400000, guid: e5cf0e35e16264ea483f8863e5115c3c, type: 2}
agentParameters:
agentCameras: []
agentRenderTextures: []

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


largeGoal.transform.position = new Vector3(m_LargeGoalPosition - 10f, 0f, 0f);
}
public override float[] Heuristic()
{
if (Input.GetKey(KeyCode.D))
{
return new float[] { 2 };
}
if (Input.GetKey(KeyCode.A))
{
return new float[] { 1 };
}
return new float[] { 0 };
}
public override void AgentOnDone()
{
}

24
UnitySDK/Assets/ML-Agents/Examples/Bouncer/Prefabs/Environment.prefab


- component: {fileID: 33085749764809866}
- component: {fileID: 65800894914404220}
- component: {fileID: 54030303118153432}
- component: {fileID: 114938751572484598}
- component: {fileID: 114878620968301562}
m_Layer: 0
m_Name: Agent

m_Script: {fileID: 11500000, guid: 0f09741cbce2e44bc88d3e92917eea0e, type: 3}
m_Name:
m_EditorClassIdentifier:
brain: {fileID: 11400000, guid: 573920e3a672d40038169c7ffdbdca05, type: 2}
agentParameters:
agentCameras: []
agentRenderTextures: []

target: {fileID: 1160631129428284}
bodyObject: {fileID: 1680588139522898}
strength: 500
--- !u!114 &114938751572484598
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1604827395706042}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 6
numStackedVectorObservations: 3
vectorActionSize: 03000000
cameraResolutions: []
vectorActionDescriptions: []
vectorActionSpaceType: 1
m_Model: {fileID: 11400000, guid: f5250a39cb2134db49b833e3c92527a1, type: 3}
m_InferenceDevice: 0
m_UseHeuristic: 0
m_BehaviorName: Bouncer

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


}
}
public override float[] Heuristic()
{
var action = new float[3];
action[0] = Input.GetAxis("Horizontal");
action[1] = Input.GetKey(KeyCode.Space) ? 1.0f : 0.0f;
action[2] = Input.GetAxis("Vertical");
return action;
}
private void Update()
{
if (m_LookDir.magnitude > float.Epsilon)

25
UnitySDK/Assets/ML-Agents/Examples/Crawler/Prefabs/DynamicPlatform.prefab


serializedVersion: 5
m_Component:
- component: {fileID: 4313455366547514}
- component: {fileID: 114060650647145362}
- component: {fileID: 114590693924030052}
- component: {fileID: 114423363226357902}
m_Layer: 0

m_UseColorTemperature: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!114 &114060650647145362
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1515093357607024}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
vectorObservationSize: 126
numStackedVectorObservations: 1
vectorActionSize: 14000000
cameraResolutions: []
vectorActionDescriptions: []
vectorActionSpaceType: 1
m_Model: {fileID: 11400000, guid: abc9c8f2180154ed7ba3f116ab0beb90, type: 3}
m_InferenceDevice: 0
m_UseHeuristic: 0