浏览代码

Develop observation collector (#3352)

* Add the VectorSensor to the CollectObservation call

* Example of API change for BalanceBall

* Modified the Examples

* Changes to the migrating doc

* Editing the docs

* Update docs/Learning-Environment-Design-Agents.md

Co-Authored-By: Chris Elion <chris.elion@unity3d.com>

* Update docs/Migrating.md

Co-Authored-By: Chris Elion <chris.elion@unity3d.com>

* Update docs/Migrating.md

Co-Authored-By: Chris Elion <chris.elion@unity3d.com>

* Update docs/Getting-Started-with-Balance-Ball.md

Co-Authored-By: Chris Elion <chris.elion@unity3d.com>

* addressing comments

* Removed the MLAgents.Sensor namespace

* Removing the MLAgents.Sensor namespace from the tests

* Editing the migrating docs

Co-authored-by: Chris Elion <celion@gmail.com>
/asymm-envs
GitHub 5 年前
当前提交
386ba66c
共有 60 个文件被更改,包括 162 次插入255 次删除
  1. 10
      Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs
  2. 8
      Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs
  3. 4
      Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
  4. 6
      Project/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs
  5. 30
      Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs
  6. 10
      Project/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorAgent.cs
  7. 2
      Project/Assets/ML-Agents/Examples/GridWorld/Scripts/GridAgent.cs
  8. 4
      Project/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs
  9. 6
      Project/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs
  10. 24
      Project/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs
  11. 2
      Project/Assets/ML-Agents/Examples/Template/Scripts/TemplateAgent.cs
  12. 20
      Project/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs
  13. 30
      Project/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs
  14. 6
      Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs
  15. 1
      com.unity.ml-agents/Editor/BehaviorParametersEditor.cs
  16. 103
      com.unity.ml-agents/Runtime/Agent.cs
  17. 1
      com.unity.ml-agents/Runtime/DecisionRequester.cs
  18. 1
      com.unity.ml-agents/Runtime/DemonstrationRecorder.cs
  19. 1
      com.unity.ml-agents/Runtime/DemonstrationStore.cs
  20. 1
      com.unity.ml-agents/Runtime/Grpc/GrpcExtensions.cs
  21. 1
      com.unity.ml-agents/Runtime/Grpc/RpcCommunicator.cs
  22. 1
      com.unity.ml-agents/Runtime/ICommunicator.cs
  23. 1
      com.unity.ml-agents/Runtime/InferenceBrain/BarracudaModelParamLoader.cs
  24. 1
      com.unity.ml-agents/Runtime/InferenceBrain/GeneratorImpl.cs
  25. 1
      com.unity.ml-agents/Runtime/InferenceBrain/ModelRunner.cs
  26. 1
      com.unity.ml-agents/Runtime/InferenceBrain/TensorGenerator.cs
  27. 1
      com.unity.ml-agents/Runtime/Policy/BarracudaPolicy.cs
  28. 1
      com.unity.ml-agents/Runtime/Policy/HeuristicPolicy.cs
  29. 1
      com.unity.ml-agents/Runtime/Policy/IPolicy.cs
  30. 1
      com.unity.ml-agents/Runtime/Policy/RemotePolicy.cs
  31. 2
      com.unity.ml-agents/Runtime/Sensor/CameraSensor.cs
  32. 2
      com.unity.ml-agents/Runtime/Sensor/CameraSensorComponent.cs
  33. 2
      com.unity.ml-agents/Runtime/Sensor/ISensor.cs
  34. 2
      com.unity.ml-agents/Runtime/Sensor/Observation.cs
  35. 2
      com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs
  36. 2
      com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensorComponent2D.cs
  37. 2
      com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensorComponent3D.cs
  38. 2
      com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensorComponentBase.cs
  39. 2
      com.unity.ml-agents/Runtime/Sensor/RenderTextureSensor.cs
  40. 2
      com.unity.ml-agents/Runtime/Sensor/RenderTextureSensorComponent.cs
  41. 2
      com.unity.ml-agents/Runtime/Sensor/SensorBase.cs
  42. 2
      com.unity.ml-agents/Runtime/Sensor/SensorComponent.cs
  43. 2
      com.unity.ml-agents/Runtime/Sensor/SensorShapeValidator.cs
  44. 2
      com.unity.ml-agents/Runtime/Sensor/StackingSensor.cs
  45. 2
      com.unity.ml-agents/Runtime/Sensor/VectorSensor.cs
  46. 2
      com.unity.ml-agents/Runtime/Sensor/WriteAdapter.cs
  47. 1
      com.unity.ml-agents/Runtime/Utilities.cs
  48. 9
      com.unity.ml-agents/Tests/Editor/DemonstrationTests.cs
  49. 5
      com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs
  50. 1
      com.unity.ml-agents/Tests/Editor/Sensor/FloatVisualSensorTests.cs
  51. 1
      com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs
  52. 1
      com.unity.ml-agents/Tests/Editor/Sensor/StackingSensorTests.cs
  53. 1
      com.unity.ml-agents/Tests/Editor/Sensor/VectorSensorTests.cs
  54. 2
      com.unity.ml-agents/Tests/Editor/Sensor/WriterAdapterTests.cs
  55. 8
      docs/Getting-Started-with-Balance-Ball.md
  56. 4
      docs/Learning-Environment-Best-Practices.md
  57. 20
      docs/Learning-Environment-Create-New.md
  58. 39
      docs/Learning-Environment-Design-Agents.md
  59. 8
      docs/Learning-Environment-Design.md
  60. 5
      docs/Migrating.md

10
Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DAgent.cs


SetResetParameters();
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(gameObject.transform.rotation.z);
AddVectorObs(gameObject.transform.rotation.x);
AddVectorObs(ball.transform.position - gameObject.transform.position);
AddVectorObs(m_BallRb.velocity);
sensor.AddObservation(gameObject.transform.rotation.z);
sensor.AddObservation(gameObject.transform.rotation.x);
sensor.AddObservation(ball.transform.position - gameObject.transform.position);
sensor.AddObservation(m_BallRb.velocity);
}
public override void AgentAction(float[] vectorAction)

