-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
VectorSensor and StackedSensor #2813
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8ff5a64
WIP VectorSensor and StackedSensor
dcd0b1b
fix a few dumb mistakes
88a67c4
more VectorSensor
bb36edb
Merge remote-tracking branch 'origin/develop' into develop-vector-obs…
0d4b4cc
remove Update(), add util methods, hook into TensorGenerator
9f9d6d2
WriteApdater to write to tensors and arrays
fde0543
write float observations
b36a3a3
used circular buffer for stacked obs
f95f7b7
cleanup
c1757d3
fix unit tests
a58f163
docstrings
7e5cc7f
undo accidental checkins
9f0e9eb
rider suggestions, add range check
3987ce3
bounds check before writing
35edfd3
undo ProjectVersion.txt change
d0e9253
fix unit tests
6cb0b3a
unit test for VectorSensor
ac6d38c
StackingSensor tests
221ebee
missing meta file
8713179
missing meta file
22d8020
WriteAdapter tests
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 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
This file contains 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
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
UnitySDK/Assets/ML-Agents/Editor/Tests/Sensor/StackingSensorTests.cs
This file contains 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,42 @@ | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using MLAgents.Sensor; | ||
|
||
namespace MLAgents.Tests | ||
{ | ||
public class StackingSensorTests | ||
{ | ||
[Test] | ||
public void TestCtor() | ||
{ | ||
ISensor wrapped = new VectorSensor(4); | ||
ISensor sensor = new StackingSensor(wrapped, 4); | ||
Assert.AreEqual("StackingSensor_size4_VectorSensor_size4", sensor.GetName()); | ||
Assert.AreEqual(sensor.GetFloatObservationShape(), new [] {16}); | ||
} | ||
|
||
[Test] | ||
public void TestStacking() | ||
{ | ||
VectorSensor wrapped = new VectorSensor(2); | ||
ISensor sensor = new StackingSensor(wrapped, 3); | ||
|
||
wrapped.AddObservation(new [] {1f, 2f}); | ||
SensorTestHelper.CompareObservation(sensor, new [] {0f, 0f, 0f, 0f, 1f, 2f}); | ||
|
||
wrapped.AddObservation(new [] {3f, 4f}); | ||
SensorTestHelper.CompareObservation(sensor, new [] {0f, 0f, 1f, 2f, 3f, 4f}); | ||
|
||
wrapped.AddObservation(new [] {5f, 6f}); | ||
SensorTestHelper.CompareObservation(sensor, new [] {1f, 2f, 3f, 4f, 5f, 6f}); | ||
|
||
wrapped.AddObservation(new [] {7f, 8f}); | ||
SensorTestHelper.CompareObservation(sensor, new [] {3f, 4f, 5f, 6f, 7f, 8f}); | ||
|
||
wrapped.AddObservation(new [] {9f, 10f}); | ||
SensorTestHelper.CompareObservation(sensor, new [] {5f, 6f, 7f, 8f, 9f, 10f}); | ||
} | ||
|
||
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
UnitySDK/Assets/ML-Agents/Editor/Tests/Sensor/StackingSensorTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
UnitySDK/Assets/ML-Agents/Editor/Tests/Sensor/VectorSensorTests.cs
This file contains 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,138 @@ | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using MLAgents.Sensor; | ||
|
||
namespace MLAgents.Tests | ||
{ | ||
public class SensorTestHelper | ||
{ | ||
public static void CompareObservation(ISensor sensor, float[] expected) | ||
{ | ||
var numExpected = expected.Length; | ||
const float fill = -1337f; | ||
var output = new float[numExpected]; | ||
for (var i = 0; i < numExpected; i++) | ||
{ | ||
output[i] = fill; | ||
} | ||
Assert.AreEqual(fill, output[0]); | ||
|
||
WriteAdapter writer = new WriteAdapter(); | ||
writer.SetTarget(output, 0); | ||
|
||
// Make sure WriteAdapter didn't touch anything | ||
Assert.AreEqual(fill, output[0]); | ||
|
||
sensor.Write(writer); | ||
for (var i = 0; i < numExpected; i++) | ||
{ | ||
Assert.AreEqual(expected[i], output[i]); | ||
} | ||
} | ||
} | ||
|
||
public class VectorSensorTests | ||
{ | ||
[Test] | ||
public void TestCtor() | ||
{ | ||
ISensor sensor = new VectorSensor(4); | ||
Assert.AreEqual("VectorSensor_size4", sensor.GetName()); | ||
|
||
sensor = new VectorSensor(3, "test_sensor"); | ||
Assert.AreEqual("test_sensor", sensor.GetName()); | ||
} | ||
|
||
[Test] | ||
public void TestWrite() | ||
{ | ||
var sensor = new VectorSensor(4); | ||
sensor.AddObservation(1f); | ||
sensor.AddObservation(2f); | ||
sensor.AddObservation(3f); | ||
sensor.AddObservation(4f); | ||
|
||
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f }); | ||
} | ||
|
||
[Test] | ||
public void TestAddObservationFloat() | ||
{ | ||
var sensor = new VectorSensor(1); | ||
sensor.AddObservation(1.2f); | ||
SensorTestHelper.CompareObservation(sensor, new []{1.2f}); | ||
} | ||
|
||
[Test] | ||
public void TestAddObservationInt() | ||
{ | ||
var sensor = new VectorSensor(1); | ||
sensor.AddObservation(42); | ||
SensorTestHelper.CompareObservation(sensor, new []{42f}); | ||
} | ||
|
||
[Test] | ||
public void TestAddObservationVec() | ||
{ | ||
var sensor = new VectorSensor(3); | ||
sensor.AddObservation(new Vector3(1,2,3)); | ||
SensorTestHelper.CompareObservation(sensor, new []{1f, 2f, 3f}); | ||
|
||
sensor = new VectorSensor(2); | ||
sensor.AddObservation(new Vector2(4,5)); | ||
SensorTestHelper.CompareObservation(sensor, new[] { 4f, 5f }); | ||
} | ||
|
||
[Test] | ||
public void TestAddObservationQuaternion() | ||
{ | ||
var sensor = new VectorSensor(4); | ||
sensor.AddObservation(Quaternion.identity); | ||
SensorTestHelper.CompareObservation(sensor, new []{0f, 0f, 0f, 1f}); | ||
} | ||
|
||
[Test] | ||
public void TestWriteEnumerable() | ||
{ | ||
var sensor = new VectorSensor(4); | ||
sensor.AddObservation(new [] {1f, 2f, 3f, 4f}); | ||
|
||
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f }); | ||
} | ||
|
||
[Test] | ||
public void TestAddObservationBool() | ||
{ | ||
var sensor = new VectorSensor(1); | ||
sensor.AddObservation(true); | ||
SensorTestHelper.CompareObservation(sensor, new []{1f}); | ||
} | ||
|
||
[Test] | ||
public void TestAddObservationOneHot() | ||
{ | ||
var sensor = new VectorSensor(4); | ||
sensor.AddOneHotObservation(2, 4); | ||
SensorTestHelper.CompareObservation(sensor, new []{0f, 0f, 1f, 0f}); | ||
} | ||
|
||
[Test] | ||
public void TestWriteTooMany() | ||
{ | ||
var sensor = new VectorSensor(2); | ||
sensor.AddObservation(new [] {1f, 2f, 3f, 4f}); | ||
|
||
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f}); | ||
} | ||
|
||
[Test] | ||
public void TestWriteNotEnough() | ||
{ | ||
var sensor = new VectorSensor(4); | ||
sensor.AddObservation(new [] {1f, 2f}); | ||
|
||
// Make sure extra zeros are added | ||
SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 0f, 0f}); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
UnitySDK/Assets/ML-Agents/Editor/Tests/Sensor/VectorSensorTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Rename since it returns Agents. This changed a fair amount in order to initialize the agents before returning