浏览代码

Merge pull request #2648 from Unity-Technologies/release-0.10.0

Release 0.10.0
/0.10.1
GitHub 5 年前
当前提交
b2fa2268
共有 821 个文件被更改,包括 10789 次插入11717 次删除
  1. 103
      .circleci/config.yml
  2. 2
      .gitignore
  3. 18
      .pre-commit-config.yaml
  4. 1
      README.md
  5. 37
      UnitySDK/Assets/ML-Agents/Editor/AgentEditor.cs
  6. 10
      UnitySDK/Assets/ML-Agents/Editor/BrainEditor.cs
  7. 176
      UnitySDK/Assets/ML-Agents/Editor/BrainParametersDrawer.cs
  8. 81
      UnitySDK/Assets/ML-Agents/Editor/BroadcastHubDrawer.cs
  9. 2
      UnitySDK/Assets/ML-Agents/Editor/Builder.cs
  10. 4
      UnitySDK/Assets/ML-Agents/Editor/BuilderUtils.cs
  11. 54
      UnitySDK/Assets/ML-Agents/Editor/DemonstrationDrawer.cs
  12. 10
      UnitySDK/Assets/ML-Agents/Editor/DemonstrationImporter.cs
  13. 6
      UnitySDK/Assets/ML-Agents/Editor/HeuristicBrainEditor.cs
  14. 40
      UnitySDK/Assets/ML-Agents/Editor/LearningBrainEditor.cs
  15. 35
      UnitySDK/Assets/ML-Agents/Editor/PlayerBrainEditor.cs
  16. 68
      UnitySDK/Assets/ML-Agents/Editor/ResetParameterDrawer.cs
  17. 38
      UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs
  18. 84
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestActionMasker.cs
  19. 71
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorApplier.cs
  20. 107
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorGenerator.cs
  21. 524
      UnitySDK/Assets/ML-Agents/Editor/Tests/MLAgentsEditModeTest.cs
  22. 182
      UnitySDK/Assets/ML-Agents/Editor/Tests/MultinomialTest.cs
  23. 128
      UnitySDK/Assets/ML-Agents/Editor/Tests/RandomNormalTest.cs
  24. 9
      UnitySDK/Assets/ML-Agents/Editor/Tests/RayPerceptionTests.cs
  25. 21
      UnitySDK/Assets/ML-Agents/Editor/Tests/UtilitiesTests.cs
  26. 3
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Brains/3DBallHardLearning.asset
  27. 3
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Brains/3DBallLearning.asset
  28. 787
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scenes/3DBall.unity
  29. 2
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scenes/3DBall.unity.meta
  30. 869
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scenes/3DBallHard.unity
  31. 3
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAcademy.cs
  32. 25
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs
  33. 22
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs
  34. 25
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
  35. 587
      UnitySDK/Assets/ML-Agents/Examples/3DBall/TFModels/3DBallHardLearning.nn
  36. 2
      UnitySDK/Assets/ML-Agents/Examples/3DBall/TFModels/3DBallHardLearning.nn.meta
  37. 498
      UnitySDK/Assets/ML-Agents/Examples/3DBall/TFModels/3DBallLearning.nn
  38. 2
      UnitySDK/Assets/ML-Agents/Examples/3DBall/TFModels/3DBallLearning.nn.meta
  39. 2
      UnitySDK/Assets/ML-Agents/Examples/Basic/Brains/BasicLearning.asset
  40. 654
      UnitySDK/Assets/ML-Agents/Examples/Basic/Prefabs/Basic.prefab
  41. 263
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scenes/Basic.unity
  42. 9
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAcademy.cs
  43. 82
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
  44. 4
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicDecision.cs
  45. 19
      UnitySDK/Assets/ML-Agents/Examples/Basic/TFModels/BasicLearning.nn
  46. 2
      UnitySDK/Assets/ML-Agents/Examples/Basic/TFModels/BasicLearning.nn.meta
  47. 3
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Brains/BouncerLearning.asset
  48. 683
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Prefabs/Environment.prefab
  49. 2
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Prefabs/Environment.prefab.meta
  50. 905
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scenes/Bouncer.unity
  51. 963
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scenes/BouncerIL.unity
  52. 13
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAcademy.cs
  53. 101
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
  54. 22
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerTarget.cs
  55. 165
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/TFModels/BouncerLearning.nn
  56. 2
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/TFModels/BouncerLearning.nn.meta
  57. 2
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Brains/CrawlerDynamicLearning.asset
  58. 2
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Brains/CrawlerStaticLearning.asset
  59. 897
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Prefabs/DynamicPlatform.prefab
  60. 829
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Prefabs/FixedPlatform.prefab
  61. 903
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Scenes/CrawlerDynamicTarget.unity
  62. 944
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Scenes/CrawlerStaticTarget.unity
  63. 2
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAcademy.cs
  64. 128
      UnitySDK/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs
  65. 1001
      UnitySDK/Assets/ML-Agents/Examples/Crawler/TFModels/CrawlerDynamicLearning.nn
  66. 2
      UnitySDK/Assets/ML-Agents/Examples/Crawler/TFModels/CrawlerDynamicLearning.nn.meta
  67. 1001
      UnitySDK/Assets/ML-Agents/Examples/Crawler/TFModels/CrawlerStaticLearning.nn
  68. 2
      UnitySDK/Assets/ML-Agents/Examples/Crawler/TFModels/CrawlerStaticLearning.nn.meta
  69. 3
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Brains/GridWorldLearning.asset
  70. 260
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scenes/GridWorld.unity
  71. 95
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAcademy.cs
  72. 75
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  73. 1001
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/TFModels/GridWorldLearning.nn
  74. 2
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/TFModels/GridWorldLearning.nn.meta
  75. 6
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Prefabs/pit.prefab
  76. 6
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Prefabs/goal.prefab
  77. 4
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Prefabs/agentRenderTexture.renderTexture
  78. 3
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Brains/HallwayLearning.asset
  79. 20
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Brains/VisualHallwayLearning.asset
  80. 629
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scenes/Hallway.unity
  81. 322
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scenes/HallwayIL.unity
  82. 702
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scenes/VisualHallway.unity
  83. 11
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs
  84. 99
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs
  85. 1001
      UnitySDK/Assets/ML-Agents/Examples/Hallway/TFModels/HallwayLearning.nn
  86. 2
      UnitySDK/Assets/ML-Agents/Examples/Hallway/TFModels/HallwayLearning.nn.meta
  87. 10
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Brains/PushBlockLearning.asset
  88. 20
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Brains/VisualPushBlockLearning.asset
  89. 539
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockArea.prefab
  90. 469
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlock.unity
  91. 401
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockIL.unity
  92. 449
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scenes/VisualPushBlock.unity
  93. 12
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/GoalDetect.cs
  94. 108
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs
  95. 22
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockAcademy.cs
  96. 1001
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/TFModels/PushBlockLearning.nn
  97. 2
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/TFModels/PushBlockLearning.nn.meta
  98. 3
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Brains/PyramidsLearning.asset
  99. 901
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Prefabs/AreaPB.prefab
  100. 82
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Prefabs/BrickPyramid.prefab