8
Project/Assets/ML-Agents/Examples/3DBall/Scripts/Ball3DHardAgent.cs


SetResetParameters();
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(gameObject.transform.rotation.z);
AddVectorObs(gameObject.transform.rotation.x);
AddVectorObs((ball.transform.position - gameObject.transform.position));
sensor.AddObservation(gameObject.transform.rotation.z);
sensor.AddObservation(gameObject.transform.rotation.x);
sensor.AddObservation((ball.transform.position - gameObject.transform.position));
}
public override void AgentAction(float[] vectorAction)

4
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs


{
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(m_Position, 20);
sensor.AddOneHotObservation(m_Position, 20);
}
public override void AgentAction(float[] vectorAction)

6
Project/Assets/ML-Agents/Examples/Bouncer/Scripts/BouncerAgent.cs


SetResetParameters();
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(gameObject.transform.localPosition);
AddVectorObs(target.transform.localPosition);
sensor.AddObservation(gameObject.transform.localPosition);
sensor.AddObservation(target.transform.localPosition);
}
public override void AgentAction(float[] vectorAction)

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


/// <summary>
/// Add relevant information on each body part to observations.
/// </summary>
public void CollectObservationBodyPart(BodyPart bp)
public void CollectObservationBodyPart(BodyPart bp, VectorSensor sensor)
AddVectorObs(bp.groundContact.touchingGround ? 1 : 0); // Whether the bp touching the ground
sensor.AddObservation(bp.groundContact.touchingGround ? 1 : 0); // Whether the bp touching the ground
AddVectorObs(velocityRelativeToLookRotationToTarget);
sensor.AddObservation(velocityRelativeToLookRotationToTarget);
AddVectorObs(angularVelocityRelativeToLookRotationToTarget);
sensor.AddObservation(angularVelocityRelativeToLookRotationToTarget);
AddVectorObs(localPosRelToBody);
AddVectorObs(bp.currentXNormalizedRot); // Current x rot
AddVectorObs(bp.currentYNormalizedRot); // Current y rot
AddVectorObs(bp.currentZNormalizedRot); // Current z rot
AddVectorObs(bp.currentStrength / m_JdController.maxJointForceLimit);
sensor.AddObservation(localPosRelToBody);
sensor.AddObservation(bp.currentXNormalizedRot); // Current x rot
sensor.AddObservation(bp.currentYNormalizedRot); // Current y rot
sensor.AddObservation(bp.currentZNormalizedRot); // Current z rot
sensor.AddObservation(bp.currentStrength / m_JdController.maxJointForceLimit);
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
{
m_JdController.GetCurrentJointForces();

RaycastHit hit;
if (Physics.Raycast(body.position, Vector3.down, out hit, 10.0f))
{
AddVectorObs(hit.distance);
sensor.AddObservation(hit.distance);
AddVectorObs(10.0f);
sensor.AddObservation(10.0f);
AddVectorObs(bodyForwardRelativeToLookRotationToTarget);
sensor.AddObservation(bodyForwardRelativeToLookRotationToTarget);
AddVectorObs(bodyUpRelativeToLookRotationToTarget);
sensor.AddObservation(bodyUpRelativeToLookRotationToTarget);
CollectObservationBodyPart(bodyPart);
CollectObservationBodyPart(bodyPart, sensor);
}
}

