浏览代码

add pre-commit hook for dotnet-format (#4362)

/MLA-1734-demo-provider
GitHub 4 年前
当前提交
e7916b08
共有 88 个文件被更改,包括 377 次插入311 次删除
  1. 18
      .circleci/config.yml
  2. 6
      .pre-commit-config.yaml
  3. 2
      DevProject/Assets/ML-Agents/Scripts/Tests/Performance/SensorPerformanceTests.cs
  4. 2
      Project/Assets/ML-Agents/Editor/DisableBurstFromMenu.cs
  5. 18
      Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs
  6. 2
      Project/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorSettings.cs
  7. 10
      Project/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  8. 2
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/AdjustTrainingTimescale.cs
  9. 4
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/DirectionIndicator.cs
  10. 14
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/JointDriveController.cs
  11. 20
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs
  12. 6
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/Monitor.cs
  13. 2
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/OrientationCubeController.cs
  14. 2
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ProjectSettingsOverrides.cs
  15. 2
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/SensorBase.cs
  16. 8
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/TargetController.cs
  17. 6
      Project/Assets/ML-Agents/Examples/Startup/Scripts/Startup.cs
  18. 14
      Project/Assets/ML-Agents/Examples/Tennis/Scripts/HitWall.cs
  19. 12
      Project/Assets/ML-Agents/Examples/Worm/Scripts/WormAgent.cs
  20. 2
      com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyJointExtractor.cs
  21. 2
      com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyPoseExtractor.cs
  22. 2
      com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodySensorComponent.cs
  23. 4
      com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsBodySensor.cs
  24. 4
      com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsSensorSettings.cs
  25. 10
      com.unity.ml-agents.extensions/Runtime/Sensors/PoseExtractor.cs
  26. 2
      com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyJointExtractor.cs
  27. 4
      com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyPoseExtractor.cs
  28. 4
      com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs
  29. 2
      com.unity.ml-agents.extensions/Tests/Editor/Sensors/ArticulationBodyPoseExtractorTests.cs
  30. 2
      com.unity.ml-agents.extensions/Tests/Editor/Sensors/ArticulationBodySensorTests.cs
  31. 10
      com.unity.ml-agents.extensions/Tests/Editor/Sensors/PoseExtractorTests.cs
  32. 2
      com.unity.ml-agents.extensions/Tests/Editor/Sensors/RigidBodyPoseExtractorTests.cs
  33. 3
      com.unity.ml-agents.extensions/Tests/Runtime/RuntimeExampleTest.cs
  34. 1
      com.unity.ml-agents/CHANGELOG.md
  35. 7
      com.unity.ml-agents/CONTRIBUTING.md
  36. 2
      com.unity.ml-agents/Editor/DemonstrationImporter.cs
  37. 16
      com.unity.ml-agents/Runtime/Academy.cs
  38. 2
      com.unity.ml-agents/Runtime/Actuators/ActionSegment.cs
  39. 2
      com.unity.ml-agents/Runtime/Actuators/ActionSpec.cs
  40. 6
      com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs
  41. 2
      com.unity.ml-agents/Runtime/Actuators/VectorActuator.cs
  42. 10
      com.unity.ml-agents/Runtime/Agent.cs
  43. 2
      com.unity.ml-agents/Runtime/Agent.deprecated.cs
  44. 6
      com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs
  45. 36
      com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs
  46. 2
      com.unity.ml-agents/Runtime/Communicator/UnityRLCapabilities.cs
  47. 20
      com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs
  48. 2
      com.unity.ml-agents/Runtime/Policies/BrainParameters.cs
  49. 2
      com.unity.ml-agents/Runtime/Policies/HeuristicPolicy.cs
  50. 14
      com.unity.ml-agents/Runtime/Sampler.cs
  51. 4
      com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs
  52. 14
      com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs
  53. 2
      com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs
  54. 4
      com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs
  55. 2
      com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs
  56. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/BoolReflectionSensor.cs
  57. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/FloatReflectionSensor.cs
  58. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/IntReflectionSensor.cs
  59. 10
      com.unity.ml-agents/Runtime/Sensors/Reflection/ObservableAttribute.cs
  60. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/QuaternionReflectionSensor.cs
  61. 6
      com.unity.ml-agents/Runtime/Sensors/Reflection/ReflectionSensorBase.cs
  62. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector2ReflectionSensor.cs
  63. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector3ReflectionSensor.cs
  64. 2
      com.unity.ml-agents/Runtime/Sensors/Reflection/Vector4ReflectionSensor.cs
  65. 6
      com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs
  66. 10
      com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs
  67. 3
      com.unity.ml-agents/Runtime/SideChannels/EnvironmentParametersChannel.cs
  68. 8
      com.unity.ml-agents/Runtime/Timer.cs
  69. 42
      com.unity.ml-agents/Tests/Editor/Actuators/ActuatorDiscreteActionMaskTests.cs
  70. 42
      com.unity.ml-agents/Tests/Editor/Actuators/ActuatorManagerTests.cs
  71. 12
      com.unity.ml-agents/Tests/Editor/Actuators/VectorActuatorTests.cs
  72. 4
      com.unity.ml-agents/Tests/Editor/DemonstrationTests.cs
  73. 8
      com.unity.ml-agents/Tests/Editor/DiscreteActionOutputApplierTest.cs
  74. 12
      com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs
  75. 8
      com.unity.ml-agents/Tests/Editor/ModelRunnerTest.cs
  76. 8
      com.unity.ml-agents/Tests/Editor/MultinomialTest.cs
  77. 12
      com.unity.ml-agents/Tests/Editor/ParameterLoaderTest.cs
  78. 4
      com.unity.ml-agents/Tests/Editor/SamplerTests.cs
  79. 10
      com.unity.ml-agents/Tests/Editor/Sensor/CameraSensorTest.cs
  80. 2
      com.unity.ml-agents/Tests/Editor/Sensor/FloatVisualSensorTests.cs
  81. 2
      com.unity.ml-agents/Tests/Editor/Sensor/ObservableAttributeTests.cs
  82. 6
      com.unity.ml-agents/Tests/Editor/Sensor/ObservationWriterTests.cs
  83. 2
      com.unity.ml-agents/Tests/Editor/Sensor/SensorShapeValidatorTests.cs
  84. 36
      com.unity.ml-agents/Tests/Editor/Sensor/StackingSensorTests.cs
  85. 22
      com.unity.ml-agents/Tests/Editor/Sensor/VectorSensorTests.cs
  86. 14
      com.unity.ml-agents/Tests/Editor/UtilitiesTests.cs
  87. 24
      utils/run_dotnet_format.py
  88. 0
      /.editorconfig

18
.circleci/config.yml


# Need ruby for search-and-replace
sudo apt-get update
sudo apt-get install ruby-full
# install dotnet and the formatter - see https://docs.microsoft.com/en-us/dotnet/core/install/linux-debian#debian-9-
pushd ~
wget -O - https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg
sudo mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
wget https://packages.microsoft.com/config/debian/9/prod.list
sudo mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
sudo chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg
sudo chown root:root /etc/apt/sources.list.d/microsoft-prod.list
popd
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-3.1 && \
dotnet tool install -g dotnet-format --version 4.1.131201
echo "Setting up venv"
python3 -m venv venv
. venv/bin/activate
pip install --upgrade pip

name: Check Code Style using pre-commit
command: |
. venv/bin/activate
export PATH="$PATH:~/.dotnet/tools"
pre-commit run --show-diff-on-failure --all-files
markdown_link_check:

6
.pre-commit-config.yaml


name: validate release links
language: script
entry: utils/validate_release_links.py
- id: dotnet-format
name: dotnet-format
language: script
entry: utils/run_dotnet_format.py
types: [c#]

2
DevProject/Assets/ML-Agents/Scripts/Tests/Performance/SensorPerformanceTests.cs


{
using (Measure.ProfilerMarkers(s_Markers))
{
for(var i=0; i<k_MarkerTestSteps; i++)
for (var i = 0; i < k_MarkerTestSteps; i++)
{
RunAgent<CollectObservationsAgent>(k_NumAgentSteps, 7, ObservableAttributeOptions.Ignore);
}

2
Project/Assets/ML-Agents/Editor/DisableBurstFromMenu.cs


#if UNITY_CLOUD_BUILD
#if UNITY_CLOUD_BUILD
using UnityEditor;
public class DisableBurstFromMenu

18
Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs


Vector3 m_WalkDir; //Direction to the target
Quaternion m_WalkDirLookRot; //Will hold the rotation to our target
[Header("Target To Walk Towards")] [Space(10)]
[Header("Target To Walk Towards")]
[Space(10)]
public TargetController target; //Target the agent will walk towards.
[Header("Body Parts")] [Space(10)] public Transform body;

public Transform leg3Lower;
[Header("Orientation")] [Space(10)]
[Header("Orientation")]
[Space(10)]
//This will be used as a stabilized model space reference point for observations
//Because ragdolls can move erratically during training, using a stabilized reference transform improves learning
public OrientationCubeController orientationCube;

[Header("Reward Functions To Use")] [Space(10)]
[Header("Reward Functions To Use")]
[Space(10)]
[Header("Foot Grounded Visualization")] [Space(10)]
[Header("Foot Grounded Visualization")]
[Space(10)]
public bool useFootGroundedVisualization;
public MeshRenderer foot0;

{
throw new ArgumentException(
"NaN in movingTowardsDot.\n" +
$" orientationCube.transform.forward: {orientationCube.transform.forward}\n"+
$" body.velocity: {m_JdController.bodyPartsDict[body].rb.velocity}\n"+
$" orientationCube.transform.forward: {orientationCube.transform.forward}\n" +
$" body.velocity: {m_JdController.bodyPartsDict[body].rb.velocity}\n" +
$" maximumWalkingSpeed: {maximumWalkingSpeed}"
);
}

{
throw new ArgumentException(
"NaN in movingTowardsDot.\n" +
$" orientationCube.transform.forward: {orientationCube.transform.forward}\n"+
$" orientationCube.transform.forward: {orientationCube.transform.forward}\n" +
$" body.forward: {body.forward}"
);
}

2
Project/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorSettings.cs


// Send stats via SideChannel so that they'll appear in TensorBoard.
// These values get averaged every summary_frequency steps, so we don't
// need to send every Update() call.
if ((Time.frameCount % 100)== 0)
if ((Time.frameCount % 100) == 0)
{
m_Recorder.Add("TotalScore", totalScore);
}

10
Project/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs


// Mask the necessary actions if selected by the user.
if (maskActions)
{
// Prevents the agent from picking an action that would make it collide with a wall
// Prevents the agent from picking an action that would make it collide with a wall
var positionX = (int)transform.position.x;
var positionZ = (int)transform.position.z;
var maxPosition = (int)m_ResetParams.GetWithDefault("gridSize", 5f) - 1;

actionMask.WriteMask(0, new []{ k_Left});
actionMask.WriteMask(0, new[] { k_Left });
actionMask.WriteMask(0, new []{k_Right});
actionMask.WriteMask(0, new[] { k_Right });
actionMask.WriteMask(0, new []{k_Down});
actionMask.WriteMask(0, new[] { k_Down });
actionMask.WriteMask(0, new []{k_Up});
actionMask.WriteMask(0, new[] { k_Up });
}
}
}

2
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/AdjustTrainingTimescale.cs


using UnityEngine;
namespace MLAgentsExamples
namespace MLAgentsExamples
{
public class AdjustTrainingTimescale : MonoBehaviour
{

4
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/DirectionIndicator.cs


using UnityEngine;
using UnityEngine;
public bool updatedByAgent; //should this be updated by the agent? If not, it will use local settings
public Transform transformToFollow; //ex: hips or body
public Transform targetToLookAt; //target in the scene the indicator will point to

14
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/JointDriveController.cs


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

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

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

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

20
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs


/// </summary>
public class ModelOverrider : MonoBehaviour
{
HashSet<string> k_SupportedExtensions = new HashSet<string>{"nn", "onnx"};
HashSet<string> k_SupportedExtensions = new HashSet<string> { "nn", "onnx" };
const string k_CommandLineModelOverrideFlag = "--mlagents-override-model";
const string k_CommandLineModelOverrideDirectoryFlag = "--mlagents-override-model-directory";
const string k_CommandLineModelOverrideExtensionFlag = "--mlagents-override-model-extension";

int TotalCompletedEpisodes
{
get { return m_PreviousAgentCompletedEpisodes + (m_Agent == null ? 0 : m_Agent.CompletedEpisodes); }
get { return m_PreviousAgentCompletedEpisodes + (m_Agent == null ? 0 : m_Agent.CompletedEpisodes); }
}
int TotalNumSteps

public bool HasOverrides
{
get { return m_BehaviorNameOverrides.Count > 0 || !string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory); }
get { return m_BehaviorNameOverrides.Count > 0 || !string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory); }
}
public static string GetOverrideBehaviorName(string originalBehaviorName)

var args = commandLineArgsOverride ?? Environment.GetCommandLineArgs();
for (var i = 0; i < args.Length; i++)
{
if (args[i] == k_CommandLineModelOverrideFlag && i < args.Length-2)
if (args[i] == k_CommandLineModelOverrideFlag && i < args.Length - 2)
else if (args[i] == k_CommandLineModelOverrideDirectoryFlag && i < args.Length-1)
else if (args[i] == k_CommandLineModelOverrideDirectoryFlag && i < args.Length - 1)
else if (args[i] == k_CommandLineModelOverrideExtensionFlag && i < args.Length-1)
else if (args[i] == k_CommandLineModelOverrideExtensionFlag && i < args.Length - 1)
{
m_OverrideExtension = args[i + 1].Trim().ToLower();
var isKnownExtension = k_SupportedExtensions.Contains(m_OverrideExtension);

#endif
}
}
else if (args[i] == k_CommandLineQuitAfterEpisodesFlag && i < args.Length-1)
else if (args[i] == k_CommandLineQuitAfterEpisodesFlag && i < args.Length - 1)
{
Int32.TryParse(args[i + 1], out maxEpisodes);
}

{
assetPath = m_BehaviorNameOverrides[behaviorName];
}
else if(!string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory))
else if (!string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory))
{
assetPath = Path.Combine(m_BehaviorNameOverrideDirectory, $"{behaviorName}.{m_OverrideExtension}");
}

{
model = File.ReadAllBytes(assetPath);
}
catch(IOException)
catch (IOException)
{
Debug.Log($"Couldn't load file {assetPath} at full path {Path.GetFullPath(assetPath)}", this);
// Cache the null so we don't repeatedly try to load a missing file

if (!overrideOk && m_QuitOnLoadFailure)
{
if(!string.IsNullOrEmpty(overrideError))
if (!string.IsNullOrEmpty(overrideError))
{
Debug.LogWarning(overrideError);
}

6
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/Monitor.cs


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

var displayValues = s_DisplayTransformValues[target];
var index = 0;
var orderedKeys = displayValues.Keys.OrderBy(x => - displayValues[x].time);
var orderedKeys = displayValues.Keys.OrderBy(x => -displayValues[x].time);
foreach (var key in orderedKeys)
{
s_KeyStyle.alignment = TextAnchor.MiddleRight;

s_RedStyle = s_ColorStyle[5];
}
}
}
}

