Example #1
0
        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 });

            sensor.Update();
            wrapped.AddObservation(new[] { 3f, 4f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 2f, 3f, 4f });

            sensor.Update();
            wrapped.AddObservation(new[] { 5f, 6f });
            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f, 5f, 6f });

            sensor.Update();
            wrapped.AddObservation(new[] { 7f, 8f });
            SensorTestHelper.CompareObservation(sensor, new[] { 3f, 4f, 5f, 6f, 7f, 8f });

            sensor.Update();
            wrapped.AddObservation(new[] { 9f, 10f });
            SensorTestHelper.CompareObservation(sensor, new[] { 5f, 6f, 7f, 8f, 9f, 10f });

            // Check that if we don't call Update(), the same observations are produced
            SensorTestHelper.CompareObservation(sensor, new[] { 5f, 6f, 7f, 8f, 9f, 10f });
        }
Example #2
0
        public void TestAddObservationFloat()
        {
            var sensor = new VectorSensor(1);

            sensor.AddObservation(1.2f);
            SensorTestHelper.CompareObservation(sensor, new[] { 1.2f });
        }
Example #3
0
        public void TestAddObservationQuaternion()
        {
            var sensor = new VectorSensor(4);

            sensor.AddObservation(Quaternion.identity);
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 1f });
        }
Example #4
0
        public void TestAddObservationInt()
        {
            var sensor = new VectorSensor(1);

            sensor.AddObservation(42);
            SensorTestHelper.CompareObservation(sensor, new[] { 42f });
        }
Example #5
0
        public void TestAddObservationOneHot()
        {
            var sensor = new VectorSensor(4);

            sensor.AddOneHotObservation(2, 4);
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 0f });
        }
Example #6
0
        public void TestAddObservationBool()
        {
            var sensor = new VectorSensor(1);

            sensor.AddObservation(true);
            SensorTestHelper.CompareObservation(sensor, new[] { 1f });
        }
Example #7
0
        public void AssertStackingReset()
        {
            var agentGo1 = new GameObject("TestAgent");
            var bp1      = agentGo1.AddComponent <BehaviorParameters>();

            bp1.BrainParameters.NumStackedVectorObservations = 3;
            bp1.BrainParameters.ActionSpec = ActionSpec.MakeContinuous(1);
            var aca    = Academy.Instance;
            var agent1 = agentGo1.AddComponent <TestAgent>();
            var policy = new TestPolicy();

            agent1.SetPolicy(policy);

            StackingSensor sensor = null;

            foreach (ISensor s in agent1.sensors)
            {
                if (s is StackingSensor)
                {
                    sensor = s as StackingSensor;
                }
            }

            Assert.NotNull(sensor);

            for (int i = 0; i < 20; i++)
            {
                agent1.RequestDecision();
                aca.EnvironmentStep();
            }

            policy.OnRequestDecision = () => SensorTestHelper.CompareObservation(sensor, new[] { 18f, 19f, 21f });
            agent1.EndEpisode();
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f });
        }
Example #8
0
        public void TestWriteEnumerable()
        {
            var sensor = new VectorSensor(4);

            sensor.AddObservation(new[] { 1f, 2f, 3f, 4f });

            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f });
        }
Example #9
0
        public void TestWriteTooMany()
        {
            var sensor = new VectorSensor(2);

            sensor.AddObservation(new[] { 1f, 2f, 3f, 4f });

            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f });
        }
Example #10
0
        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 });
        }
Example #11
0
        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 });
        }
Example #12
0
        public void TestStackingReset()
        {
            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 });

            sensor.Update();
            wrapped.AddObservation(new[] { 3f, 4f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 2f, 3f, 4f });

            sensor.Reset();
            wrapped.AddObservation(new[] { 5f, 6f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f, 5f, 6f });
        }
