浏览代码

[change] Make Agent non-abstract, update Basic scene. (#3528)

/bug-failed-api-check
GitHub 5 年前
当前提交
e7ec5007
共有 10 个文件被更改,包括 312 次插入117 次删除
  1. 53
      Project/Assets/ML-Agents/Examples/Basic/Scenes/Basic.unity
  2. 2
      com.unity.ml-agents/CHANGELOG.md
  3. 13
      com.unity.ml-agents/Runtime/Agent.cs
  4. 110
      Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicController.cs
  5. 69
      Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicSensorComponent.cs
  6. 3
      Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicSensorComponent.cs.meta
  7. 56
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/SensorBase.cs
  8. 11
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/SensorBase.cs.meta
  9. 112
      Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
  10. 0
      /Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicController.cs.meta

53
Project/Assets/ML-Agents/Examples/Basic/Scenes/Basic.unity


objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 5889392e3f05b448a8a06c5def6c2dec, type: 3}
--- !u!1 &1335907378 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1263463520136984, guid: c5eb289873aca4f5a8cc59c7464ab7c1,
type: 3}
m_PrefabInstance: {fileID: 1783603361}
m_PrefabAsset: {fileID: 0}
--- !u!114 &1335907380
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1335907378}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 88b6042bc9a5d4aa58d931eae49442e5, type: 3}
m_Name:
m_EditorClassIdentifier:
agentParameters:
maxStep: 0
hasUpgradedFromAgentParameters: 1
maxStep: 0
--- !u!114 &1335907381 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 114827551040495112, guid: c5eb289873aca4f5a8cc59c7464ab7c1,
type: 3}
m_PrefabInstance: {fileID: 1783603361}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1335907378}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 624480a72e46148118ab2e2d89b537de, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1335907384
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1335907378}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6ee410d6d45349218d5e69bb2a347c63, type: 3}
m_Name:
m_EditorClassIdentifier:
basicController: {fileID: 1335907381}
--- !u!1001 &1502457254
PrefabInstance:
m_ObjectHideFlags: 0

propertyPath: m_LocalScale.z
value: 6.270299
objectReference: {fileID: 0}
- target: {fileID: 114502619508238574, guid: c5eb289873aca4f5a8cc59c7464ab7c1,
type: 3}
propertyPath: m_BrainParameters.vectorObservationSize
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: c5eb289873aca4f5a8cc59c7464ab7c1, type: 3}
--- !u!1 &1889211226

m_Name:
m_EditorClassIdentifier:
gravityMultiplier: 1
monitorVerticalOffset: 0
fixedDeltaTime: 0.02
maximumDeltaTime: 0.33333334
solverIterations: 6

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


- `RayPerceptionSensorComponent` and related classes now display the debug gizmos whenever the Agent is selected (not just Play mode).
- Most fields on `RayPerceptionSensorComponent` can now be changed while the editor is in Play mode. The exceptions to this are fields that affect the number of observations.
- Unused static methods from the `Utilities` class (ShiftLeft, ReplaceRange, AddRangeNoAlloc, and GetSensorFloatObservationSize) were removed.
- The `Agent` class is no longer abstract.
- SensorBase was moved out of the package and into the Examples directory.
## [0.14.1-preview] - 2020-02-25

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


"docs/Learning-Environment-Design-Agents.md")]
[Serializable]
[RequireComponent(typeof(BehaviorParameters))]
public abstract class Agent : MonoBehaviour, ISerializationCallbackReceiver
public class Agent : MonoBehaviour, ISerializationCallbackReceiver
{
IPolicy m_Brain;
BehaviorParameters m_PolicyFactory;

m_RequestAction = true;
}
/// Helper function that resets all the data structures associated with
/// the agent. Typically used when the agent is being initialized or reset
/// at the end of an episode.

/// </returns>
public virtual float[] Heuristic()
{
throw new UnityAgentsException(
"The Heuristic method was not implemented for the Agent on the " +
$"{gameObject.name} GameObject.");
Debug.LogWarning("Heuristic method called but not implemented. Return placeholder actions.");
var param = m_PolicyFactory.brainParameters;
var actionSize = param.vectorActionSpaceType == SpaceType.Continuous ?
param.vectorActionSize[0] :
param.vectorActionSize.Length;
return new float[actionSize];
}
/// <summary>