2
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/OrientationCubeController.cs


using UnityEngine;
using UnityEngine;
namespace Unity.MLAgentsExamples
{

2
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ProjectSettingsOverrides.cs


m_OriginalMaximumDeltaTime = Time.maximumDeltaTime;
m_OriginalSolverIterations = Physics.defaultSolverIterations;
m_OriginalSolverVelocityIterations = Physics.defaultSolverVelocityIterations;
m_OriginalReuseCollisionCallbacks = Physics.reuseCollisionCallbacks ;
m_OriginalReuseCollisionCallbacks = Physics.reuseCollisionCallbacks;
// Override
Physics.gravity *= gravityMultiplier;

2
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/SensorBase.cs


}
/// <inheritdoc/>
public void Update() {}
public void Update() { }
/// <inheritdoc/>
public void Reset() { }

8
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/TargetController.cs


using UnityEngine;
using UnityEngine;
using Random = UnityEngine.Random;
using Unity.MLAgents;
using UnityEngine.Events;

/// </summary>
public class TargetController : MonoBehaviour
{
[Header("Target Fell Protection")]
public bool respawnIfFallsOffPlatform = true; //If the target falls off the platform, reset the position.
public float fallDistance = 5; //distance below the starting height that will trigger a respawn

6
Project/Assets/ML-Agents/Examples/Startup/Scripts/Startup.cs


// no scene environment variable is found.
var args = Environment.GetCommandLineArgs();
Console.WriteLine("Command line arguments passed: " + String.Join(" ", args));
for (int i = 0; i < args.Length; i++) {
if (args [i] == k_SceneCommandLineFlag && i < args.Length - 1) {
for (int i = 0; i < args.Length; i++)
{
if (args[i] == k_SceneCommandLineFlag && i < args.Length - 1)
{
sceneName = args[i + 1];
}
}

14
Project/Assets/ML-Agents/Examples/Tennis/Scripts/HitWall.cs


public bool net;
public enum FloorHit
{
Service,
FloorHitUnset,
FloorAHit,
FloorBHit
}
{
Service,
FloorHitUnset,
FloorAHit,
FloorBHit
}
public FloorHit lastFloorHit;

lastFloorHit = FloorHit.Service;
net = false;
}
void AgentAWins()
{
m_AgentA.SetReward(1);

12
Project/Assets/ML-Agents/Examples/Worm/Scripts/WormAgent.cs


public bool respawnTargetWhenTouched;
public float targetSpawnRadius;
[Header("Body Parts")] [Space(10)]
[Header("Body Parts")]
[Space(10)]
[Header("Joint Settings")] [Space(10)]
[Header("Joint Settings")]
[Space(10)]
JointDriveController m_JdController;
Vector3 m_DirToTarget;
float m_MovingTowardsDot;

//We want to collect this info because it is the actual rotation, not the "target rotation"
public Quaternion GetJointRotation(ConfigurableJoint joint)
{
return(Quaternion.FromToRotation(joint.axis, joint.connectedBody.transform.rotation.eulerAngles));
return (Quaternion.FromToRotation(joint.axis, joint.connectedBody.transform.rotation.eulerAngles));
}
/// <summary>

float maxDist = 10;
if (Physics.Raycast(bodySegment0.position, Vector3.down, out hit, maxDist))
{
sensor.AddObservation(hit.distance/maxDist);
sensor.AddObservation(hit.distance / maxDist);
}
else
sensor.AddObservation(1);

bpDict[bodySegment2].SetJointStrength(continuousActions[++i]);
bpDict[bodySegment3].SetJointStrength(continuousActions[++i]);
if (bodySegment0.position.y < ground.position.y -2)
if (bodySegment0.position.y < ground.position.y - 2)
{
EndEpisode();
}

2
com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyJointExtractor.cs


}
}
}
#endif
#endif