103
.circleci/config.yml


version: 2.0
version: 2.1
executors:
python361:
docker:
- image: circleci/python:3.6.1
python373:
docker:
- image: circleci/python:3.7.3
build:
docker:
- image: circleci/python:3.6.1
build_python:
parameters:
executor:
type: executor
pyversion:
type: string
description: python version to being used (currently only affects caching).
pip_constraints:
type: string
description: Constraints file that is passed to "pip install". We constraint older versions of libraries for older python runtime, in order to help ensure compatibility.
executor: << parameters.executor >>
- run:
# Combine all the python dependencies into one file so that we can use that for the cache checksum
name: Combine pip dependencies for caching
command: cat ml-agents/setup.py ml-agents-envs/setup.py gym-unity/setup.py test_requirements.txt << parameters.pip_constraints >> > python_deps.txt
- v1-dependencies-{{ checksum "ml-agents/setup.py" }}
# Parameterize the cache so that different python versions can get different versions of the packages
- v1-dependencies-py<< parameters.pyversion >>-{{ checksum "python_deps.txt" }}
- v1-dependencies-
- v1-dependencies-py<< parameters.pyversion >>-
- run:
name: Install Dependencies

pip install --upgrade pip
pip install --upgrade setuptools
cd ml-agents-envs && pip install -e .
cd ../ml-agents && pip install -e .
pip install pre-commit pytest-cov==2.6.1
cd ../gym-unity && pip install -e .
pip install --progress-bar=off -e ./ml-agents-envs -c << parameters.pip_constraints >>
pip install --progress-bar=off -e ./ml-agents -c << parameters.pip_constraints >>
pip install --progress-bar=off -r test_requirements.txt -c << parameters.pip_constraints >>
pip install --progress-bar=off -e ./gym-unity -c << parameters.pip_constraints >>
key: v1-dependencies-{{ checksum "ml-agents/setup.py" }}
key: v1-dependencies-py<< parameters.pyversion >>-{{ checksum "python_deps.txt" }}
# This also dumps the installed pip packages to a file, so we can see what versions are actually being used.
pytest --cov=mlagents --cov-report xml --junitxml=test-reports/junit.xml -p no:warnings
pip freeze > test-reports/pip_versions.txt
pytest --cov=ml-agents --cov=ml-agents-envs --cov=gym-unity --cov-report html --junitxml=test-reports/junit.xml -p no:warnings
- run:
name: Check Code Style using pre-commit

