Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion Project/Assets/ML-Agents/Examples/Basic/Scenes/Basic.unity
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,53 @@ PrefabInstance:
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
Expand Down Expand Up @@ -395,6 +442,11 @@ PrefabInstance:
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
Expand Down Expand Up @@ -427,7 +479,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
gravityMultiplier: 1
monitorVerticalOffset: 0
fixedDeltaTime: 0.02
maximumDeltaTime: 0.33333334
solverIterations: 6
Expand Down
112 changes: 0 additions & 112 deletions Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs

This file was deleted.

110 changes: 110 additions & 0 deletions Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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";
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace MLAgents.Sensors
using MLAgents.Sensors;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: moved out of com.unity.ml-agents


namespace MLAgentsExamples
{
/// <summary>
/// A base sensor that provides a number default implementations.
/// A simple sensor that provides a number default implementations.
/// </summary>
public abstract class SensorBase : ISensor
{
Expand Down
2 changes: 2 additions & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `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
Expand Down
Loading