浏览代码

Fix Code convention warnings in Rider. (#2801)

/develop-gpu-test
GitHub 5 年前
当前提交
5d2e466f
共有 68 个文件被更改,包括 335 次插入345 次删除
  1. 1
      UnitySDK/Assets/ML-Agents/Editor/AgentEditor.cs
  2. 24
      UnitySDK/Assets/ML-Agents/Editor/BehaviorParametersEditor.cs
  3. 26
      UnitySDK/Assets/ML-Agents/Editor/BrainParametersDrawer.cs
  4. 2
      UnitySDK/Assets/ML-Agents/Editor/DemonstrationImporter.cs
  5. 16
      UnitySDK/Assets/ML-Agents/Editor/ResetParameterDrawer.cs
  6. 6
      UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs
  7. 4
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorApplier.cs
  8. 5
      UnitySDK/Assets/ML-Agents/Editor/Tests/EditModeTestInternalBrainTensorGenerator.cs
  9. 12
      UnitySDK/Assets/ML-Agents/Editor/Tests/MLAgentsEditModeTest.cs
  10. 6
      UnitySDK/Assets/ML-Agents/Editor/Tests/RandomNormalTest.cs
  11. 3
      UnitySDK/Assets/ML-Agents/Editor/Tests/RayPerceptionTests.cs
  12. 4
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs
  13. 4
      UnitySDK/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
  14. 6
      UnitySDK/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
  15. 4
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
  16. 2
      UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerTarget.cs
  17. 8
      UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorAgent.cs
  18. 18
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  19. 4
      UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridArea.cs
  20. 2
      UnitySDK/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs
  21. 4
      UnitySDK/Assets/ML-Agents/Examples/PushBlock/Scripts/PushAgentBasic.cs
  22. 12
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs
  23. 2
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidArea.cs
  24. 12
      UnitySDK/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidSwitch.cs
  25. 14
      UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs
  26. 6
      UnitySDK/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherGoal.cs
  27. 6
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/FlyCamera.cs
  28. 2
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/GroundContact.cs
  29. 2
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/RayPerception3D.cs
  30. 2
      UnitySDK/Assets/ML-Agents/Examples/SharedAssets/Scripts/TargetContact.cs
  31. 4
      UnitySDK/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
  32. 10
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/HitWall.cs
  33. 16
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs
  34. 2
      UnitySDK/Assets/ML-Agents/Examples/Tennis/Scripts/TennisArea.cs
  35. 8
      UnitySDK/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs
  36. 4
      UnitySDK/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs
  37. 6
      UnitySDK/Assets/ML-Agents/Plugins/Barracuda.Core/Barracuda/Plugins/Editor/BarracudaEditor/NNModelImporter.cs
  38. 20
      UnitySDK/Assets/ML-Agents/Scripts/Academy.cs
  39. 10
      UnitySDK/Assets/ML-Agents/Scripts/ActionMasker.cs
  40. 40
      UnitySDK/Assets/ML-Agents/Scripts/Agent.cs
  41. 14
      UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs
  42. 24
      UnitySDK/Assets/ML-Agents/Scripts/DemonstrationStore.cs
  43. 41
      UnitySDK/Assets/ML-Agents/Scripts/Grpc/RpcCommunicator.cs
  44. 4
      UnitySDK/Assets/ML-Agents/Scripts/ICommunicator.cs
  45. 12
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ApplierImpl.cs
  46. 25
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs
  47. 35
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/GeneratorImpl.cs
  48. 36
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/ModelRunner.cs
  49. 3
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorApplier.cs
  50. 12
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorGenerator.cs
  51. 2
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/TensorProxy.cs
  52. 2
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/Utils/Multinomial.cs
  53. 10
      UnitySDK/Assets/ML-Agents/Scripts/InferenceBrain/Utils/RandomNormal.cs
  54. 18
      UnitySDK/Assets/ML-Agents/Scripts/Policy/BarracudaPolicy.cs
  55. 16
      UnitySDK/Assets/ML-Agents/Scripts/Policy/BehaviorParameters.cs
  56. 6
      UnitySDK/Assets/ML-Agents/Scripts/Policy/HeuristicPolicy.cs
  57. 1
      UnitySDK/Assets/ML-Agents/Scripts/Policy/IPolicy.cs
  58. 21
      UnitySDK/Assets/ML-Agents/Scripts/Policy/RemotePolicy.cs
  59. 5
      UnitySDK/Assets/ML-Agents/Scripts/ResetParameters.cs
  60. 12
      UnitySDK/Assets/ML-Agents/Scripts/Sensor/CameraSensor.cs
  61. 2
      UnitySDK/Assets/ML-Agents/Scripts/Sensor/CameraSensorComponent.cs
  62. 1
      UnitySDK/Assets/ML-Agents/Scripts/Sensor/CompressedObservation.cs
  63. 2
      UnitySDK/Assets/ML-Agents/Scripts/Sensor/ISensor.cs
  64. 13
      UnitySDK/Assets/ML-Agents/Scripts/Sensor/RenderTextureSensor.cs
  65. 2
      UnitySDK/Assets/ML-Agents/Scripts/Sensor/RenderTextureSensorComponent.cs
  66. 6
      UnitySDK/Assets/ML-Agents/Scripts/Startup.cs
  67. 15
      UnitySDK/Assets/ML-Agents/Scripts/Timer.cs
  68. 1
      UnitySDK/UnitySDK.sln.DotSettings

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


using UnityEngine;
using UnityEditor;
using Barracuda;
namespace MLAgents
{

24
UnitySDK/Assets/ML-Agents/Editor/BehaviorParametersEditor.cs


[CanEditMultipleObjects]
public class BehaviorParametersEditor : Editor
{
private const float k_TimeBetweenModelReloads = 2f;
const float k_TimeBetweenModelReloads = 2f;
private float m_TimeSinceModelReload;
float m_TimeSinceModelReload;
private bool m_RequireReload;
bool m_RequireReload;
var serializedObject = base.serializedObject;
serializedObject.Update();
var so = serializedObject;
so.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_BehaviorName"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_BrainParameters"), true);
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Model"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_BehaviorName"));
EditorGUILayout.PropertyField(so.FindProperty("m_BrainParameters"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_Model"), true);
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_InferenceDevice"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_InferenceDevice"), true);
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_UseHeuristic"));
EditorGUILayout.PropertyField(so.FindProperty("m_UseHeuristic"));
// EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Heuristic"), true);
EditorGUI.indentLevel--;
if (EditorGUI.EndChangeCheck())