2
com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodyPoseExtractor.cs


}
}
}
#endif // UNITY_2020_1_OR_NEWER
#endif // UNITY_2020_1_OR_NEWER

2
com.unity.ml-agents.extensions/Runtime/Sensors/ArticulationBodySensorComponent.cs


}
}
#endif // UNITY_2020_1_OR_NEWER
#endif // UNITY_2020_1_OR_NEWER

4
com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsBodySensor.cs


var numJointExtractorObservations = 0;
m_JointExtractors = new List<IJointExtractor>(poseExtractor.NumEnabledPoses);
foreach(var rb in poseExtractor.GetEnabledRigidbodies())
foreach (var rb in poseExtractor.GetEnabledRigidbodies())
{
var jointExtractor = new RigidBodyJointExtractor(rb);
numJointExtractorObservations += jointExtractor.NumObservations(settings);

}
/// <inheritdoc/>
public void Reset() {}
public void Reset() { }
/// <inheritdoc/>
public SensorCompressionType GetCompressionType()

4
com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsSensorSettings.cs


}
}
foreach(var vel in poseExtractor.GetEnabledModelSpaceVelocities())
foreach (var vel in poseExtractor.GetEnabledModelSpaceVelocities())
{
if (settings.UseModelSpaceLinearVelocity)
{

}
}
foreach(var vel in poseExtractor.GetEnabledLocalSpaceVelocities())
foreach (var vel in poseExtractor.GetEnabledLocalSpaceVelocities())
{
if (settings.UseLocalSpaceLinearVelocity)
{

10
com.unity.ml-agents.extensions/Runtime/Sensors/PoseExtractor.cs


var localUp = localPose[i].rotation * Vector3.up;
var localFwd = localPose[i].rotation * Vector3.forward;
var localRight = localPose[i].rotation * Vector3.right;
Debug.DrawLine(current.position+offset, current.position+offset+.1f*localUp, Color.red);
Debug.DrawLine(current.position+offset, current.position+offset+.1f*localFwd, Color.green);
Debug.DrawLine(current.position+offset, current.position+offset+.1f*localRight, Color.blue);
Debug.DrawLine(current.position + offset, current.position + offset + .1f * localUp, Color.red);
Debug.DrawLine(current.position + offset, current.position + offset + .1f * localFwd, Color.green);
Debug.DrawLine(current.position + offset, current.position + offset + .1f * localRight, Color.blue);
}
}

{
// Push to the stack in reverse order
var children = tree[current];
for (var childIdx = children.Count-1; childIdx >= 0; childIdx--)
for (var childIdx = children.Count - 1; childIdx >= 0; childIdx--)
stack.Push((children[childIdx], depth+1));
stack.Push((children[childIdx], depth + 1));
}
}

2
com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyJointExtractor.cs


public static int NumObservations(Rigidbody body, Joint joint, PhysicsSensorSettings settings)
{
if(body == null || joint == null)
if (body == null || joint == null)
{
return 0;
}

4
com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodyPoseExtractor.cs


if (rootGameObject == null)
{
rbs = rootBody.GetComponentsInChildren<Rigidbody>();
joints = rootBody.GetComponentsInChildren <Joint>();
joints = rootBody.GetComponentsInChildren<Joint>();
}
else
{

for (var i = 0; i < rbs.Length; i++)
{
if(rbs[i] != null)
if (rbs[i] != null)
{
bodyToIndex[rbs[i]] = i;
}

4
com.unity.ml-agents.extensions/Runtime/Sensors/RigidBodySensorComponent.cs


/// <summary>
/// Editor component that creates a PhysicsBodySensor for the Agent.
/// </summary>
public class RigidBodySensorComponent : SensorComponent
public class RigidBodySensorComponent : SensorComponent
{
/// <summary>
/// The root Rigidbody of the system.

var numPoseObservations = poseExtractor.GetNumPoseObservations(Settings);
var numJointObservations = 0;
foreach(var rb in poseExtractor.GetEnabledRigidbodies())
foreach (var rb in poseExtractor.GetEnabledRigidbodies())
{
var joint = rb.GetComponent<Joint>();
numJointObservations += RigidBodyJointExtractor.NumObservations(rb, joint, Settings);

2
com.unity.ml-agents.extensions/Tests/Editor/Sensors/ArticulationBodyPoseExtractorTests.cs


}
}
}
#endif
#endif

2
com.unity.ml-agents.extensions/Tests/Editor/Sensors/ArticulationBodySensorTests.cs


}
}
}
#endif // #if UNITY_2020_1_OR_NEWER
#endif // #if UNITY_2020_1_OR_NEWER

10
com.unity.ml-agents.extensions/Tests/Editor/Sensors/PoseExtractorTests.cs


return Pose.identity;
}
protected internal override Vector3 GetLinearVelocityAt(int index)
protected internal override Vector3 GetLinearVelocityAt(int index)
{
return Vector3.zero;
}

}
// Getting a parent index should throw an index exception
Assert.Throws <NullReferenceException>(
Assert.Throws<NullReferenceException>(
() => poseExtractor.GetParentIndex(0)
);

};
}
protected internal override Vector3 GetLinearVelocityAt(int index)
protected internal override Vector3 GetLinearVelocityAt(int index)
{
return Vector3.zero;
}

