File size: 1,791 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Unity.MLAgents.Sensors;

namespace Unity.MLAgentsExamples
{
    /// <summary>
    /// A simple sensor that provides a number default implementations.
    /// </summary>
    public abstract class SensorBase : ISensor
    {
        /// <summary>
        /// Write the observations to the output buffer. This size of the buffer will be product
        /// of the Shape array values returned by <see cref="ObservationSpec"/>.
        /// </summary>
        /// <param name="output"></param>
        public abstract void WriteObservation(float[] output);

        /// <inheritdoc/>
        public abstract ObservationSpec GetObservationSpec();

        /// <inheritdoc/>
        public abstract string GetName();

        /// <summary>
        /// Default implementation of Write interface. This creates a temporary array,
        /// calls WriteObservation, and then writes the results to the ObservationWriter.
        /// </summary>
        /// <param name="writer"></param>
        /// <returns>The number of elements written.</returns>
        public virtual int Write(ObservationWriter writer)
        {
            // TODO reuse buffer for similar agents
            var numFloats = this.ObservationSize();
            float[] buffer = new float[numFloats];
            WriteObservation(buffer);

            writer.AddList(buffer);

            return numFloats;
        }

        /// <inheritdoc/>
        public void Update() { }

        /// <inheritdoc/>
        public void Reset() { }

        /// <inheritdoc/>
        public virtual byte[] GetCompressedObservation()
        {
            return null;
        }

        /// <inheritdoc/>
        public virtual CompressionSpec GetCompressionSpec()
        {
            return CompressionSpec.Default();
        }
    }
}