10
Project/Assets/ML-Agents/Examples/FoodCollector/Scripts/FoodCollectorAgent.cs


SetResetParameters();
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(localVelocity.x);
AddVectorObs(localVelocity.z);
AddVectorObs(System.Convert.ToInt32(m_Frozen));
AddVectorObs(System.Convert.ToInt32(m_Shoot));
sensor.AddObservation(localVelocity.x);
sensor.AddObservation(localVelocity.z);
sensor.AddObservation(System.Convert.ToInt32(m_Frozen));
sensor.AddObservation(System.Convert.ToInt32(m_Shoot));
}
}

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


{
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
{
// There are no numeric observations to collect as this environment uses visual
// observations.

4
Project/Assets/ML-Agents/Examples/Hallway/Scripts/HallwayAgent.cs


m_GroundMaterial = m_GroundRenderer.material;
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(GetStepCount() / (float)maxStep);
sensor.AddObservation(GetStepCount() / (float)maxStep);
}
}

6
Project/Assets/ML-Agents/Examples/Pyramids/Scripts/PyramidAgent.cs


m_SwitchLogic = areaSwitch.GetComponent<PyramidSwitch>();
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(m_SwitchLogic.GetState());
AddVectorObs(transform.InverseTransformDirection(m_AgentRb.velocity));
sensor.AddObservation(m_SwitchLogic.GetState());
sensor.AddObservation(transform.InverseTransformDirection(m_AgentRb.velocity));
}
}

24
Project/Assets/ML-Agents/Examples/Reacher/Scripts/ReacherAgent.cs


/// We collect the normalized rotations, angularal velocities, and velocities of both
/// limbs of the reacher as well as the relative position of the target and hand.
/// </summary>
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(pendulumA.transform.localPosition);
AddVectorObs(pendulumA.transform.rotation);
AddVectorObs(m_RbA.angularVelocity);
AddVectorObs(m_RbA.velocity);
sensor.AddObservation(pendulumA.transform.localPosition);
sensor.AddObservation(pendulumA.transform.rotation);
sensor.AddObservation(m_RbA.angularVelocity);
sensor.AddObservation(m_RbA.velocity);
AddVectorObs(pendulumB.transform.localPosition);
AddVectorObs(pendulumB.transform.rotation);
AddVectorObs(m_RbB.angularVelocity);
AddVectorObs(m_RbB.velocity);
sensor.AddObservation(pendulumB.transform.localPosition);
sensor.AddObservation(pendulumB.transform.rotation);
sensor.AddObservation(m_RbB.angularVelocity);
sensor.AddObservation(m_RbB.velocity);
AddVectorObs(goal.transform.localPosition);
AddVectorObs(hand.transform.localPosition);
sensor.AddObservation(goal.transform.localPosition);
sensor.AddObservation(hand.transform.localPosition);
AddVectorObs(m_GoalSpeed);
sensor.AddObservation(m_GoalSpeed);
}
/// <summary>

2
Project/Assets/ML-Agents/Examples/Template/Scripts/TemplateAgent.cs


public class TemplateAgent : Agent
{
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
{
}

20
Project/Assets/ML-Agents/Examples/Tennis/Scripts/TennisAgent.cs


SetResetParameters();
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(m_InvertMult * (transform.position.x - myArea.transform.position.x));
AddVectorObs(transform.position.y - myArea.transform.position.y);
AddVectorObs(m_InvertMult * m_AgentRb.velocity.x);
AddVectorObs(m_AgentRb.velocity.y);
sensor.AddObservation(m_InvertMult * (transform.position.x - myArea.transform.position.x));
sensor.AddObservation(transform.position.y - myArea.transform.position.y);
sensor.AddObservation(m_InvertMult * m_AgentRb.velocity.x);
sensor.AddObservation(m_AgentRb.velocity.y);
AddVectorObs(m_InvertMult * (ball.transform.position.x - myArea.transform.position.x));
AddVectorObs(ball.transform.position.y - myArea.transform.position.y);
AddVectorObs(m_InvertMult * m_BallRb.velocity.x);
AddVectorObs(m_BallRb.velocity.y);
sensor.AddObservation(m_InvertMult * (ball.transform.position.x - myArea.transform.position.x));
sensor.AddObservation(ball.transform.position.y - myArea.transform.position.y);
sensor.AddObservation(m_InvertMult * m_BallRb.velocity.x);
sensor.AddObservation(m_BallRb.velocity.y);
AddVectorObs(m_InvertMult * gameObject.transform.rotation.z);
sensor.AddObservation(m_InvertMult * gameObject.transform.rotation.z);
}
public override void AgentAction(float[] vectorAction)