{
// Degenerate case with a loop
var poseExtractor = new UselessPoseExtractor();
poseExtractor.Init(new[] {-1, 2, 1});
poseExtractor.Init(new[] { -1, 2, 1 });
poseExtractor.Init(new[] {-1, 1});
poseExtractor.Init(new[] { -1, 1 });
// This just shouldn't blow up
poseExtractor.GetDisplayNodes();

2
com.unity.ml-agents.extensions/Tests/Editor/Sensors/RigidBodyPoseExtractorTests.cs


// body 1 has parent 0
Assert.AreEqual(0, poseExtractor.GetParentIndex(1));
var virtualRootPos = new Vector3(0,2,0);
var virtualRootPos = new Vector3(0, 2, 0);
var virtualRootRot = Quaternion.Euler(0, 42, 0);
virtualRoot.transform.position = virtualRootPos;
virtualRoot.transform.rotation = virtualRootRot;

3
com.unity.ml-agents.extensions/Tests/Runtime/RuntimeExampleTest.cs


namespace Unity.MLAgents.Extensions.Tests
{
internal class RuntimeExampleTest {
internal class RuntimeExampleTest
{
[Test]
public void RuntimeTestMath()

1
com.unity.ml-agents/CHANGELOG.md


### Minor Changes
#### com.unity.ml-agents (C#)
- Update Barracuda to 1.0.2.
- Enabled C# formatting using `dotnet-format`.
#### ml-agents / ml-agents-envs / gym-unity (Python)
### Bug Fixes

7
com.unity.ml-agents/CONTRIBUTING.md


### Code style
All python code should be formatted with
[`black`](https://github.com/ambv/black). Style and formatting for C# may be
enforced later.
[`black`](https://github.com/ambv/black).
C# code is formatted using [`dotnet-format`](https://github.com/dotnet/format).
You must have [dotnet](https://dotnet.microsoft.com/download) and
`dotnet-format` installed first.
### Python type annotations

2
com.unity.ml-agents/Editor/DemonstrationImporter.cs


/// <summary>
/// Asset Importer used to parse demonstration files.
/// </summary>
[ScriptedImporter(1, new[] {"demo"})]
[ScriptedImporter(1, new[] { "demo" })]
internal class DemonstrationImporter : ScriptedImporter
{
const string k_IconPath = "Packages/com.unity.ml-agents/Editor/Icons/DemoIcon.png";

16
com.unity.ml-agents/Runtime/Academy.cs


// This try-catch is because DontDestroyOnLoad cannot be used in Editor Tests
GameObject.DontDestroyOnLoad(m_StepperObject);
}
catch {}
catch { }
}
/// <summary>

void ResetActions()
{
DecideAction = () => {};
DestroyAction = () => {};
AgentPreStep = i => {};
AgentSendState = () => {};
AgentAct = () => {};
AgentForceReset = () => {};
OnEnvironmentReset = () => {};
DecideAction = () => { };
DestroyAction = () => { };
AgentPreStep = i => { };
AgentSendState = () => { };
AgentAct = () => { };
AgentForceReset = () => { };
OnEnvironmentReset = () => { };
}
static void OnQuitCommandReceived()

2
com.unity.ml-agents/Runtime/Actuators/ActionSegment.cs


/// </summary>
/// <param name="actionArray">The action array to use for the this segment.</param>
public ActionSegment(T[] actionArray)
: this(actionArray ?? System.Array.Empty<T>(), 0, actionArray?.Length ?? 0) {}
: this(actionArray ?? System.Array.Empty<T>(), 0, actionArray?.Length ?? 0) { }
/// <summary>
/// Construct an <see cref="ActionSegment{T}"/> with an underlying array

2
com.unity.ml-agents/Runtime/Actuators/ActionSpec.cs


{
NumContinuousActions = numContinuousActions;
NumDiscreteActions = numDiscreteActions;
BranchSizes = branchSizes;
BranchSizes = branchSizes;
SumOfDiscreteBranchSizes = branchSizes?.Sum() ?? 0;
}
}

6
com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs


/// <see cref="ActionSegment{T}"/> initialized from a float array.</returns>
public static ActionBuffers FromDiscreteActions(float[] discreteActions)
{
return new ActionBuffers(ActionSegment<float>.Empty, discreteActions == null ? ActionSegment<int>.Empty
: new ActionSegment<int>(Array.ConvertAll(discreteActions,
x => (int)x)));
return new ActionBuffers(ActionSegment<float>.Empty, discreteActions == null ? ActionSegment<int>.Empty
: new ActionSegment<int>(Array.ConvertAll(discreteActions,
x => (int)x)));
}
public ActionBuffers(float[] continuousActions, int[] discreteActions)

2
com.unity.ml-agents/Runtime/Actuators/VectorActuator.cs


public void WriteDiscreteActionMask(IDiscreteActionMask actionMask)
{
m_ActionReceiver.WriteDiscreteActionMask(actionMask);
m_ActionReceiver.WriteDiscreteActionMask(actionMask);
}
/// <summary>

10
com.unity.ml-agents/Runtime/Agent.cs


public int maxStep;
}
[SerializeField][HideInInspector]
[SerializeField]
[HideInInspector]
[SerializeField][HideInInspector]
[SerializeField]
[HideInInspector]
internal bool hasUpgradedFromAgentParameters;
/// <summary>

///
/// [GameObject]: https://docs.unity3d.com/Manual/GameObjects.html
/// </remarks>
public virtual void Initialize() {}
public virtual void Initialize() { }
/// <summary>
/// Implement `Heuristic()` to choose an action for this agent using a custom heuristic.

/// </summary>
/// <seealso cref="Initialize"/>
/// <seealso cref="EndEpisode"/>
public virtual void OnEpisodeBegin() {}
public virtual void OnEpisodeBegin() { }
/// <summary>
/// Gets the last ActionBuffer for this agent.

2
com.unity.ml-agents/Runtime/Agent.deprecated.cs


Array.Clear(actionsOut, 0, actionsOut.Length);
}
public virtual void OnActionReceived(float[] vectorAction) {}
public virtual void OnActionReceived(float[] vectorAction) { }
/// <summary>
/// Returns the last action that was decided on by the Agent.

6
com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs


var agentInfoProto = ai.ToAgentInfoProto();
var agentActionProto = new AgentActionProto();
if(ai.storedVectorActions != null)
if (ai.storedVectorActions != null)
{
agentActionProto.VectorActions.AddRange(ai.storedVectorActions);
}

var brainParametersProto = new BrainParametersProto
{
VectorActionSize = { bp.VectorActionSize },
VectorActionSpaceType = (SpaceTypeProto) bp.VectorActionSpaceType,
VectorActionSpaceType = (SpaceTypeProto)bp.VectorActionSpaceType,
if(bp.VectorActionDescriptions != null)
if (bp.VectorActionDescriptions != null)
{
brainParametersProto.VectorActionDescriptions.AddRange(bp.VectorActionDescriptions);
}

36
com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs


Dictionary<string, BrainParameters> m_UnsentBrainKeys = new Dictionary<string, BrainParameters>();
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
/// The Unity to External client.
UnityToExternalProto.UnityToExternalProtoClient m_Client;
#endif

UnityInputProto Initialize(UnityOutputProto unityOutput,
out UnityInputProto unityInput)
{
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
m_IsOpen = true;
var channel = new Channel(
"localhost:" + m_CommunicatorInitParameters.port,

/// </summary>
public void Dispose()
{
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
if (!m_IsOpen)
{
return;

switch (command)
{
case CommandProto.Quit:
{
QuitCommandReceived?.Invoke();
return;
}
{
QuitCommandReceived?.Invoke();
return;
}
{
foreach (var brainName in m_OrderedAgentsRequestingDecisions.Keys)
m_OrderedAgentsRequestingDecisions[brainName].Clear();
foreach (var brainName in m_OrderedAgentsRequestingDecisions.Keys)
{
m_OrderedAgentsRequestingDecisions[brainName].Clear();
}
ResetCommandReceived?.Invoke();
return;
ResetCommandReceived?.Invoke();
return;
}
{
return;
}
{
return;
}
}
}

/// <param name="sensors">Sensors that will produce the observations</param>
public void PutObservations(string behaviorName, AgentInfo info, List<ISensor> sensors)
{
# if DEBUG
#if DEBUG
if (!m_SensorShapeValidators.ContainsKey(behaviorName))
{
m_SensorShapeValidators[behaviorName] = new SensorShapeValidator();

/// <param name="unityOutput">The UnityOutput to be sent.</param>
UnityInputProto Exchange(UnityOutputProto unityOutput)
{
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
if (!m_IsOpen)
{
return null;

2
com.unity.ml-agents/Runtime/Communicator/UnityRLCapabilities.cs


/// A class holding the capabilities flags for Reinforcement Learning across C# and the Trainer codebase. This
/// struct will be used to inform users if and when they are using C# / Trainer features that are mismatched.
/// </summary>
public UnityRLCapabilities(bool baseRlCapabilities=true)
public UnityRLCapabilities(bool baseRlCapabilities = true)
{
m_BaseRLCapabilities = baseRlCapabilities;
}

20
com.unity.ml-agents/Runtime/Policies/BehaviorParameters.cs


public InferenceDevice InferenceDevice
{
get { return m_InferenceDevice; }
set { m_InferenceDevice = value; UpdateAgentPolicy();}
set { m_InferenceDevice = value; UpdateAgentPolicy(); }
}
[HideInInspector, SerializeField]

case BehaviorType.HeuristicOnly:
return GenerateHeuristicPolicy(heuristic);
case BehaviorType.InferenceOnly:
{
if (m_Model == null)
var behaviorType = BehaviorType.InferenceOnly.ToString();
throw new UnityAgentsException(
$"Can't use Behavior Type {behaviorType} without a model. " +
"Either assign a model, or change to a different Behavior Type."
);
if (m_Model == null)
{
var behaviorType = BehaviorType.InferenceOnly.ToString();
throw new UnityAgentsException(
$"Can't use Behavior Type {behaviorType} without a model. " +
"Either assign a model, or change to a different Behavior Type."
);
}
return new BarracudaPolicy(m_BrainParameters, m_Model, m_InferenceDevice);
return new BarracudaPolicy(m_BrainParameters, m_Model, m_InferenceDevice);
}
case BehaviorType.Default:
if (Academy.Instance.IsCommunicatorOn)
{

2
com.unity.ml-agents/Runtime/Policies/BrainParameters.cs


/// For the discrete action space: the number of branches in the action space.
/// </value>
[FormerlySerializedAs("vectorActionSize")]
public int[] VectorActionSize = new[] {1};
public int[] VectorActionSize = new[] { 1 };
/// <summary>
/// The list of strings describing what the actions correspond to.

2
com.unity.ml-agents/Runtime/Policies/HeuristicPolicy.cs


public float this[int index]
{
get { return 0.0f; }
set {}
set { }
}
}

14
com.unity.ml-agents/Runtime/Sampler.cs


using System.Collections.Generic;
using Unity.MLAgents.Inference.Utils;
using UnityEngine;
using Random=System.Random;
using Random = System.Random;
namespace Unity.MLAgents
{

// Will be used to normalize intervalFuncs
float sumIntervalSizes = 0;
//The number of intervals
int numIntervals = (int)(intervals.Count/2);
int numIntervals = (int)(intervals.Count / 2);
// List that will store interval lengths
float[] intervalSizes = new float[numIntervals];
// List that will store uniform distributions

for(int i = 0; i < numIntervals; i++)
for (int i = 0; i < numIntervals; i++)
{
var min = intervals[2 * i];
var max = intervals[2 * i + 1];

intervalFuncs[i] = () => min + (float)distr.NextDouble() * intervalSize;
}
// Normalize interval lengths
for(int i = 0; i < numIntervals; i++)
for (int i = 0; i < numIntervals; i++)
}
}
for(int i = 1; i < numIntervals; i++)
for (int i = 1; i < numIntervals; i++)
}
}
Multinomial intervalDistr = new Multinomial(seed + 1);
float MultiRange()
{

4
com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs


/// </summary>
public SensorCompressionType CompressionType
{
get { return m_CompressionType; }
get { return m_CompressionType; }
set { m_CompressionType = value; }
}

}
/// <inheritdoc/>
public void Update() {}
public void Update() { }
/// <inheritdoc/>
public void Reset() { }

14
com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs


/// </summary>
public Camera Camera
{
get { return m_Camera; }
get { return m_Camera; }
set { m_Camera = value; UpdateSensor(); }
}

/// </summary>
public string SensorName
{
get { return m_SensorName; }
get { return m_SensorName; }
set { m_SensorName = value; }
}

/// </summary>
public int Width
{
get { return m_Width; }
get { return m_Width; }
set { m_Width = value; }
}

/// </summary>
public int Height
{
get { return m_Height; }
set { m_Height = value; }
get { return m_Height; }
set { m_Height = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("grayscale")]

/// </summary>
public bool Grayscale
{
get { return m_Grayscale; }
get { return m_Grayscale; }
set { m_Grayscale = value; }
}

/// </summary>
public SensorCompressionType CompressionType
{
get { return m_Compression; }
get { return m_Compression; }
set { m_Compression = value; UpdateSensor(); }
}

2
com.unity.ml-agents/Runtime/Sensors/ObservationWriter.cs


TensorShape m_TensorShape;
internal ObservationWriter() {}
internal ObservationWriter() { }
/// <summary>
/// Set the writer to write to an IList at the given channelOffset.

4
com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs


var startPositionWorld = Transform.TransformPoint(startPositionLocal);
var endPositionWorld = Transform.TransformPoint(endPositionLocal);
return (StartPositionWorld : startPositionWorld, EndPositionWorld : endPositionWorld);
return (StartPositionWorld: startPositionWorld, EndPositionWorld: endPositionWorld);
}
/// <summary>

}
/// <inheritdoc/>
public void Reset() {}
public void Reset() { }
/// <inheritdoc/>
public int[] GetObservationShape()

2
com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensorComponentBase.cs


{
get { return m_RaysPerDirection; }
// Note: can't change at runtime
set { m_RaysPerDirection = value;}
set { m_RaysPerDirection = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("maxRayDegrees")]

2
com.unity.ml-agents/Runtime/Sensors/Reflection/BoolReflectionSensor.cs


{
public BoolReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 1)
{}
{ }
internal override void WriteReflectedField(ObservationWriter writer)
{

2
com.unity.ml-agents/Runtime/Sensors/Reflection/FloatReflectionSensor.cs


{
public FloatReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 1)
{}
{ }
internal override void WriteReflectedField(ObservationWriter writer)
{

2
com.unity.ml-agents/Runtime/Sensors/Reflection/IntReflectionSensor.cs


{
public IntReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 1)
{}
{ }
internal override void WriteReflectedField(ObservationWriter writer)
{

10
com.unity.ml-agents/Runtime/Sensors/Reflection/ObservableAttribute.cs


internal static List<ISensor> CreateObservableSensors(object o, bool excludeInherited)
{
var sensorsOut = new List<ISensor>();
foreach (var(field, attr) in GetObservableFields(o, excludeInherited))
foreach (var (field, attr) in GetObservableFields(o, excludeInherited))
{
var sensor = CreateReflectionSensor(o, field, null, attr);
if (sensor != null)

}
foreach (var(prop, attr) in GetObservableProperties(o, excludeInherited))
foreach (var (prop, attr) in GetObservableProperties(o, excludeInherited))
{
if (!prop.CanRead)
{

else
{
var (_, sensorType) = s_TypeToSensorInfo[memberType];
sensor = (ISensor) Activator.CreateInstance(sensorType, reflectionSensorInfo);
sensor = (ISensor)Activator.CreateInstance(sensorType, reflectionSensorInfo);
}
// Wrap the base sensor in a StackingSensor if we're using stacking.

internal static int GetTotalObservationSize(object o, bool excludeInherited, List<string> errorsOut)
{
int sizeOut = 0;
foreach (var(field, attr) in GetObservableFields(o, excludeInherited))
foreach (var (field, attr) in GetObservableFields(o, excludeInherited))
{
if (s_TypeToSensorInfo.ContainsKey(field.FieldType))
{

}
}
foreach (var(prop, attr) in GetObservableProperties(o, excludeInherited))
foreach (var (prop, attr) in GetObservableProperties(o, excludeInherited))
{
if (!prop.CanRead)
{

2
com.unity.ml-agents/Runtime/Sensors/Reflection/QuaternionReflectionSensor.cs


{
public QuaternionReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 4)
{}
{ }
internal override void WriteReflectedField(ObservationWriter writer)
{

6
com.unity.ml-agents/Runtime/Sensors/Reflection/ReflectionSensorBase.cs


m_PropertyInfo = reflectionSensorInfo.PropertyInfo;
m_ObservableAttribute = reflectionSensorInfo.ObservableAttribute;
m_SensorName = reflectionSensorInfo.SensorName;
m_Shape = new[] {size};
m_Shape = new[] { size };
}
/// <inheritdoc/>

}
/// <inheritdoc/>
public void Update() {}
public void Update() { }
public void Reset() {}
public void Reset() { }
/// <inheritdoc/>
public SensorCompressionType GetCompressionType()

2
com.unity.ml-agents/Runtime/Sensors/Reflection/Vector2ReflectionSensor.cs


{
public Vector2ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 2)
{}
{ }
internal override void WriteReflectedField(ObservationWriter writer)
{

2
com.unity.ml-agents/Runtime/Sensors/Reflection/Vector3ReflectionSensor.cs


{
public Vector3ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 3)
{}
{ }
internal override void WriteReflectedField(ObservationWriter writer)
{

2
com.unity.ml-agents/Runtime/Sensors/Reflection/Vector4ReflectionSensor.cs


{
public Vector4ReflectionSensor(ReflectionSensorInfo reflectionSensorInfo)
: base(reflectionSensorInfo, 4)
{}
{ }
internal override void WriteReflectedField(ObservationWriter writer)
{

6
com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs


/// </summary>
public SensorCompressionType CompressionType
{
get { return m_CompressionType; }
get { return m_CompressionType; }
set { m_CompressionType = value; }
}

}
/// <inheritdoc/>
public void Update() {}
public void Update() { }
public void Reset() {}
public void Reset() { }
/// <inheritdoc/>
public SensorCompressionType GetCompressionType()

10
com.unity.ml-agents/Runtime/Sensors/RenderTextureSensorComponent.cs


/// </summary>
public RenderTexture RenderTexture
{
get { return m_RenderTexture; }
set { m_RenderTexture = value; }
get { return m_RenderTexture; }
set { m_RenderTexture = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("sensorName")]

/// </summary>
public string SensorName
{
get { return m_SensorName; }
get { return m_SensorName; }
set { m_SensorName = value; }
}

/// </summary>
public bool Grayscale
{
get { return m_Grayscale; }
get { return m_Grayscale; }
set { m_Grayscale = value; }
}

/// </summary>
public SensorCompressionType CompressionType
{
get { return m_Compression; }
get { return m_Compression; }
set { m_Compression = value; UpdateSensor(); }
}

3
com.unity.ml-agents/Runtime/SideChannels/EnvironmentParametersChannel.cs


IList<float> intervals = msg.ReadFloatList();
sampler = SamplerFactory.CreateMultiRangeUniformSampler(intervals, seed);
}
else{
else
{
Debug.LogWarning("EnvironmentParametersChannel received an unknown data type.");
}
m_Parameters[key] = sampler;

8
com.unity.ml-agents/Runtime/Timer.cs


public double TotalSeconds
{
get { return CurrentTicks * s_TicksToSeconds; }
set {} // Serialization needs this, but unused.
set { } // Serialization needs this, but unused.
}
/// <summary>

var selfTicks = Mathf.Max(0, CurrentTicks - totalChildTicks);
return selfTicks * s_TicksToSeconds;
}
set {} // Serialization needs this, but unused.
set { } // Serialization needs this, but unused.
}
public IReadOnlyDictionary<string, TimerNode> Children

[DataMember(Name = "gauges", EmitDefaultValue = false)]
Dictionary<string, GaugeNode> m_Gauges = new Dictionary<string, GaugeNode>();
public RootNode(string name="root") : base(name, true)
public RootNode(string name = "root") : base(name, true)
{
m_Metadata.Add("timer_format_version", k_TimerFormatVersion);
m_Metadata.Add("start_time_seconds", $"{DateTimeOffset.Now.ToUnixTimeSeconds()}");

/// <param name="filename"></param>
public void SaveJsonTimers(string filename = null)
{
# if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX
try
{
if (filename == null)

42
com.unity.ml-agents/Tests/Editor/Actuators/ActuatorDiscreteActionMaskTests.cs


[Test]
public void FirstBranchMask()
{
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {4, 5, 6}), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] {actuator1}, 15, 3);
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 4, 5, 6 }), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] { actuator1 }, 15, 3);
masker.WriteMask(0, new[] {1, 2, 3});
masker.WriteMask(0, new[] { 1, 2, 3 });
mask = masker.GetMask();
Assert.IsFalse(mask[0]);
Assert.IsTrue(mask[1]);

[Test]
public void SecondBranchMask()
{
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {4, 5, 6}), "actuator1");
var masker = new ActuatorDiscreteActionMask(new[] {actuator1}, 15, 3);
masker.WriteMask(1, new[] {1, 2, 3});
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 4, 5, 6 }), "actuator1");
var masker = new ActuatorDiscreteActionMask(new[] { actuator1 }, 15, 3);
masker.WriteMask(1, new[] { 1, 2, 3 });
var mask = masker.GetMask();
Assert.IsFalse(mask[0]);
Assert.IsFalse(mask[4]);

[Test]
public void MaskReset()
{
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {4, 5, 6}), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] {actuator1}, 15, 3);
masker.WriteMask(1, new[] {1, 2, 3});
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 4, 5, 6 }), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] { actuator1 }, 15, 3);
masker.WriteMask(1, new[] { 1, 2, 3 });
masker.ResetMask();
var mask = masker.GetMask();
for (var i = 0; i < 15; i++)

[Test]
public void ThrowsError()
{
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {4, 5, 6}), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] {actuator1}, 15, 3);
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 4, 5, 6 }), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] { actuator1 }, 15, 3);
() => masker.WriteMask(0, new[] {5}));
() => masker.WriteMask(0, new[] { 5 }));
() => masker.WriteMask(1, new[] {5}));
masker.WriteMask(2, new[] {5});
() => masker.WriteMask(1, new[] { 5 }));
masker.WriteMask(2, new[] { 5 });
() => masker.WriteMask(3, new[] {1}));
() => masker.WriteMask(3, new[] { 1 }));
masker.WriteMask(0, new[] {0, 1, 2, 3});
masker.WriteMask(0, new[] { 0, 1, 2, 3 });
Assert.Catch<UnityAgentsException>(
() => masker.GetMask());
}

