Skip to content

Commit 1073ee6

Browse files
author
Chris Elion
authored
[change] Make Agent non-abstract, update Basic scene. (#3528)
1 parent 34bb957 commit 1073ee6

File tree

10 files changed

+312
-117
lines changed

10 files changed

+312
-117
lines changed

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,53 @@ PrefabInstance:
153153
objectReference: {fileID: 0}
154154
m_RemovedComponents: []
155155
m_SourcePrefab: {fileID: 100100000, guid: 5889392e3f05b448a8a06c5def6c2dec, type: 3}
156+
--- !u!1 &1335907378 stripped
157+
GameObject:
158+
m_CorrespondingSourceObject: {fileID: 1263463520136984, guid: c5eb289873aca4f5a8cc59c7464ab7c1,
159+
type: 3}
160+
m_PrefabInstance: {fileID: 1783603361}
161+
m_PrefabAsset: {fileID: 0}
162+
--- !u!114 &1335907380
163+
MonoBehaviour:
164+
m_ObjectHideFlags: 0
165+
m_CorrespondingSourceObject: {fileID: 0}
166+
m_PrefabInstance: {fileID: 0}
167+
m_PrefabAsset: {fileID: 0}
168+
m_GameObject: {fileID: 1335907378}
169+
m_Enabled: 1
170+
m_EditorHideFlags: 0
171+
m_Script: {fileID: 11500000, guid: 88b6042bc9a5d4aa58d931eae49442e5, type: 3}
172+
m_Name:
173+
m_EditorClassIdentifier:
174+
agentParameters:
175+
maxStep: 0
176+
hasUpgradedFromAgentParameters: 1
177+
maxStep: 0
178+
--- !u!114 &1335907381 stripped
179+
MonoBehaviour:
180+
m_CorrespondingSourceObject: {fileID: 114827551040495112, guid: c5eb289873aca4f5a8cc59c7464ab7c1,
181+
type: 3}
182+
m_PrefabInstance: {fileID: 1783603361}
183+
m_PrefabAsset: {fileID: 0}
184+
m_GameObject: {fileID: 1335907378}
185+
m_Enabled: 1
186+
m_EditorHideFlags: 0
187+
m_Script: {fileID: 11500000, guid: 624480a72e46148118ab2e2d89b537de, type: 3}
188+
m_Name:
189+
m_EditorClassIdentifier:
190+
--- !u!114 &1335907384
191+
MonoBehaviour:
192+
m_ObjectHideFlags: 0
193+
m_CorrespondingSourceObject: {fileID: 0}
194+
m_PrefabInstance: {fileID: 0}
195+
m_PrefabAsset: {fileID: 0}
196+
m_GameObject: {fileID: 1335907378}
197+
m_Enabled: 1
198+
m_EditorHideFlags: 0
199+
m_Script: {fileID: 11500000, guid: 6ee410d6d45349218d5e69bb2a347c63, type: 3}
200+
m_Name:
201+
m_EditorClassIdentifier:
202+
basicController: {fileID: 1335907381}
156203
--- !u!1001 &1502457254
157204
PrefabInstance:
158205
m_ObjectHideFlags: 0
@@ -395,6 +442,11 @@ PrefabInstance:
395442
propertyPath: m_LocalScale.z
396443
value: 6.270299
397444
objectReference: {fileID: 0}
445+
- target: {fileID: 114502619508238574, guid: c5eb289873aca4f5a8cc59c7464ab7c1,
446+
type: 3}
447+
propertyPath: m_BrainParameters.vectorObservationSize
448+
value: 0
449+
objectReference: {fileID: 0}
398450
m_RemovedComponents: []
399451
m_SourcePrefab: {fileID: 100100000, guid: c5eb289873aca4f5a8cc59c7464ab7c1, type: 3}
400452
--- !u!1 &1889211226
@@ -427,7 +479,6 @@ MonoBehaviour:
427479
m_Name:
428480
m_EditorClassIdentifier:
429481
gravityMultiplier: 1
430-
monitorVerticalOffset: 0
431482
fixedDeltaTime: 0.02
432483
maximumDeltaTime: 0.33333334
433484
solverIterations: 6

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

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using UnityEngine;
2+
using MLAgents;
3+
4+
/// <summary>
5+
/// An example of how to use ML-Agents without inheriting from the Agent class.
6+
/// Observations are generated by the attached SensorComponent, and the actions
7+
/// are retrieved from the Agent.
8+
/// </summary>
9+
public class BasicController : MonoBehaviour
10+
{
11+
public float timeBetweenDecisionsAtInference;
12+
float m_TimeSinceDecision;
13+
[HideInInspector]
14+
public int m_Position;
15+
const int k_SmallGoalPosition = 7;
16+
const int k_LargeGoalPosition = 17;
17+
public GameObject largeGoal;
18+
public GameObject smallGoal;
19+
const int k_MinPosition = 0;
20+
const int k_MaxPosition = 20;
21+
public const int k_Extents = k_MaxPosition - k_MinPosition;
22+
23+
Agent m_Agent;
24+
25+
public void OnEnable()
26+
{
27+
m_Agent = GetComponent<Agent>();
28+
ResetAgent();
29+
}
30+
31+
/// <summary>
32+
/// Controls the movement of the GameObject based on the actions received.
33+
/// </summary>
34+
/// <param name="vectorAction"></param>
35+
public void ApplyAction(float[] vectorAction)
36+
{
37+
var movement = (int)vectorAction[0];
38+
39+
var direction = 0;
40+
41+
switch (movement)
42+
{
43+
case 1:
44+
direction = -1;
45+
break;
46+
case 2:
47+
direction = 1;
48+
break;
49+
}
50+
51+
m_Position += direction;
52+
if (m_Position < k_MinPosition) { m_Position = k_MinPosition; }
53+
if (m_Position > k_MaxPosition) { m_Position = k_MaxPosition; }
54+
55+
gameObject.transform.position = new Vector3(m_Position - 10f, 0f, 0f);
56+
57+
m_Agent.AddReward(-0.01f);
58+
59+
if (m_Position == k_SmallGoalPosition)
60+
{
61+
m_Agent.AddReward(0.1f);
62+
m_Agent.Done();
63+
ResetAgent();
64+
}
65+
66+
if (m_Position == k_LargeGoalPosition)
67+
{
68+
m_Agent.AddReward(1f);
69+
m_Agent.Done();
70+
ResetAgent();
71+
}
72+
}
73+
74+
public void ResetAgent()
75+
{
76+
m_Position = 10;
77+
smallGoal.transform.position = new Vector3(k_SmallGoalPosition - 10f, 0f, 0f);
78+
largeGoal.transform.position = new Vector3(k_LargeGoalPosition - 10f, 0f, 0f);
79+
}
80+
81+
public void FixedUpdate()
82+
{
83+
WaitTimeInference();
84+
}
85+
86+
void WaitTimeInference()
87+
{
88+
if (Academy.Instance.IsCommunicatorOn)
89+
{
90+
// Apply the previous step's actions
91+
ApplyAction(m_Agent.GetAction());
92+
m_Agent.RequestDecision();
93+
}
94+
else
95+
{
96+
if (m_TimeSinceDecision >= timeBetweenDecisionsAtInference)
97+
{
98+
// Apply the previous step's actions
99+
ApplyAction(m_Agent.GetAction());
100+
101+
m_TimeSinceDecision = 0f;
102+
m_Agent.RequestDecision();
103+
}
104+
else
105+
{
106+
m_TimeSinceDecision += Time.fixedDeltaTime;
107+
}
108+
}
109+
}
110+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using MLAgents.Sensors;
3+
using UnityEngine.Serialization;
4+
5+
namespace MLAgentsExamples
6+
{
7+
/// <summary>
8+
/// A simple example of a SensorComponent.
9+
/// This should be added to the same GameObject as the BasicController
10+
/// </summary>
11+
public class BasicSensorComponent : SensorComponent
12+
{
13+
public BasicController basicController;
14+
15+
/// <summary>
16+
/// Creates a BasicSensor.
17+
/// </summary>
18+
/// <returns></returns>
19+
public override ISensor CreateSensor()
20+
{
21+
return new BasicSensor(basicController);
22+
}
23+
24+
/// <inheritdoc/>
25+
public override int[] GetObservationShape()
26+
{
27+
return new[] { BasicController.k_Extents };
28+
}
29+
}
30+
31+
/// <summary>
32+
/// Simple Sensor implementation that uses a one-hot encoding of the Agent's
33+
/// position as the observation.
34+
/// </summary>
35+
public class BasicSensor : SensorBase
36+
{
37+
public BasicController basicController;
38+
39+
public BasicSensor(BasicController controller)
40+
{
41+
basicController = controller;
42+
}
43+
44+
/// <summary>
45+
/// Generate the observations for the sensor.
46+
/// In this case, the observations are all 0 except for a 1 at the position of the agent.
47+
/// </summary>
48+
/// <param name="output"></param>
49+
public override void WriteObservation(float[] output)
50+
{
51+
// One-hot encoding of the position
52+
Array.Clear(output, 0, output.Length);
53+
output[basicController.m_Position] = 1;
54+
}
55+
56+
/// <inheritdoc/>
57+
public override int[] GetObservationShape()
58+
{
59+
return new[] { BasicController.k_Extents };
60+
}
61+
62+
/// <inheritdoc/>
63+
public override string GetName()
64+
{
65+
return "Basic";
66+
}
67+
68+
}
69+
}

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

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)