30
Project/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs


/// <summary>
/// Add relevant information on each body part to observations.
/// </summary>
public void CollectObservationBodyPart(BodyPart bp)
public void CollectObservationBodyPart(BodyPart bp, VectorSensor sensor)
AddVectorObs(bp.groundContact.touchingGround ? 1 : 0); // Is this bp touching the ground
AddVectorObs(rb.velocity);
AddVectorObs(rb.angularVelocity);
sensor.AddObservation(bp.groundContact.touchingGround ? 1 : 0); // Is this bp touching the ground
sensor.AddObservation(rb.velocity);
sensor.AddObservation(rb.angularVelocity);
AddVectorObs(localPosRelToHips);
sensor.AddObservation(localPosRelToHips);
AddVectorObs(bp.currentXNormalizedRot);
AddVectorObs(bp.currentYNormalizedRot);
AddVectorObs(bp.currentZNormalizedRot);
AddVectorObs(bp.currentStrength / m_JdController.maxJointForceLimit);
sensor.AddObservation(bp.currentXNormalizedRot);
sensor.AddObservation(bp.currentYNormalizedRot);
sensor.AddObservation(bp.currentZNormalizedRot);
sensor.AddObservation(bp.currentStrength / m_JdController.maxJointForceLimit);
}
}

public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(m_DirToTarget.normalized);
AddVectorObs(m_JdController.bodyPartsDict[hips].rb.position);
AddVectorObs(hips.forward);
AddVectorObs(hips.up);
sensor.AddObservation(m_DirToTarget.normalized);
sensor.AddObservation(m_JdController.bodyPartsDict[hips].rb.position);
sensor.AddObservation(hips.forward);
sensor.AddObservation(hips.up);
CollectObservationBodyPart(bodyPart);
CollectObservationBodyPart(bodyPart, sensor);
}
}

6
Project/Assets/ML-Agents/Examples/WallJump/Scripts/WallJumpAgent.cs


}
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(agentPos / 20f);
AddVectorObs(DoGroundCheck(true) ? 1 : 0);
sensor.AddObservation(agentPos / 20f);
sensor.AddObservation(DoGroundCheck(true) ? 1 : 0);
}
/// <summary>

1
com.unity.ml-agents/Editor/BehaviorParametersEditor.cs


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

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


using System.Collections.Generic;
using UnityEngine;
using Barracuda;
using MLAgents.Sensor;
using UnityEngine.Serialization;
namespace MLAgents

UpdateSensors();
using (TimerStack.Instance.Scoped("CollectObservations"))
{
CollectObservations();
CollectObservations(collectObservationsSensor);
}
m_Info.actionMasks = m_ActionMasker.GetMask();

}
/// <summary>
/// Collects the (vector, visual) observations of the agent.
/// Collects the vector observations of the agent.
/// Simply, an agents observation is any environment information that helps
/// the Agent acheive its goal. For example, for a fighting Agent, its
/// An agents observation is any environment information that helps
/// the Agent achieve its goal. For example, for a fighting Agent, its
/// Vector observations are added by calling the provided helper methods:
/// - <see cref="AddVectorObs(int)"/>
/// - <see cref="AddVectorObs(float)"/>
/// - <see cref="AddVectorObs(Vector3)"/>
/// - <see cref="AddVectorObs(Vector2)"/>
/// Vector observations are added by calling the provided helper methods
/// on the VectorSensor input:
/// - <see cref="AddObservation(int)"/>
/// - <see cref="AddObservation(float)"/>
/// - <see cref="AddObservation(Vector3)"/>
/// - <see cref="AddObservation(Vector2)"/>
/// - <see>
/// <cref>AddVectorObs(float[])</cref>
/// </see>

/// - <see cref="AddVectorObs(Quaternion)"/>
/// - <see cref="AddVectorObs(bool)"/>
/// - <see cref="AddVectorObs(int, int)"/>
/// - <see cref="AddObservation(Quaternion)"/>
/// - <see cref="AddObservation(bool)"/>
/// - <see cref="AddOneHotObservation(int, int)"/>
/// Depending on your environment, any combination of these helpers can
/// be used. They just need to be used in the exact same order each time
/// this method is called and the resulting size of the vector observation

/// </remarks>
public virtual void CollectObservations()
public virtual void CollectObservations(VectorSensor sensor)
{
}