- store_artifacts:
path: test-reports
destination: test-reports
- store_artifacts:
path: htmlcov
destination: htmlcov
markdown_link_check:
docker:
- image: circleci/node:12.6.0
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v1-node-dependencies-{{ checksum ".pre-commit-config.yaml" }}
# fallback to using the latest cache if no exact match is found
- v1-node-dependencies-
- run:
name: Install Dependencies
command: |
sudo apt-get install python3-venv
python3 -m venv venv
. venv/bin/activate
pip install pre-commit
- run: sudo npm install -g markdown-link-check
- save_cache:
paths:
- ./venv
key: v1-node-dependencies-{{ checksum ".pre-commit-config.yaml" }}
- run:
name: Run markdown-link-check via precommit
command: |
. venv/bin/activate
pre-commit run --hook-stage manual markdown-link-check --all-files
workflows:
workflow:
jobs:
- build_python:
name: python_3.6.1
executor: python361
pyversion: 3.6.1
# Test python 3.6 with the oldest supported versions
pip_constraints: test_constraints_min_version.txt
- build_python:
name: python_3.7.3
executor: python373
pyversion: 3.7.3
# Test python 3.7 with the newest supported versions
pip_constraints: test_constraints_max_version.txt
- markdown_link_check

2
.gitignore


# Code coverage report
.coverage
coverage.xml
/htmlcov/

18
.pre-commit-config.yaml


files: "ml-agents-envs/.*"
# Exclude protobuf files and don't follow them when imported
exclude: ".*_pb2.py"
# TODO get disallow-incomplete-defs working
args: [--ignore-missing-imports, --follow-imports=silent]
args: [--ignore-missing-imports, --disallow-incomplete-defs]
- id: mypy
name: mypy-gym-unity
files: "gym-unity/.*"

.*_pb2.py|
.*_pb2_grpc.py
)$
# "Local" hooks, see https://pre-commit.com/#repository-local-hooks
- repo: local
hooks:
- id: markdown-link-check
name: markdown-link-check
# markdown-link-check doesn't support multiple files on the commandline, so this hacks around that.
# Note that you must install the package separately via npm. For example:
# brew install npm; npm install -g markdown-link-check
entry: bash -xc 'echo "$@" | xargs -n1 -t markdown-link-check -c markdown-link-check.config.json' --
language: system
types: [markdown]
# Don't check localized files since their target might not be localized.
exclude: ".*localized.*"
# Only run manually, e.g. pre-commit run --hook-stage manual markdown-link-check
stages: [manual]

1
README.md


* Unity environment control from Python
* 10+ sample Unity environments
* Two deep reinforcement learning algorithms, [Proximal Policy Optimization](docs/Training-PPO.md) (PPO) and [Soft Actor-Critic](docs/Training-SAC.md) (SAC)
* Support for multiple environment configurations and training scenarios
* Train memory-enhanced agents using deep reinforcement learning
* Easily definable Curriculum Learning and Generalization scenarios

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


using UnityEngine;
using UnityEngine;
using UnityEditor;
namespace MLAgents