{
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {4, 5, 6}), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] {actuator1}, 15, 3);
masker.WriteMask(0, new[] {0, 1});
masker.WriteMask(0, new[] {3});
masker.WriteMask(2, new[] {1});
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 4, 5, 6 }), "actuator1");
var masker = new ActuatorDiscreteActionMask(new IActuator[] { actuator1 }, 15, 3);
masker.WriteMask(0, new[] { 0, 1 });
masker.WriteMask(0, new[] { 3 });
masker.WriteMask(2, new[] { 1 });
var mask = masker.GetMask();
for (var i = 0; i < 15; i++)
{

42
com.unity.ml-agents/Tests/Editor/Actuators/ActuatorManagerTests.cs


public void TestEnsureBufferDiscrete()
{
var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1 , 2, 3, 4}), "actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 1, 1}), "actuator2");
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3, 4 }), "actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 1, 1 }), "actuator2");
manager.Add(actuator1);
manager.Add(actuator2);
var actuator1ActionSpaceDef = actuator1.ActionSpec;

actuator1ActionSpaceDef.NumDiscreteActions + actuator2ActionSpaceDef.NumDiscreteActions);
manager.UpdateActions(new ActionBuffers(Array.Empty<float>(),
new[] { 0, 1, 2, 3, 4, 5, 6}));
new[] { 0, 1, 2, 3, 4, 5, 6 }));
Assert.IsTrue(0 == manager.NumContinuousActions);
Assert.IsTrue(7 == manager.NumDiscreteActions);