protected void SetActionMask(int branch, IEnumerable<int> actionIndices)
{
m_ActionMasker.SetActionMask(branch, actionIndices);
}
/// <summary>
/// Adds a float observation to the vector observations of the agent.
/// Increases the size of the agents vector observation by 1.
/// </summary>
/// <param name="observation">Observation.</param>
protected void AddVectorObs(float observation)
{
collectObservationsSensor.AddObservation(observation);
}
/// <summary>
/// Adds an integer observation to the vector observations of the agent.
/// Increases the size of the agents vector observation by 1.
/// </summary>
/// <param name="observation">Observation.</param>
protected void AddVectorObs(int observation)
{
collectObservationsSensor.AddObservation(observation);
}
/// <summary>
/// Adds an Vector3 observation to the vector observations of the agent.
/// Increases the size of the agents vector observation by 3.
/// </summary>
/// <param name="observation">Observation.</param>
protected void AddVectorObs(Vector3 observation)
{
collectObservationsSensor.AddObservation(observation);
}
/// <summary>
/// Adds an Vector2 observation to the vector observations of the agent.
/// Increases the size of the agents vector observation by 2.
/// </summary>
/// <param name="observation">Observation.</param>
protected void AddVectorObs(Vector2 observation)
{
collectObservationsSensor.AddObservation(observation);
}
/// <summary>
/// Adds a collection of float observations to the vector observations of the agent.
/// Increases the size of the agents vector observation by size of the collection.
/// </summary>
/// <param name="observation">Observation.</param>
protected void AddVectorObs(IEnumerable<float> observation)
{
collectObservationsSensor.AddObservation(observation);
}
/// <summary>
/// Adds a quaternion observation to the vector observations of the agent.
/// Increases the size of the agents vector observation by 4.
/// </summary>
/// <param name="observation">Observation.</param>
protected void AddVectorObs(Quaternion observation)
{
collectObservationsSensor.AddObservation(observation);
}
/// <summary>
/// Adds a boolean observation to the vector observation of the agent.
/// Increases the size of the agent's vector observation by 1.
/// </summary>
/// <param name="observation"></param>
protected void AddVectorObs(bool observation)
{
collectObservationsSensor.AddObservation(observation);
}
protected void AddVectorObs(int observation, int range)
{
collectObservationsSensor.AddOneHotObservation(observation, range);
}
/// <summary>

1
com.unity.ml-agents/Runtime/DecisionRequester.cs


using System.Collections.Generic;
using UnityEngine;
using Barracuda;
using MLAgents.Sensor;
using UnityEngine.Serialization;
namespace MLAgents

1
com.unity.ml-agents/Runtime/DemonstrationRecorder.cs