DisplayFailedModelChecks();
serializedObject.ApplyModifiedProperties();
so.ApplyModifiedProperties();
private void DisplayFailedModelChecks()
void DisplayFailedModelChecks()
{
if (m_RequireReload && m_TimeSinceModelReload > k_TimeBetweenModelReloads)
{

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


public class BrainParametersDrawer : PropertyDrawer
{
// The height of a line in the Unity Inspectors
private const float k_LineHeight = 17f;
private const int k_VecObsNumLine = 3;
private const string k_ActionSizePropName = "vectorActionSize";
private const string k_ActionTypePropName = "vectorActionSpaceType";
private const string k_ActionDescriptionPropName = "vectorActionDescriptions";
private const string k_VecObsPropName = "vectorObservationSize";
private const string k_NumVecObsPropName = "numStackedVectorObservations";
const float k_LineHeight = 17f;
const int k_VecObsNumLine = 3;
const string k_ActionSizePropName = "vectorActionSize";
const string k_ActionTypePropName = "vectorActionSpaceType";
const string k_ActionDescriptionPropName = "vectorActionDescriptions";
const string k_VecObsPropName = "vectorObservationSize";
const string k_NumVecObsPropName = "numStackedVectorObservations";
/// <inheritdoc />
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)

/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
/// <param name="property">The SerializedProperty of the BrainParameters
/// to make the custom GUI for.</param>
private static void DrawVectorObservation(Rect position, SerializedProperty property)
static void DrawVectorObservation(Rect position, SerializedProperty property)
{
EditorGUI.LabelField(position, "Vector Observation");
position.y += k_LineHeight;

/// The Height required to draw the Vector Observations paramaters
/// </summary>
/// <returns>The height of the drawer of the Vector Observations </returns>
private static float GetHeightDrawVectorObservation()
static float GetHeightDrawVectorObservation()
{
return k_VecObsNumLine * k_LineHeight;
}

/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
/// <param name="property">The SerializedProperty of the BrainParameters
/// to make the custom GUI for.</param>
private static void DrawVectorAction(Rect position, SerializedProperty property)
static void DrawVectorAction(Rect position, SerializedProperty property)
{
EditorGUI.LabelField(position, "Vector Action");
position.y += k_LineHeight;

/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
/// <param name="property">The SerializedProperty of the BrainParameters
/// to make the custom GUI for.</param>
private static void DrawContinuousVectorAction(Rect position, SerializedProperty property)
static void DrawContinuousVectorAction(Rect position, SerializedProperty property)
{
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
vecActionSize.arraySize = 1;

/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
/// <param name="property">The SerializedProperty of the BrainParameters
/// to make the custom GUI for.</param>
private static void DrawDiscreteVectorAction(Rect position, SerializedProperty property)
static void DrawDiscreteVectorAction(Rect position, SerializedProperty property)
{
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
vecActionSize.arraySize = EditorGUI.IntField(

/// The Height required to draw the Vector Action parameters
/// </summary>
/// <returns>The height of the drawer of the Vector Action </returns>
private static float GetHeightDrawVectorAction(SerializedProperty property)
static float GetHeightDrawVectorAction(SerializedProperty property)
{
var actionSize = 2 + property.FindPropertyRelative(k_ActionSizePropName).arraySize;
if (property.FindPropertyRelative(k_ActionTypePropName).enumValueIndex == 0)

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


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

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


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

/// </summary>
/// <param name="addRect">The rectangle for the Add New button.</param>
/// <param name="removeRect">The rectangle for the Remove Last button.</param>
private void DrawAddRemoveButtons(Rect addRect, Rect removeRect)
void DrawAddRemoveButtons(Rect addRect, Rect removeRect)
{
// This is the Add button
if (m_Parameters.Count == 0)

/// Signals that the property has been modified and requires the scene to be saved for
/// the changes to persist. Only works when the Editor is not playing.
/// </summary>
private static void MarkSceneAsDirty()
static void MarkSceneAsDirty()
{
if (!EditorApplication.isPlaying)
{

/// </summary>
/// <param name="property">The SerializedProperty of the ResetParameters
/// to make the custom GUI for.</param>
private void LazyInitializeParameters(SerializedProperty property)
void LazyInitializeParameters(SerializedProperty property)
{
if (m_Parameters != null)
{

/// <summary>
/// Removes the last ResetParameter from the ResetParameters
/// </summary>
private void RemoveLastParameter()
void RemoveLastParameter()
{
if (m_Parameters.Count > 0)
{

/// <summary>
/// Adds a new ResetParameter to the ResetParameters with a default name.
/// </summary>
private void AddParameter()
void AddParameter()
{
var key = k_NewKeyPrefix + m_Parameters.Count;
var value = default(float);

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


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

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


{
public class EditModeTestInternalBrainTensorApplier
{
private class TestAgent : Agent
class TestAgent : Agent
{
public AgentAction GetAction()
{

}
}
private List<Agent> GetFakeAgentInfos()
List<Agent> GetFakeAgentInfos()
{
var goA = new GameObject("goA");
var agentA = goA.AddComponent<TestAgent>();

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


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

[Test]
public void Construction()
{
var bp = new BrainParameters();
var tensorGenerator = new TensorGenerator(bp, 0, alloc, mem);
var tensorGenerator = new TensorGenerator(0, alloc, mem);
Assert.IsNotNull(tensorGenerator);
alloc.Dispose();
}

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


{
initializeAgentCalls += 1;
// Add in some custom sensors so we can confirm they get sorted as expected.
// Add in some custom Sensors so we can confirm they get sorted as expected.
m_Sensors.Add(sensor2);
m_Sensors.Add(sensor1);
sensors.Add(sensor2);
sensors.Add(sensor1);
}
public override void CollectObservations()

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

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


{
public class RandomNormalTest
{
private const float k_FirstValue = -1.19580f;
private const float k_SecondValue = -0.97345f;
private const double k_Epsilon = 0.0001;
const float k_FirstValue = -1.19580f;
const float k_SecondValue = -0.97345f;
const double k_Epsilon = 0.0001;
[Test]
public void RandomNormalTestTwoDouble()

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


var go = new GameObject("MyGameObject");
var rayPer3D = go.AddComponent<RayPerception3D>();
var result = rayPer3D.Perceive(1f, angles ,
tags, 0f, 0f);
var result = rayPer3D.Perceive(1f, angles, tags);
Debug.Log(result.Count);
Assert.IsTrue(result.Count == angles.Length * (tags.Length + 2));
}

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


{
[Header("Specific to Ball3D")]
public GameObject ball;
private Rigidbody m_BallRb;
private ResetParameters m_ResetParams;
Rigidbody m_BallRb;
ResetParameters m_ResetParams;
public override void InitializeAgent()
{

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


{
[Header("Specific to Ball3DHard")]
public GameObject ball;
private Rigidbody m_BallRb;
private ResetParameters m_ResetParams;
Rigidbody m_BallRb;
ResetParameters m_ResetParams;
public override void InitializeAgent()
{

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


public class BasicAgent : Agent
{
[Header("Specific to Basic")]
private BasicAcademy m_Academy;
BasicAcademy m_Academy;
private float m_TimeSinceDecision;
float m_TimeSinceDecision;
int m_Position;
int m_SmallGoalPosition;
int m_LargeGoalPosition;

WaitTimeInference();
}
private void WaitTimeInference()
void WaitTimeInference()
{
if (!m_Academy.GetIsInference())
{

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


{
}
private void FixedUpdate()
void FixedUpdate()
{
if (Physics.Raycast(transform.position, new Vector3(0f, -1f, 0f), 0.51f) && m_JumpCooldown <= 0f)
{

return action;
}
private void Update()
void Update()
{
if (m_LookDir.magnitude > float.Epsilon)
{

2
UnitySDK/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerTarget.cs


gameObject.transform.Rotate(new Vector3(1, 0, 0), 0.5f);
}
private void OnTriggerEnter(Collider collision)
void OnTriggerEnter(Collider collision)
{
var agent = collision.gameObject.GetComponent<Agent>();
if (agent != null)

8
UnitySDK/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorAgent.cs


public class FoodCollectorAgent : Agent
{
private FoodCollectorAcademy m_MyAcademy;
FoodCollectorAcademy m_MyAcademy;
public GameObject area;
FoodCollectorArea m_MyArea;
bool m_Frozen;

float m_FrozenTime;
float m_EffectTime;
Rigidbody m_AgentRb;
private float m_LaserLength;
float m_LaserLength;
// Speed of agent rotation.
public float turnSpeed = 300;

public Material frozenMaterial;
public GameObject myLaser;
public bool contribute;
private RayPerception3D m_RayPer;
RayPerception3D m_RayPer;
public bool useVectorObs;

const float rayDistance = 50f;
float[] rayAngles = { 20f, 90f, 160f, 45f, 135f, 70f, 110f };
string[] detectableObjects = { "food", "agent", "wall", "badFood", "frozenAgent" };
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles, detectableObjects, 0f, 0f));
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles, detectableObjects));
var localVelocity = transform.InverseTransformDirection(m_AgentRb.velocity);
AddVectorObs(localVelocity.x);
AddVectorObs(localVelocity.z);

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


public class GridAgent : Agent
{
private Academy m_Academy;
Academy m_Academy;
private float m_TimeSinceDecision;
float m_TimeSinceDecision;
[Tooltip("Because we want an observation right before making a decision, we can force " +
"a camera to render before making a decision. Place the agentCam here if using " +

"masking turned on may not behave optimally when action masking is turned off.")]
public bool maskActions = true;
private const int k_NoAction = 0; // do nothing!
private const int k_Up = 1;
private const int k_Down = 2;
private const int k_Left = 3;
private const int k_Right = 4;
const int k_NoAction = 0; // do nothing!
const int k_Up = 1;
const int k_Down = 2;
const int k_Left = 3;
const int k_Right = 4;
public override void InitializeAgent()
{

/// <summary>
/// Applies the mask for the agents action to disallow unnecessary actions.
/// </summary>
private void SetMask()
void SetMask()
{
// Prevents the agent from picking an action that would make it collide with a wall
var positionX = (int)transform.position.x;

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

4
UnitySDK/Assets/ML-Agents/Examples/GridWorld/Scripts/GridArea.cs


public GameObject trueAgent;
private ResetParameters m_ResetParameters;
ResetParameters m_ResetParameters;
Camera m_AgentCam;

GameObject m_Se;
GameObject m_Sw;
private Vector3 m_InitialPosition;
Vector3 m_InitialPosition;
public void Awake()
{

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


float[] rayAngles = { 20f, 60f, 90f, 120f, 160f };
string[] detectableObjects = { "symbol_O_Goal", "symbol_X_Goal", "symbol_O", "symbol_X", "wall" };
AddVectorObs(GetStepCount() / (float)agentParameters.maxStep);
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles, detectableObjects, 0f, 0f));
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles, detectableObjects));
}
}

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


{
var rayDistance = 12f;
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, m_DetectableObjects, 0f, 0f));
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, m_DetectableObjects, 1.5f, 0f));
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, m_DetectableObjects));
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, m_DetectableObjects, 1.5f));
}
}

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


public class PyramidAgent : Agent
{
public GameObject area;
private PyramidArea m_MyArea;
private Rigidbody m_AgentRb;
private RayPerception m_RayPer;
private PyramidSwitch m_SwitchLogic;
PyramidArea m_MyArea;
Rigidbody m_AgentRb;
RayPerception m_RayPer;
PyramidSwitch m_SwitchLogic;
public GameObject areaSwitch;
public bool useVectorObs;

float[] rayAngles2 = { 15f, 85f, 155f, 40f, 130f, 65f, 105f };
string[] detectableObjects = { "block", "wall", "goal", "switchOff", "switchOn", "stone" };
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles, detectableObjects, 0f, 0f));
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles, detectableObjects));
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles1, detectableObjects, 0f, 5f));
AddVectorObs(m_RayPer.Perceive(rayDistance, rayAngles2, detectableObjects, 0f, 10f));
AddVectorObs(m_SwitchLogic.GetState());

m_MyArea.CreateStonePyramid(1, items[8]);
}
private void OnCollisionEnter(Collision collision)
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("goal"))
{

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


CreateObject(numObjects, stonePyramid, spawnAreaIndex);
}
private void CreateObject(int numObjects, GameObject desiredObject, int spawnAreaIndex)
void CreateObject(int numObjects, GameObject desiredObject, int spawnAreaIndex)
{
for (var i = 0; i < numObjects; i++)
{

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


public Material onMaterial;
public Material offMaterial;
public GameObject myButton;
private bool m_State;
private GameObject m_Area;
private PyramidArea m_AreaComponent;
private int m_PyramidIndex;
bool m_State;
GameObject m_Area;
PyramidArea m_AreaComponent;
int m_PyramidIndex;
public bool GetState()
{

private void Start()
void Start()
{
m_Area = gameObject.transform.parent.gameObject;
m_AreaComponent = m_Area.GetComponent<PyramidArea>();

myButton.GetComponent<Renderer>().material = offMaterial;
}
private void OnCollisionEnter(Collision other)
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("agent") && m_State == false)
{

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


public GameObject pendulumB;
public GameObject hand;
public GameObject goal;
private ReacherAcademy m_MyAcademy;
ReacherAcademy m_MyAcademy;
private Rigidbody m_RbA;
private Rigidbody m_RbB;
Rigidbody m_RbA;
Rigidbody m_RbB;
private float m_GoalSpeed;
float m_GoalSpeed;
private float m_GoalSize;
float m_GoalSize;
private float m_Deviation;
float m_Deviation;
private float m_DeviationFreq;
float m_DeviationFreq;
/// <summary>
/// Collect the rigidbodies of the reacher in order to resue them for

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


public GameObject hand;
public GameObject goalOn;
private void OnTriggerEnter(Collider other)
void OnTriggerEnter(Collider other)
{
if (other.gameObject == hand)
{

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

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

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


public bool rotateOnlyIfMousedown = true;
public bool movementStaysFlat = true;
private Vector3
Vector3
private float m_TotalRun = 1.0f;
float m_TotalRun = 1.0f;
void Awake()
{

}
}
private Vector3 GetBaseInput()
Vector3 GetBaseInput()
{
// returns the basic values, if it's 0 than it's not active.
var pVelocity = new Vector3();

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


public bool penalizeGroundContact; // Whether to penalize on contact.
public float groundContactPenalty; // Penalty amount (ex: -1).
public bool touchingGround;
private const string k_Ground = "ground"; // Tag of ground object.
const string k_Ground = "ground"; // Tag of ground object.
/// <summary>
/// Check for collision with ground, and optionally penalize agent.

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


{
Vector3 m_EndPosition;
RaycastHit m_Hit;
private float[] m_SubList;
float[] m_SubList;
/// <summary>
/// Creates perception vector to be used as part of an observation of an agent.

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


public class TargetContact : MonoBehaviour
{
[Header("Detect Targets")] public bool touchingTarget;
private const string k_Target = "target"; // Tag on target object.
const string k_Target = "target"; // Tag on target object.
/// <summary>
/// Check for collision with a target.

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


{
detectableObjects = m_DetectableObjectsBlue;
}
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, detectableObjects, 0f, 0f));
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, detectableObjects, 1f, 0f));
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, detectableObjects));
AddVectorObs(m_RayPer.Perceive(rayDistance, m_RayAngles, detectableObjects, 1f));
}
public void MoveAgent(float[] act)

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


public GameObject areaObject;
public int lastAgentHit;
private TennisArea m_Area;
private TennisAgent m_AgentA;
private TennisAgent m_AgentB;
TennisArea m_Area;
TennisAgent m_AgentA;
TennisAgent m_AgentB;
// Use this for initialization
void Start()

m_AgentB = m_Area.agentB.GetComponent<TennisAgent>();
}
private void OnTriggerExit(Collider other)
void OnTriggerExit(Collider other)
{
if (other.name == "over")
{

}
}
private void OnCollisionEnter(Collision collision)
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("iWall"))
{

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


public float angle;
public float scale;
private Text m_TextComponent;
private Rigidbody m_AgentRb;
private Rigidbody m_BallRb;
private float m_InvertMult;
private ResetParameters m_ResetParams;
Text m_TextComponent;
Rigidbody m_AgentRb;
Rigidbody m_BallRb;
float m_InvertMult;
ResetParameters m_ResetParams;
private const string k_CanvasName = "Canvas";
private const string k_ScoreBoardAName = "ScoreA";
private const string k_ScoreBoardBName = "ScoreB";
const string k_CanvasName = "Canvas";
const string k_ScoreBoardAName = "ScoreA";
const string k_ScoreBoardBName = "ScoreB";
public override void InitializeAgent()
{

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


public GameObject ball;
public GameObject agentA;
public GameObject agentB;
private Rigidbody m_BallRb;
Rigidbody m_BallRb;
// Use this for initialization
void Start()

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


bool m_IsNewDecisionStep;
int m_CurrentDecisionStep;
private Rigidbody m_HipsRb;
private Rigidbody m_ChestRb;
private Rigidbody m_SpineRb;
Rigidbody m_HipsRb;
Rigidbody m_ChestRb;
Rigidbody m_SpineRb;
private ResetParameters m_ResetParams;
ResetParameters m_ResetParams;
public override void InitializeAgent()
{

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


var rayDistance = 20f;
float[] rayAngles = { 0f, 45f, 90f, 135f, 180f, 110f, 70f };
AddVectorObs(m_RayPer.Perceive(
rayDistance, rayAngles, m_DetectableObjects, 0f, 0f));
rayDistance, rayAngles, m_DetectableObjects));
AddVectorObs(m_RayPer.Perceive(
rayDistance, rayAngles, m_DetectableObjects, 2.5f, 2.5f));
var agentPos = m_AgentRb.position - ground.transform.position;

m_AgentRb.velocity = default(Vector3);
}
private void FixedUpdate()
void FixedUpdate()
{
if (m_Configuration != -1)
{

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


[ScriptedImporter(1, new[] {"nn"})]
public class NNModelImporter : ScriptedImporter
{
private const string k_IconName = "NNModelIcon";
const string k_IconName = "NNModelIcon";
private Texture2D m_IconTexture;
Texture2D m_IconTexture;
public override void OnImportAsset(AssetImportContext ctx)
{

ctx.SetMainObject(asset);
}
private Texture2D LoadIconTexture()
Texture2D LoadIconTexture()
{
if (m_IconTexture == null)
{

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


"docs/Learning-Environment-Design-Academy.md")]
public abstract class Academy : MonoBehaviour
{
private const string k_ApiVersion = "API-10";
const string k_ApiVersion = "API-10";
private Vector3 m_OriginalGravity;
Vector3 m_OriginalGravity;
private float m_OriginalFixedDeltaTime;
float m_OriginalFixedDeltaTime;
private float m_OriginalMaximumDeltaTime;
float m_OriginalMaximumDeltaTime;
// Fields provided in the Inspector

/// Pointer to the communicator currently in use by the Academy.
public ICommunicator Communicator;
private bool m_Initialized;
private List<ModelRunner> m_ModelRunners = new List<ModelRunner>();
bool m_Initialized;
List<ModelRunner> m_ModelRunners = new List<ModelRunner>();
// Flag used to keep track of the first time the Academy is reset.
bool m_FirstAcademyReset;

}
// Used to read Python-provided environment parameters
private static int ReadArgs()
static int ReadArgs()
{
var args = System.Environment.GetCommandLineArgs();
var inputPort = "";

/// <summary>
/// Initializes the environment, configures it and initialized the Academy.
/// </summary>
private void InitializeEnvironment()
void InitializeEnvironment()
{
m_OriginalGravity = Physics.gravity;
m_OriginalFixedDeltaTime = Time.fixedDeltaTime;

Application.Quit();
}
private void OnResetCommand(EnvironmentResetParameters newResetParameters)
void OnResetCommand(EnvironmentResetParameters newResetParameters)
{
UpdateResetParameters(newResetParameters);
ForcedFullReset();

m_IsInference = !inputParams.isTraining;
}
private void UpdateResetParameters(EnvironmentResetParameters newResetParameters)
void UpdateResetParameters(EnvironmentResetParameters newResetParameters)
{
if (newResetParameters.resetParameters != null)
{

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


{
/// When using discrete control, is the starting indices of the actions
/// when all the branches are concatenated with each other.
private int[] m_StartingActionIndices;
int[] m_StartingActionIndices;
private bool[] m_CurrentMask;
bool[] m_CurrentMask;
private readonly BrainParameters m_BrainParameters;
readonly BrainParameters m_BrainParameters;
public ActionMasker(BrainParameters brainParameters)
{

/// <summary>
/// Makes sure that the current mask is usable.
/// </summary>
private void AssertMask()
void AssertMask()
{
// Action Masks can only be used in Discrete Control.
if (m_BrainParameters.vectorActionSpaceType != SpaceType.Discrete)

/// </summary>
/// <param name="branch"> The index of the branch to check</param>
/// <returns> True if all the actions of the branch are masked</returns>
private bool AreAllActionsMasked(int branch)
bool AreAllActionsMasked(int branch)
{
if (m_CurrentMask == null)
{

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


using UnityEngine;
using Barracuda;
using MLAgents.Sensor;
using UnityEngine.Serialization;
namespace MLAgents
{

[RequireComponent(typeof(BehaviorParameters))]
public abstract class Agent : MonoBehaviour
{
private IPolicy m_Brain;
private BehaviorParameters m_PolicyFactory;
IPolicy m_Brain;
BehaviorParameters m_PolicyFactory;
/// <summary>
/// Agent parameters specified within the Editor via AgentEditor.

int m_Id;
/// Keeps track of the actions that are masked at each step.
private ActionMasker m_ActionMasker;
ActionMasker m_ActionMasker;
private DemonstrationRecorder m_Recorder;
DemonstrationRecorder m_Recorder;
public List<ISensor> m_Sensors;
[FormerlySerializedAs("m_Sensors")]
public List<ISensor> sensors;
/// Monobehavior function that is called when the attached GameObject
/// MonoBehaviour function that is called when the attached GameObject
/// becomes enabled or active.
void OnEnable()
{

{
m_Info = new AgentInfo();
m_Action = new AgentAction();
m_Sensors = new List<ISensor>();
sensors = new List<ISensor>();
if (academy == null)
{

/// will categorize the agent when training.
/// </param>
/// <param name="model"> The model to use for inference.</param>
/// <param name = "inferenceDevide"> Define on what device the model
/// <param name = "inferenceDevice"> Define on what device the model
/// will be run.</param>
public void GiveModel(
string behaviorName,

public void InitializeSensors()
{
var attachedSensorComponents = GetComponents<SensorComponent>();
m_Sensors.Capacity += attachedSensorComponents.Length;
sensors.Capacity += attachedSensorComponents.Length;
m_Sensors.Add(component.CreateSensor());
sensors.Add(component.CreateSensor());
// Sort the sensors by name to ensure determinism
m_Sensors.Sort((x, y) => x.GetName().CompareTo(y.GetName()));
// Sort the Sensors by name to ensure determinism
sensors.Sort((x, y) => x.GetName().CompareTo(y.GetName()));
for (var i = 0; i < m_Sensors.Count - 1; i++)
for (var i = 0; i < sensors.Count - 1; i++)
Debug.Assert(!m_Sensors[i].GetName().Equals(m_Sensors[i + 1].GetName()), "Sensor names must be unique.");
Debug.Assert(!sensors[i].GetName().Equals(sensors[i + 1].GetName()), "Sensor names must be unique.");
}
#endif
}

/// <summary>
/// Generate data for each sensor and store it on the Agent's AgentInfo.
/// NOTE: At the moment, this is only called during training or when using a DemonstrationRecorder;
/// during inference the sensors are used to write directly to the Tensor data. This will likely change in the
/// during inference the Sensors are used to write directly to the Tensor data. This will likely change in the
// Generate data for all sensors
// Generate data for all Sensors
for (var i = 0; i < m_Sensors.Count; i++)
for (var i = 0; i < sensors.Count; i++)
var sensor = m_Sensors[i];
var sensor = sensors[i];
var compressedObs = new CompressedObservation
{
Data = sensor.GetCompressedObservation(),

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


{
public bool record;
public string demonstrationName;
private Agent m_RecordingAgent;
private string m_FilePath;
private DemonstrationStore m_DemoStore;
Agent m_RecordingAgent;
string m_FilePath;
DemonstrationStore m_DemoStore;
private void Start()
void Start()
{
if (Application.isEditor && record)
{

private void Update()
void Update()
{
if (Application.isEditor && record && m_DemoStore == null)
{

/// <summary>
/// Creates demonstration store for use in recording.
/// </summary>
private void InitializeDemoStore()
void InitializeDemoStore()
{
m_RecordingAgent = GetComponent<Agent>();
m_DemoStore = new DemonstrationStore();

/// <summary>
/// Closes Demonstration store.
/// </summary>
private void OnApplicationQuit()
void OnApplicationQuit()
{
if (Application.isEditor && record && m_DemoStore != null)
{

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


public class DemonstrationStore
{
public const int MetaDataBytes = 32; // Number of bytes allocated to metadata in demo file.
private readonly IFileSystem m_FileSystem;
private const string k_DemoDirecory = "Assets/Demonstrations/";
private const string k_ExtensionType = ".demo";
readonly IFileSystem m_FileSystem;
const string k_DemoDirecory = "Assets/Demonstrations/";
const string k_ExtensionType = ".demo";
private string m_FilePath;
private DemonstrationMetaData m_MetaData;
private Stream m_Writer;
private float m_CumulativeReward;
string m_FilePath;
DemonstrationMetaData m_MetaData;
Stream m_Writer;
float m_CumulativeReward;
public DemonstrationStore(IFileSystem fileSystem)
{

/// Checks for the existence of the Demonstrations directory
/// and creates it if it does not exist.
/// </summary>
private void CreateDirectory()
void CreateDirectory()
{
if (!m_FileSystem.Directory.Exists(k_DemoDirecory))
{

/// <summary>
/// Creates demonstration file.
/// </summary>
private void CreateDemonstrationFile(string demonstrationName)
void CreateDemonstrationFile(string demonstrationName)
{
// Creates demonstration file.
var literalName = demonstrationName;

/// <summary>
/// Writes brain parameters to file.
/// </summary>
private void WriteBrainParameters(string brainName, BrainParameters brainParameters)
void WriteBrainParameters(string brainName, BrainParameters brainParameters)
{
// Writes BrainParameters to file.
m_Writer.Seek(MetaDataBytes + 1, 0);

/// <summary>
/// Performs necessary episode-completion steps.
/// </summary>
private void EndEpisode()
void EndEpisode()
{
m_MetaData.numberEpisodes += 1;
}

/// </summary>
private void WriteMetadata()
void WriteMetadata()
{
var metaProto = m_MetaData.ToProto();
var metaProtoBytes = metaProto.ToByteArray();

41
UnitySDK/Assets/ML-Agents/Scripts/Grpc/RpcCommunicator.cs


bool m_IsOpen;
/// The default number of agents in the scene
private const int k_NumAgents = 32;
const int k_NumAgents = 32;
/// Keeps track of the agents of each brain on the current step
Dictionary<string, List<Agent>> m_CurrentAgents =

new Dictionary<string, Dictionary<Agent, AgentAction>>();
// Brains that we have sent over the communicator with agents.
HashSet<string> m_sentBrainKeys = new HashSet<string>();
Dictionary<string, BrainParameters> m_unsentBrainKeys = new Dictionary<string, BrainParameters>();
HashSet<string> m_SentBrainKeys = new HashSet<string>();
Dictionary<string, BrainParameters> m_UnsentBrainKeys = new Dictionary<string, BrainParameters>();
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX

SendCommandEvent(rlInput.Command, rlInput.EnvironmentParameters);
}
private UnityInputProto Initialize(UnityOutputProto unityOutput,
UnityInputProto Initialize(UnityOutputProto unityOutput,
out UnityInputProto unityInput)
{
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX

#endregion
#region Sending Events
private void SendCommandEvent(CommandProto command, EnvironmentParametersProto environmentParametersProto)
void SendCommandEvent(CommandProto command, EnvironmentParametersProto environmentParametersProto)
{
switch (command)
{

}
}
private void SendRLInputReceivedEvent(bool isTraining)
void SendRLInputReceivedEvent(bool isTraining)
{
RLInputReceived?.Invoke(new UnityRLInputParameters { isTraining = isTraining });
}

/// <summary>
/// Sends the observations of one Agent.
/// </summary>
/// <param name="key">Batch Key.</param>
/// <param name="agents">Agent info.</param>
/// <param name="brainKey">Batch Key.</param>
/// <param name="agent">Agent info.</param>
public void PutObservations(string brainKey, Agent agent)
{
m_CurrentAgents[brainKey].Add(agent);

/// </summary>
/// <returns>The next UnityInput.</returns>
/// <param name="unityOutput">The UnityOutput to be sent.</param>
private UnityInputProto Exchange(UnityOutputProto unityOutput)
UnityInputProto Exchange(UnityOutputProto unityOutput)
{
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
if (!m_IsOpen)

/// <returns>The UnityMessage corresponding.</returns>
/// <param name="content">The UnityOutput to be wrapped.</param>
/// <param name="status">The status of the message.</param>
private static UnityMessageProto WrapMessage(UnityOutputProto content, int status)
static UnityMessageProto WrapMessage(UnityOutputProto content, int status)
{
return new UnityMessageProto
{

}
private void CacheBrainParameters(string brainKey, BrainParameters brainParameters)
void CacheBrainParameters(string brainKey, BrainParameters brainParameters)
if (m_sentBrainKeys.Contains(brainKey))
if (m_SentBrainKeys.Contains(brainKey))
m_unsentBrainKeys[brainKey] = brainParameters;
m_UnsentBrainKeys[brainKey] = brainParameters;
private UnityRLInitializationOutputProto GetTempUnityRlInitializationOutput()