[CanEditMultipleObjects]
public class AgentEditor : Editor
{
SerializedObject serializedAgent = serializedObject;
var serializedAgent = serializedObject;
SerializedProperty brain = serializedAgent.FindProperty("brain");
SerializedProperty actionsPerDecision = serializedAgent.FindProperty(
var brain = serializedAgent.FindProperty("brain");
var actionsPerDecision = serializedAgent.FindProperty(
SerializedProperty maxSteps = serializedAgent.FindProperty(
var maxSteps = serializedAgent.FindProperty(
SerializedProperty isResetOnDone = serializedAgent.FindProperty(
var isResetOnDone = serializedAgent.FindProperty(
SerializedProperty isODD = serializedAgent.FindProperty(
var isOdd = serializedAgent.FindProperty(
SerializedProperty cameras = serializedAgent.FindProperty(
var cameras = serializedAgent.FindProperty(
SerializedProperty renderTextures = serializedAgent.FindProperty(
var renderTextures = serializedAgent.FindProperty(
"agentParameters.agentRenderTextures");
EditorGUILayout.PropertyField(brain);

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);
for (int i = 0; i < cameras.arraySize; i++)
for (var i = 0; i < cameras.arraySize; i++)
new GUIContent("Camera " + (i + 1).ToString() + ": "));
new GUIContent("Camera " + (i + 1) + ": "));
}
EditorGUILayout.BeginHorizontal();

}
EditorGUILayout.EndHorizontal();
for (int i = 0; i < renderTextures.arraySize; i++)
for (var i = 0; i < renderTextures.arraySize; i++)
new GUIContent("RenderTexture " + (i + 1).ToString() + ": "));
new GUIContent("RenderTexture " + (i + 1) + ": "));
}
EditorGUILayout.BeginHorizontal();

"Reset On Done",
"If checked, the agent will reset on done. Else, AgentOnDone() will be called."));
EditorGUILayout.PropertyField(
isODD,
isOdd,
if (!isODD.boolValue)
if (!isOdd.boolValue)
{
EditorGUILayout.PropertyField(
actionsPerDecision,

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);
}

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


using UnityEngine;
using UnityEngine;
using UnityEditor;
namespace MLAgents

public class BrainParametersDrawer : PropertyDrawer
{
// The height of a line in the Unity Inspectors
private const float LineHeight = 17f;
private const int VecObsNumLine = 3;
private const string CamResPropName = "cameraResolutions";
private const string ActionSizePropName = "vectorActionSize";
private const string ActionTypePropName = "vectorActionSpaceType";
private const string ActionDescriptionPropName = "vectorActionDescriptions";
private const string VecObsPropName = "vectorObservationSize";
private const string NumVecObsPropName ="numStackedVectorObservations";
private const string CamWidthPropName = "width";
private const string CamHeightPropName = "height";
private const string CamGrayPropName = "blackAndWhite";
private const int DefaultCameraWidth = 84;
private const int DefaultCameraHeight = 84;
private const bool DefaultCameraGray = false;
private const float k_LineHeight = 17f;
private const int k_VecObsNumLine = 3;
private const string k_CamResPropName = "cameraResolutions";
private const string k_ActionSizePropName = "vectorActionSize";
private const string k_ActionTypePropName = "vectorActionSpaceType";
private const string k_ActionDescriptionPropName = "vectorActionDescriptions";
private const string k_VecObsPropName = "vectorObservationSize";
private const string k_NumVecObsPropName = "numStackedVectorObservations";
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;
return LineHeight +
GetHeightDrawVectorObservation() +
GetHeightDrawVisualObservation(property) +
GetHeightDrawVectorAction(property) +
GetHeightDrawVectorActionDescriptions(property);
return k_LineHeight +
GetHeightDrawVectorObservation() +
GetHeightDrawVisualObservation(property) +
GetHeightDrawVectorAction(property) +
GetHeightDrawVectorActionDescriptions(property);
return LineHeight;
return k_LineHeight;
}
/// <inheritdoc />

EditorGUI.indentLevel = 0;
position.height = LineHeight;
position.height = k_LineHeight;
position.y += LineHeight;
position.y += k_LineHeight;
// Vector Action Descriptions
DrawVectorActionDescriptions(position, property);
position.y += GetHeightDrawVectorActionDescriptions(property);

private static void DrawVectorObservation(Rect position, SerializedProperty property)
{
EditorGUI.LabelField(position, "Vector Observation");
position.y += LineHeight;
position.y += k_LineHeight;
property.FindPropertyRelative(VecObsPropName),
property.FindPropertyRelative(k_VecObsPropName),
position.y += LineHeight;
position.y += k_LineHeight;
property.FindPropertyRelative(NumVecObsPropName),
property.FindPropertyRelative(k_NumVecObsPropName),
position.y += LineHeight;
EditorGUI.indentLevel--;
position.y += k_LineHeight;
EditorGUI.indentLevel--;
}
/// <summary>

private static float GetHeightDrawVectorObservation()
{
return VecObsNumLine * LineHeight;
return k_VecObsNumLine * k_LineHeight;
}
/// <summary>

/// <param name="property">The SerializedProperty of the BrainParameters
/// to make the custom GUI for.</param>
private static void DrawVisualObservations(Rect position, SerializedProperty property)
{
{
position.y += LineHeight;
position.y += k_LineHeight;
var resolutions = property.FindPropertyRelative(CamResPropName);
var resolutions = property.FindPropertyRelative(k_CamResPropName);
position.y += LineHeight;
position.y += k_LineHeight;
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);
indexRect.y += LineHeight;
indexRect.y += k_LineHeight;
widthRect.y += LineHeight;
widthRect.y += k_LineHeight;
heightRect.y += LineHeight;
heightRect.y += k_LineHeight;
bwRect.y += LineHeight;
bwRect.y += k_LineHeight;
}
// Iterate over the resolutions

indexRect.y += LineHeight;
indexRect.y += k_LineHeight;
widthRect.y += LineHeight;
widthRect.y += k_LineHeight;
heightRect.y += LineHeight;
heightRect.y += k_LineHeight;
bwRect.y += LineHeight;
bwRect.y += k_LineHeight;
}
EditorGUI.indentLevel--;
}

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);

{
resolutions.arraySize += 1;
var newRes = resolutions.GetArrayElementAtIndex(resolutions.arraySize - 1);
newRes.FindPropertyRelative(CamWidthPropName).intValue = DefaultCameraWidth;
newRes.FindPropertyRelative(CamHeightPropName).intValue = DefaultCameraHeight;
newRes.FindPropertyRelative(CamGrayPropName).boolValue = DefaultCameraGray;
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)
{

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

var visObsSize = property.FindPropertyRelative(CamResPropName).arraySize + 2;
if (property.FindPropertyRelative(CamResPropName).arraySize > 0)
var visObsSize = property.FindPropertyRelative(k_CamResPropName).arraySize + 2;
if (property.FindPropertyRelative(k_CamResPropName).arraySize > 0)
return LineHeight * visObsSize;
return k_LineHeight * visObsSize;
}
/// <summary>

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