public void TestFailOnMixedActionSpace()
{
var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1 , 2, 3, 4}), "actuator1");
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3, 4 }), "actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeContinuous(3), "actuator2");
manager.Add(actuator1);
manager.Add(actuator2);

public void TestExecuteActionsDiscrete()
{
var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1 , 2, 3, 4}), "actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 1, 1}), "actuator2");
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3, 4 }), "actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 1, 1 }), "actuator2");
var discreteActionBuffer = new[] { 0, 1, 2, 3, 4, 5, 6};
var discreteActionBuffer = new[] { 0, 1, 2, 3, 4, 5, 6 };
manager.UpdateActions(new ActionBuffers(Array.Empty<float>(),
discreteActionBuffer));

manager.Add(actuator1);
manager.Add(actuator2);
var continuousActionBuffer = new[] { 0f, 1f, 2f, 3f, 4f, 5f};
var continuousActionBuffer = new[] { 0f, 1f, 2f, 3f, 4f, 5f };
manager.UpdateActions(new ActionBuffers(continuousActionBuffer,
Array.Empty<int>()));

var actuator2 = new TestActuator(ActionSpec.MakeContinuous(3), "actuator2");
manager.Add(actuator1);
manager.Add(actuator2);
var continuousActionBuffer = new[] { 0f, 1f, 2f, 3f, 4f, 5f};
var continuousActionBuffer = new[] { 0f, 1f, 2f, 3f, 4f, 5f };
manager.UpdateActions(new ActionBuffers(continuousActionBuffer,
Array.Empty<int>()));

var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }),
"actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 2, 3}), "actuator2");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }), "actuator2");
var discreteActionBuffer = new[] { 0, 1, 2, 3, 4, 5};
var discreteActionBuffer = new[] { 0, 1, 2, 3, 4, 5 };
manager.UpdateActions(new ActionBuffers(Array.Empty<float>(),
discreteActionBuffer));

var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }),
"actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 2, 3}), "actuator2");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }), "actuator2");
manager.Add(actuator1);
manager.Add(actuator2);

var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }),
"actuator1");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 2, 3}), "actuator2");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }), "actuator2");
manager.Add(actuator1);
manager.Add(actuator2);

public void TestIndexSet()
{
var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3, 4}),
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3, 4 }),
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 2, 3}), "actuator2");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }), "actuator2");
manager.Add(actuator1);
Assert.IsTrue(manager.NumDiscreteActions == 4);
Assert.IsTrue(manager.SumOfDiscreteBranchSizes == 10);

public void TestInsert()
{
var manager = new ActuatorManager();
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3, 4}),
var actuator1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3, 4 }),
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 2, 3}), "actuator2");
var actuator2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }), "actuator2");
manager.Add(actuator1);
Assert.IsTrue(manager.NumDiscreteActions == 4);
Assert.IsTrue(manager.SumOfDiscreteBranchSizes == 10);

var actuator2 = new TestActuator(ActionSpec.MakeContinuous(3), "actuator2");
manager.Add(actuator1);
manager.Add(actuator2);
var continuousActionBuffer = new[] { 0f, 1f, 2f, 3f, 4f, 5f};
var continuousActionBuffer = new[] { 0f, 1f, 2f, 3f, 4f, 5f };
manager.UpdateActions(new ActionBuffers(continuousActionBuffer,
Array.Empty<int>()));

Assert.IsTrue(manager.StoredActions.ContinuousActions.SequenceEqual(new[] { 0f, 0f, 0f, 0f, 0f, 0f}));
Assert.IsTrue(manager.StoredActions.ContinuousActions.SequenceEqual(new[] { 0f, 0f, 0f, 0f, 0f, 0f }));
}
[Test]

var va1 = new TestActuator(ActionSpec.MakeDiscrete(new[] {1, 2, 3}), "name");
var va2 = new TestActuator(ActionSpec.MakeDiscrete(new[] {3, 2, 1}), "name1");
var va1 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 1, 2, 3 }), "name");
var va2 = new TestActuator(ActionSpec.MakeDiscrete(new[] { 3, 2, 1 }), "name1");
manager.Add(va1);
manager.Add(va2);

12
com.unity.ml-agents/Tests/Editor/Actuators/VectorActuatorTests.cs