110
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicController.cs


using UnityEngine;
using MLAgents;
/// <summary>
/// An example of how to use ML-Agents without inheriting from the Agent class.
/// Observations are generated by the attached SensorComponent, and the actions
/// are retrieved from the Agent.
/// </summary>
public class BasicController : MonoBehaviour
{
public float timeBetweenDecisionsAtInference;
float m_TimeSinceDecision;
[HideInInspector]
public int m_Position;
const int k_SmallGoalPosition = 7;
const int k_LargeGoalPosition = 17;
public GameObject largeGoal;
public GameObject smallGoal;
const int k_MinPosition = 0;
const int k_MaxPosition = 20;
public const int k_Extents = k_MaxPosition - k_MinPosition;
Agent m_Agent;
public void OnEnable()
{
m_Agent = GetComponent<Agent>();
ResetAgent();
}
/// <summary>
/// Controls the movement of the GameObject based on the actions received.
/// </summary>
/// <param name="vectorAction"></param>
public void ApplyAction(float[] vectorAction)
{
var movement = (int)vectorAction[0];
var direction = 0;
switch (movement)
{
case 1:
direction = -1;
break;
case 2:
direction = 1;
break;
}
m_Position += direction;
if (m_Position < k_MinPosition) { m_Position = k_MinPosition; }
if (m_Position > k_MaxPosition) { m_Position = k_MaxPosition; }
gameObject.transform.position = new Vector3(m_Position - 10f, 0f, 0f);
m_Agent.AddReward(-0.01f);
if (m_Position == k_SmallGoalPosition)
{
m_Agent.AddReward(0.1f);
m_Agent.Done();
ResetAgent();
}
if (m_Position == k_LargeGoalPosition)
{
m_Agent.AddReward(1f);
m_Agent.Done();
ResetAgent();
}
}
public void ResetAgent()
{
m_Position = 10;
smallGoal.transform.position = new Vector3(k_SmallGoalPosition - 10f, 0f, 0f);
largeGoal.transform.position = new Vector3(k_LargeGoalPosition - 10f, 0f, 0f);
}
public void FixedUpdate()
{
WaitTimeInference();
}
void WaitTimeInference()
{
if (Academy.Instance.IsCommunicatorOn)
{
// Apply the previous step's actions
ApplyAction(m_Agent.GetAction());
m_Agent.RequestDecision();
}
else
{
if (m_TimeSinceDecision >= timeBetweenDecisionsAtInference)
{
// Apply the previous step's actions
ApplyAction(m_Agent.GetAction());
m_TimeSinceDecision = 0f;
m_Agent.RequestDecision();
}
else
{
m_TimeSinceDecision += Time.fixedDeltaTime;
}
}
}
}

69
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicSensorComponent.cs


using System;
using MLAgents.Sensors;
using UnityEngine.Serialization;
namespace MLAgentsExamples
{
/// <summary>
/// A simple example of a SensorComponent.
/// This should be added to the same GameObject as the BasicController
/// </summary>
public class BasicSensorComponent : SensorComponent
{
public BasicController basicController;
/// <summary>
/// Creates a BasicSensor.
/// </summary>
/// <returns></returns>
public override ISensor CreateSensor()
{
return new BasicSensor(basicController);
}
/// <inheritdoc/>
public override int[] GetObservationShape()
{
return new[] { BasicController.k_Extents };
}
}
/// <summary>
/// Simple Sensor implementation that uses a one-hot encoding of the Agent's
/// position as the observation.
/// </summary>
public class BasicSensor : SensorBase
{
public BasicController basicController;
public BasicSensor(BasicController controller)
{
basicController = controller;
}
/// <summary>
/// Generate the observations for the sensor.
/// In this case, the observations are all 0 except for a 1 at the position of the agent.
/// </summary>
/// <param name="output"></param>
public override void WriteObservation(float[] output)
{
// One-hot encoding of the position
Array.Clear(output, 0, output.Length);
output[basicController.m_Position] = 1;
}
/// <inheritdoc/>
public override int[] GetObservationShape()
{
return new[] { BasicController.k_Extents };
}
/// <inheritdoc/>
public override string GetName()
{
return "Basic";
}
}
}