position.y += LineHeight;
position.y += k_LineHeight;
if (bpVectorActionType.enumValueIndex == 1)
{
DrawContinuousVectorAction(position, property);

/// to make the custom GUI for.</param>
private static void DrawContinuousVectorAction(Rect position, SerializedProperty property)
{
var vecActionSize = property.FindPropertyRelative(ActionSizePropName);
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
SerializedProperty continuousActionSize =
var continuousActionSize =
vecActionSize.GetArrayElementAtIndex(0);
EditorGUI.PropertyField(
position,

/// <summary>
/// Draws the Discrete Vector Actions parameters for the Brain Parameters
/// </summary>

private static void DrawDiscreteVectorAction(Rect position, SerializedProperty property)
{
var vecActionSize = property.FindPropertyRelative(ActionSizePropName);
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
position.y += LineHeight;
position.y += k_LineHeight;
branchIndex < vecActionSize.arraySize;
branchIndex++)
branchIndex < vecActionSize.arraySize;
branchIndex++)
SerializedProperty branchActionSize =
var branchActionSize =
position.y += LineHeight;
position.y += k_LineHeight;
/// <summary>
/// The Height required to draw the Vector Action parameters
/// </summary>

var actionSize = 2 + property.FindPropertyRelative(ActionSizePropName).arraySize;
if (property.FindPropertyRelative(ActionTypePropName).enumValueIndex == 0)
var actionSize = 2 + property.FindPropertyRelative(k_ActionSizePropName).arraySize;
if (property.FindPropertyRelative(k_ActionTypePropName).enumValueIndex == 0)
return actionSize * LineHeight;
return actionSize * k_LineHeight;
}
/// <summary>

/// to make the custom GUI for.</param>
private static void DrawVectorActionDescriptions(Rect position, SerializedProperty property)
{
var bpVectorActionType = property.FindPropertyRelative(ActionTypePropName);
var vecActionSize = property.FindPropertyRelative(ActionSizePropName);
var bpVectorActionType = property.FindPropertyRelative(k_ActionTypePropName);
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
var numberOfDescriptions = 0;
if (bpVectorActionType.enumValueIndex == 1)
{

EditorGUI.indentLevel++;
var vecActionDescriptions =
property.FindPropertyRelative(ActionDescriptionPropName);
property.FindPropertyRelative(k_ActionDescriptionPropName);
vecActionDescriptions.arraySize = numberOfDescriptions;
if (bpVectorActionType.enumValueIndex == 1)
{

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."),
position.y += LineHeight;
position.y += k_LineHeight;
}
else
{

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."),
position.y += LineHeight;
position.y += k_LineHeight;
/// <summary>
/// The Height required to draw the Action Descriptions
/// </summary>

var descriptionSize = 1;
if (property.FindPropertyRelative(ActionDescriptionPropName).isExpanded)
if (property.FindPropertyRelative(k_ActionDescriptionPropName).isExpanded)
var descriptions = property.FindPropertyRelative(ActionDescriptionPropName);
var descriptions = property.FindPropertyRelative(k_ActionDescriptionPropName);
return descriptionSize * LineHeight;
return descriptionSize * k_LineHeight;
}
}
}

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


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

[CustomPropertyDrawer(typeof(BroadcastHub))]
public class BroadcastHubDrawer : PropertyDrawer
{
private BroadcastHub _hub;
private BroadcastHub m_Hub;
private const float LineHeight = 17f;
private const float k_LineHeight = 17f;
private const float ExtraSpaceBelow = 10f;
private const float k_ExtraSpaceBelow = 10f;
private const int ControlSize = 80;
private const int k_ControlSize = 80;
/// <summary>
/// Computes the height of the Drawer depending on the property it is showing

/// <returns>The vertical space needed to draw the property.</returns>
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
LazyInitializeHub(property, label);
var numLines = _hub.Count + 2 + (_hub.Count > 0 ? 1 : 0);
return (numLines) * LineHeight + ExtraSpaceBelow;
LazyInitializeHub(property);
var numLines = m_Hub.Count + 2 + (m_Hub.Count > 0 ? 1 : 0);
return (numLines) * k_LineHeight + k_ExtraSpaceBelow;
LazyInitializeHub(property, label);
position.height = LineHeight;
EditorGUI.LabelField(position, new GUIContent(label.text,
LazyInitializeHub(property);
position.height = k_LineHeight;
EditorGUI.LabelField(position, new GUIContent(label.text,
position.y += LineHeight;
position.y += k_LineHeight;
position.y += LineHeight;
position.y += k_LineHeight;
var brainWidth = position.width - ControlSize;
var brainWidth = position.width - k_ControlSize;
position.x + brainWidth, position.y, ControlSize, position.height);
if (_hub.Count > 0)
position.x + brainWidth, position.y, k_ControlSize, position.height);
if (m_Hub.Count > 0)
brainRect.y += LineHeight;
brainRect.y += k_LineHeight;
controlRect.y += LineHeight;
controlRect.y += k_LineHeight;
controlRect.x += 15;
}
DrawBrains(brainRect, controlRect);

/// <summary>
/// Draws the Add and Remove buttons.
/// </summary>

// This is the rectangle for the Add button
var addButtonRect = position;
addButtonRect.x += 20;
if (_hub.Count > 0)
if (m_Hub.Count > 0)
{
addButtonRect.width /= 2;
addButtonRect.width -= 24;

/// <param name="controlRect">The Rect to draw the control checkbox.</param>
private void DrawBrains(Rect brainRect, Rect controlRect)
{
for (var index = 0; index < _hub.Count; index++)
for (var index = 0; index < m_Hub.Count; index++)
var exposedBrains = _hub.broadcastingBrains;
var exposedBrains = m_Hub.broadcastingBrains;
brainRect.y += LineHeight;
brainRect.y += k_LineHeight;
_hub.broadcastingBrains.RemoveAt(index);
m_Hub.broadcastingBrains.RemoveAt(index);
var brainToInsert = exposedBrains.Contains(newBrain) ? null : newBrain;
exposedBrains.Insert(index, brainToInsert);
break;

if (brain is LearningBrain)
{
var isTraining = _hub.IsControlled(brain);
var isTraining = m_Hub.IsControlled(brain);
_hub.SetControlled(brain, isTraining);
m_Hub.SetControlled(brain, isTraining);
controlRect.y += LineHeight;
controlRect.y += k_LineHeight;
if (EditorGUI.EndChangeCheck())
{
MarkSceneAsDirty();

/// </summary>
/// <param name="property">The SerializedProperty of the BroadcastHub
/// to make the custom GUI for.</param>
/// <param name="label">The label of this property.</param>
private void LazyInitializeHub(SerializedProperty property, GUIContent label)
private void LazyInitializeHub(SerializedProperty property)
if (_hub != null)
if (m_Hub != null)
_hub = fieldInfo.GetValue(target) as BroadcastHub;
if (_hub == null)
m_Hub = fieldInfo.GetValue(target) as BroadcastHub;
if (m_Hub == null)
_hub = new BroadcastHub();
fieldInfo.SetValue(target, _hub);
m_Hub = new BroadcastHub();
fieldInfo.SetValue(target, m_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.

if (!EditorApplication.isPlaying)
{
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
}
}

private void RemoveLastBrain()
{
if (_hub.Count > 0)
if (m_Hub.Count > 0)
_hub.broadcastingBrains.RemoveAt(_hub.broadcastingBrains.Count - 1);
m_Hub.broadcastingBrains.RemoveAt(m_Hub.broadcastingBrains.Count - 1);
}
}

private void AddBrain()
{
_hub.broadcastingBrains.Add(null);
m_Hub.broadcastingBrains.Add(null);
}
}
}

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

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


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

[CanEditMultipleObjects]
public class DemonstrationEditor : Editor
{
SerializedProperty brainParameters;
SerializedProperty demoMetaData;
SerializedProperty m_BrainParameters;
SerializedProperty m_DemoMetaData;
brainParameters = serializedObject.FindProperty("brainParameters");
demoMetaData = serializedObject.FindProperty("metaData");
m_BrainParameters = serializedObject.FindProperty("brainParameters");
m_DemoMetaData = serializedObject.FindProperty("metaData");
}
/// <summary>

static string BuildActionArrayLabel(SerializedProperty actionSizeProperty)
{
var actionSize = actionSizeProperty.arraySize;
StringBuilder actionLabel = new StringBuilder("[ ");
for (int i = 0; i < actionSize; i++)
var actionLabel = new StringBuilder("[ ");
for (var i = 0; i < actionSize; i++)
{
actionLabel.Append(actionSizeProperty.GetArrayElementAtIndex(i).intValue);
if (i < actionSize - 1)

}
/// <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.
/// </summary>
void MakeBrainParametersProperty(SerializedProperty property)

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

{
serializedObject.Update();
EditorGUILayout.LabelField("Meta Data", EditorStyles.boldLabel);
MakeMetaDataProperty(demoMetaData);
MakeMetaDataProperty(m_DemoMetaData);
MakeBrainParametersProperty(brainParameters);
MakeBrainParametersProperty(m_BrainParameters);
serializedObject.ApplyModifiedProperties();
}
}

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


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

[ScriptedImporter(1, new[] {"demo"})]
public class DemonstrationImporter : ScriptedImporter
{
private const string IconPath = "Assets/ML-Agents/Resources/DemoIcon.png";
private const string k_IconPath = "Assets/ML-Agents/Resources/DemoIcon.png";
public override void OnImportAsset(AssetImportContext ctx)
{

demonstration.Initialize(brainParameters, metaData);
userData = demonstration.ToString();
Texture2D texture = (Texture2D)
AssetDatabase.LoadAssetAtPath(IconPath, typeof(Texture2D));
var texture = (Texture2D)
AssetDatabase.LoadAssetAtPath(k_IconPath, typeof(Texture2D));
#if UNITY_2017_3_OR_NEWER
ctx.AddObjectToAsset(ctx.assetPath, demonstration, texture);

}
catch
{
return;
// ignored
}
}
}

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;

40
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.
private const string ModelPropName = "model";
private const string InferenceDevicePropName = "inferenceDevice";
private const float TimeBetweenModelReloads = 2f;
private const string k_ModelPropName = "model";
private const string k_InferenceDevicePropName = "inferenceDevice";
private const float k_TimeBetweenModelReloads = 2f;
private float _timeSinceModelReload;
private float m_TimeSinceModelReload;
private bool _requireReload;
private bool m_RequireReload;
_requireReload = true;
m_RequireReload = true;
/// <summary>
/// Called when the user leaves the Inspector for the LearningBrain
/// </summary>

}
var brain = (LearningBrain) target;
var brain = (LearningBrain)target;
serializedBrain.Update();
var tfGraphModel = serializedBrain.FindProperty(ModelPropName);
serializedBrain.Update();
var tfGraphModel = serializedBrain.FindProperty(k_ModelPropName);
var inferenceDevice = serializedBrain.FindProperty(InferenceDevicePropName);
var inferenceDevice = serializedBrain.FindProperty(k_InferenceDevicePropName);
_requireReload = true;
m_RequireReload = true;
if (_requireReload && _timeSinceModelReload > TimeBetweenModelReloads)
if (m_RequireReload && m_TimeSinceModelReload > k_TimeBetweenModelReloads)
_requireReload = false;
_timeSinceModelReload = 0;
m_RequireReload = false;
m_TimeSinceModelReload = 0;
}
// Display all failed checks
var failedChecks = brain.GetModelFailedChecks();

/// </summary>
private void IncreaseTimeSinceLastModelReload()
{
_timeSinceModelReload += Time.deltaTime;
m_TimeSinceModelReload += Time.deltaTime;
}
}
}

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


