File size: 7,161 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;
namespace Unity.MLAgents.Sensors
{
/// <summary>
/// A sensor implementation for vector observations.
/// </summary>
public class VectorSensor : ISensor, IBuiltInSensor
{
// TODO use float[] instead
// TODO allow setting float[]
List<float> m_Observations;
ObservationSpec m_ObservationSpec;
string m_Name;
/// <summary>
/// Initializes the sensor.
/// </summary>
/// <param name="observationSize">Number of vector observations.</param>
/// <param name="name">Name of the sensor.</param>
/// <param name="observationType"></param>
public VectorSensor(int observationSize, string name = null, ObservationType observationType = ObservationType.Default)
{
if (string.IsNullOrEmpty(name))
{
name = $"VectorSensor_size{observationSize}";
if (observationType != ObservationType.Default)
{
name += $"_{observationType.ToString()}";
}
}
m_Observations = new List<float>(observationSize);
m_Name = name;
m_ObservationSpec = ObservationSpec.Vector(observationSize, observationType);
}
/// <inheritdoc/>
public int Write(ObservationWriter writer)
{
var expectedObservations = m_ObservationSpec.Shape[0];
if (m_Observations.Count > expectedObservations)
{
// Too many observations, truncate
Debug.LogWarningFormat(
"More observations ({0}) made than vector observation size ({1}). The observations will be truncated.",
m_Observations.Count, expectedObservations
);
m_Observations.RemoveRange(expectedObservations, m_Observations.Count - expectedObservations);
}
else if (m_Observations.Count < expectedObservations)
{
// Not enough observations; pad with zeros.
Debug.LogWarningFormat(
"Fewer observations ({0}) made than vector observation size ({1}). The observations will be padded.",
m_Observations.Count, expectedObservations
);
for (int i = m_Observations.Count; i < expectedObservations; i++)
{
m_Observations.Add(0);
}
}
writer.AddList(m_Observations);
return expectedObservations;
}
/// <summary>
/// Returns a read-only view of the observations that added.
/// </summary>
/// <returns>A read-only view of the observations list.</returns>
internal ReadOnlyCollection<float> GetObservations()
{
return m_Observations.AsReadOnly();
}
/// <inheritdoc/>
public void Update()
{
Clear();
}
/// <inheritdoc/>
public void Reset()
{
Clear();
}
/// <inheritdoc/>
public ObservationSpec GetObservationSpec()
{
return m_ObservationSpec;
}
/// <inheritdoc/>
public string GetName()
{
return m_Name;
}
/// <inheritdoc/>
public virtual byte[] GetCompressedObservation()
{
return null;
}
/// <inheritdoc/>
public CompressionSpec GetCompressionSpec()
{
return CompressionSpec.Default();
}
/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.VectorSensor;
}
void Clear()
{
m_Observations.Clear();
}
void AddFloatObs(float obs)
{
Utilities.DebugCheckNanAndInfinity(obs, nameof(obs), nameof(AddFloatObs));
m_Observations.Add(obs);
}
// Compatibility methods with Agent observation. These should be removed eventually.
/// <summary>
/// Adds a float observation to the vector observations of the agent.
/// </summary>
/// <param name="observation">Observation.</param>
public void AddObservation(float observation)
{
AddFloatObs(observation);
}
/// <summary>
/// Adds an integer observation to the vector observations of the agent.
/// </summary>
/// <param name="observation">Observation.</param>
public void AddObservation(int observation)
{
AddFloatObs(observation);
}
/// <summary>
/// Adds an Vector3 observation to the vector observations of the agent.
/// </summary>
/// <param name="observation">Observation.</param>
public void AddObservation(Vector3 observation)
{
AddFloatObs(observation.x);
AddFloatObs(observation.y);
AddFloatObs(observation.z);
}
/// <summary>
/// Adds an Vector2 observation to the vector observations of the agent.
/// </summary>
/// <param name="observation">Observation.</param>
public void AddObservation(Vector2 observation)
{
AddFloatObs(observation.x);
AddFloatObs(observation.y);
}
/// <summary>
/// Adds a list or array of float observations to the vector observations of the agent.
/// </summary>
/// <param name="observation">Observation.</param>
public void AddObservation(IList<float> observation)
{
for (var i = 0; i < observation.Count; i++)
{
AddFloatObs(observation[i]);
}
}
/// <summary>
/// Adds a quaternion observation to the vector observations of the agent.
/// </summary>
/// <param name="observation">Observation.</param>
public void AddObservation(Quaternion observation)
{
AddFloatObs(observation.x);
AddFloatObs(observation.y);
AddFloatObs(observation.z);
AddFloatObs(observation.w);
}
/// <summary>
/// Adds a boolean observation to the vector observation of the agent.
/// </summary>
/// <param name="observation">Observation.</param>
public void AddObservation(bool observation)
{
AddFloatObs(observation ? 1f : 0f);
}
/// <summary>
/// Adds a one-hot encoding observation.
/// </summary>
/// <param name="observation">The index of this observation.</param>
/// <param name="range">The upper limit on the value observation can take (exclusive).</param>
public void AddOneHotObservation(int observation, int range)
{
for (var i = 0; i < range; i++)
{
AddFloatObs(i == observation ? 1.0f : 0.0f);
}
}
}
}
|