-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
62e4618
Agent not abstract, use SensorComponent
b7f9d12
comments
ab64547
move SensorBase to examples
e98fd08
undo ProjectVersion
1d2866c
changelog
6ecf8ed
warn and 0 array for default Heuristic
f0d2ddf
rename BasicAgent to BasicController
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 0 additions & 112 deletions
112
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicAgent.cs
This file was deleted.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
69 changes: 69 additions & 0 deletions
69
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicSensorComponent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicSensorComponent.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 4 additions & 2 deletions
6
...ty.ml-agents/Runtime/Sensor/SensorBase.cs → ...amples/SharedAssets/Scripts/SensorBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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