public void TestConstruct()
{
var ar = new TestActionReceiver();
var va = new VectorActuator(ar, new[] {1, 2, 3}, SpaceType.Discrete, "name");
var va = new VectorActuator(ar, new[] { 1, 2, 3 }, SpaceType.Discrete, "name");
var va1 = new VectorActuator(ar, new[] {4}, SpaceType.Continuous, "name");
var va1 = new VectorActuator(ar, new[] { 4 }, SpaceType.Continuous, "name");
Assert.IsTrue(va1.ActionSpec.NumContinuousActions == 4);
Assert.IsTrue(va1.ActionSpec.SumOfDiscreteBranchSizes == 0);

public void TestOnActionReceived()
{
var ar = new TestActionReceiver();
var va = new VectorActuator(ar, new[] {1, 2, 3}, SpaceType.Discrete, "name");
var va = new VectorActuator(ar, new[] { 1, 2, 3 }, SpaceType.Discrete, "name");
var discreteActions = new[] { 0, 1, 1 };
var ab = new ActionBuffers(ActionSegment<float>.Empty,

public void TestResetData()
{
var ar = new TestActionReceiver();
var va = new VectorActuator(ar, new[] {1, 2, 3}, SpaceType.Discrete, "name");
var va = new VectorActuator(ar, new[] { 1, 2, 3 }, SpaceType.Discrete, "name");
var discreteActions = new[] { 0, 1, 1 };
var ab = new ActionBuffers(ActionSegment<float>.Empty,

public void TestWriteDiscreteActionMask()
{
var ar = new TestActionReceiver();
var va = new VectorActuator(ar, new[] {1, 2, 3}, SpaceType.Discrete, "name");
var bdam = new ActuatorDiscreteActionMask(new[] {va}, 6, 3);
var va = new VectorActuator(ar, new[] { 1, 2, 3 }, SpaceType.Discrete, "name");
var bdam = new ActuatorDiscreteActionMask(new[] { va }, 6, 3);
var groundTruthMask = new[] { false, true, false, false, true, true };

4
com.unity.ml-agents/Tests/Editor/DemonstrationTests.cs


var agentSendInfo = typeof(Agent).GetMethod("SendInfo",
BindingFlags.Instance | BindingFlags.NonPublic);
agentEnableMethod?.Invoke(agent1, new object[] {});
agentEnableMethod?.Invoke(agent1, new object[] { });
agentSendInfo?.Invoke(agent1, new object[] {});
agentSendInfo?.Invoke(agent1, new object[] { });
demoRecorder.Close();

8
com.unity.ml-agents/Tests/Editor/DiscreteActionOutputApplierTest.cs


var src = new TensorProxy
{
data = new Tensor(1, 3, new[] {0.1f, 0.2f, 0.7f}),
data = new Tensor(1, 3, new[] { 0.1f, 0.2f, 0.7f }),
valueType = TensorProxy.TensorType.FloatingPoint
};

DiscreteActionOutputApplier.Eval(src, dst, m);
float[] reference = {2, 2, 1};
float[] reference = { 2, 2, 1 };
for (var i = 0; i < dst.data.length; i++)
{
Assert.AreEqual(reference[i], dst.data[i]);

DiscreteActionOutputApplier.Eval(src, dst, m);
float[] reference = {2, 2, 2};
float[] reference = { 2, 2, 2 };
for (var i = 0; i < dst.data.length; i++)
{
Assert.AreEqual(reference[i], dst.data[i]);

DiscreteActionOutputApplier.Eval(src, dst, m);
float[] reference = {2, 2, 2, 0, 1, 0};
float[] reference = { 2, 2, 2, 0, 1, 0 };
for (var i = 0; i < dst.data.length; i++)
{
Assert.AreEqual(reference[i], dst.data[i]);

12
com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs


public ref readonly ActionBuffers DecideAction() { return ref s_EmptyActionBuffers; }
public void Dispose() {}
public void Dispose() { }
}
public class TestAgent : Agent

return sensorName;
}
public void Update() {}
public void Update() { }
public void Reset()
{

StackingSensor sensor = null;
foreach (ISensor s in agent1.sensors)
{
if (s is StackingSensor)
if (s is StackingSensor)
{
sensor = s as StackingSensor;
}

aca.EnvironmentStep();
}
policy.OnRequestDecision = () => SensorTestHelper.CompareObservation(sensor, new[] {18f, 19f, 21f});
policy.OnRequestDecision = () => SensorTestHelper.CompareObservation(sensor, new[] { 18f, 19f, 21f });
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 0f});
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f });
}
}

(ObservableAttributeOptions.ExamineAll, 2)
};
foreach (var(behavior, expectedNumSensors) in variants)
foreach (var (behavior, expectedNumSensors) in variants)
{
var go = new GameObject();
var agent = go.AddComponent<DerivedObservableAgent>();

8
com.unity.ml-agents/Tests/Editor/ModelRunnerTest.cs


{
var validBrainParameters = new BrainParameters();
validBrainParameters.VectorObservationSize = 8;
validBrainParameters.VectorActionSize = new [] { 2 };
validBrainParameters.VectorActionSize = new[] { 2 };
validBrainParameters.NumStackedVectorObservations = 1;
validBrainParameters.VectorActionSpaceType = SpaceType.Continuous;
return validBrainParameters;

{
var validBrainParameters = new BrainParameters();
validBrainParameters.VectorObservationSize = 0;
validBrainParameters.VectorActionSize = new [] { 2, 3 };
validBrainParameters.VectorActionSize = new[] { 2, 3 };
validBrainParameters.NumStackedVectorObservations = 1;
validBrainParameters.VectorActionSpaceType = SpaceType.Discrete;
return validBrainParameters;

var modelRunner = new ModelRunner(discrete1vis0vec_2_3action_recurrModel, brainParameters);
var info1 = new AgentInfo();
info1.episodeId = 1;
modelRunner.PutObservations(info1, new [] { sensor_21_20_3.CreateSensor() }.ToList());
modelRunner.PutObservations(info1, new[] { sensor_21_20_3.CreateSensor() }.ToList());
modelRunner.PutObservations(info2, new [] { sensor_21_20_3.CreateSensor() }.ToList());
modelRunner.PutObservations(info2, new[] { sensor_21_20_3.CreateSensor() }.ToList());
modelRunner.DecideBatch();

8
com.unity.ml-agents/Tests/Editor/MultinomialTest.cs


public void TestDim1()
{
var m = new Multinomial(2018);
var cdf = new[] {1f};
var cdf = new[] { 1f };
Assert.AreEqual(0, m.Sample(cdf));
Assert.AreEqual(0, m.Sample(cdf));

public void TestDim1Unscaled()
{
var m = new Multinomial(2018);
var cdf = new[] {0.1f};
var cdf = new[] { 0.1f };
Assert.AreEqual(0, m.Sample(cdf));
Assert.AreEqual(0, m.Sample(cdf));

public void TestDim3()
{
var m = new Multinomial(2018);
var cdf = new[] {0.1f, 0.3f, 1.0f};
var cdf = new[] { 0.1f, 0.3f, 1.0f };
Assert.AreEqual(2, m.Sample(cdf));
Assert.AreEqual(2, m.Sample(cdf));

public void TestDim3Unscaled()
{
var m = new Multinomial(2018);
var cdf = new[] {0.05f, 0.15f, 0.5f};
var cdf = new[] { 0.05f, 0.15f, 0.5f };
Assert.AreEqual(2, m.Sample(cdf));
Assert.AreEqual(2, m.Sample(cdf));

12
com.unity.ml-agents/Tests/Editor/ParameterLoaderTest.cs


public int[] GetObservationShape()
{
return new [] {m_Height, m_Width, m_Channels };
return new[] { m_Height, m_Width, m_Channels };
}
public int Write(ObservationWriter writer)

return new byte[0];
}
public void Update() {}
public void Update() { }
public void Reset() { }
public SensorCompressionType GetCompressionType()

{
var validBrainParameters = new BrainParameters();
validBrainParameters.VectorObservationSize = 8;
validBrainParameters.VectorActionSize = new [] { 2 };
validBrainParameters.VectorActionSize = new[] { 2 };
validBrainParameters.NumStackedVectorObservations = 1;
validBrainParameters.VectorActionSpaceType = SpaceType.Continuous;
return validBrainParameters;

{
var validBrainParameters = new BrainParameters();
validBrainParameters.VectorObservationSize = 0;
validBrainParameters.VectorActionSize = new [] { 2, 3 };
validBrainParameters.VectorActionSize = new[] { 2, 3 };
validBrainParameters.NumStackedVectorObservations = 1;
validBrainParameters.VectorActionSpaceType = SpaceType.Discrete;
return validBrainParameters;

var model = ModelLoader.Load(continuous2vis8vec2actionModel);
var brainParameters = GetContinuous2vis8vec2actionBrainParameters();
brainParameters.VectorActionSize = new [] { 3 }; // Invalid action
brainParameters.VectorActionSize = new[] { 3 }; // Invalid action
var errors = BarracudaModelParamLoader.CheckModel(model, brainParameters, new SensorComponent[] { sensor_21_20_3, sensor_20_22_3 });
Assert.Greater(errors.Count(), 0);

var model = ModelLoader.Load(discrete1vis0vec_2_3action_recurrModel);
var brainParameters = GetDiscrete1vis0vec_2_3action_recurrModelBrainParameters();
brainParameters.VectorActionSize = new [] { 3, 3 }; // Invalid action
brainParameters.VectorActionSize = new[] { 3, 3 }; // Invalid action
var errors = BarracudaModelParamLoader.CheckModel(model, brainParameters, new SensorComponent[] { sensor_21_20_3 });
Assert.Greater(errors.Count(), 0);

4
com.unity.ml-agents/Tests/Editor/SamplerTests.cs


const int k_Seed = 1337;
const double k_Epsilon = 0.0001;
EnvironmentParametersChannel m_Channel;
public SamplerTests()
{
m_Channel = SideChannelManager.GetSideChannel<EnvironmentParametersChannel>();

binaryWriter.Write(message.Length);
binaryWriter.Write(message);
}
return memStream.ToArray();
return memStream.ToArray();
}
}
}

10
com.unity.ml-agents/Tests/Editor/Sensor/CameraSensorTest.cs


using System;
using NUnit.Framework;
using UnityEngine;
using Unity.MLAgents.Sensors;
using System;
using NUnit.Framework;
using UnityEngine;
using Unity.MLAgents.Sensors;
namespace Unity.MLAgents.Tests
{

var obsWriter = new ObservationWriter();
var obs = sensor.GetObservationProto(obsWriter);
Assert.AreEqual((int) compression, (int) obs.CompressionType);
Assert.AreEqual((int)compression, (int)obs.CompressionType);
var expectedShape = new[] { height, width, grayscale ? 1 : 3 };
Assert.AreEqual(expectedShape, obs.Shape);
}

2
com.unity.ml-agents/Tests/Editor/Sensor/FloatVisualSensorTests.cs


}
}
public void Update() {}
public void Update() { }
public void Reset() { }
public SensorCompressionType GetCompressionType()

2
com.unity.ml-agents/Tests/Editor/Sensor/ObservableAttributeTests.cs


Assert.AreEqual("derived", sensorsDerivedOnly[0].GetName());
var b = new BaseClass();
var baseSensors = ObservableAttribute.CreateObservableSensors(b, false);
var baseSensors = ObservableAttribute.CreateObservableSensors(b, false);
Assert.AreEqual(2, baseSensors.Count);
}
}

6
com.unity.ml-agents/Tests/Editor/Sensor/ObservationWriterTests.cs


// AddRange
writer.SetTarget(buffer, shape, 0);
writer.AddRange(new[] {4f, 5f});
writer.AddRange(new[] { 4f, 5f });
writer.AddRange(new[] {6f, 7f});
writer.AddRange(new[] { 6f, 7f });
Assert.AreEqual(new[] { 4f, 6f, 7f }, buffer);
}

};
writer.SetTarget(t, 1, 1);
writer.AddRange(new[] {-1f, -2f});
writer.AddRange(new[] { -1f, -2f });
Assert.AreEqual(0f, t.data[0, 0]);
Assert.AreEqual(0f, t.data[0, 1]);
Assert.AreEqual(0f, t.data[0, 2]);