3
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicSensorComponent.cs.meta


fileFormatVersion: 2
guid: 6ee410d6d45349218d5e69bb2a347c63
timeCreated: 1582857786

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


using MLAgents.Sensors;
namespace MLAgentsExamples
{
/// <summary>
/// A simple sensor that provides a number default implementations.
/// </summary>
public abstract class SensorBase : ISensor
{
/// <summary>
/// Write the observations to the output buffer. This size of the buffer will be product
/// of the sizes returned by <see cref="GetObservationShape"/>.
/// </summary>
/// <param name="output"></param>
public abstract void WriteObservation(float[] output);
/// <inheritdoc/>
public abstract int[] GetObservationShape();
/// <inheritdoc/>
public abstract string GetName();
/// <summary>
/// Default implementation of Write interface. This creates a temporary array,
/// calls WriteObservation, and then writes the results to the WriteAdapter.
/// </summary>
/// <param name="adapter"></param>
/// <returns>The number of elements written.</returns>
public virtual int Write(WriteAdapter adapter)
{
// TODO reuse buffer for similar agents, don't call GetObservationShape()
var numFloats = this.ObservationSize();
float[] buffer = new float[numFloats];
WriteObservation(buffer);
adapter.AddRange(buffer);
return numFloats;
}
/// <inheritdoc/>
public void Update() {}
/// <inheritdoc/>
public virtual byte[] GetCompressedObservation()
{
return null;
}
/// <inheritdoc/>
public virtual SensorCompressionType GetCompressionType()
{
return SensorCompressionType.None;
}
}
}

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


fileFormatVersion: 2
guid: 553b05a1b59a94260b3e545f13190389
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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


using UnityEngine;
using MLAgents;
using MLAgents.Sensors;
public class BasicAgent : Agent
{
[Header("Specific to Basic")]
public float timeBetweenDecisionsAtInference;
float m_TimeSinceDecision;
int m_Position;
int m_SmallGoalPosition;
int m_LargeGoalPosition;
public GameObject largeGoal;
public GameObject smallGoal;
int m_MinPosition;
int m_MaxPosition;
public override void InitializeAgent()
{
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddOneHotObservation(m_Position, 20);
}
public override void AgentAction(float[] vectorAction)
{
var movement = (int)vectorAction[0];
var direction = 0;
switch (movement)
{
case 1:
direction = -1;
break;
case 2:
direction = 1;
break;
}
m_Position += direction;
if (m_Position < m_MinPosition) { m_Position = m_MinPosition; }
if (m_Position > m_MaxPosition) { m_Position = m_MaxPosition; }
gameObject.transform.position = new Vector3(m_Position - 10f, 0f, 0f);
AddReward(-0.01f);
if (m_Position == m_SmallGoalPosition)
{
AddReward(0.1f);
Done();
}
if (m_Position == m_LargeGoalPosition)
{
AddReward(1f);
Done();
}
}
public override void AgentReset()
{
m_Position = 10;
m_MinPosition = 0;
m_MaxPosition = 20;
m_SmallGoalPosition = 7;
m_LargeGoalPosition = 17;
smallGoal.transform.position = new Vector3(m_SmallGoalPosition - 10f, 0f, 0f);
largeGoal.transform.position = new Vector3(m_LargeGoalPosition - 10f, 0f, 0f);
}
public override float[] Heuristic()
{
if (Input.GetKey(KeyCode.D))
{
return new float[] { 2 };
}
if (Input.GetKey(KeyCode.A))
{
return new float[] { 1 };
}
return new float[] { 0 };
}
public void FixedUpdate()
{
WaitTimeInference();
}
void WaitTimeInference()
{
if (!Academy.Instance.IsCommunicatorOn)
{
RequestDecision();
}
else
{
if (m_TimeSinceDecision >= timeBetweenDecisionsAtInference)
{
m_TimeSinceDecision = 0f;
RequestDecision();
}
else
{
m_TimeSinceDecision += Time.fixedDeltaTime;
}
}
}
}

/Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs.meta → /Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicController.cs.meta

正在加载...
取消
保存