using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace MLAgents
{

[CustomEditor(typeof(PlayerBrain))]
public class PlayerBrainEditor : BrainEditor
{
private const string KeyContinuousPropName = "keyContinuousPlayerActions";
private const string KeyDiscretePropName = "discretePlayerActions";
private const string AxisContinuousPropName = "axisContinuousPlayerActions";
private const string k_KeyContinuousPropName = "keyContinuousPlayerActions";
private const string k_KeyDiscretePropName = "discretePlayerActions";
private const string k_AxisContinuousPropName = "axisContinuousPlayerActions";
var brain = (PlayerBrain) target;
var brain = (PlayerBrain)target;
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
if (brain.brainParameters.vectorActionSpaceType == SpaceType.Continuous)
{
DrawContinuousKeyMapping(serializedBrain, brain);
}

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

SerializedObject serializedBrain, PlayerBrain brain)
{
GUILayout.Label("Edit the continuous inputs for your actions", EditorStyles.boldLabel);
var keyActionsProp = serializedBrain.FindProperty(KeyContinuousPropName);
var axisActionsProp = serializedBrain.FindProperty(AxisContinuousPropName);
var keyActionsProp = serializedBrain.FindProperty(k_KeyContinuousPropName);
var axisActionsProp = serializedBrain.FindProperty(k_AxisContinuousPropName);
EditorGUILayout.PropertyField(keyActionsProp , true);
EditorGUILayout.PropertyField(axisActionsProp, true);
var keyContinuous = brain.keyContinuousPlayerActions;

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}",
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",
var dhas = serializedBrain.FindProperty(KeyDiscretePropName);
var dhas = serializedBrain.FindProperty(k_KeyDiscretePropName);
serializedBrain.Update();
EditorGUILayout.PropertyField(dhas, true);
}

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