using System.Text.RegularExpressions;
using UnityEngine;
using System.Collections.Generic;
using MLAgents.Sensor;
namespace MLAgents
{

1
com.unity.ml-agents/Runtime/DemonstrationStore.cs


using System.IO.Abstractions;
using Google.Protobuf;
using System.Collections.Generic;
using MLAgents.Sensor;
namespace MLAgents
{

1
com.unity.ml-agents/Runtime/Grpc/GrpcExtensions.cs


using Google.Protobuf;
using Google.Protobuf.Collections;
using MLAgents.CommunicatorObjects;
using MLAgents.Sensor;
using UnityEngine;
using System.Runtime.CompilerServices;

1
com.unity.ml-agents/Runtime/Grpc/RpcCommunicator.cs


using MLAgents.CommunicatorObjects;
using System.IO;
using Google.Protobuf;
using MLAgents.Sensor;
namespace MLAgents
{

1
com.unity.ml-agents/Runtime/ICommunicator.cs


using System.Collections.Generic;
using UnityEngine;
using MLAgents.CommunicatorObjects;
using MLAgents.Sensor;
namespace MLAgents
{

1
com.unity.ml-agents/Runtime/InferenceBrain/BarracudaModelParamLoader.cs


using System.Collections.Generic;
using System.Linq;
using Barracuda;
using MLAgents.Sensor;
using UnityEngine;
namespace MLAgents.InferenceBrain

1
com.unity.ml-agents/Runtime/InferenceBrain/GeneratorImpl.cs


using System;
using Barracuda;
using MLAgents.InferenceBrain.Utils;
using MLAgents.Sensor;
using UnityEngine;
namespace MLAgents.InferenceBrain

1
com.unity.ml-agents/Runtime/InferenceBrain/ModelRunner.cs


using Barracuda;
using UnityEngine.Profiling;
using System;
using MLAgents.Sensor;
namespace MLAgents.InferenceBrain
{

1
com.unity.ml-agents/Runtime/InferenceBrain/TensorGenerator.cs


using System.Collections.Generic;
using Barracuda;
using MLAgents.Sensor;
namespace MLAgents.InferenceBrain
{

1
com.unity.ml-agents/Runtime/Policy/BarracudaPolicy.cs


using System.Collections.Generic;
using MLAgents.InferenceBrain;
using System;
using MLAgents.Sensor;
namespace MLAgents
{

1
com.unity.ml-agents/Runtime/Policy/HeuristicPolicy.cs


using MLAgents.Sensor;
using System.Collections.Generic;
using System;

1
com.unity.ml-agents/Runtime/Policy/IPolicy.cs


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

1
com.unity.ml-agents/Runtime/Policy/RemotePolicy.cs


using UnityEngine;
using System.Collections.Generic;
using MLAgents.Sensor;
using System;
namespace MLAgents

2
com.unity.ml-agents/Runtime/Sensor/CameraSensor.cs


using System;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
public class CameraSensor : ISensor
{

2
com.unity.ml-agents/Runtime/Sensor/CameraSensorComponent.cs


using System;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
[AddComponentMenu("ML Agents/Camera Sensor", (int)MenuGroup.Sensors)]
public class CameraSensorComponent : SensorComponent

2
com.unity.ml-agents/Runtime/Sensor/ISensor.cs


namespace MLAgents.Sensor
namespace MLAgents
{
public enum SensorCompressionType
{

2
com.unity.ml-agents/Runtime/Sensor/Observation.cs


using System;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
internal struct Observation
{

2
com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs


using System.Collections.Generic;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
public class RayPerceptionSensor : ISensor
{

2
com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensorComponent2D.cs


using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
[AddComponentMenu("ML Agents/Ray Perception Sensor 2D", (int)MenuGroup.Sensors)]
public class RayPerceptionSensorComponent2D : RayPerceptionSensorComponentBase

2
com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensorComponent3D.cs


using System;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
[AddComponentMenu("ML Agents/Ray Perception Sensor 3D", (int)MenuGroup.Sensors)]
public class RayPerceptionSensorComponent3D : RayPerceptionSensorComponentBase

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


using System.Collections.Generic;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
public abstract class RayPerceptionSensorComponentBase : SensorComponent
{

2
com.unity.ml-agents/Runtime/Sensor/RenderTextureSensor.cs


using System;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
public class RenderTextureSensor : ISensor
{

2
com.unity.ml-agents/Runtime/Sensor/RenderTextureSensorComponent.cs


using System;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
[AddComponentMenu("ML Agents/Render Texture Sensor", (int)MenuGroup.Sensors)]
public class RenderTextureSensorComponent : SensorComponent

2
com.unity.ml-agents/Runtime/Sensor/SensorBase.cs


using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
public abstract class SensorBase : ISensor
{

2
com.unity.ml-agents/Runtime/Sensor/SensorComponent.cs


using System;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
/// <summary>
/// Editor components for creating Sensors. Generally an ISensor implementation should have a corresponding

2
com.unity.ml-agents/Runtime/Sensor/SensorShapeValidator.cs


using System.Collections.Generic;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
internal class SensorShapeValidator
{

2
com.unity.ml-agents/Runtime/Sensor/StackingSensor.cs


namespace MLAgents.Sensor
namespace MLAgents
{
/// <summary>
/// Sensor that wraps around another Sensor to provide temporal stacking.

2
com.unity.ml-agents/Runtime/Sensor/VectorSensor.cs


using System.Collections.Generic;
using UnityEngine;
namespace MLAgents.Sensor
namespace MLAgents
{
public class VectorSensor : ISensor
{

2
com.unity.ml-agents/Runtime/Sensor/WriteAdapter.cs


using Barracuda;
using MLAgents.InferenceBrain;
namespace MLAgents.Sensor
namespace MLAgents
{
/// <summary>
/// Allows sensors to write to both TensorProxy and float arrays/lists.

1
com.unity.ml-agents/Runtime/Utilities.cs


using UnityEngine;
using System.Collections.Generic;
using MLAgents.Sensor;
namespace MLAgents
{

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


using System.IO.Abstractions.TestingHelpers;
using System.Reflection;
using MLAgents.CommunicatorObjects;
using MLAgents.Sensor;
namespace MLAgents.Tests
{

public class ObservationAgent : TestAgent
{
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(1f);
AddVectorObs(2f);
AddVectorObs(3f);
sensor.AddObservation(1f);
sensor.AddObservation(2f);
sensor.AddObservation(3f);
}
}

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


using UnityEngine;
using NUnit.Framework;
using System.Reflection;
using MLAgents.Sensor;
using System.Collections.Generic;
namespace MLAgents.Tests

sensors.Add(sensor1);
}
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(0f);
sensor.AddObservation(0f);
}
public override void AgentAction(float[] vectorAction)

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


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

1
com.unity.ml-agents/Tests/Editor/Sensor/RayPerceptionSensorTests.cs


using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using MLAgents.Sensor;
namespace MLAgents.Tests
{

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


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

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


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

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


using NUnit.Framework;
using UnityEngine;
using MLAgents.Sensor;
using Barracuda;
using MLAgents.InferenceBrain;

8
docs/Getting-Started-with-Balance-Ball.md


agent cube and ball. The function randomizes the reset values so that the
training generalizes to more than a specific starting position and agent cube
attitude.
* agent.CollectObservations() — Called every simulation step. Responsible for
* agent.CollectObservations(VectorSensor sensor) — Called every simulation step. Responsible for
space with a state size of 8, the `CollectObservations()` must call
`AddVectorObs` such that vector size adds up to 8.
space with a state size of 8, the `CollectObservations(VectorSensor sensor)` must call
`VectorSensor.AddObservation()` such that vector size adds up to 8.
* agent.AgentAction() — Called every simulation step. Receives the action chosen
by the Agent. The vector action spaces result in a
small change in the agent cube's rotation at each step. The `AgentAction()` function

vector containing the Agent's observations contains eight elements: the `x` and
`z` components of the agent cube's rotation and the `x`, `y`, and `z` components
of the ball's relative position and velocity. (The observation values are
defined in the Agent's `CollectObservations()` function.)
defined in the Agent's `CollectObservations(VectorSensor sensor)` method.)
#### Behavior Parameters : Vector Action Space

4
docs/Learning-Environment-Best-Practices.md


* Besides encoding non-numeric values, all inputs should be normalized to be in
the range 0 to +1 (or -1 to 1). For example, the `x` position information of
an agent where the maximum possible value is `maxValue` should be recorded as
`AddVectorObs(transform.position.x / maxValue);` rather than
`AddVectorObs(transform.position.x);`. See the equation below for one approach
`VectorSensor.AddObservation(transform.position.x / maxValue);` rather than
`VectorSensor.AddObservation(transform.position.x);`. See the equation below for one approach
of normalization.
* Positional information of relevant GameObjects should be encoded in relative
coordinates wherever possible. This is often relative to the agent position.

20
docs/Learning-Environment-Create-New.md


}
```
Next, let's implement the `Agent.CollectObservations()` method.
Next, let's implement the `Agent.CollectObservations(VectorSensor sensor)` method.
### Observing the Environment

* Position of the target.
```csharp
AddVectorObs(Target.position);
sensor.AddObservation(Target.position);
AddVectorObs(this.transform.position);
sensor.AddObservation(this.transform.position);
```
* The velocity of the Agent. This helps the Agent learn to control its speed so

// Agent velocity
AddVectorObs(rBody.velocity.x);
AddVectorObs(rBody.velocity.z);
sensor.AddObservation(rBody.velocity.x);
sensor.AddObservation(rBody.velocity.z);
```
In total, the state observation contains 8 values and we need to use the

public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(Target.position);
AddVectorObs(this.transform.position);
sensor.AddObservation(Target.position);
sensor.AddObservation(this.transform.position);
AddVectorObs(rBody.velocity.x);
AddVectorObs(rBody.velocity.z);
sensor.AddObservation(rBody.velocity.x);
sensor.AddObservation(rBody.velocity.z);
}
```

39
docs/Learning-Environment-Design-Agents.md


agent that only needs to make decisions when certain game or simulation events
occur, should call `Agent.RequestDecision()` manually.
## Observations
To make decisions, an agent must observe its environment in order to infer the

* **Visual Observations** — one or more camera images and/or render textures.
When you use vector observations for an Agent, implement the
`Agent.CollectObservations()` method to create the feature vector. When you use
`Agent.CollectObservations(VectorSensor sensor)` method to create the feature vector. When you use
You do not need to implement the `CollectObservations()` method when your Agent
You do not need to implement the `CollectObservations(VectorSensor sensor)` method when your Agent
uses visual observations (unless it also uses vector observations).
### Vector Observation Space: Feature Vectors

class calls the `CollectObservations()` method of each Agent. Your
implementation of this function must call `AddVectorObs` to add vector
class calls the `CollectObservations(VectorSensor sensor)` method of each Agent. Your
implementation of this function must call `VectorSensor.AddObservation` to add vector
observations.
The observation must include all the information an agents needs to accomplish

public GameObject ball;
private List<float> state = new List<float>();
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs(gameObject.transform.rotation.z);
AddVectorObs(gameObject.transform.rotation.x);
AddVectorObs((ball.transform.position.x - gameObject.transform.position.x));
AddVectorObs((ball.transform.position.y - gameObject.transform.position.y));
AddVectorObs((ball.transform.position.z - gameObject.transform.position.z));
AddVectorObs(ball.transform.GetComponent<Rigidbody>().velocity.x);
AddVectorObs(ball.transform.GetComponent<Rigidbody>().velocity.y);
AddVectorObs(ball.transform.GetComponent<Rigidbody>().velocity.z);
sensor.AddObservation(gameObject.transform.rotation.z);
sensor.AddObservation(gameObject.transform.rotation.x);
sensor.AddObservation((ball.transform.position.x - gameObject.transform.position.x));
sensor.AddObservation((ball.transform.position.y - gameObject.transform.position.y));
sensor.AddObservation((ball.transform.position.z - gameObject.transform.position.z));
sensor.AddObservation(ball.transform.GetComponent<Rigidbody>().velocity.x);
sensor.AddObservation(ball.transform.GetComponent<Rigidbody>().velocity.y);
sensor.AddObservation(ball.transform.GetComponent<Rigidbody>().velocity.z);
}
```

The observation feature vector is a list of floating point numbers, which means
you must convert any other data types to a float or a list of floats.
The `AddVectorObs` method provides a number of overloads for adding common types
The `VectorSensor.AddObservation` method provides a number of overloads for adding common types
of data to your observation vector. You can add Integers and booleans directly to
the observation vector, as well as some common Unity data types such as `Vector2`,
`Vector3`, and `Quaternion`.

```csharp
enum CarriedItems { Sword, Shield, Bow, LastItem }
private List<float> state = new List<float>();
public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs((int)currentItem == ci ? 1.0f : 0.0f);
sensor.AddObservation((int)currentItem == ci ? 1.0f : 0.0f);
`AddVectorObs` also provides a two-argument version as a shortcut for _one-hot_
`VectorSensor.AddObservation` also provides a two-argument version as a shortcut for _one-hot_
style observations. The following example is identical to the previous one.
```csharp

public override void CollectObservations()
public override void CollectObservations(VectorSensor sensor)
AddVectorObs((int)currentItem, NUM_ITEM_TYPES);
sensor.AddOneHotObservation((int)currentItem, NUM_ITEM_TYPES);
}
```

8
docs/Learning-Environment-Design.md


1. Calls your Academy's `OnEnvironmentReset` delegate.
2. Calls the `AgentReset()` function for each Agent in the scene.
3. Calls the `CollectObservations()` function for each Agent in the scene.
3. Calls the `CollectObservations(VectorSensor sensor)` function for each Agent in the scene.
4. Uses each Agent's Policy to decide on the Agent's next action.
5. Calls the `AgentAction()` function for each Agent in the scene, passing in
the action chosen by the Agent's Policy. (This function is not called if the

To create a training environment, extend the Agent class to
implement the above methods. The `Agent.CollectObservations()` and
implement the above methods. The `Agent.CollectObservations(VectorSensor sensor)` and
`Agent.AgentAction()` functions are required; the other methods are optional —
whether you need to implement them or not depends on your specific scenario.

have appropriate `Behavior Parameters`.
To create an Agent, extend the Agent class and implement the essential
`CollectObservations()` and `AgentAction()` methods:
`CollectObservations(VectorSensor sensor)` and `AgentAction()` methods:
* `CollectObservations()` — Collects the Agent's observation of its environment.
* `CollectObservations(VectorSensor sensor)` — Collects the Agent's observation of its environment.
* `AgentAction()` — Carries out the action chosen by the Agent's Policy and
assigns a reward to the current state.

5
docs/Migrating.md


## Migrating from 0.14 to latest
### Important changes
* The `Agent.CollectObservations()` virtual method now takes as input a `VectorSensor` sensor as argument. The `Agent.AddVectorObs()` methods were removed.
* The `MLAgents.Sensor` namespace has been removed. All sensors now belong to the `MLAgents` namespace.
* Replace your Agent's implementation of `CollectObservations()` with `CollectObservations(VectorSensor sensor)`. In addition, replace all calls to `AddVectorObs()` with `sensor.AddObservation()` or `sensor.AddOneHotObservation()` on the `VectorSensor` passed as argument.
## Migrating from 0.13 to 0.14

正在加载...
取消
保存