File size: 1,984 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
using System.Collections.Generic;
using UnityEngine;

namespace Unity.MLAgents.Sensors
{
    public class SensorShapeValidator
    {
        List<ObservationSpec> m_SensorShapes;

        /// <summary>
        /// Check that the List Sensors are the same shape as the previous ones.
        /// If this is the first List of Sensors being checked, its Sensor sizes will be saved.
        /// </summary>
        public void ValidateSensors(List<ISensor> sensors)
        {
            if (m_SensorShapes == null)
            {
                m_SensorShapes = new List<ObservationSpec>(sensors.Count);
                // First agent, save the sensor sizes
                foreach (var sensor in sensors)
                {
                    m_SensorShapes.Add(sensor.GetObservationSpec());
                }
            }
            else
            {
                // Check for compatibility with the other Agents' Sensors
                if (m_SensorShapes.Count != sensors.Count)
                {
                    Debug.AssertFormat(
                        m_SensorShapes.Count == sensors.Count,
                        "Number of Sensors must match. {0} != {1}",
                        m_SensorShapes.Count,
                        sensors.Count
                    );
                }
                for (var i = 0; i < Mathf.Min(m_SensorShapes.Count, sensors.Count); i++)
                {
                    var cachedSpec = m_SensorShapes[i];
                    var sensorSpec = sensors[i].GetObservationSpec();
                    if (cachedSpec.Shape != sensorSpec.Shape)
                    {
                        Debug.AssertFormat(
                            cachedSpec.Shape == sensorSpec.Shape,
                            "Sensor shapes must match. {0} != {1}",
                            cachedSpec.Shape,
                            sensorSpec.Shape
                        );
                    }
                }
            }
        }
    }
}