using UnityEngine;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace MLAgents
{

[CustomPropertyDrawer(typeof(ResetParameters))]
public class ResetParameterDrawer : PropertyDrawer
{
private ResetParameters _parameters;
private ResetParameters m_Parameters;
private const float LineHeight = 17f;
private const float k_LineHeight = 17f;
private const string NewKeyPrefix = "Param-";
private const string k_NewKeyPrefix = "Param-";
/// <summary>
/// Computes the height of the Drawer depending on the property it is showing

/// <returns>The vertical space needed to draw the property.</returns>
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
LazyInitializeParameters(property, label);
return (_parameters.Count + 2) * LineHeight;
LazyInitializeParameters(property);
return (m_Parameters.Count + 2) * k_LineHeight;
LazyInitializeParameters(property, label);
position.height = LineHeight;
LazyInitializeParameters(property);
position.height = k_LineHeight;
position.y += LineHeight;
position.y += k_LineHeight;
foreach (var parameter in _parameters)
foreach (var parameter in m_Parameters)
keyRect.y += LineHeight;
valueRect.y += LineHeight;
keyRect.y += k_LineHeight;
valueRect.y += k_LineHeight;
EditorGUI.BeginChangeCheck();
var newKey = EditorGUI.TextField(keyRect, key);
if (EditorGUI.EndChangeCheck())

{
_parameters.Remove(key);
_parameters.Add(newKey, value);
m_Parameters.Remove(key);
m_Parameters.Add(newKey, value);
}
catch (Exception e)
{

if (EditorGUI.EndChangeCheck())
{
MarkSceneAsDirty();
_parameters[key] = value;
m_Parameters[key] = value;
break;
}
}

private void DrawAddRemoveButtons(Rect addRect, Rect removeRect)
{
// This is the Add button
if (_parameters.Count == 0)
if (m_Parameters.Count == 0)
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 (_parameters.Count == 0)
if (m_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();

{
if (!EditorApplication.isPlaying)
{
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
}
}

/// <param name="property">The SerializedProperty of the ResetParameters
/// to make the custom GUI for.</param>
/// <param name="label">The label of this property.</param>
private void LazyInitializeParameters(SerializedProperty property, GUIContent label)
private void LazyInitializeParameters(SerializedProperty property)
if (_parameters != null)
if (m_Parameters != null)
_parameters = fieldInfo.GetValue(target) as ResetParameters;
if (_parameters == null)
m_Parameters = fieldInfo.GetValue(target) as ResetParameters;
if (m_Parameters == null)
_parameters = new ResetParameters();
fieldInfo.SetValue(target, _parameters);
m_Parameters = new ResetParameters();
fieldInfo.SetValue(target, m_Parameters);
}
}

private void RemoveLastParameter()
{
if (_parameters.Count > 0)
if (m_Parameters.Count > 0)
string key = _parameters.Keys.ToList()[_parameters.Count - 1];
_parameters.Remove(key);
var key = m_Parameters.Keys.ToList()[m_Parameters.Count - 1];
m_Parameters.Remove(key);
}
}

private void AddParameter()
{
string key = NewKeyPrefix + _parameters.Count;
var key = k_NewKeyPrefix + m_Parameters.Count;
_parameters.Add(key, value);
m_Parameters.Add(key, value);
}
catch (Exception e)
{

38
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;

public class DemonstrationTests : MonoBehaviour
{
private const string DemoDirecory = "Assets/Demonstrations/";
private const string ExtensionType = ".demo";
private const string DemoName = "Test";
private const string k_DemoDirecory = "Assets/Demonstrations/";
private const string k_ExtensionType = ".demo";
private const string k_DemoName = "Test";
[Test]
public void TestSanitization()
{

var fileSystem = new MockFileSystem();
var demoStore = new DemonstrationStore(fileSystem);
Assert.IsFalse(fileSystem.Directory.Exists(DemoDirecory));
Assert.IsFalse(fileSystem.Directory.Exists(k_DemoDirecory));
cameraResolutions = new [] {new Resolution()},
vectorActionDescriptions = new [] {"TestActionA", "TestActionB"},
vectorActionSize = new [] {2, 2},
vectorActionSpaceType = SpaceType.discrete
cameraResolutions = new[] {new Resolution()},
vectorActionDescriptions = new[] {"TestActionA", "TestActionB"},
vectorActionSize = new[] {2, 2},
vectorActionSpaceType = SpaceType.Discrete
demoStore.Initialize(DemoName, brainParameters, "TestBrain");
Assert.IsTrue(fileSystem.Directory.Exists(DemoDirecory));
Assert.IsTrue(fileSystem.FileExists(DemoDirecory + DemoName + ExtensionType));
demoStore.Initialize(k_DemoName, brainParameters, "TestBrain");
Assert.IsTrue(fileSystem.Directory.Exists(k_DemoDirecory));
Assert.IsTrue(fileSystem.FileExists(k_DemoDirecory + k_DemoName + k_ExtensionType));
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();
}

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


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

public void FailsWithContinuous()
{
var bp = new BrainParameters();
bp.vectorActionSpaceType = SpaceType.continuous;
bp.vectorActionSize = new int[1] {4};
bp.vectorActionSpaceType = SpaceType.Continuous;
bp.vectorActionSize = new[] {4};
masker.SetActionMask(0, new int[1] {0});
masker.SetActionMask(0, new[] {0});
}
[Test]

bp.vectorActionSpaceType = SpaceType.discrete;
bp.vectorActionSpaceType = SpaceType.Discrete;
bp.vectorActionSpaceType = SpaceType.discrete;
bp.vectorActionSize = new int[3] {4, 5, 6};
bp.vectorActionSpaceType = SpaceType.Discrete;
bp.vectorActionSize = new[] {4, 5, 6};
masker.SetActionMask(0, new int[]{1,2,3});
masker.SetActionMask(0, new[] {1, 2, 3});
mask = masker.GetMask();
Assert.IsFalse(mask[0]);
Assert.IsTrue(mask[1]);

Assert.AreEqual(mask.Length, 15);
}
var bp = new BrainParameters();
bp.vectorActionSpaceType = SpaceType.discrete;
bp.vectorActionSize = new int[3] {4, 5, 6};
var bp = new BrainParameters
{
vectorActionSpaceType = SpaceType.Discrete,
vectorActionSize = new[] { 4, 5, 6 }
};
bool[] mask = masker.GetMask();
masker.SetActionMask(1, new int[]{1,2,3});
mask = masker.GetMask();
masker.SetActionMask(1, new[] {1, 2, 3});
var mask = masker.GetMask();
Assert.IsFalse(mask[0]);
Assert.IsFalse(mask[4]);
Assert.IsTrue(mask[5]);

Assert.IsFalse(mask[9]);
}
var bp = new BrainParameters();
bp.vectorActionSpaceType = SpaceType.discrete;
bp.vectorActionSize = new int[3] {4, 5, 6};
var bp = new BrainParameters
{
vectorActionSpaceType = SpaceType.Discrete,
vectorActionSize = new[] { 4, 5, 6 }
};
var mask = masker.GetMask();
masker.SetActionMask(1, new int[3]{1,2,3});
mask = masker.GetMask();
masker.SetActionMask(1, new[] {1, 2, 3});
mask = masker.GetMask();
var mask = masker.GetMask();
for (var i = 0; i < 15; i++)
{
Assert.IsFalse(mask[i]);

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