Example #13
0
        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 });
            // Check that if we don't call Update(), the same observations are produced
            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f });

            // Check that Update() clears the data
            sensor.Update();
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f });
        }
        public void Test3DStacking()
        {
            var wrapped = new Dummy3DSensor();

            wrapped.ObservationSpec = ObservationSpec.Visual(2, 1, 2);
            var sensor = new StackingSensor(wrapped, 2);

            // Check the stacking is on the last dimension
            wrapped.CurrentObservation = new[, , ] {
                { { 1f, 2f } }, { { 3f, 4f } }
            };
            SensorTestHelper.CompareObservation(sensor, new[, , ] {
                { { 0f, 0f, 1f, 2f } }, { { 0f, 0f, 3f, 4f } }
            });

            sensor.Update();
            wrapped.CurrentObservation = new[, , ] {
                { { 5f, 6f } }, { { 7f, 8f } }
            };
            SensorTestHelper.CompareObservation(sensor, new[, , ] {
                { { 1f, 2f, 5f, 6f } }, { { 3f, 4f, 7f, 8f } }
            });

            sensor.Update();
            wrapped.CurrentObservation = new[, , ] {
                { { 9f, 10f } }, { { 11f, 12f } }
            };
            SensorTestHelper.CompareObservation(sensor, new[, , ] {
                { { 5f, 6f, 9f, 10f } }, { { 7f, 8f, 11f, 12f } }
            });

            // Check that if we don't call Update(), the same observations are produced
            SensorTestHelper.CompareObservation(sensor, new[, , ] {
                { { 5f, 6f, 9f, 10f } }, { { 7f, 8f, 11f, 12f } }
            });

            // Test reset
            sensor.Reset();
            wrapped.CurrentObservation = new[, , ] {
                { { 13f, 14f } }, { { 15f, 16f } }
            };
            SensorTestHelper.CompareObservation(sensor, new[, , ] {
                { { 0f, 0f, 13f, 14f } }, { { 0f, 0f, 15f, 16f } }
            });
        }
Example #15
0
        public void TestVectorStacking()
        {
            VectorSensor   wrapped = new VectorSensor(2);
            StackingSensor sensor  = new StackingSensor(wrapped, 3);

            wrapped.AddObservation(new[] { 1f, 2f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f, 1f, 2f });
            var data = sensor.GetStackedObservations();

            Assert.IsTrue(data.ToArray().SequenceEqual(new[] { 0f, 0f, 0f, 0f, 1f, 2f }));

            sensor.Update();
            wrapped.AddObservation(new[] { 3f, 4f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 2f, 3f, 4f });
            data = sensor.GetStackedObservations();
            Assert.IsTrue(data.ToArray().SequenceEqual(new[] { 0f, 0f, 1f, 2f, 3f, 4f }));

            sensor.Update();
            wrapped.AddObservation(new[] { 5f, 6f });
            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f, 5f, 6f });
            data = sensor.GetStackedObservations();
            Assert.IsTrue(data.ToArray().SequenceEqual(new[] { 1f, 2f, 3f, 4f, 5f, 6f }));

            sensor.Update();
            wrapped.AddObservation(new[] { 7f, 8f });
            SensorTestHelper.CompareObservation(sensor, new[] { 3f, 4f, 5f, 6f, 7f, 8f });
            data = sensor.GetStackedObservations();
            Assert.IsTrue(data.ToArray().SequenceEqual(new[] { 3f, 4f, 5f, 6f, 7f, 8f }));

            sensor.Update();
            wrapped.AddObservation(new[] { 9f, 10f });
            SensorTestHelper.CompareObservation(sensor, new[] { 5f, 6f, 7f, 8f, 9f, 10f });
            data = sensor.GetStackedObservations();
            Assert.IsTrue(data.ToArray().SequenceEqual(new[] { 5f, 6f, 7f, 8f, 9f, 10f }));

            // Check that if we don't call Update(), the same observations are produced
            SensorTestHelper.CompareObservation(sensor, new[] { 5f, 6f, 7f, 8f, 9f, 10f });
            data = sensor.GetStackedObservations();
            Assert.IsTrue(data.ToArray().SequenceEqual(new[] { 5f, 6f, 7f, 8f, 9f, 10f }));
        }