浏览代码

Namespacification (#814)

* [Namespace created] Added the namespace MLAgents on the C# scripts
/develop-generalizationTraining-TrainerController
Arthur Juliani 7 年前
当前提交
d4a2df66
共有 76 个文件被更改,包括 2966 次插入2532 次删除
  1. 130
      unity-environment/Assets/ML-Agents/Editor/AgentEditor.cs
  2. 159
      unity-environment/Assets/ML-Agents/Editor/BrainEditor.cs
  3. 2
      unity-environment/Assets/ML-Agents/Editor/MLAgentsEditModeTest.cs
  4. 199
      unity-environment/Assets/ML-Agents/Editor/ResetParameterDrawer.cs
  5. 1
      unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAcademy.cs
  6. 1
      unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs
  7. 1
      unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs
  8. 1
      unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
  9. 1
      unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAcademy.cs
  10. 1
      unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAgent.cs
  11. 1
      unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaArea.cs
  12. 1
      unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicAcademy.cs
  13. 1
      unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
  14. 1
      unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicDecision.cs
  15. 1
      unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAcademy.cs
  16. 1
      unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
  17. 1
      unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerBanana.cs
  18. 1
      unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAcademy.cs
  19. 1
      unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgentConfigurable.cs
  20. 1
      unity-environment/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAcademy.cs
  21. 1
      unity-environment/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  22. 1
      unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs
  23. 1
      unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs
  24. 1
      unity-environment/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs
  25. 1
      unity-environment/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockAcademy.cs
  26. 1
      unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAcademy.cs
  27. 1
      unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs
  28. 1
      unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidArea.cs
  29. 1
      unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAcademy.cs
  30. 1
      unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs
  31. 1
      unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherDecision.cs
  32. 32
      unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/Area.cs
  33. 34
      unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/CameraFollow.cs
  34. 183
      unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/FlyCamera.cs
  35. 70
      unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/GroundContact.cs
  36. 71
      unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/RandomDecision.cs
  37. 130
      unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs
  38. 1
      unity-environment/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
  39. 1
      unity-environment/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerAcademy.cs
  40. 1
      unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateAcademy.cs
  41. 1
      unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateAgent.cs
  42. 1
      unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateDecision.cs
  43. 1
      unity-environment/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAcademy.cs
  44. 1
      unity-environment/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs
  45. 1
      unity-environment/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAcademy.cs
  46. 1
      unity-environment/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs
  47. 1
      unity-environment/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAcademy.cs
  48. 1
      unity-environment/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs
  49. 992
      unity-environment/Assets/ML-Agents/Scripts/Academy.cs
  50. 998
      unity-environment/Assets/ML-Agents/Scripts/Agent.cs
  51. 85
      unity-environment/Assets/ML-Agents/Scripts/BCTeacherHelper.cs
  52. 339
      unity-environment/Assets/ML-Agents/Scripts/Brain.cs
  53. 26
      unity-environment/Assets/ML-Agents/Scripts/CoreBrain.cs
  54. 79
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainExternal.cs
  55. 133
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainHeuristic.cs
  56. 322
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainInternal.cs
  57. 228
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs
  58. 87
      unity-environment/Assets/ML-Agents/Scripts/Decision.cs
  59. 835
      unity-environment/Assets/ML-Agents/Scripts/Monitor.cs
  60. 66
      unity-environment/Assets/ML-Agents/Scripts/ResetParameters.cs
  61. 30
      unity-environment/Assets/ML-Agents/Scripts/UnityAgentsException.cs
  62. 54
      unity-environment/Assets/PlayGround/Communicator/Communicatror_Test.cs
  63. 54
      unity-environment/Assets/PlayGround/Docker/DockerTest.cs
  64. 18
      unity-environment/Assets/PlayGround/PersistentSingleton/Editor/SingletonWindow.cs
  65. 18
      unity-environment/Assets/PlayGround/PersistentSingleton/SingletonTest.cs
  66. 18
      unity-environment/Assets/PlayGround/PersistentStatic/Editor/StaticWindow.cs
  67. 31
      unity-environment/Assets/PlayGround/PersistentStatic/StaticTest.cs
  68. 37
      unity-environment/Assets/PlayGround/TensorflowTest/TensorflowTester.cs

130
unity-environment/Assets/ML-Agents/Editor/AgentEditor.cs


using UnityEngine;
using UnityEditor;
namespace MLAgents
{
[CustomEditor(typeof(Agent), true)]
[CanEditMultipleObjects]
public class AgentEditor : Editor
{
public override void OnInspectorGUI()
[CustomEditor(typeof(Agent), true)]
[CanEditMultipleObjects]
public class AgentEditor : Editor
SerializedObject serializedAgent = serializedObject;
serializedAgent.Update();
SerializedProperty brain = serializedAgent.FindProperty("brain");
SerializedProperty actionsPerDecision = serializedAgent.FindProperty(
"agentParameters.numberOfActionsBetweenDecisions");
SerializedProperty maxSteps = serializedAgent.FindProperty(
"agentParameters.maxStep");
SerializedProperty isResetOnDone = serializedAgent.FindProperty(
"agentParameters.resetOnDone");
SerializedProperty isODD = serializedAgent.FindProperty(
"agentParameters.onDemandDecision");
SerializedProperty cameras = serializedAgent.FindProperty(
"agentParameters.agentCameras");
public override void OnInspectorGUI()
{
SerializedObject serializedAgent = serializedObject;
serializedAgent.Update();
EditorGUILayout.PropertyField(brain);
SerializedProperty brain = serializedAgent.FindProperty("brain");
SerializedProperty actionsPerDecision = serializedAgent.FindProperty(
"agentParameters.numberOfActionsBetweenDecisions");
SerializedProperty maxSteps = serializedAgent.FindProperty(
"agentParameters.maxStep");
SerializedProperty isResetOnDone = serializedAgent.FindProperty(
"agentParameters.resetOnDone");
SerializedProperty isODD = serializedAgent.FindProperty(
"agentParameters.onDemandDecision");
SerializedProperty cameras = serializedAgent.FindProperty(
"agentParameters.agentCameras");
EditorGUILayout.LabelField("Agent Cameras");
for (int i = 0; i < cameras.arraySize; i++)
{
EditorGUILayout.PropertyField(
cameras.GetArrayElementAtIndex(i),
new GUIContent("Camera " + (i + 1).ToString() + ": "));
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Add Camera", EditorStyles.miniButton))
{
cameras.arraySize++;
}
if (GUILayout.Button("Remove Camera", EditorStyles.miniButton))
{
cameras.arraySize--;
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(brain);
EditorGUILayout.PropertyField(
maxSteps,
new GUIContent(
"Max Step", "The per-agent maximum number of steps."));
EditorGUILayout.PropertyField(
isResetOnDone,
new GUIContent(
"Reset On Done",
"If checked, the agent will reset on done. Else, AgentOnDone() will be called."));
EditorGUILayout.PropertyField(
isODD,
new GUIContent(
"On Demand Decisions",
"If checked, you must manually request decisions."));
if (!isODD.boolValue)
{
EditorGUILayout.LabelField("Agent Cameras");
for (int i = 0; i < cameras.arraySize; i++)
{
EditorGUILayout.PropertyField(
cameras.GetArrayElementAtIndex(i),
new GUIContent("Camera " + (i + 1).ToString() + ": "));
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Add Camera", EditorStyles.miniButton))
{
cameras.arraySize++;
}
if (GUILayout.Button("Remove Camera", EditorStyles.miniButton))
{
cameras.arraySize--;
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(
maxSteps,
new GUIContent(
"Max Step", "The per-agent maximum number of steps."));
EditorGUILayout.PropertyField(
isResetOnDone,
new GUIContent(
"Reset On Done",
"If checked, the agent will reset on done. Else, AgentOnDone() will be called."));
actionsPerDecision,
isODD,
"Decision Frequency",
"The agent will automatically request a decision every X" +
" steps and perform an action at every step."));
actionsPerDecision.intValue = Mathf.Max(1, actionsPerDecision.intValue);
}
"On Demand Decisions",
"If checked, you must manually request decisions."));
if (!isODD.boolValue)
{
EditorGUILayout.PropertyField(
actionsPerDecision,
new GUIContent(
"Decision Frequency",
"The agent will automatically request a decision every X" +
" steps and perform an action at every step."));
actionsPerDecision.intValue = Mathf.Max(1, actionsPerDecision.intValue);
}
serializedAgent.ApplyModifiedProperties();
serializedAgent.ApplyModifiedProperties();
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
base.OnInspectorGUI();
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
base.OnInspectorGUI();
}
}
}

159
unity-environment/Assets/ML-Agents/Editor/BrainEditor.cs


using UnityEditor;
using System.Linq;
namespace MLAgents
{
[CustomEditor (typeof(Brain))]
public class BrainEditor : Editor
{
[SerializeField]
bool _Foldout = true;
public override void OnInspectorGUI ()
[CustomEditor(typeof(Brain))]
public class BrainEditor : Editor
Brain myBrain = (Brain)target;
SerializedObject serializedBrain = serializedObject;
[SerializeField] bool _Foldout = true;
if (myBrain.transform.parent == null) {
EditorGUILayout.HelpBox ("A Brain GameObject must be a child of an Academy GameObject!", MessageType.Error);
} else if (myBrain.transform.parent.GetComponent<Academy> () == null) {
EditorGUILayout.HelpBox ("The Parent of a Brain must have an Academy Component attached to it!", MessageType.Error);
}
public override void OnInspectorGUI()
{
Brain myBrain = (Brain) target;
SerializedObject serializedBrain = serializedObject;
BrainParameters parameters = myBrain.brainParameters;
if (parameters.vectorActionDescriptions == null || parameters.vectorActionDescriptions.Length != parameters.vectorActionSize)
parameters.vectorActionDescriptions = new string[parameters.vectorActionSize];
serializedBrain.Update();
if (myBrain.transform.parent == null)
{
EditorGUILayout.HelpBox(
"A Brain GameObject must be a child of an Academy GameObject!",
MessageType.Error);
}
else if (myBrain.transform.parent.GetComponent<Academy>() == null)
{
EditorGUILayout.HelpBox(
"The Parent of a Brain must have an Academy Component attached to it!",
MessageType.Error);
}
_Foldout = EditorGUILayout.Foldout(_Foldout, "Brain Parameters");
int indentLevel = EditorGUI.indentLevel;
if (_Foldout)
{
EditorGUI.indentLevel++;
EditorGUILayout.LabelField("Vector Observation");
EditorGUI.indentLevel++;
BrainParameters parameters = myBrain.brainParameters;
if (parameters.vectorActionDescriptions == null ||
parameters.vectorActionDescriptions.Length != parameters.vectorActionSize)
parameters.vectorActionDescriptions = new string[parameters.vectorActionSize];
SerializedProperty bpVectorObsType = serializedBrain.FindProperty("brainParameters.vectorObservationSpaceType");
EditorGUILayout.PropertyField(bpVectorObsType, new GUIContent("Space Type", "Corresponds to whether state " +
"vector contains a single integer (Discrete) " +
"or a series of real-valued floats (Continuous)."));
serializedBrain.Update();
_Foldout = EditorGUILayout.Foldout(_Foldout, "Brain Parameters");
int indentLevel = EditorGUI.indentLevel;
if (_Foldout)
{
EditorGUI.indentLevel++;
EditorGUILayout.LabelField("Vector Observation");
EditorGUI.indentLevel++;
SerializedProperty bpVectorObsType =
serializedBrain.FindProperty("brainParameters.vectorObservationSpaceType");
EditorGUILayout.PropertyField(bpVectorObsType, new GUIContent("Space Type",
"Corresponds to whether state " +
"vector contains a single integer (Discrete) " +
"or a series of real-valued floats (Continuous)."));
SerializedProperty bpVectorObsSize =
serializedBrain.FindProperty("brainParameters.vectorObservationSize");
EditorGUILayout.PropertyField(bpVectorObsSize, new GUIContent("Space Size",
"Length of state " +
"vector for brain (In Continuous state space)." +
"Or number of possible values (in Discrete state space)."));
SerializedProperty bpVectorObsSize = serializedBrain.FindProperty("brainParameters.vectorObservationSize");
EditorGUILayout.PropertyField(bpVectorObsSize, new GUIContent("Space Size", "Length of state " +
"vector for brain (In Continuous state space)." +
"Or number of possible values (in Discrete state space)."));
SerializedProperty bpNumStackedVectorObs =
serializedBrain.FindProperty("brainParameters.numStackedVectorObservations");
EditorGUILayout.PropertyField(bpNumStackedVectorObs, new GUIContent(
"Stacked Vectors", "Number of states that" +
" will be stacked before beeing fed to the neural network."));
SerializedProperty bpNumStackedVectorObs = serializedBrain.FindProperty("brainParameters.numStackedVectorObservations");
EditorGUILayout.PropertyField(bpNumStackedVectorObs, new GUIContent("Stacked Vectors", "Number of states that" +
" will be stacked before beeing fed to the neural network."));
EditorGUI.indentLevel--;
SerializedProperty bpCamResol =
serializedBrain.FindProperty("brainParameters.cameraResolutions");
EditorGUILayout.PropertyField(bpCamResol, new GUIContent("Visual Observation",
"Describes height, " +
"width, and whether to greyscale visual observations for the Brain."), true);
EditorGUI.indentLevel--;
SerializedProperty bpCamResol = serializedBrain.FindProperty("brainParameters.cameraResolutions");
EditorGUILayout.PropertyField(bpCamResol, new GUIContent("Visual Observation", "Describes height, " +
"width, and whether to greyscale visual observations for the Brain."), true);
EditorGUILayout.LabelField("Vector Action");
EditorGUI.indentLevel++;
EditorGUILayout.LabelField("Vector Action");
EditorGUI.indentLevel++;
SerializedProperty bpVectorActionType =
serializedBrain.FindProperty("brainParameters.vectorActionSpaceType");
EditorGUILayout.PropertyField(bpVectorActionType, new GUIContent("Space Type",
"Corresponds to whether state" +
" vector contains a single integer (Discrete) " +
"or a series of real-valued floats (Continuous)."));
SerializedProperty bpVectorActionType = serializedBrain.FindProperty("brainParameters.vectorActionSpaceType");
EditorGUILayout.PropertyField(bpVectorActionType, new GUIContent("Space Type", "Corresponds to whether state" +
" vector contains a single integer (Discrete) " +
"or a series of real-valued floats (Continuous)."));
SerializedProperty bpVectorActionSize =
serializedBrain.FindProperty("brainParameters.vectorActionSize");
EditorGUILayout.PropertyField(bpVectorActionSize, new GUIContent("Space Size",
"Length of action vector " +
"for brain (In Continuous state space)." +
"Or number of possible values (In Discrete action space)."));
SerializedProperty bpVectorActionSize = serializedBrain.FindProperty("brainParameters.vectorActionSize");
EditorGUILayout.PropertyField(bpVectorActionSize, new GUIContent("Space Size", "Length of action vector " +
"for brain (In Continuous state space)." +
"Or number of possible values (In Discrete action space)."));
SerializedProperty bpVectorActionDescription =
serializedBrain.FindProperty("brainParameters.vectorActionDescriptions");
EditorGUILayout.PropertyField(bpVectorActionDescription, new GUIContent(
"Action Descriptions", "A list of strings used to name" +
" the available actions for the Brain."), true);
SerializedProperty bpVectorActionDescription = serializedBrain.FindProperty("brainParameters.vectorActionDescriptions");
EditorGUILayout.PropertyField(bpVectorActionDescription, new GUIContent("Action Descriptions", "A list of strings used to name" +
" the available actions for the Brain."), true);
}
}
EditorGUI.indentLevel = indentLevel;
SerializedProperty bt = serializedBrain.FindProperty("brainType");
EditorGUILayout.PropertyField(bt);
EditorGUI.indentLevel = indentLevel;
SerializedProperty bt = serializedBrain.FindProperty("brainType");
EditorGUILayout.PropertyField(bt);
if (bt.enumValueIndex < 0) {
bt.enumValueIndex = (int)BrainType.Player;
}
if (bt.enumValueIndex < 0)
{
bt.enumValueIndex = (int) BrainType.Player;
}
serializedBrain.ApplyModifiedProperties();
serializedBrain.ApplyModifiedProperties();
myBrain.UpdateCoreBrains ();
myBrain.coreBrain.OnInspector ();
myBrain.UpdateCoreBrains();
myBrain.coreBrain.OnInspector();
#if !NET_4_6 && ENABLE_TENSORFLOW
#if !NET_4_6 && ENABLE_TENSORFLOW
}
}
}

2
unity-environment/Assets/ML-Agents/Editor/MLAgentsEditModeTest.cs


using NUnit.Framework;
using System.Reflection;
namespace MLAgentsTests
namespace MLAgents.Tests
{
public class TestAcademy : Academy
{

199
unity-environment/Assets/ML-Agents/Editor/ResetParameterDrawer.cs


using System.Linq;
using UnityEditor.SceneManagement;
[CustomPropertyDrawer(typeof(ResetParameters))]
public class ResetParameterDrawer : PropertyDrawer
namespace MLAgents
private ResetParameters _Dictionary;
private const float lineHeight = 17f;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
[CustomPropertyDrawer(typeof(ResetParameters))]
public class ResetParameterDrawer : PropertyDrawer
CheckInitialize(property, label);
return (_Dictionary.Count + 2) * lineHeight;
}
private ResetParameters _Dictionary;
private const float lineHeight = 17f;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
CheckInitialize(property, label);
position.height = lineHeight;
EditorGUI.LabelField(position, label);
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
CheckInitialize(property, label);
return (_Dictionary.Count + 2) * lineHeight;
}
EditorGUI.BeginProperty(position, label, property);
foreach (var item in _Dictionary)
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
var key = item.Key;
var value = item.Value;
position.y += lineHeight;
CheckInitialize(property, label);
position.height = lineHeight;
EditorGUI.LabelField(position, label);
// This is the rectangle for the key
var keyRect = position;
keyRect.x += 20;
keyRect.width /= 2;
keyRect.width -= 24;
EditorGUI.BeginChangeCheck();
var newKey = EditorGUI.TextField(keyRect, key);
if (EditorGUI.EndChangeCheck())
EditorGUI.BeginProperty(position, label, property);
foreach (var item in _Dictionary)
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
try
var key = item.Key;
var value = item.Value;
position.y += lineHeight;
// This is the rectangle for the key
var keyRect = position;
keyRect.x += 20;
keyRect.width /= 2;
keyRect.width -= 24;
EditorGUI.BeginChangeCheck();
var newKey = EditorGUI.TextField(keyRect, key);
if (EditorGUI.EndChangeCheck())
_Dictionary.Remove(key);
_Dictionary.Add(newKey, value);
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
try
{
_Dictionary.Remove(key);
_Dictionary.Add(newKey, value);
}
catch (Exception e)
{
Debug.Log(e.Message);
}
break;
catch (Exception e)
// This is the Rectangle for the value
var valueRect = position;
valueRect.x = position.width / 2 + 15;
valueRect.width = keyRect.width - 18;
EditorGUI.BeginChangeCheck();
value = EditorGUI.FloatField(valueRect, value);
if (EditorGUI.EndChangeCheck())
Debug.Log(e.Message);
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
_Dictionary[key] = value;
break;
break;
// This is the Rectangle for the value
var valueRect = position;
valueRect.x = position.width / 2 + 15;
valueRect.width = keyRect.width - 18;
EditorGUI.BeginChangeCheck();
value = EditorGUI.FloatField(valueRect, value);
if (EditorGUI.EndChangeCheck())
// This is the rectangle for the Add button
position.y += lineHeight;
var AddButtonRect = position;
AddButtonRect.x += 20;
AddButtonRect.width /= 2;
AddButtonRect.width -= 24;
if (GUI.Button(AddButtonRect, new GUIContent("Add New",
"Add a new item to the default reset paramters"), EditorStyles.miniButton))
_Dictionary[key] = value;
break;
AddNewItem();
}
// This is the rectangle for the Add button
position.y += lineHeight;
var AddButtonRect = position;
AddButtonRect.x += 20;
AddButtonRect.width /= 2;
AddButtonRect.width -= 24;
if (GUI.Button(AddButtonRect, new GUIContent("Add New",
"Add a new item to the default reset paramters"), EditorStyles.miniButton))
{
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
AddNewItem();
}
// This is the rectangle for the Remove button
var RemoveButtonRect = position;
RemoveButtonRect.x = position.width / 2 + 15;
RemoveButtonRect.width = AddButtonRect.width - 18;
if (GUI.Button(RemoveButtonRect, new GUIContent("Remove Last",
"Remove the last item to the default reset paramters"), EditorStyles.miniButton))
{
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
RemoveLastItem();
}
// This is the rectangle for the Remove button
var RemoveButtonRect = position;
RemoveButtonRect.x = position.width / 2 + 15;
RemoveButtonRect.width = AddButtonRect.width - 18;
if (GUI.Button(RemoveButtonRect, new GUIContent("Remove Last",
"Remove the last item to the default reset paramters"), EditorStyles.miniButton))
{
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
RemoveLastItem();
}
EditorGUI.EndProperty();
EditorGUI.EndProperty();
}
}
private void CheckInitialize(SerializedProperty property, GUIContent label)
{
if (_Dictionary == null)
private void CheckInitialize(SerializedProperty property, GUIContent label)
var target = property.serializedObject.targetObject;
_Dictionary = fieldInfo.GetValue(target) as ResetParameters;
_Dictionary = new ResetParameters();
fieldInfo.SetValue(target, _Dictionary);
var target = property.serializedObject.targetObject;
_Dictionary = fieldInfo.GetValue(target) as ResetParameters;
if (_Dictionary == null)
{
_Dictionary = new ResetParameters();
fieldInfo.SetValue(target, _Dictionary);
}
}
private void ClearResetParamters()
{
_Dictionary.Clear();
}
private void RemoveLastItem()
{
if (_Dictionary.Count > 0)
private void ClearResetParamters()
string key = _Dictionary.Keys.ToList()[_Dictionary.Count - 1];
_Dictionary.Remove(key);
_Dictionary.Clear();
}
private void AddNewItem()
{
string key = "Param-" + _Dictionary.Count.ToString();
var value = default(float);
try
private void RemoveLastItem()
_Dictionary.Add(key, value);
if (_Dictionary.Count > 0)
{
string key = _Dictionary.Keys.ToList()[_Dictionary.Count - 1];
_Dictionary.Remove(key);
}
catch (Exception e)
private void AddNewItem()
Debug.Log(e.Message);
string key = "Param-" + _Dictionary.Count.ToString();
var value = default(float);
try
{
_Dictionary.Add(key, value);
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
}

1
unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class Ball3DAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class Ball3DAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DDecision.cs


using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class Ball3DDecision : MonoBehaviour, Decision
{

1
unity-environment/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class Ball3DHardAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAcademy.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MLAgents;
public class BananaAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BananaAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/BananaCollectors/Scripts/BananaArea.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BananaArea : Area
{

1
unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BasicAcademy : Academy {

1
unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BasicAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/Basic/Scripts/BasicDecision.cs


using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BasicDecision : MonoBehaviour, Decision
{

1
unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BouncerAcademy : Academy {

1
unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BouncerAgent : Agent {

1
unity-environment/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerBanana.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class BouncerBanana : MonoBehaviour {

1
unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class CrawlerAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgentConfigurable.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class CrawlerAgentConfigurable : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAcademy.cs


using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using MLAgents;
public class GridAcademy : Academy

1
unity-environment/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs


using UnityEngine;
using System.Linq;
using MLAgents;
public class GridAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class HallwayAcademy : Academy {

1
unity-environment/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class HallwayAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class PushAgentBasic : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class PushBlockAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAcademy.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MLAgents;
public class PyramidAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs


using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;
using MLAgents;
public class PyramidAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidArea.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class PyramidArea : Area
{

1
unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class ReacherAcademy : Academy {

1
unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs


using UnityEngine;
using MLAgents;
public class ReacherAgent : Agent {

1
unity-environment/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherDecision.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class ReacherDecision : MonoBehaviour, Decision {

32
unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/Area.cs


using System.Collections.Generic;
using UnityEngine;
public class Area : MonoBehaviour {
namespace MLAgents
{
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public class Area : MonoBehaviour
{
public virtual void ResetArea() {
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public virtual void ResetArea()
{
}
}
}

34
unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/CameraFollow.cs


using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
namespace MLAgents
{
public Transform target;
Vector3 offset;
public class CameraFollow : MonoBehaviour
{
// Use this for initialization
void Start () {
offset = gameObject.transform.position - target.position;
}
// Update is called once per frame
void Update () {
// gameObject.transform.position = target.position + offset;
Vector3 newPosition = new Vector3(target.position.x + offset.x, transform.position.y, target.position.z + offset.z);
gameObject.transform.position = newPosition;
public Transform target;
Vector3 offset;
// Use this for initialization
void Start()
{
offset = gameObject.transform.position - target.position;
}
// Update is called once per frame
void Update()
{
// gameObject.transform.position = target.position + offset;
Vector3 newPosition = new Vector3(target.position.x + offset.x, transform.position.y,
target.position.z + offset.z);
gameObject.transform.position = newPosition;
}
}
}

183
unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/FlyCamera.cs


using UnityEngine;
using System.Collections;
public class FlyCamera : MonoBehaviour
namespace MLAgents
/*
wasd : basic movement
shift : Makes camera accelerate
space : Moves camera on X and Z axis only. So camera doesn't gain any height*/
public class FlyCamera : MonoBehaviour
{
/*
wasd : basic movement
shift : Makes camera accelerate
space : Moves camera on X and Z axis only. So camera doesn't gain any height*/
public float mainSpeed = 100.0f; // regular speed
public float shiftAdd = 250.0f; // multiplied by how long shift is held. Basically running
public float maxShift = 1000.0f; // Maximum speed when holdin gshift
public float camSens = 0.25f; // How sensitive it with mouse
public bool rotateOnlyIfMousedown = true;
public bool movementStaysFlat = true;
private Vector3 lastMouse = new Vector3(255, 255, 255); // kind of in the middle of the screen, rather than at the top (play)
private float totalRun = 1.0f;
void Awake()
{
Debug.Log("FlyCamera Awake() - RESETTING CAMERA POSITION"); // nop?
// nop:
// transform.position.Set(0,8,-32);
// transform.rotation.Set(15,0,0,1);
transform.position = new Vector3(0, 8, -32);
transform.rotation = Quaternion.Euler(25, 0, 0);
}
public float mainSpeed = 100.0f; // regular speed
public float shiftAdd = 250.0f; // multiplied by how long shift is held. Basically running
public float maxShift = 1000.0f; // Maximum speed when holdin gshift
public float camSens = 0.25f; // How sensitive it with mouse
public bool rotateOnlyIfMousedown = true;
public bool movementStaysFlat = true;
private Vector3
lastMouse =
new Vector3(255, 255,
255); // kind of in the middle of the screen, rather than at the top (play)
void Update()
{
private float totalRun = 1.0f;
if (Input.GetMouseButtonDown(1))
void Awake()
lastMouse = Input.mousePosition; // $CTK reset when we begin
Debug.Log("FlyCamera Awake() - RESETTING CAMERA POSITION"); // nop?
// nop:
// transform.position.Set(0,8,-32);
// transform.rotation.Set(15,0,0,1);
transform.position = new Vector3(0, 8, -32);
transform.rotation = Quaternion.Euler(25, 0, 0);
if (!rotateOnlyIfMousedown ||
(rotateOnlyIfMousedown && Input.GetMouseButton(1)))
void Update()
lastMouse = Input.mousePosition - lastMouse;
lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x, transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
// Mouse camera angle done.
}
if (Input.GetMouseButtonDown(1))
{
lastMouse = Input.mousePosition; // $CTK reset when we begin
}
if (!rotateOnlyIfMousedown ||
(rotateOnlyIfMousedown && Input.GetMouseButton(1)))
{
lastMouse = Input.mousePosition - lastMouse;
lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
lastMouse = new Vector3(transform.eulerAngles.x + lastMouse.x,
transform.eulerAngles.y + lastMouse.y, 0);
transform.eulerAngles = lastMouse;
lastMouse = Input.mousePosition;
// Mouse camera angle done.
}
// Keyboard commands
Vector3 p = GetBaseInput();
if (Input.GetKey(KeyCode.LeftShift))
{
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
}
else
{
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
p = p * mainSpeed;
}
p = p * Time.deltaTime;
Vector3 newPosition = transform.position;
if (Input.GetKey(KeyCode.Space)
|| (movementStaysFlat && !(rotateOnlyIfMousedown && Input.GetMouseButton(1))))
{
// If player wants to move on X and Z axis only
transform.Translate(p);
newPosition.x = transform.position.x;
newPosition.z = transform.position.z;
transform.position = newPosition;
}
else
{
transform.Translate(p);
}
// Keyboard commands
Vector3 p = GetBaseInput();
if (Input.GetKey(KeyCode.LeftShift))
{
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
else
private Vector3 GetBaseInput()
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
p = p * mainSpeed;
}
// returns the basic values, if it's 0 than it's not active.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey(KeyCode.W))
{
p_Velocity += new Vector3(0, 0, 1);
}
if (Input.GetKey(KeyCode.S))
{
p_Velocity += new Vector3(0, 0, -1);
}
p = p * Time.deltaTime;
Vector3 newPosition = transform.position;
if (Input.GetKey(KeyCode.Space)
|| (movementStaysFlat && !(rotateOnlyIfMousedown && Input.GetMouseButton(1))))
{ // If player wants to move on X and Z axis only
transform.Translate(p);
newPosition.x = transform.position.x;
newPosition.z = transform.position.z;
transform.position = newPosition;
}
else
{
transform.Translate(p);
}
if (Input.GetKey(KeyCode.A))
{
p_Velocity += new Vector3(-1, 0, 0);
}
}
if (Input.GetKey(KeyCode.D))
{
p_Velocity += new Vector3(1, 0, 0);
}
private Vector3 GetBaseInput()
{ // returns the basic values, if it's 0 than it's not active.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey(KeyCode.W))
{
p_Velocity += new Vector3(0, 0, 1);
}
if (Input.GetKey(KeyCode.S))
{
p_Velocity += new Vector3(0, 0, -1);
}
if (Input.GetKey(KeyCode.A))
{
p_Velocity += new Vector3(-1, 0, 0);
return p_Velocity;
if (Input.GetKey(KeyCode.D))
{
p_Velocity += new Vector3(1, 0, 0);
}
return p_Velocity;
}
}

70
unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/GroundContact.cs


using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This class contains logic for locomotion agents with joints which might make contact with the ground.
/// By attaching this as a component to those joints, their contact with the ground can be used as either
/// an observation for that agent, and/or a means of punishing the agent for making undesirable contact.
/// </summary>
public class GroundContact : MonoBehaviour
namespace MLAgents
public Agent agent;
public float contactPenalty;
public bool touchingGround;
public bool penalizeOnContact;
private const string Ground = "ground";
/// Obtain reference to agent.
/// This class contains logic for locomotion agents with joints which might make contact with the ground.
/// By attaching this as a component to those joints, their contact with the ground can be used as either
/// an observation for that agent, and/or a means of punishing the agent for making undesirable contact.
void Start()
public class GroundContact : MonoBehaviour
agent = transform.root.GetComponent<Agent>();
}
public Agent agent;
public float contactPenalty;
public bool touchingGround;
public bool penalizeOnContact;
private const string Ground = "ground";
/// <summary>
/// Check for collision with ground, and optionally penalize agent.
/// </summary>
void OnCollisionEnter(Collision other)
{
if (other.transform.CompareTag(Ground))
/// <summary>
/// Obtain reference to agent.
/// </summary>
void Start()
touchingGround = true;
if (penalizeOnContact)
agent = transform.root.GetComponent<Agent>();
}
/// <summary>
/// Check for collision with ground, and optionally penalize agent.
/// </summary>
void OnCollisionEnter(Collision other)
{
if (other.transform.CompareTag(Ground))
agent.Done();
agent.SetReward(contactPenalty);
touchingGround = true;
if (penalizeOnContact)
{
agent.Done();
agent.SetReward(contactPenalty);
}
}
/// <summary>
/// Check for end of ground collision and reset flag appropriately.
/// </summary>
void OnCollisionExit(Collision other)
{
if (other.transform.CompareTag(Ground))
/// <summary>
/// Check for end of ground collision and reset flag appropriately.
/// </summary>
void OnCollisionExit(Collision other)
touchingGround = false;
if (other.transform.CompareTag(Ground))
{
touchingGround = false;
}
}
}
}

71
unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/RandomDecision.cs


using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class RandomDecision : MonoBehaviour, Decision
namespace MLAgents
BrainParameters brainParameters;
SpaceType actionSpaceType;
int actionSpaceSize;
public void Awake()
public class RandomDecision : MonoBehaviour, Decision
brainParameters =
gameObject.GetComponent<Brain>().brainParameters;
actionSpaceType = brainParameters.vectorActionSpaceType;
actionSpaceSize = brainParameters.vectorActionSize;
}
BrainParameters brainParameters;
SpaceType actionSpaceType;
int actionSpaceSize;
public float[] Decide(
List<float> vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory)
{
if (actionSpaceType == SpaceType.continuous)
public void Awake()
List<float> act = new List<float>();
brainParameters =
gameObject.GetComponent<Brain>().brainParameters;
actionSpaceType = brainParameters.vectorActionSpaceType;
actionSpaceSize = brainParameters.vectorActionSize;
}
for (int i = 0; i < actionSpaceSize; i++)
public float[] Decide(
List<float> vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory)
{
if (actionSpaceType == SpaceType.continuous)
act.Add(2 * Random.value - 1);
List<float> act = new List<float>();
for (int i = 0; i < actionSpaceSize; i++)
{
act.Add(2 * Random.value - 1);
}
return act.ToArray();
return act.ToArray();
return new float[1] {Random.Range(0, actionSpaceSize)};
return new float[1] { Random.Range(0, actionSpaceSize) };
}
public List<float> MakeMemory(
List<float> vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory)
{
return new List<float>();
public List<float> MakeMemory(
List<float> vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory)
{
return new List<float>();
}
}
}

130
unity-environment/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception.cs


using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Ray perception component. Attach this to agents to enable "local perception"
/// via the use of ray casts directed outward from the agent.
/// </summary>
public class RayPerception : MonoBehaviour
namespace MLAgents
List<float> perceptionBuffer = new List<float>();
Vector3 endPosition;
RaycastHit hit;
/// Creates perception vector to be used as part of an observation of an agent.
/// Ray perception component. Attach this to agents to enable "local perception"
/// via the use of ray casts directed outward from the agent.
/// <returns>The partial vector observation corresponding to the set of rays</returns>
/// <param name="rayDistance">Radius of rays</param>
/// <param name="rayAngles">Anlges of rays (starting from (1,0) on unit circle).</param>
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
/// <param name="startOffset">Starting heigh offset of ray from center of agent.</param>
/// <param name="endOffset">Ending height offset of ray from center of agent.</param>
public List<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects,
float startOffset, float endOffset)
public class RayPerception : MonoBehaviour
perceptionBuffer.Clear();
// For each ray sublist stores categorial information on detected object
// along with object distance.
foreach (float angle in rayAngles)
List<float> perceptionBuffer = new List<float>();
Vector3 endPosition;
RaycastHit hit;
/// <summary>
/// Creates perception vector to be used as part of an observation of an agent.
/// </summary>
/// <returns>The partial vector observation corresponding to the set of rays</returns>
/// <param name="rayDistance">Radius of rays</param>
/// <param name="rayAngles">Anlges of rays (starting from (1,0) on unit circle).</param>
/// <param name="detectableObjects">List of tags which correspond to object types agent can see</param>
/// <param name="startOffset">Starting heigh offset of ray from center of agent.</param>
/// <param name="endOffset">Ending height offset of ray from center of agent.</param>
public List<float> Perceive(float rayDistance,
float[] rayAngles, string[] detectableObjects,
float startOffset, float endOffset)
endPosition = transform.TransformDirection(
PolarToCartesian(rayDistance, angle));
endPosition.y = endOffset;
if (Application.isEditor)
{
Debug.DrawRay(transform.position + new Vector3(0f, startOffset, 0f),
endPosition, Color.black, 0.01f, true);
}
float[] subList = new float[detectableObjects.Length + 2];
if (Physics.SphereCast(transform.position +
new Vector3(0f, startOffset, 0f), 0.5f,
endPosition, out hit, rayDistance))
perceptionBuffer.Clear();
// For each ray sublist stores categorial information on detected object
// along with object distance.
foreach (float angle in rayAngles)
for (int i = 0; i < detectableObjects.Length; i++)
endPosition = transform.TransformDirection(
PolarToCartesian(rayDistance, angle));
endPosition.y = endOffset;
if (Application.isEditor)
if (hit.collider.gameObject.CompareTag(detectableObjects[i]))
Debug.DrawRay(transform.position + new Vector3(0f, startOffset, 0f),
endPosition, Color.black, 0.01f, true);
}
float[] subList = new float[detectableObjects.Length + 2];
if (Physics.SphereCast(transform.position +
new Vector3(0f, startOffset, 0f), 0.5f,
endPosition, out hit, rayDistance))
{
for (int i = 0; i < detectableObjects.Length; i++)
subList[i] = 1;
subList[detectableObjects.Length + 1] = hit.distance / rayDistance;
break;
if (hit.collider.gameObject.CompareTag(detectableObjects[i]))
{
subList[i] = 1;
subList[detectableObjects.Length + 1] = hit.distance / rayDistance;
break;
}
}
else
{
subList[detectableObjects.Length] = 1f;
else
{
subList[detectableObjects.Length] = 1f;
}
perceptionBuffer.AddRange(subList);
perceptionBuffer.AddRange(subList);
return perceptionBuffer;
return perceptionBuffer;
}
/// <summary>
/// Converts polar coordinate to cartesian coordinate.
/// </summary>
public static Vector3 PolarToCartesian(float radius, float angle)
{
float x = radius * Mathf.Cos(DegreeToRadian(angle));
float z = radius * Mathf.Sin(DegreeToRadian(angle));
return new Vector3(x, 0f, z);
}
/// <summary>
/// Converts polar coordinate to cartesian coordinate.
/// </summary>
public static Vector3 PolarToCartesian(float radius, float angle)
{
float x = radius * Mathf.Cos(DegreeToRadian(angle));
float z = radius * Mathf.Sin(DegreeToRadian(angle));
return new Vector3(x, 0f, z);
}
/// <summary>
/// Converts degrees to radians.
/// </summary>
public static float DegreeToRadian(float degree)
{
return degree * Mathf.PI / 180f;
/// <summary>
/// Converts degrees to radians.
/// </summary>
public static float DegreeToRadian(float degree)
{
return degree * Mathf.PI / 180f;
}
}
}

1
unity-environment/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class AgentSoccer : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class SoccerAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class TemplateAcademy : Academy {

1
unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class TemplateAgent : Agent {

1
unity-environment/Assets/ML-Agents/Examples/Template/Scripts/TemplateDecision.cs


using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class TemplateDecision : MonoBehaviour, Decision
{

1
unity-environment/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAcademy.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MLAgents;
public class TennisAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MLAgents;
public class TennisAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class WalkerAcademy : Academy
{

1
unity-environment/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class WalkerAgent : Agent
{

1
unity-environment/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAcademy.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAgents;
public class WallJumpAcademy : Academy {

1
unity-environment/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs


using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using MLAgents;
public class WallJumpAgent : Agent
{

992
unity-environment/Assets/ML-Agents/Scripts/Academy.cs
文件差异内容过多而无法显示
查看文件

998
unity-environment/Assets/ML-Agents/Scripts/Agent.cs
文件差异内容过多而无法显示
查看文件

85
unity-environment/Assets/ML-Agents/Scripts/BCTeacherHelper.cs


using System.Collections.Generic;
using UnityEngine;
namespace MLAgents
{
/// <summary>
/// Behavioral Cloning Helper script. Attach to teacher agent to enable
/// resetting the experience buffer, as well as toggling session recording.
/// </summary>
public class BCTeacherHelper : MonoBehaviour {
/// <summary>
/// Behavioral Cloning Helper script. Attach to teacher agent to enable
/// resetting the experience buffer, as well as toggling session recording.
/// </summary>
public class BCTeacherHelper : MonoBehaviour
{
bool recordExperiences;
bool resetBuffer;
Agent myAgent;
float bufferResetTime;
bool recordExperiences;
bool resetBuffer;
Agent myAgent;
float bufferResetTime;
public KeyCode recordKey = KeyCode.R;
public KeyCode resetKey = KeyCode.C;
public KeyCode recordKey = KeyCode.R;
public KeyCode resetKey = KeyCode.C;
// Use this for initialization
void Start () {
recordExperiences = true;
resetBuffer = false;
myAgent = GetComponent<Agent>();
bufferResetTime = Time.time;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(recordKey))
// Use this for initialization
void Start()
recordExperiences = !recordExperiences;
recordExperiences = true;
resetBuffer = false;
myAgent = GetComponent<Agent>();
bufferResetTime = Time.time;
if (Input.GetKeyDown(resetKey))
// Update is called once per frame
void Update()
resetBuffer = true;
bufferResetTime = Time.time;
if (Input.GetKeyDown(recordKey))
{
recordExperiences = !recordExperiences;
}
if (Input.GetKeyDown(resetKey))
{
resetBuffer = true;
bufferResetTime = Time.time;
}
else
{
resetBuffer = false;
}
Monitor.Log("Recording experiences " + recordKey, recordExperiences.ToString());
float timeSinceBufferReset = Time.time - bufferResetTime;
Monitor.Log("Seconds since buffer reset " + resetKey,
Mathf.FloorToInt(timeSinceBufferReset).ToString());
else
void FixedUpdate()
resetBuffer = false;
// Convert both bools into single comma separated string. Python makes
// assumption that this structure is preserved.
myAgent.SetTextObs(recordExperiences + "," + resetBuffer);
Monitor.Log("Recording experiences " + recordKey, recordExperiences.ToString());
float timeSinceBufferReset = Time.time - bufferResetTime;
Monitor.Log("Seconds since buffer reset " + resetKey, Mathf.FloorToInt(timeSinceBufferReset).ToString());
}
void FixedUpdate()
{
// Convert both bools into single comma separated string. Python makes
// assumption that this structure is preserved.
myAgent.SetTextObs(recordExperiences + "," + resetBuffer);
}
}

339
unity-environment/Assets/ML-Agents/Scripts/Brain.cs


using System.Linq;
namespace MLAgents
{
public enum BrainType
{
Player,
Heuristic,
External,
Internal
}
public enum BrainType
{
Player,
Heuristic,
External,
Internal
}
public enum SpaceType
{
discrete,
continuous
}
;
public enum SpaceType
{
discrete,
continuous
};

*/
[System.Serializable]
public struct resolution
{
public int width;
/**< \brief The width of the observation in pixels */
public int height;
/**< \brief The height of the observation in pixels */
public bool blackAndWhite;
/**< \brief If true, the image will be in black and white.
* If false, it will be in colors RGB */
}
[System.Serializable]
public struct resolution
{
public int width;
/**< \brief The width of the observation in pixels */
public int height;
/**< \brief The height of the observation in pixels */
public bool blackAndWhite;
/**< \brief If true, the image will be in black and white.
* If false, it will be in colors RGB */
}
[System.Serializable]
public class BrainParameters
{
public int vectorObservationSize = 1;
/**< \brief If continuous : The length of the float vector that represents
* the state
* <br> If discrete : The number of possible values the state can take*/
[System.Serializable]
public class BrainParameters
{
public int vectorObservationSize = 1;
/**< \brief If continuous : The length of the float vector that represents
* the state
* <br> If discrete : The number of possible values the state can take*/
[Range(1, 50)]
public int numStackedVectorObservations = 1;
[Range(1, 50)] public int numStackedVectorObservations = 1;
public int vectorActionSize = 1;
/**< \brief If continuous : The length of the float vector that represents
* the action
* <br> If discrete : The number of possible values the action can take*/
public int vectorActionSize = 1;
/**< \brief If continuous : The length of the float vector that represents
* the action
* <br> If discrete : The number of possible values the action can take*/
public resolution[] cameraResolutions;
/**<\brief The list of observation resolutions for the brain */
public resolution[] cameraResolutions;
/**<\brief The list of observation resolutions for the brain */
public string[] vectorActionDescriptions;
/**< \brief The list of strings describing what the actions correpond to */
public string[] vectorActionDescriptions;
/**< \brief The list of strings describing what the actions correpond to */
public SpaceType vectorActionSpaceType = SpaceType.discrete;
/**< \brief Defines if the action is discrete or continuous */
public SpaceType vectorActionSpaceType = SpaceType.discrete;
/**< \brief Defines if the action is discrete or continuous */
public SpaceType vectorObservationSpaceType = SpaceType.continuous;
/**< \brief Defines if the state is discrete or continuous */
}
public SpaceType vectorObservationSpaceType = SpaceType.continuous;
/**< \brief Defines if the state is discrete or continuous */
}
[HelpURL("https://github.com/Unity-Technologies/ml-agents/blob/master/" +
"docs/Learning-Environment-Design-Brains.md")]
[HelpURL("https://github.com/Unity-Technologies/ml-agents/blob/master/" +
"docs/Learning-Environment-Design-Brains.md")]
/**
* Contains all high-level Brain logic.
* Add this component to an empty GameObject in your scene and drag this

*/
public class Brain : MonoBehaviour
{
private bool isInitialized = false;
public class Brain : MonoBehaviour
{
private bool isInitialized = false;
private Dictionary<Agent, AgentInfo> agentInfos =
new Dictionary<Agent, AgentInfo>(1024);
private Dictionary<Agent, AgentInfo> agentInfos =
new Dictionary<Agent, AgentInfo>(1024);
[Tooltip("Define state, observation, and action spaces for the Brain.")]
/**< \brief Defines brain specific parameters such as the state size*/
public BrainParameters brainParameters = new BrainParameters();
[Tooltip("Define state, observation, and action spaces for the Brain.")]
/**< \brief Defines brain specific parameters such as the state size*/
public BrainParameters brainParameters = new BrainParameters();
/**< \brief Defines what is the type of the brain :
* External / Internal / Player / Heuristic*/
[Tooltip("Describes how the Brain will decide actions.")]
public BrainType brainType;
/**< \brief Defines what is the type of the brain :
* External / Internal / Player / Heuristic*/
[Tooltip("Describes how the Brain will decide actions.")]
public BrainType brainType;
//[HideInInspector]
///**< \brief Keeps track of the agents which subscribe to this brain*/
// public Dictionary<int, Agent> agents = new Dictionary<int, Agent>();
//[HideInInspector]
///**< \brief Keeps track of the agents which subscribe to this brain*/
// public Dictionary<int, Agent> agents = new Dictionary<int, Agent>();
[SerializeField]
ScriptableObject[] CoreBrains;
[SerializeField] ScriptableObject[] CoreBrains;
/**< \brief Reference to the current CoreBrain used by the brain*/
public CoreBrain coreBrain;
/**< \brief Reference to the current CoreBrain used by the brain*/
public CoreBrain coreBrain;
// Ensures the coreBrains are not dupplicated with the brains
[SerializeField]
private int instanceID;
// Ensures the coreBrains are not dupplicated with the brains
[SerializeField] private int instanceID;
/// Ensures the brain has an up to date array of coreBrains
/** Is called when the inspector is modified and into InitializeBrain.
* If the brain gameObject was just created, it generates a list of
* coreBrains (one for each brainType) */
public void UpdateCoreBrains()
{
// If CoreBrains is null, this means the Brain object was just
// instanciated and we create instances of each CoreBrain
if (CoreBrains == null)
/// Ensures the brain has an up to date array of coreBrains
/** Is called when the inspector is modified and into InitializeBrain.
* If the brain gameObject was just created, it generates a list of
* coreBrains (one for each brainType) */
public void UpdateCoreBrains()
int numCoreBrains = System.Enum.GetValues(typeof(BrainType)).Length;
CoreBrains = new ScriptableObject[numCoreBrains];
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
{
CoreBrains[(int)bt] =
ScriptableObject.CreateInstance(
"CoreBrain" + bt.ToString());
}
}
else
{
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
// If CoreBrains is null, this means the Brain object was just
// instanciated and we create instances of each CoreBrain
if (CoreBrains == null)
if ((int)bt >= CoreBrains.Length)
break;
if (CoreBrains[(int)bt] == null)
int numCoreBrains = System.Enum.GetValues(typeof(BrainType)).Length;
CoreBrains = new ScriptableObject[numCoreBrains];
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
CoreBrains[(int)bt] =
CoreBrains[(int) bt] =
}
}
// If the length of CoreBrains does not match the number of BrainTypes,
// we increase the length of CoreBrains
if (CoreBrains.Length < System.Enum.GetValues(typeof(BrainType)).Length)
{
int numCoreBrains = System.Enum.GetValues(typeof(BrainType)).Length;
ScriptableObject[] new_CoreBrains =
new ScriptableObject[numCoreBrains];
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
}
else
if ((int)bt < CoreBrains.Length)
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
new_CoreBrains[(int)bt] = CoreBrains[(int)bt];
if ((int) bt >= CoreBrains.Length)
break;
if (CoreBrains[(int) bt] == null)
{
CoreBrains[(int) bt] =
ScriptableObject.CreateInstance(
"CoreBrain" + bt.ToString());
}
else
}
// If the length of CoreBrains does not match the number of BrainTypes,
// we increase the length of CoreBrains
if (CoreBrains.Length < System.Enum.GetValues(typeof(BrainType)).Length)
{
int numCoreBrains = System.Enum.GetValues(typeof(BrainType)).Length;
ScriptableObject[] new_CoreBrains =
new ScriptableObject[numCoreBrains];
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
new_CoreBrains[(int)bt] =
ScriptableObject.CreateInstance(
"CoreBrain" + bt.ToString());
if ((int) bt < CoreBrains.Length)
{
new_CoreBrains[(int) bt] = CoreBrains[(int) bt];
}
else
{
new_CoreBrains[(int) bt] =
ScriptableObject.CreateInstance(
"CoreBrain" + bt.ToString());
}
CoreBrains = new_CoreBrains;
CoreBrains = new_CoreBrains;
}
// If the stored instanceID does not match the current instanceID,
// this means that the Brain GameObject was duplicated, and
// we need to make a new copy of each CoreBrain
if (instanceID != gameObject.GetInstanceID())
{
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
// If the stored instanceID does not match the current instanceID,
// this means that the Brain GameObject was duplicated, and
// we need to make a new copy of each CoreBrain
if (instanceID != gameObject.GetInstanceID())
if (CoreBrains[(int)bt] == null)
foreach (BrainType bt in System.Enum.GetValues(typeof(BrainType)))
CoreBrains[(int)bt] =
ScriptableObject.CreateInstance(
"CoreBrain" + bt.ToString());
if (CoreBrains[(int) bt] == null)
{
CoreBrains[(int) bt] =
ScriptableObject.CreateInstance(
"CoreBrain" + bt.ToString());
}
else
{
CoreBrains[(int) bt] =
ScriptableObject.Instantiate(CoreBrains[(int) bt]);
}
else
{
CoreBrains[(int)bt] =
ScriptableObject.Instantiate(CoreBrains[(int)bt]);
}
instanceID = gameObject.GetInstanceID();
instanceID = gameObject.GetInstanceID();
}
// The coreBrain to display is the one defined in brainType
coreBrain = (CoreBrain)CoreBrains[(int)brainType];
// The coreBrain to display is the one defined in brainType
coreBrain = (CoreBrain) CoreBrains[(int) brainType];
coreBrain.SetBrain(this);
}
/// This is called by the Academy at the start of the environemnt.
public void InitializeBrain(Academy aca, MLAgents.Batcher brainBatcher)
{
UpdateCoreBrains();
coreBrain.InitializeCoreBrain(brainBatcher);
aca.BrainDecideAction += DecideAction;
isInitialized = true;
}
coreBrain.SetBrain(this);
}
public void SendState(Agent agent, AgentInfo info)
{
// If the brain is not active or not properly initialized, an error is
// thrown.
if (!gameObject.activeSelf)
/// This is called by the Academy at the start of the environemnt.
public void InitializeBrain(Academy aca, MLAgents.Batcher brainBatcher)
throw new UnityAgentsException(
string.Format("Agent {0} tried to request an action " +
"from brain {1} but it is not active.",
agent.gameObject.name, gameObject.name));
UpdateCoreBrains();
coreBrain.InitializeCoreBrain(brainBatcher);
aca.BrainDecideAction += DecideAction;
isInitialized = true;
else if (!isInitialized)
public void SendState(Agent agent, AgentInfo info)
throw new UnityAgentsException(
string.Format("Agent {0} tried to request an action " +
"from brain {1} but it was not initialized.",
agent.gameObject.name, gameObject.name));
// If the brain is not active or not properly initialized, an error is
// thrown.
if (!gameObject.activeSelf)
{
throw new UnityAgentsException(
string.Format("Agent {0} tried to request an action " +
"from brain {1} but it is not active.",
agent.gameObject.name, gameObject.name));
}
else if (!isInitialized)
{
throw new UnityAgentsException(
string.Format("Agent {0} tried to request an action " +
"from brain {1} but it was not initialized.",
agent.gameObject.name, gameObject.name));
}
else
{
agentInfos.Add(agent, info);
}
else
void DecideAction()
agentInfos.Add(agent, info);
coreBrain.DecideAction(agentInfos);
agentInfos.Clear();
void DecideAction()
{
coreBrain.DecideAction(agentInfos);
agentInfos.Clear();
}
}
}

26
unity-environment/Assets/ML-Agents/Scripts/CoreBrain.cs


using System.Collections.Generic;
using UnityEngine;
namespace MLAgents
{
public interface CoreBrain
{
public interface CoreBrain
{
/// Implement setBrain so let the coreBrain know what brain is using it
void SetBrain(Brain b);
/// Implement this method to initialize CoreBrain
void InitializeCoreBrain(MLAgents.Batcher brainBatcher);
/// Implement this method to define the logic for deciding actions
void DecideAction(Dictionary<Agent, AgentInfo> agentInfo);
/// Implement this method to define what should be displayed in the brain Inspector
void OnInspector();
/// Implement setBrain so let the coreBrain know what brain is using it
void SetBrain(Brain b);
/// Implement this method to initialize CoreBrain
void InitializeCoreBrain(MLAgents.Batcher brainBatcher);
/// Implement this method to define the logic for deciding actions
void DecideAction(Dictionary<Agent, AgentInfo> agentInfo);
/// Implement this method to define what should be displayed in the brain Inspector
void OnInspector();
}
}

79
unity-environment/Assets/ML-Agents/Scripts/CoreBrainExternal.cs


using System.Collections.Generic;
using UnityEngine;
/// CoreBrain which decides actions via communication with an external system such as Python.
public class CoreBrainExternal : ScriptableObject, CoreBrain
namespace MLAgents
/**< Reference to the brain that uses this CoreBrainExternal */
public Brain brain;
MLAgents.Batcher brainBatcher;
/// Creates the reference to the brain
public void SetBrain(Brain b)
/// CoreBrain which decides actions via communication with an external system such as Python.
public class CoreBrainExternal : ScriptableObject, CoreBrain
brain = b;
}
/**< Reference to the brain that uses this CoreBrainExternal */
public Brain brain;
/// Generates the communicator for the Academy if none was present and
/// subscribe to ExternalCommunicator if it was present.
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
{
if (brainBatcher == null)
MLAgents.Batcher brainBatcher;
/// Creates the reference to the brain
public void SetBrain(Brain b)
brainBatcher = null;
throw new UnityAgentsException(string.Format("The brain {0} was set to" +
" External mode" +
" but Unity was unable to read the" +
" arguments passed at launch.", brain.gameObject.name));
brain = b;
else
/// Generates the communicator for the Academy if none was present and
/// subscribe to ExternalCommunicator if it was present.
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
this.brainBatcher = brainBatcher;
this.brainBatcher.SubscribeBrain(brain.gameObject.name);
if (brainBatcher == null)
{
brainBatcher = null;
throw new UnityAgentsException(string.Format("The brain {0} was set to" +
" External mode" +
" but Unity was unable to read the" +
" arguments passed at launch.",
brain.gameObject.name));
}
else
{
this.brainBatcher = brainBatcher;
this.brainBatcher.SubscribeBrain(brain.gameObject.name);
}
}
/// Uses the communicator to retrieve the actions, memories and values and
/// sends them to the agents
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
if (brainBatcher != null)
{
brainBatcher.SendBrainInfo(brain.gameObject.name, agentInfo);
}
/// Uses the communicator to retrieve the actions, memories and values and
/// sends them to the agents
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
if (brainBatcher != null)
{
brainBatcher.SendBrainInfo(brain.gameObject.name, agentInfo);
return;
return;
}
/// Nothing needs to appear in the inspector
public void OnInspector()
{
/// Nothing needs to appear in the inspector
public void OnInspector()
{
}
}
}

133
unity-environment/Assets/ML-Agents/Scripts/CoreBrainHeuristic.cs


using UnityEditor;
#endif
/// CoreBrain which decides actions using developer-provided Decision script.
public class CoreBrainHeuristic : ScriptableObject, CoreBrain
namespace MLAgents
[SerializeField]
private bool broadcast = true;
/// CoreBrain which decides actions using developer-provided Decision script.
public class CoreBrainHeuristic : ScriptableObject, CoreBrain
{
[SerializeField] private bool broadcast = true;
/**< Reference to the brain that uses this CoreBrainHeuristic */
public Brain brain;
MLAgents.Batcher brainBatcher;
/**< Reference to the Decision component used to decide the actions */
public Decision decision;
/**< Reference to the brain that uses this CoreBrainHeuristic */
public Brain brain;
/// Create the reference to the brain
public void SetBrain(Brain b)
{
brain = b;
}
MLAgents.Batcher brainBatcher;
/// Create the reference to decision
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
{
decision = brain.gameObject.GetComponent<Decision>();
/**< Reference to the Decision component used to decide the actions */
public Decision decision;
if ((brainBatcher == null)
|| (!broadcast))
/// Create the reference to the brain
public void SetBrain(Brain b)
this.brainBatcher = null;
brain = b;
else
/// Create the reference to decision
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
this.brainBatcher = brainBatcher; ;
this.brainBatcher.SubscribeBrain(brain.gameObject.name);
decision = brain.gameObject.GetComponent<Decision>();
if ((brainBatcher == null)
|| (!broadcast))
{
this.brainBatcher = null;
}
else
{
this.brainBatcher = brainBatcher;
;
this.brainBatcher.SubscribeBrain(brain.gameObject.name);
}
}
/// Uses the Decision Component to decide that action to take
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
if (brainBatcher != null)
/// Uses the Decision Component to decide that action to take
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
brainBatcher.SendBrainInfo(brain.gameObject.name, agentInfo);
}
if (brainBatcher != null)
{
brainBatcher.SendBrainInfo(brain.gameObject.name, agentInfo);
}
if (decision == null)
{
throw new UnityAgentsException(
"The Brain is set to Heuristic, but no decision script attached to it");
}
if (decision == null)
{
throw new UnityAgentsException("The Brain is set to Heuristic, but no decision script attached to it");
}
foreach (Agent agent in agentInfo.Keys)
{
agent.UpdateVectorAction(decision.Decide(
agentInfo[agent].stackedVectorObservation,
agentInfo[agent].visualObservations,
agentInfo[agent].reward,
agentInfo[agent].done,
agentInfo[agent].memories));
foreach (Agent agent in agentInfo.Keys)
{
agent.UpdateVectorAction(decision.Decide(
agentInfo[agent].stackedVectorObservation,
agentInfo[agent].visualObservations,
agentInfo[agent].reward,
agentInfo[agent].done,
agentInfo[agent].memories));
}
}
foreach (Agent agent in agentInfo.Keys)
{
agent.UpdateMemoriesAction(decision.MakeMemory(
agentInfo[agent].stackedVectorObservation,
agentInfo[agent].visualObservations,
agentInfo[agent].reward,
agentInfo[agent].done,
agentInfo[agent].memories));
foreach (Agent agent in agentInfo.Keys)
{
agent.UpdateMemoriesAction(decision.MakeMemory(
agentInfo[agent].stackedVectorObservation,
agentInfo[agent].visualObservations,
agentInfo[agent].reward,
agentInfo[agent].done,
agentInfo[agent].memories));
}
}
/// Displays an error if no decision component is attached to the brain
public void OnInspector()
{
#if UNITY_EDITOR
/// Displays an error if no decision component is attached to the brain
public void OnInspector()
{
#if UNITY_EDITOR
"If checked, the brain will broadcast states and actions to Python."), broadcast);
"If checked, the brain will broadcast states and actions to Python."), broadcast);
EditorGUILayout.HelpBox("You need to add a 'Decision' component to this gameObject", MessageType.Error);
EditorGUILayout.HelpBox("You need to add a 'Decision' component to this gameObject",
MessageType.Error);
#endif
}
#endif
}
}
}

322
unity-environment/Assets/ML-Agents/Scripts/CoreBrainInternal.cs


using TensorFlow;
#endif
/// CoreBrain which decides actions using internally embedded TensorFlow model.
public class CoreBrainInternal : ScriptableObject, CoreBrain
namespace MLAgents
/// CoreBrain which decides actions using internally embedded TensorFlow model.
public class CoreBrainInternal : ScriptableObject, CoreBrain
{
[SerializeField]
[Tooltip("If checked, the brain will broadcast states and actions to Python.")]
#pragma warning disable
private bool broadcast = true;
#pragma warning restore
[SerializeField]
[Tooltip("If checked, the brain will broadcast states and actions to Python.")]
#pragma warning disable
private bool broadcast = true;
#pragma warning restore
[System.Serializable]
private struct TensorFlowAgentPlaceholder
{
public enum tensorType
[System.Serializable]
private struct TensorFlowAgentPlaceholder
Integer,
FloatingPoint
public enum tensorType
{
Integer,
FloatingPoint
};
public string name;
public tensorType valueType;
public float minValue;
public float maxValue;
;
MLAgents.Batcher brainBatcher;
[Tooltip("This must be the bytes file corresponding to the pretrained TensorFlow graph.")]
/// Modify only in inspector : Reference to the Graph asset
public TextAsset graphModel;
/// Modify only in inspector : If a scope was used when training the model, specify it here
public string graphScope;
public string name;
public tensorType valueType;
public float minValue;
public float maxValue;
[SerializeField]
[Tooltip(
"If your graph takes additional inputs that are fixed (example: noise level) you can specify them here.")]
/// Modify only in inspector : If your graph takes additional inputs that are fixed you can specify them here.
private TensorFlowAgentPlaceholder[] graphPlaceholders;
}
/// Modify only in inspector : Name of the placholder of the batch size
public string BatchSizePlaceholderName = "batch_size";
MLAgents.Batcher brainBatcher;
/// Modify only in inspector : Name of the state placeholder
public string VectorObservationPlacholderName = "vector_observation";
[Tooltip("This must be the bytes file corresponding to the pretrained TensorFlow graph.")]
/// Modify only in inspector : Reference to the Graph asset
public TextAsset graphModel;
/// Modify only in inspector : Name of the recurrent input
public string RecurrentInPlaceholderName = "recurrent_in";
/// Modify only in inspector : If a scope was used when training the model, specify it here
public string graphScope;
[SerializeField]
[Tooltip("If your graph takes additional inputs that are fixed (example: noise level) you can specify them here.")]
/// Modify only in inspector : If your graph takes additional inputs that are fixed you can specify them here.
private TensorFlowAgentPlaceholder[] graphPlaceholders;
/// Modify only in inspector : Name of the placholder of the batch size
public string BatchSizePlaceholderName = "batch_size";
/// Modify only in inspector : Name of the state placeholder
public string VectorObservationPlacholderName = "vector_observation";
/// Modify only in inspector : Name of the recurrent input
public string RecurrentInPlaceholderName = "recurrent_in";
/// Modify only in inspector : Name of the recurrent output
public string RecurrentOutPlaceholderName = "recurrent_out";
/// Modify only in inspector : Names of the observations placeholders
public string[] VisualObservationPlaceholderName;
/// Modify only in inspector : Name of the action node
public string ActionPlaceholderName = "action";
/// Modify only in inspector : Name of the previous action node
public string PreviousActionPlaceholderName = "prev_action";
/// Modify only in inspector : Name of the recurrent output
public string RecurrentOutPlaceholderName = "recurrent_out";
/// Modify only in inspector : Names of the observations placeholders
public string[] VisualObservationPlaceholderName;
/// Modify only in inspector : Name of the action node
public string ActionPlaceholderName = "action";
/// Modify only in inspector : Name of the previous action node
public string PreviousActionPlaceholderName = "prev_action";
#if ENABLE_TENSORFLOW
TFGraph graph;
TFSession session;

int memorySize;
#endif
/// Reference to the brain that uses this CoreBrainInternal
public Brain brain;
/// Reference to the brain that uses this CoreBrainInternal
public Brain brain;
/// Create the reference to the brain
public void SetBrain(Brain b)
{
brain = b;
}
/// Create the reference to the brain
public void SetBrain(Brain b)
{
brain = b;
}
/// Loads the tensorflow graph model to generate a TFGraph object
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
{
/// Loads the tensorflow graph model to generate a TFGraph object
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
{
#if ENABLE_TENSORFLOW
#if UNITY_ANDROID
// This needs to ba called only once and will raise an exception if

observationMatrixList = new List<float[,,,]>();
texturesHolder = new List<Texture2D>();
#endif
}
}
/// Uses the stored information to run the tensorflow graph and generate
/// the actions.
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
/// Uses the stored information to run the tensorflow graph and generate
/// the actions.
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
#if ENABLE_TENSORFLOW
if (brainBatcher != null)
{

{
stateLength = brain.brainParameters.vectorObservationSize;
}
inputState = new float[currentBatchSize, stateLength * brain.brainParameters.numStackedVectorObservations];
inputState =
new float[currentBatchSize, stateLength * brain.brainParameters.numStackedVectorObservations];
for (int j = 0; j < stateLength * brain.brainParameters.numStackedVectorObservations; j++)
for (int j =
0; j < stateLength * brain.brainParameters.numStackedVectorObservations; j++)
{
inputState[i, j] = state_list[j];
}

observationMatrixList.Clear();
for (int observationIndex = 0; observationIndex < brain.brainParameters.cameraResolutions.Count(); observationIndex++){
for (int observationIndex =
0; observationIndex < brain.brainParameters.cameraResolutions.Count(); observationIndex++){
texturesHolder.Clear();
foreach (Agent agent in agentList){
texturesHolder.Add(agentInfo[agent].visualObservations[observationIndex]);

}
// Create the observation tensors
for (int obs_number = 0; obs_number < brain.brainParameters.cameraResolutions.Length; obs_number++)
for (int obs_number =
0; obs_number < brain.brainParameters.cameraResolutions.Length; obs_number++)
{
runner.AddInput(graph[graphScope + VisualObservationPlaceholderName[obs_number]][0], observationMatrixList[obs_number]);
}

string errorMessage = e.Message;
try
{
errorMessage = string.Format(@"The tensorflow graph needs an input for {0} of type {1}",
errorMessage =
string.Format(@"The tensorflow graph needs an input for {0} of type {1}",
e.Message.Split(new string[] { "Node: " }, 0)[1].Split('=')[0],
e.Message.Split(new string[] { "dtype=" }, 0)[1].Split(',')[0]);
}

#else
if (agentInfo.Count > 0)
{
throw new UnityAgentsException(string.Format(@"The brain {0} was set to Internal but the Tensorflow
if (agentInfo.Count > 0)
{
throw new UnityAgentsException(string.Format(
@"The brain {0} was set to Internal but the Tensorflow
brain.gameObject.name));
}
brain.gameObject.name));
}
}
}
/// Displays the parameters of the CoreBrainInternal in the Inspector
public void OnInspector()
{
/// Displays the parameters of the CoreBrainInternal in the Inspector
public void OnInspector()
{
#if ENABLE_TENSORFLOW && UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle(new GUIContent("Broadcast",

}
graphScope = EditorGUILayout.TextField(new GUIContent("Graph Scope", "If you set a scope while training your tensorflow model, " +
graphScope =
EditorGUILayout.TextField(new GUIContent("Graph Scope", "If you set a scope while training your tensorflow model, " +
"all your placeholder name will have a prefix. You must specify that prefix here."), graphScope);
if (BatchSizePlaceholderName == "")

BatchSizePlaceholderName = EditorGUILayout.TextField(new GUIContent("Batch Size Node Name", "If the batch size is one of " +
BatchSizePlaceholderName =
EditorGUILayout.TextField(new GUIContent("Batch Size Node Name", "If the batch size is one of " +
VectorObservationPlacholderName = EditorGUILayout.TextField(new GUIContent("Vector Observation Node Name", "If your graph uses the state as an input, " +
VectorObservationPlacholderName =
EditorGUILayout.TextField(new GUIContent("Vector Observation Node Name", "If your graph uses the state as an input, " +
RecurrentInPlaceholderName = EditorGUILayout.TextField(new GUIContent("Recurrent Input Node Name", "If your graph uses a " +
RecurrentInPlaceholderName =
EditorGUILayout.TextField(new GUIContent("Recurrent Input Node Name", "If your graph uses a " +
"recurrent input / memory as input and outputs new recurrent input / memory, " +
"you must specify the name if the input placeholder here."), RecurrentInPlaceholderName);
if (RecurrentOutPlaceholderName == "")

RecurrentOutPlaceholderName = EditorGUILayout.TextField(new GUIContent("Recurrent Output Node Name", " If your graph uses a " +
RecurrentOutPlaceholderName =
EditorGUILayout.TextField(new GUIContent("Recurrent Output Node Name", " If your graph uses a " +
"recurrent input / memory as input and outputs new recurrent input / memory, you must specify the name if " +
"the output placeholder here."), RecurrentOutPlaceholderName);

{
if (VisualObservationPlaceholderName == null)
{
VisualObservationPlaceholderName = new string[brain.brainParameters.cameraResolutions.Count()];
VisualObservationPlaceholderName =
new string[brain.brainParameters.cameraResolutions.Count()];
VisualObservationPlaceholderName = new string[brain.brainParameters.cameraResolutions.Count()];
VisualObservationPlaceholderName =
new string[brain.brainParameters.cameraResolutions.Count()];
for (int obs_number = 0; obs_number < brain.brainParameters.cameraResolutions.Count(); obs_number++)
for (int obs_number =
0; obs_number < brain.brainParameters.cameraResolutions.Count(); obs_number++)
VisualObservationPlaceholderName[obs_number] = "visual_observation_" + obs_number;
VisualObservationPlaceholderName[obs_number] =
"visual_observation_" + obs_number;
}
}
var opn = serializedBrain.FindProperty("VisualObservationPlaceholderName");

{
ActionPlaceholderName = "action";
}
ActionPlaceholderName = EditorGUILayout.TextField(new GUIContent("Action Node Name", "Specify the name of the " +
ActionPlaceholderName =
EditorGUILayout.TextField(new GUIContent("Action Node Name", "Specify the name of the " +
"placeholder corresponding to the actions of the brain in your graph. If the action space type is " +
"continuous, the output must be a one dimensional tensor of float of length Action Space Size, " +
"if the action space type is discrete, the output must be a one dimensional tensor of int " +

serializedBrain.ApplyModifiedProperties();
#endif
#if !ENABLE_TENSORFLOW && UNITY_EDITOR
EditorGUILayout.HelpBox (
"You need to install and enable the TensorflowSharp plugin in"+
"order to use the internal brain.", MessageType.Error);
if (GUILayout.Button("Show me how"))
{
Application.OpenURL("https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Getting-Started-with-Balance-Ball.md#embedding-the-trained-brain-into-the-unity-environment-experimental");
EditorGUILayout.HelpBox(
"You need to install and enable the TensorflowSharp plugin in" +
"order to use the internal brain.", MessageType.Error);
if (GUILayout.Button("Show me how"))
{
Application.OpenURL(
"https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Getting-Started-with-Balance-Ball.md#embedding-the-trained-brain-into-the-unity-environment-experimental");
}
#endif
#endif
}
/// <summary>
/// Converts a list of Texture2D into a Tensor.
/// </summary>
/// <returns>
/// A 4 dimensional float Tensor of dimension
/// [batch_size, height, width, channel].
/// Where batch_size is the number of input textures,
/// height corresponds to the height of the texture,
/// width corresponds to the width of the texture,
/// channel corresponds to the number of channels extracted from the
/// input textures (based on the input blackAndWhite flag
/// (3 if the flag is false, 1 otherwise).
/// The values of the Tensor are between 0 and 1.
/// </returns>
/// <param name="textures">
/// The list of textures to be put into the tensor.
/// Note that the textures must have same width and height.
/// </param>
/// <param name="blackAndWhite">
/// If set to <c>true</c> the textures
/// will be converted to grayscale before being stored in the tensor.
/// </param>
public static float[,,,] BatchVisualObservations(
List<Texture2D> textures, bool blackAndWhite)
{
int batchSize = textures.Count();
int width = textures[0].width;
int height = textures[0].height;
int pixels = 0;
if (blackAndWhite)
pixels = 1;
else
pixels = 3;
float[,,,] result = new float[batchSize, height, width, pixels];
/// <summary>
/// Converts a list of Texture2D into a Tensor.
/// </summary>
/// <returns>
/// A 4 dimensional float Tensor of dimension
/// [batch_size, height, width, channel].
/// Where batch_size is the number of input textures,
/// height corresponds to the height of the texture,
/// width corresponds to the width of the texture,
/// channel corresponds to the number of channels extracted from the
/// input textures (based on the input blackAndWhite flag
/// (3 if the flag is false, 1 otherwise).
/// The values of the Tensor are between 0 and 1.
/// </returns>
/// <param name="textures">
/// The list of textures to be put into the tensor.
/// Note that the textures must have same width and height.
/// </param>
/// <param name="blackAndWhite">
/// If set to <c>true</c> the textures
/// will be converted to grayscale before being stored in the tensor.
/// </param>
public static float[,,,] BatchVisualObservations(
List<Texture2D> textures, bool blackAndWhite)
{
int batchSize = textures.Count();
int width = textures[0].width;
int height = textures[0].height;
int pixels = 0;
if (blackAndWhite)
pixels = 1;
else
pixels = 3;
float[,,,] result = new float[batchSize, height, width, pixels];
for (int b = 0; b < batchSize; b++)
{
Color32[] cc = textures[b].GetPixels32();
for (int w = 0; w < width; w++)
for (int b = 0; b < batchSize; b++)
for (int h = 0; h < height; h++)
Color32[] cc = textures[b].GetPixels32();
for (int w = 0; w < width; w++)
Color32 currentPixel = cc[h * width + w];
if (!blackAndWhite)
{
// For Color32, the r, g and b values are between
// 0 and 255.
result[b, textures[b].height - h - 1, w, 0] =
currentPixel.r / 255.0f;
result[b, textures[b].height - h - 1, w, 1] =
currentPixel.g / 255.0f;
result[b, textures[b].height - h - 1, w, 2] =
currentPixel.b / 255.0f;
}
else
for (int h = 0; h < height; h++)
result[b, textures[b].height - h - 1, w, 0] =
(currentPixel.r + currentPixel.g + currentPixel.b)
/ 3;
Color32 currentPixel = cc[h * width + w];
if (!blackAndWhite)
{
// For Color32, the r, g and b values are between
// 0 and 255.
result[b, textures[b].height - h - 1, w, 0] =
currentPixel.r / 255.0f;
result[b, textures[b].height - h - 1, w, 1] =
currentPixel.g / 255.0f;
result[b, textures[b].height - h - 1, w, 2] =
currentPixel.b / 255.0f;
}
else
{
result[b, textures[b].height - h - 1, w, 0] =
(currentPixel.r + currentPixel.g + currentPixel.b)
/ 3;
}
return result;
return result;
}

228
unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs


using UnityEditor;
#endif
/// CoreBrain which decides actions using Player input.
public class CoreBrainPlayer : ScriptableObject, CoreBrain
namespace MLAgents
[SerializeField]
private bool broadcast = true;
[System.Serializable]
private struct DiscretePlayerAction
/// CoreBrain which decides actions using Player input.
public class CoreBrainPlayer : ScriptableObject, CoreBrain
public KeyCode key;
public int value;
}
[SerializeField] private bool broadcast = true;
[System.Serializable]
private struct ContinuousPlayerAction
{
public KeyCode key;
public int index;
public float value;
}
[System.Serializable]
private struct DiscretePlayerAction
{
public KeyCode key;
public int value;
}
MLAgents.Batcher brainBatcher;
[System.Serializable]
private struct ContinuousPlayerAction
{
public KeyCode key;
public int index;
public float value;
}
[SerializeField]
[Tooltip("The list of keys and the value they correspond to for continuous control.")]
/// Contains the mapping from input to continuous actions
private ContinuousPlayerAction[] continuousPlayerActions;
[SerializeField]
[Tooltip("The list of keys and the value they correspond to for discrete control.")]
/// Contains the mapping from input to discrete actions
private DiscretePlayerAction[] discretePlayerActions;
[SerializeField]
private int defaultAction = 0;
MLAgents.Batcher brainBatcher;
/// Reference to the brain that uses this CoreBrainPlayer
public Brain brain;
[SerializeField]
[Tooltip("The list of keys and the value they correspond to for continuous control.")]
/// Contains the mapping from input to continuous actions
private ContinuousPlayerAction[] continuousPlayerActions;
/// Create the reference to the brain
public void SetBrain(Brain b)
{
brain = b;
}
[SerializeField]
[Tooltip("The list of keys and the value they correspond to for discrete control.")]
/// Contains the mapping from input to discrete actions
private DiscretePlayerAction[] discretePlayerActions;
[SerializeField] private int defaultAction = 0;
/// Reference to the brain that uses this CoreBrainPlayer
public Brain brain;
/// Nothing to implement
/// Nothing to implement
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
{
if ((brainBatcher == null)
|| (!broadcast))
/// Create the reference to the brain
public void SetBrain(Brain b)
this.brainBatcher = null;
brain = b;
else
/// Nothing to implement
/// Nothing to implement
public void InitializeCoreBrain(MLAgents.Batcher brainBatcher)
this.brainBatcher = brainBatcher;
this.brainBatcher.SubscribeBrain(brain.gameObject.name);
if ((brainBatcher == null)
|| (!broadcast))
{
this.brainBatcher = null;
}
else
{
this.brainBatcher = brainBatcher;
this.brainBatcher.SubscribeBrain(brain.gameObject.name);
}
}
/// Uses the continuous inputs or dicrete inputs of the player to
/// decide action
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
{
if (brainBatcher != null)
{
brainBatcher.SendBrainInfo(brain.gameObject.name, agentInfo);
}
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
/// Uses the continuous inputs or dicrete inputs of the player to
/// decide action
public void DecideAction(Dictionary<Agent, AgentInfo> agentInfo)
foreach (Agent agent in agentInfo.Keys)
if (brainBatcher != null)
var action = new float[brain.brainParameters.vectorActionSize];
foreach (ContinuousPlayerAction cha in continuousPlayerActions)
brainBatcher.SendBrainInfo(brain.gameObject.name, agentInfo);
}
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
{
foreach (Agent agent in agentInfo.Keys)
if (Input.GetKey(cha.key))
var action = new float[brain.brainParameters.vectorActionSize];
foreach (ContinuousPlayerAction cha in continuousPlayerActions)
action[cha.index] = cha.value;
if (Input.GetKey(cha.key))
{
action[cha.index] = cha.value;
}
agent.UpdateVectorAction(action);
agent.UpdateVectorAction(action);
}
else
{
foreach (Agent agent in agentInfo.Keys)
else
var action = new float[1] { defaultAction };
foreach (DiscretePlayerAction dha in discretePlayerActions)
foreach (Agent agent in agentInfo.Keys)
if (Input.GetKey(dha.key))
var action = new float[1] {defaultAction};
foreach (DiscretePlayerAction dha in discretePlayerActions)
action[0] = (float)dha.value;
break;
if (Input.GetKey(dha.key))
{
action[0] = (float) dha.value;
break;
}
}
agent.UpdateVectorAction(action);
agent.UpdateVectorAction(action);
}
}
/// Displays continuous or discrete input mapping in the inspector
public void OnInspector()
{
#if UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle(new GUIContent("Broadcast",
"If checked, the brain will broadcast states and actions to Python."), broadcast);
var serializedBrain = new SerializedObject(this);
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
/// Displays continuous or discrete input mapping in the inspector
public void OnInspector()
GUILayout.Label("Edit the continuous inputs for your actions", EditorStyles.boldLabel);
var chas = serializedBrain.FindProperty("continuousPlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(chas, true);
serializedBrain.ApplyModifiedProperties();
if (continuousPlayerActions == null)
#if UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle(new GUIContent("Broadcast",
"If checked, the brain will broadcast states and actions to Python."), broadcast);
var serializedBrain = new SerializedObject(this);
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
continuousPlayerActions = new ContinuousPlayerAction[0];
GUILayout.Label("Edit the continuous inputs for your actions",
EditorStyles.boldLabel);
var chas = serializedBrain.FindProperty("continuousPlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(chas, true);
serializedBrain.ApplyModifiedProperties();
if (continuousPlayerActions == null)
{
continuousPlayerActions = new ContinuousPlayerAction[0];
}
foreach (ContinuousPlayerAction cha in continuousPlayerActions)
{
if (cha.index >= brain.brainParameters.vectorActionSize)
{
EditorGUILayout.HelpBox(string.Format(
"Key {0} is assigned to index {1} but the action size is only of size {2}"
, cha.key.ToString(), cha.index.ToString(),
brain.brainParameters.vectorActionSize.ToString()), MessageType.Error);
}
}
foreach (ContinuousPlayerAction cha in continuousPlayerActions)
else
if (cha.index >= brain.brainParameters.vectorActionSize)
{
EditorGUILayout.HelpBox(string.Format("Key {0} is assigned to index {1} but the action size is only of size {2}"
, cha.key.ToString(), cha.index.ToString(), brain.brainParameters.vectorActionSize.ToString()), MessageType.Error);
}
GUILayout.Label("Edit the discrete inputs for your actions",
EditorStyles.boldLabel);
defaultAction = EditorGUILayout.IntField("Default Action", defaultAction);
var dhas = serializedBrain.FindProperty("discretePlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(dhas, true);
serializedBrain.ApplyModifiedProperties();
}
else
{
GUILayout.Label("Edit the discrete inputs for your actions", EditorStyles.boldLabel);
defaultAction = EditorGUILayout.IntField("Default Action", defaultAction);
var dhas = serializedBrain.FindProperty("discretePlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(dhas, true);
serializedBrain.ApplyModifiedProperties();
#endif
#endif
}
}

87
unity-environment/Assets/ML-Agents/Scripts/Decision.cs


using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Interface for implementing the behavior of an Agent that uses a Heuristic
/// Brain. The behavior of an Agent in this case is fully decided using the
/// implementation of these methods and no training or inference takes place.
/// Currently, the Heuristic Brain does not support text observations and actions.
/// </summary>
public interface Decision
namespace MLAgents
/// Defines the decision-making logic of the agent. Given the information
/// about the agent, returns a vector of actions.
/// Interface for implementing the behavior of an Agent that uses a Heuristic
/// Brain. The behavior of an Agent in this case is fully decided using the
/// implementation of these methods and no training or inference takes place.
/// Currently, the Heuristic Brain does not support text observations and actions.
/// <returns>Vector action vector.</returns>
/// <param name="vectorObs">The vector observations of the agent.</param>
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
/// <param name="reward">The reward the agent received at the previous step.</param>
/// <param name="done">Whether or not the agent is done.</param>
/// <param name="memory">
/// The memories stored from the previous step with
/// <see cref="MakeMemory(List{float}, List{Texture2D}, float, bool, List{float})"/>
/// </param>
float[] Decide(
List<float>
vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory);
public interface Decision
{
/// <summary>
/// Defines the decision-making logic of the agent. Given the information
/// about the agent, returns a vector of actions.
/// </summary>
/// <returns>Vector action vector.</returns>
/// <param name="vectorObs">The vector observations of the agent.</param>
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
/// <param name="reward">The reward the agent received at the previous step.</param>
/// <param name="done">Whether or not the agent is done.</param>
/// <param name="memory">
/// The memories stored from the previous step with
/// <see cref="MakeMemory(List{float}, List{Texture2D}, float, bool, List{float})"/>
/// </param>
float[] Decide(
List<float>
vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory);
/// <summary>
/// Defines the logic for creating the memory vector for the Agent.
/// </summary>
/// <returns>The vector of memories the agent will use at the next step.</returns>
/// <param name="vectorObs">The vector observations of the agent.</param>
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
/// <param name="reward">The reward the agent received at the previous step.</param>
/// <param name="done">Whether or not the agent is done.</param>
/// <param name="memory">
/// The memories stored from the previous call to this method.
/// </param>
List<float> MakeMemory(
List<float> vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory);
/// <summary>
/// Defines the logic for creating the memory vector for the Agent.
/// </summary>
/// <returns>The vector of memories the agent will use at the next step.</returns>
/// <param name="vectorObs">The vector observations of the agent.</param>
/// <param name="visualObs">The cameras the agent uses for visual observations.</param>
/// <param name="reward">The reward the agent received at the previous step.</param>
/// <param name="done">Whether or not the agent is done.</param>
/// <param name="memory">
/// The memories stored from the previous call to this method.
/// </param>
List<float> MakeMemory(
List<float> vectorObs,
List<Texture2D> visualObs,
float reward,
bool done,
List<float> memory);
}
}

835
unity-environment/Assets/ML-Agents/Scripts/Monitor.cs


using System.Linq;
using UnityEngine;
/// <summary>
/// Monitor is used to display information about the Agent within the Unity
/// scene. Use the log function to add information to your monitor.
/// </summary>
public class Monitor : MonoBehaviour
namespace MLAgents
/// The type of monitor the information must be displayed in.
/// <slider> corresponds to a single rectangle whose width is given
/// by a float between -1 and 1. (green is positive, red is negative)
/// <hist> corresponds to n vertical sliders.
/// <text> is a text field.
/// <bar> is a rectangle of fixed length to represent the proportions
/// of a list of floats.
/// Monitor is used to display information about the Agent within the Unity
/// scene. Use the log function to add information to your monitor.
public enum DisplayType
public class Monitor : MonoBehaviour
INDEPENDENT,
PROPORTION
}
/// <summary>
/// Represents how high above the target the monitors will be.
/// </summary>
[HideInInspector]
static public float verticalOffset = 3f;
static bool isInstantiated;
static GameObject canvas;
static Dictionary<Transform, Dictionary<string, DisplayValue>> displayTransformValues;
static Color[] barColors;
struct DisplayValue
{
public float time;
public string stringValue;
public float floatValue;
public float[] floatArrayValues;
public enum ValueType
/// <summary>
/// The type of monitor the information must be displayed in.
/// <slider> corresponds to a single rectangle whose width is given
/// by a float between -1 and 1. (green is positive, red is negative)
/// <hist> corresponds to n vertical sliders.
/// <text> is a text field.
/// <bar> is a rectangle of fixed length to represent the proportions
/// of a list of floats.
/// </summary>
public enum DisplayType
FLOAT,
FLOATARRAY_INDEPENDENT,
FLOATARRAY_PROPORTION,
STRING
INDEPENDENT,
PROPORTION
public ValueType valueType;
}
static GUIStyle keyStyle;
static GUIStyle valueStyle;
static GUIStyle greenStyle;
static GUIStyle redStyle;
static GUIStyle[] colorStyle;
static bool initialized;
/// <summary>
/// Represents how high above the target the monitors will be.
/// </summary>
[HideInInspector] static public float verticalOffset = 3f;
/// <summary>
/// Use the Monitor.Log static function to attach information to a transform.
/// </summary>
/// <returns>The log.</returns>
/// <param name="key">The name of the information you wish to Log.</param>
/// <param name="value">The string value you want to display.</param>
/// <param name="target">The transform you want to attach the information to.
/// </param>
public static void Log(
string key,
string value,
Transform target = null)
{
if (!isInstantiated)
{
InstantiateCanvas();
isInstantiated = true;
}
static bool isInstantiated;
static GameObject canvas;
static Dictionary<Transform, Dictionary<string, DisplayValue>> displayTransformValues;
static Color[] barColors;
if (target == null)
struct DisplayValue
target = canvas.transform;
}
public float time;
public string stringValue;
public float floatValue;
public float[] floatArrayValues;
if (!displayTransformValues.Keys.Contains(target))
{
displayTransformValues[target] =
new Dictionary<string, DisplayValue>();
public enum ValueType
{
FLOAT,
FLOATARRAY_INDEPENDENT,
FLOATARRAY_PROPORTION,
STRING
}
public ValueType valueType;
Dictionary<string, DisplayValue> displayValues =
displayTransformValues[target];
static GUIStyle keyStyle;
static GUIStyle valueStyle;
static GUIStyle greenStyle;
static GUIStyle redStyle;
static GUIStyle[] colorStyle;
static bool initialized;
if (value == null)
{
RemoveValue(target, key);
return;
}
if (!displayValues.ContainsKey(key))
/// <summary>
/// Use the Monitor.Log static function to attach information to a transform.
/// </summary>
/// <returns>The log.</returns>
/// <param name="key">The name of the information you wish to Log.</param>
/// <param name="value">The string value you want to display.</param>
/// <param name="target">The transform you want to attach the information to.
/// </param>
public static void Log(
string key,
string value,
Transform target = null)
var dv = new DisplayValue();
dv.time = Time.timeSinceLevelLoad;
dv.stringValue = value;
dv.valueType = DisplayValue.ValueType.STRING;
displayValues[key] = dv;
while (displayValues.Count > 20)
if (!isInstantiated)
string max = (
displayValues
.Aggregate((l, r) => l.Value.time < r.Value.time ? l : r)
.Key
);
RemoveValue(target, max);
InstantiateCanvas();
isInstantiated = true;
}
else
{
DisplayValue dv = displayValues[key];
dv.stringValue = value;
dv.valueType = DisplayValue.ValueType.STRING;
displayValues[key] = dv;
}
}
/// <summary>
/// Use the Monitor.Log static function to attach information to a transform.
/// </summary>
/// <returns>The log.</returns>
/// <param name="key">The name of the information you wish to Log.</param>
/// <param name="value">The float value you want to display.</param>
/// <param name="target">The transform you want to attach the information to.
/// </param>
public static void Log(
string key,
float value,
Transform target = null)
{
if (!isInstantiated)
{
InstantiateCanvas();
isInstantiated = true;
}
if (target == null)
{
target = canvas.transform;
}
if (target == null)
{
target = canvas.transform;
}
if (!displayTransformValues.Keys.Contains(target))
{
displayTransformValues[target] =
new Dictionary<string, DisplayValue>();
}
if (!displayTransformValues.Keys.Contains(target))
{
displayTransformValues[target] = new Dictionary<string, DisplayValue>();
}
Dictionary<string, DisplayValue> displayValues =
displayTransformValues[target];
Dictionary<string, DisplayValue> displayValues = displayTransformValues[target];
if (value == null)
{
RemoveValue(target, key);
return;
}
if (!displayValues.ContainsKey(key))
{
var dv = new DisplayValue();
dv.time = Time.timeSinceLevelLoad;
dv.floatValue = value;
dv.valueType = DisplayValue.ValueType.FLOAT;
displayValues[key] = dv;
while (displayValues.Count > 20)
if (!displayValues.ContainsKey(key))
{
var dv = new DisplayValue();
dv.time = Time.timeSinceLevelLoad;
dv.stringValue = value;
dv.valueType = DisplayValue.ValueType.STRING;
displayValues[key] = dv;
while (displayValues.Count > 20)
{
string max = (
displayValues
.Aggregate((l, r) => l.Value.time < r.Value.time ? l : r)
.Key
);
RemoveValue(target, max);
}
}
else
string max = (
displayValues.Aggregate((l, r) => l.Value.time < r.Value.time ? l : r).Key);
RemoveValue(target, max);
DisplayValue dv = displayValues[key];
dv.stringValue = value;
dv.valueType = DisplayValue.ValueType.STRING;
displayValues[key] = dv;
else
/// <summary>
/// Use the Monitor.Log static function to attach information to a transform.
/// </summary>
/// <returns>The log.</returns>
/// <param name="key">The name of the information you wish to Log.</param>
/// <param name="value">The float value you want to display.</param>
/// <param name="target">The transform you want to attach the information to.
/// </param>
public static void Log(
string key,
float value,
Transform target = null)
DisplayValue dv = displayValues[key];
dv.floatValue = value;
dv.valueType = DisplayValue.ValueType.FLOAT;
displayValues[key] = dv;
}
}
if (!isInstantiated)
{
InstantiateCanvas();
isInstantiated = true;
}
if (target == null)
{
target = canvas.transform;
}
/// <summary>
/// Use the Monitor.Log static function to attach information to a transform.
/// </summary>
/// <returns>The log.</returns>
/// <param name="key">The name of the information you wish to Log.</param>
/// <param name="value">The array of float you want to display.</param>
/// <param name="displayType">The type of display.</param>
/// <param name="target">The transform you want to attach the information to.
/// </param>
public static void Log(
string key,
float[] value,
Transform target = null,
DisplayType displayType = DisplayType.INDEPENDENT
)
{
if (!isInstantiated)
{
InstantiateCanvas();
isInstantiated = true;
}
if (!displayTransformValues.Keys.Contains(target))
{
displayTransformValues[target] = new Dictionary<string, DisplayValue>();
}
if (target == null)
{
target = canvas.transform;
}
Dictionary<string, DisplayValue> displayValues = displayTransformValues[target];
if (!displayTransformValues.Keys.Contains(target))
{
displayTransformValues[target] = new Dictionary<string, DisplayValue>();
if (!displayValues.ContainsKey(key))
{
var dv = new DisplayValue();
dv.time = Time.timeSinceLevelLoad;
dv.floatValue = value;
dv.valueType = DisplayValue.ValueType.FLOAT;
displayValues[key] = dv;
while (displayValues.Count > 20)
{
string max = (
displayValues.Aggregate((l, r) => l.Value.time < r.Value.time ? l : r).Key);
RemoveValue(target, max);
}
}
else
{
DisplayValue dv = displayValues[key];
dv.floatValue = value;
dv.valueType = DisplayValue.ValueType.FLOAT;
displayValues[key] = dv;
}
Dictionary<string, DisplayValue> displayValues = displayTransformValues[target];
if (!displayValues.ContainsKey(key))
/// <summary>
/// Use the Monitor.Log static function to attach information to a transform.
/// </summary>
/// <returns>The log.</returns>
/// <param name="key">The name of the information you wish to Log.</param>
/// <param name="value">The array of float you want to display.</param>
/// <param name="displayType">The type of display.</param>
/// <param name="target">The transform you want to attach the information to.
/// </param>
public static void Log(
string key,
float[] value,
Transform target = null,
DisplayType displayType = DisplayType.INDEPENDENT
)
var dv = new DisplayValue();
dv.time = Time.timeSinceLevelLoad;
dv.floatArrayValues = value;
if (displayType == DisplayType.INDEPENDENT)
if (!isInstantiated)
dv.valueType = DisplayValue.ValueType.FLOATARRAY_INDEPENDENT;
InstantiateCanvas();
isInstantiated = true;
else
if (target == null)
dv.valueType = DisplayValue.ValueType.FLOATARRAY_PROPORTION;
target = canvas.transform;
displayValues[key] = dv;
while (displayValues.Count > 20)
if (!displayTransformValues.Keys.Contains(target))
string max = (
displayValues.Aggregate((l, r) => l.Value.time < r.Value.time ? l : r).Key);
RemoveValue(target, max);
displayTransformValues[target] = new Dictionary<string, DisplayValue>();
}
else
{
DisplayValue dv = displayValues[key];
dv.floatArrayValues = value;
if (displayType == DisplayType.INDEPENDENT)
Dictionary<string, DisplayValue> displayValues = displayTransformValues[target];
if (!displayValues.ContainsKey(key))
dv.valueType = DisplayValue.ValueType.FLOATARRAY_INDEPENDENT;
var dv = new DisplayValue();
dv.time = Time.timeSinceLevelLoad;
dv.floatArrayValues = value;
if (displayType == DisplayType.INDEPENDENT)
{
dv.valueType = DisplayValue.ValueType.FLOATARRAY_INDEPENDENT;
}
else
{
dv.valueType = DisplayValue.ValueType.FLOATARRAY_PROPORTION;
}
displayValues[key] = dv;
while (displayValues.Count > 20)
{
string max = (
displayValues.Aggregate((l, r) => l.Value.time < r.Value.time ? l : r).Key);
RemoveValue(target, max);
}
dv.valueType = DisplayValue.ValueType.FLOATARRAY_PROPORTION;
DisplayValue dv = displayValues[key];
dv.floatArrayValues = value;
if (displayType == DisplayType.INDEPENDENT)
{
dv.valueType = DisplayValue.ValueType.FLOATARRAY_INDEPENDENT;
}
else
{
dv.valueType = DisplayValue.ValueType.FLOATARRAY_PROPORTION;
}
displayValues[key] = dv;
displayValues[key] = dv;
}
/// <summary>
/// Remove a value from a monitor.
/// </summary>
/// <param name="target">
/// The transform to which the information is attached.
/// </param>
/// <param name="key">The key of the information you want to remove.</param>
public static void RemoveValue(Transform target, string key)
{
if (target == null)
{
target = canvas.transform;
}
if (displayTransformValues.Keys.Contains(target))
/// <summary>
/// Remove a value from a monitor.
/// </summary>
/// <param name="target">
/// The transform to which the information is attached.
/// </param>
/// <param name="key">The key of the information you want to remove.</param>
public static void RemoveValue(Transform target, string key)
if (displayTransformValues[target].ContainsKey(key))
if (target == null)
{
target = canvas.transform;
}
if (displayTransformValues.Keys.Contains(target))
displayTransformValues[target].Remove(key);
if (displayTransformValues[target].Keys.Count == 0)
if (displayTransformValues[target].ContainsKey(key))
displayTransformValues.Remove(target);
displayTransformValues[target].Remove(key);
if (displayTransformValues[target].Keys.Count == 0)
{
displayTransformValues.Remove(target);
}
}
/// <summary>
/// Remove all information from a monitor.
/// </summary>
/// <param name="target">
/// The transform to which the information is attached.
/// </param>
public static void RemoveAllValues(Transform target)
{
if (target == null)
{
target = canvas.transform;
}
if (displayTransformValues.Keys.Contains(target))
/// <summary>
/// Remove all information from a monitor.
/// </summary>
/// <param name="target">
/// The transform to which the information is attached.
/// </param>
public static void RemoveAllValues(Transform target)
displayTransformValues.Remove(target);
}
if (target == null)
{
target = canvas.transform;
}
}
/// <summary>
/// Use SetActive to enable or disable the Monitor via script
/// </summary>
/// <param name="active">Value to set the Monitor's status to.</param>
public static void SetActive(bool active)
{
if (!isInstantiated)
{
InstantiateCanvas();
isInstantiated = true;
if (displayTransformValues.Keys.Contains(target))
{
displayTransformValues.Remove(target);
}
if (canvas != null)
/// <summary>
/// Use SetActive to enable or disable the Monitor via script
/// </summary>
/// <param name="active">Value to set the Monitor's status to.</param>
public static void SetActive(bool active)
canvas.SetActive(active);
}
if (!isInstantiated)
{
InstantiateCanvas();
isInstantiated = true;
}
}
/// Initializes the canvas.
static void InstantiateCanvas()
{
canvas = GameObject.Find("AgentMonitorCanvas");
if (canvas == null)
{
canvas = new GameObject();
canvas.name = "AgentMonitorCanvas";
canvas.AddComponent<Monitor>();
}
displayTransformValues = new Dictionary<Transform,
Dictionary<string, DisplayValue>>();
}
if (canvas != null)
{
canvas.SetActive(active);
}
/// <summary> <inheritdoc/> </summary>
void OnGUI()
{
if (!initialized)
{
Initialize();
initialized = true;
var toIterate = displayTransformValues.Keys.ToList();
foreach (Transform target in toIterate)
/// Initializes the canvas.
static void InstantiateCanvas()
if (target == null)
canvas = GameObject.Find("AgentMonitorCanvas");
if (canvas == null)
displayTransformValues.Remove(target);
continue;
canvas = new GameObject();
canvas.name = "AgentMonitorCanvas";
canvas.AddComponent<Monitor>();
float widthScaler = (Screen.width / 1000f);
float keyPixelWidth = 100 * widthScaler;
float keyPixelHeight = 20 * widthScaler;
float paddingwidth = 10 * widthScaler;
displayTransformValues = new Dictionary<Transform,
Dictionary<string, DisplayValue>>();
}
float scale = 1f;
var origin = new Vector3(
Screen.width / 2 - keyPixelWidth, Screen.height);
if (!(target == canvas.transform))
/// <summary> <inheritdoc/> </summary>
void OnGUI()
{
if (!initialized)
Vector3 cam2obj = target.position - Camera.main.transform.position;
scale = Mathf.Min(
1,
20f / (Vector3.Dot(cam2obj, Camera.main.transform.forward)));
Vector3 worldPosition = Camera.main.WorldToScreenPoint(
target.position + new Vector3(0, verticalOffset, 0));
origin = new Vector3(
worldPosition.x - keyPixelWidth * scale, Screen.height - worldPosition.y);
Initialize();
initialized = true;
keyPixelWidth *= scale;
keyPixelHeight *= scale;
paddingwidth *= scale;
keyStyle.fontSize = (int)(keyPixelHeight * 0.8f);
if (keyStyle.fontSize < 2)
var toIterate = displayTransformValues.Keys.ToList();
foreach (Transform target in toIterate)
continue;
}
if (target == null)
{
displayTransformValues.Remove(target);
continue;
}
float widthScaler = (Screen.width / 1000f);
float keyPixelWidth = 100 * widthScaler;
float keyPixelHeight = 20 * widthScaler;
float paddingwidth = 10 * widthScaler;
Dictionary<string, DisplayValue> displayValues = displayTransformValues[target];
float scale = 1f;
var origin = new Vector3(
Screen.width / 2 - keyPixelWidth, Screen.height);
if (!(target == canvas.transform))
{
Vector3 cam2obj = target.position - Camera.main.transform.position;
scale = Mathf.Min(
1,
20f / (Vector3.Dot(cam2obj, Camera.main.transform.forward)));
Vector3 worldPosition = Camera.main.WorldToScreenPoint(
target.position + new Vector3(0, verticalOffset, 0));
origin = new Vector3(
worldPosition.x - keyPixelWidth * scale, Screen.height - worldPosition.y);
}
int index = 0;
var orderedKeys = displayValues.Keys.OrderBy(x => -displayValues[x].time);
float[] vals;
GUIStyle s;
foreach (string key in orderedKeys)
{
keyStyle.alignment = TextAnchor.MiddleRight;
GUI.Label(
new Rect(
origin.x, origin.y - (index + 1) * keyPixelHeight,
keyPixelWidth, keyPixelHeight),
key,
keyStyle);
switch(displayValues[key].valueType)
keyPixelWidth *= scale;
keyPixelHeight *= scale;
paddingwidth *= scale;
keyStyle.fontSize = (int) (keyPixelHeight * 0.8f);
if (keyStyle.fontSize < 2)
case DisplayValue.ValueType.STRING:
valueStyle.alignment = TextAnchor.MiddleLeft;
GUI.Label(
new Rect(
origin.x + paddingwidth + keyPixelWidth,
origin.y - (index + 1) * keyPixelHeight,
keyPixelWidth, keyPixelHeight),
displayValues[key].stringValue,
valueStyle);
break;
case DisplayValue.ValueType.FLOAT:
float sliderValue = displayValues[key].floatValue;
sliderValue = Mathf.Min(1f, sliderValue);
s = greenStyle;
if (sliderValue < 0)
{
sliderValue = Mathf.Min(1f, -sliderValue);
s = redStyle;
}
GUI.Box(
new Rect(
origin.x + paddingwidth + keyPixelWidth,
origin.y - (index + 0.9f) * keyPixelHeight,
keyPixelWidth * sliderValue, keyPixelHeight * 0.8f),
GUIContent.none,
s);
break;
continue;
}
Dictionary<string, DisplayValue> displayValues = displayTransformValues[target];
case DisplayValue.ValueType.FLOATARRAY_INDEPENDENT:
float histWidth = 0.15f;
vals = displayValues[key].floatArrayValues;
for (int i = 0; i < vals.Length; i++)
{
float value = Mathf.Min(vals[i], 1);
int index = 0;
var orderedKeys = displayValues.Keys.OrderBy(x => -displayValues[x].time);
float[] vals;
GUIStyle s;
foreach (string key in orderedKeys)
{
keyStyle.alignment = TextAnchor.MiddleRight;
GUI.Label(
new Rect(
origin.x, origin.y - (index + 1) * keyPixelHeight,
keyPixelWidth, keyPixelHeight),
key,
keyStyle);
switch (displayValues[key].valueType)
{
case DisplayValue.ValueType.STRING:
valueStyle.alignment = TextAnchor.MiddleLeft;
GUI.Label(
new Rect(
origin.x + paddingwidth + keyPixelWidth,
origin.y - (index + 1) * keyPixelHeight,
keyPixelWidth, keyPixelHeight),
displayValues[key].stringValue,
valueStyle);
break;
case DisplayValue.ValueType.FLOAT:
float sliderValue = displayValues[key].floatValue;
sliderValue = Mathf.Min(1f, sliderValue);
if (value < 0)
if (sliderValue < 0)
value = Mathf.Min(1f, -value);
sliderValue = Mathf.Min(1f, -sliderValue);
origin.x + paddingwidth + keyPixelWidth +
(keyPixelWidth * histWidth + paddingwidth / 2) * i,
origin.y - (index + 0.1f) * keyPixelHeight,
keyPixelWidth * histWidth, -keyPixelHeight * value),
origin.x + paddingwidth + keyPixelWidth,
origin.y - (index + 0.9f) * keyPixelHeight,
keyPixelWidth * sliderValue, keyPixelHeight * 0.8f),
}
break;
break;
case DisplayValue.ValueType.FLOATARRAY_PROPORTION:
float valsSum = 0f;
float valsCum = 0f;
vals = displayValues[key].floatArrayValues;
foreach (float f in vals)
{
valsSum += Mathf.Max(f, 0);
}
if (valsSum < float.Epsilon)
{
Debug.LogError(
string.Format("The Monitor value for key {0} " +
"must be a list or array of " +
"positive values and cannot " +
"be empty.", key));
}
else
{
case DisplayValue.ValueType.FLOATARRAY_INDEPENDENT:
float histWidth = 0.15f;
vals = displayValues[key].floatArrayValues;
float value = Mathf.Max(vals[i], 0) / valsSum;
float value = Mathf.Min(vals[i], 1);
s = greenStyle;
if (value < 0)
{
value = Mathf.Min(1f, -value);
s = redStyle;
}
origin.x + paddingwidth +
keyPixelWidth + keyPixelWidth * valsCum,
origin.y - (index + 0.9f) * keyPixelHeight,
keyPixelWidth * value, keyPixelHeight * 0.8f),
origin.x + paddingwidth + keyPixelWidth +
(keyPixelWidth * histWidth + paddingwidth / 2) * i,
origin.y - (index + 0.1f) * keyPixelHeight,
keyPixelWidth * histWidth, -keyPixelHeight * value),
colorStyle[i % colorStyle.Length]);
valsCum += value;
s);
}
break;
case DisplayValue.ValueType.FLOATARRAY_PROPORTION:
float valsSum = 0f;
float valsCum = 0f;
vals = displayValues[key].floatArrayValues;
foreach (float f in vals)
{
valsSum += Mathf.Max(f, 0);
}
break;
}
if (valsSum < float.Epsilon)
{
Debug.LogError(
string.Format("The Monitor value for key {0} " +
"must be a list or array of " +
"positive values and cannot " +
"be empty.", key));
}
else
{
for (int i = 0; i < vals.Length; i++)
{
float value = Mathf.Max(vals[i], 0) / valsSum;
GUI.Box(
new Rect(
origin.x + paddingwidth +
keyPixelWidth + keyPixelWidth * valsCum,
origin.y - (index + 0.9f) * keyPixelHeight,
keyPixelWidth * value, keyPixelHeight * 0.8f),
GUIContent.none,
colorStyle[i % colorStyle.Length]);
valsCum += value;
index++;
}
}
break;
}
index++;
}
}
/// Helper method used to initialize the GUI. Called once.
void Initialize()
{
keyStyle = GUI.skin.label;
valueStyle = GUI.skin.label;
valueStyle.clipping = TextClipping.Overflow;
valueStyle.wordWrap = false;
barColors = new Color[6] {
Color.magenta,
Color.blue,
Color.cyan,
Color.green,
Color.yellow,
Color.red};
colorStyle = new GUIStyle[barColors.Length];
for (int i = 0; i < barColors.Length; i++)
/// Helper method used to initialize the GUI. Called once.
void Initialize()
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
texture.SetPixel(0, 0, barColors[i]);
texture.Apply();
var staticRectStyle = new GUIStyle();
staticRectStyle.normal.background = texture;
colorStyle[i] = staticRectStyle;
keyStyle = GUI.skin.label;
valueStyle = GUI.skin.label;
valueStyle.clipping = TextClipping.Overflow;
valueStyle.wordWrap = false;
barColors = new Color[6]
{
Color.magenta,
Color.blue,
Color.cyan,
Color.green,
Color.yellow,
Color.red
};
colorStyle = new GUIStyle[barColors.Length];
for (int i = 0; i < barColors.Length; i++)
{
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
texture.SetPixel(0, 0, barColors[i]);
texture.Apply();
var staticRectStyle = new GUIStyle();
staticRectStyle.normal.background = texture;
colorStyle[i] = staticRectStyle;
}
greenStyle = colorStyle[3];
redStyle = colorStyle[5];
greenStyle = colorStyle[3];
redStyle = colorStyle[5];
}
}

66
unity-environment/Assets/ML-Agents/Scripts/ResetParameters.cs


using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ResetParameters : Dictionary<string, float>, ISerializationCallbackReceiver
namespace MLAgents
public struct ResetParameter
public class ResetParameters : Dictionary<string, float>, ISerializationCallbackReceiver
public string key;
public float value;
}
[SerializeField]
private List<ResetParameter> resetParameters = new List<ResetParameter>();
public void OnBeforeSerialize()
{
resetParameters.Clear();
foreach (KeyValuePair<string, float> pair in this)
[System.Serializable]
public struct ResetParameter
ResetParameter rp = new ResetParameter();
rp.key = pair.Key;
rp.value = pair.Value;
resetParameters.Add(rp);
public string key;
public float value;
}
[SerializeField] private List<ResetParameter> resetParameters = new List<ResetParameter>();
public void OnAfterDeserialize()
{
this.Clear();
public void OnBeforeSerialize()
{
resetParameters.Clear();
foreach (KeyValuePair<string, float> pair in this)
{
ResetParameter rp = new ResetParameter();
rp.key = pair.Key;
rp.value = pair.Value;
resetParameters.Add(rp);
}
for (int i = 0; i < resetParameters.Count; i++)
}
public void OnAfterDeserialize()
if (this.ContainsKey(resetParameters[i].key))
this.Clear();
for (int i = 0; i < resetParameters.Count; i++)
Debug.LogError("The ResetParameters contains the same key twice");
}
else
{
this.Add(resetParameters[i].key, resetParameters[i].value);
if (this.ContainsKey(resetParameters[i].key))
{
Debug.LogError("The ResetParameters contains the same key twice");
}
else
{
this.Add(resetParameters[i].key, resetParameters[i].value);
}
}
}

30
unity-environment/Assets/ML-Agents/Scripts/UnityAgentsException.cs


using UnityEngine;
using System;
[System.Serializable]
/// Contains exceptions specific to ML-Agents.
public class UnityAgentsException : System.Exception
namespace MLAgents
/// When a UnityAgentsException is called, the timeScale is set to 0.
/// The simulation will end since no steps will be taken.
public UnityAgentsException(string message) : base(message)
[System.Serializable]
/// Contains exceptions specific to ML-Agents.
public class UnityAgentsException : System.Exception
/// When a UnityAgentsException is called, the timeScale is set to 0.
/// The simulation will end since no steps will be taken.
public UnityAgentsException(string message) : base(message)
{
}
/// A constructor is needed for serialization when an exception propagates
/// from a remoting server to the client.
protected UnityAgentsException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
{
}
/// A constructor is needed for serialization when an exception propagates
/// from a remoting server to the client.
protected UnityAgentsException(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
{ }
}

54
unity-environment/Assets/PlayGround/Communicator/Communicatror_Test.cs


//using System.Collections;
//using System.Collections.Generic;
//using MLAgents;
//using MLAgents.CommunicatorObjects;
//using UnityEngine;
//
//public class Communicatror_Test : MonoBehaviour
//{
// private Communicator _comm;
// private bool _communicationIsActive;
// private int _lastInput;
//
// // Use this for initialization
// private void Start () {
// _comm = new RPCCommunicator(
// new CommunicatorParameters
// {
// port = 5005
// });
// var initOut = new UnityOutput();
// initOut.CustomDataOutput = "First Unity Output";
// UnityInput firstInput;
// UnityInput secondInput;
// firstInput = _comm.Initialize(initOut, out secondInput);
// Debug.Log(firstInput.CustomDataInput);
// Debug.Log(secondInput.CustomDataInput);
// _communicationIsActive = true;
// _lastInput = secondInput.CustomDataInput;
// }
//
// // Update is called once per frame
// private void Update()
// {
// if (!_communicationIsActive)
// {
// return;
// }
//
// UnityInput input = _comm.Exchange(new UnityOutput
// {
// CustomDataOutput = "Ok, received " + _lastInput.ToString()
// });
// if (input == null)
// {
// Debug.Log("The communication is now over.");
// _communicationIsActive = false;
// }
// else
// {
// Debug.Log(input.CustomDataInput);
// _lastInput = input.CustomDataInput;
// }
// }
//}

54
unity-environment/Assets/PlayGround/Docker/DockerTest.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Docker.DotNet;
using System.Buffers;
using System;
using Docker.DotNet.Models;
using System.IO;
public class DockerTest : MonoBehaviour {
// Use this for initialization
void Start ()
{
Test();
}
private async void Test()
{
DockerClient client = new DockerClientConfiguration(
// new Uri("http://ubuntu-docker.cloudapp.net:4243"))
new Uri("localhost:5004"))
.CreateClient();
// client.Containers.StartContainerAsync("39e3317fd25", new ContainerStartParameters(){});
using (FileStream stream = File.Open(Path.GetFullPath("..") + "/Dockerfile", FileMode.Open))
{
await client.Images.BuildImageFromDockerfileAsync(stream, new ImageBuildParameters
{
Tags = new List<string>{"TestFromUnity"}
});
}
await client.Containers.StartContainerAsync("TestFromUnity", new ContainerStartParameters()
{
});
Debug.Log("Finished");
IList<ContainerListResponse> containers = await client.Containers.ListContainersAsync(
new ContainersListParameters(){
Limit = 10,
});
foreach (ContainerListResponse clr in containers)
{
Debug.Log(clr.ID);
}
}
// Update is called once per frame
void Update () {
// var client = new DockerClientConfiguration()
}
}

18
unity-environment/Assets/PlayGround/PersistentSingleton/Editor/SingletonWindow.cs


using UnityEngine;
using UnityEditor;
class SingletonTestWindow : EditorWindow
{
[MenuItem("Window/SingletonTest")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(SingletonTestWindow));
}
void OnGUI()
{
SingletonTest.something = GUILayout.HorizontalSlider(SingletonTest.something, 1, 5);
GUILayout.Label("Current value of something is " + SingletonTest.something);
}
}

18
unity-environment/Assets/PlayGround/PersistentSingleton/SingletonTest.cs


using UnityEngine;
public class SingletonTest
{
public static float something;
protected SingletonTest() { }
private static SingletonTest _instance = null;
public static SingletonTest Instance
{
get
{
return SingletonTest._instance == null ? new SingletonTest() : SingletonTest._instance;
}
}
}

18
unity-environment/Assets/PlayGround/PersistentStatic/Editor/StaticWindow.cs


using UnityEngine;
using UnityEditor;
class StaticTestWindow : EditorWindow
{
[MenuItem("Window/StaticTest")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(StaticTestWindow));
}
void OnGUI()
{
StaticTest.something = GUILayout.HorizontalSlider(StaticTest.something, 1, 5);
GUILayout.Label("Current value of something is " + StaticTest.something);
}
}

31
unity-environment/Assets/PlayGround/PersistentStatic/StaticTest.cs


using UnityEngine;
public static class StaticTest
{
public static float something;
//
// {"State":
// {"Predicates":[
// {"Name":"AT","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}}]},
// {"Name":"START","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},
// {"Name":"LOCKED","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"KEY_COLOR","Args":[{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"1", "Color":"Black"}},{"Type":"KeyDomain.Color","Value":"Black"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"2", "Color":"Black"}},{"Type":"KeyDomain.Color","Value":"Black"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"3", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"4", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"5", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"6", "Color":"Black"}},{"Type":"KeyDomain.Color","Value":"Black"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"7", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"8", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},
// {"Name":"END","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"9", "Color":"White"}}]}]
//
// },
// "Action":{"Name":"UNLOCK_ROOM","Parameters":[]}
// ,"Value":-0.5804855227470398}
// {"State":{"Predicates":[{"Name":"AT","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},{"Name":"START","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},{"Name":"LOCKED","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}}]},{"Name":"ROOM_COLOR","Args":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"White"}]},{"Name":"KEY_COLOR","Args":[{"Type":"KeyDomain.Color","Value":"White"}]}]},"Action":{"Name":"PICKUP_WHITE_KEY","Parameters":[{"Type":"KeyDomain.Room","Value":{"ID":"0", "Color":"White"}},{"Type":"KeyDomain.Color","Value":"Black"},{"Type":"KeyDomain.Color","Value":"White"}]},"Value":-1.9235280752182007}
}

37
unity-environment/Assets/PlayGround/TensorflowTest/TensorflowTester.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if ENABLE_TENSORFLOW
using TensorFlow;
#endif
public class TensorflowTester : MonoBehaviour {
#if ENABLE_TENSORFLOW
TFGraph graph;
TFSession session;
public TextAsset graphModelGrid;
// Use this for initialization
void Start () {
graph = new TFGraph();
graph.Import(graphModelGrid.bytes);
session = new TFSession(graph);
}
// Update is called once per frame
void Update () {
var runner = session.GetRunner();
float[,,,] visual = new float[1,84,84,3];
runner.Fetch(graph["action"][0]);
runner.AddInput(graph["visual_observation_0"][0], visual );
// runner.AddInput(graph["batch_size"][0], new int[] { 0 });
TFTensor[] networkOutput;
networkOutput = runner.Run();
long[,] output = networkOutput[0].GetValue() as long[,];
Debug.Log(output[0,0]);
}
#endif
}
正在加载...
取消
保存