2
com.unity.ml-agents/Tests/Editor/Sensor/SensorShapeValidatorTests.cs


public DummySensor(int dim1, int dim2, int dim3)
{
m_Shape = new[] { dim1, dim2, dim3};
m_Shape = new[] { dim1, dim2, dim3 };
}
public string GetName()

36
com.unity.ml-agents/Tests/Editor/Sensor/StackingSensorTests.cs


ISensor wrapped = new VectorSensor(4);
ISensor sensor = new StackingSensor(wrapped, 4);
Assert.AreEqual("StackingSensor_size4_VectorSensor_size4", sensor.GetName());
Assert.AreEqual(sensor.GetObservationShape(), new[] {16});
Assert.AreEqual(sensor.GetObservationShape(), new[] { 16 });
}
[Test]

ISensor sensor = new StackingSensor(wrapped, 3);
wrapped.AddObservation(new[] {1f, 2f});
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 0f, 0f, 1f, 2f});
wrapped.AddObservation(new[] { 1f, 2f });
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f, 1f, 2f });
wrapped.AddObservation(new[] {3f, 4f});
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 1f, 2f, 3f, 4f});
wrapped.AddObservation(new[] { 3f, 4f });
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 2f, 3f, 4f });
wrapped.AddObservation(new[] {5f, 6f});
SensorTestHelper.CompareObservation(sensor, new[] {1f, 2f, 3f, 4f, 5f, 6f});
wrapped.AddObservation(new[] { 5f, 6f });
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f, 5f, 6f });
wrapped.AddObservation(new[] {7f, 8f});
SensorTestHelper.CompareObservation(sensor, new[] {3f, 4f, 5f, 6f, 7f, 8f});
wrapped.AddObservation(new[] { 7f, 8f });
SensorTestHelper.CompareObservation(sensor, new[] { 3f, 4f, 5f, 6f, 7f, 8f });
wrapped.AddObservation(new[] {9f, 10f});
SensorTestHelper.CompareObservation(sensor, new[] {5f, 6f, 7f, 8f, 9f, 10f});
wrapped.AddObservation(new[] { 9f, 10f });
SensorTestHelper.CompareObservation(sensor, new[] { 5f, 6f, 7f, 8f, 9f, 10f });
SensorTestHelper.CompareObservation(sensor, new[] {5f, 6f, 7f, 8f, 9f, 10f});
SensorTestHelper.CompareObservation(sensor, new[] { 5f, 6f, 7f, 8f, 9f, 10f });
}
[Test]

ISensor sensor = new StackingSensor(wrapped, 3);
wrapped.AddObservation(new[] {1f, 2f});
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 0f, 0f, 1f, 2f});
wrapped.AddObservation(new[] { 1f, 2f });
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f, 1f, 2f });
wrapped.AddObservation(new[] {3f, 4f});
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 1f, 2f, 3f, 4f});
wrapped.AddObservation(new[] { 3f, 4f });
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 2f, 3f, 4f });
wrapped.AddObservation(new[] {5f, 6f});
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 0f, 0f, 5f, 6f});
wrapped.AddObservation(new[] { 5f, 6f });
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f, 5f, 6f });
}
}
}

22
com.unity.ml-agents/Tests/Editor/Sensor/VectorSensorTests.cs


{
var sensor = new VectorSensor(1);
sensor.AddObservation(1.2f);
SensorTestHelper.CompareObservation(sensor, new[] {1.2f});
SensorTestHelper.CompareObservation(sensor, new[] { 1.2f });
}
[Test]

sensor.AddObservation(42);
SensorTestHelper.CompareObservation(sensor, new[] {42f});
SensorTestHelper.CompareObservation(sensor, new[] { 42f });
}
[Test]

sensor.AddObservation(new Vector3(1, 2, 3));
SensorTestHelper.CompareObservation(sensor, new[] {1f, 2f, 3f});
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f });
sensor = new VectorSensor(2);
sensor.AddObservation(new Vector2(4, 5));

{
var sensor = new VectorSensor(4);
sensor.AddObservation(Quaternion.identity);
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 0f, 1f});
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 1f });
}
[Test]

sensor.AddObservation(new[] {1f, 2f, 3f, 4f});
sensor.AddObservation(new[] { 1f, 2f, 3f, 4f });
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f });
}

{
var sensor = new VectorSensor(1);
sensor.AddObservation(true);
SensorTestHelper.CompareObservation(sensor, new[] {1f});
SensorTestHelper.CompareObservation(sensor, new[] { 1f });
}
[Test]

sensor.AddOneHotObservation(2, 4);
SensorTestHelper.CompareObservation(sensor, new[] {0f, 0f, 1f, 0f});
SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 0f });
}
[Test]

sensor.AddObservation(new[] {1f, 2f, 3f, 4f});
sensor.AddObservation(new[] { 1f, 2f, 3f, 4f });
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f});
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f });
}
[Test]

sensor.AddObservation(new[] {1f, 2f});
sensor.AddObservation(new[] { 1f, 2f });
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 0f, 0f});
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 0f, 0f });
}
}
}

14
com.unity.ml-agents/Tests/Editor/UtilitiesTests.cs


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

24
utils/run_dotnet_format.py


#!/usr/bin/env python3
import argparse
import os
import subprocess
import tempfile
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="*")
args = parser.parse_args()
with tempfile.TemporaryDirectory() as tempdir:
# Could potentially hit the commandline limit, so write files to a response file
# See https://github.com/dotnet/format/issues/699
resp_file = os.path.join(tempdir, "response.txt")
with open(resp_file, "w") as fp:
for f in args.files:
fp.write(f + "\n")
subprocess_args = ["dotnet", "format", "--folder", "--include", f"@{resp_file}"]
subprocess.check_call(subprocess_args)

/com.unity.ml-agents/.editorconfig → /.editorconfig

正在加载...
取消
保存