浏览代码

Adding The sensors for goal conditioning

Default is the default
/goal-conditioning/sensors-2
vincentpierre 4 年前
当前提交
b293dd9e
共有 9 个文件被更改,包括 170 次插入7 次删除
  1. 1
      com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs
  2. 19
      com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs
  3. 15
      com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs
  4. 4
      com.unity.ml-agents/Runtime/Sensors/ITypedSensor.cs
  5. 12
      com.unity.ml-agents/Runtime/Sensors/VectorSensor.cs
  6. 42
      com.unity.ml-agents/Editor/VectorSensorComponentEditor.cs
  7. 11
      com.unity.ml-agents/Editor/VectorSensorComponentEditor.cs.meta
  8. 62
      com.unity.ml-agents/Runtime/Sensors/VectorSensorComponent.cs
  9. 11
      com.unity.ml-agents/Runtime/Sensors/VectorSensorComponent.cs.meta

1
com.unity.ml-agents/Editor/CameraSensorComponentEditor.cs


EditorGUILayout.PropertyField(so.FindProperty("m_Height"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_Grayscale"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_ObservationStacks"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_ObservationType"), true);
}
EditorGUI.EndDisabledGroup();
EditorGUILayout.PropertyField(so.FindProperty("m_Compression"), true);

19
com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs


/// <summary>
/// A sensor that wraps a Camera object to generate visual observations for an agent.
/// </summary>
public class CameraSensor : ISensor, IBuiltInSensor, IDimensionPropertiesSensor
public class CameraSensor : ISensor, ITypedSensor, IBuiltInSensor, IDimensionPropertiesSensor
{
Camera m_Camera;
int m_Width;

int[] m_Shape;
SensorCompressionType m_CompressionType;
ObservationType m_ObservationType;
static DimensionProperty[] s_DimensionProperties = new DimensionProperty[] {
DimensionProperty.TranslationalEquivariance,
DimensionProperty.TranslationalEquivariance,

set { m_CompressionType = value; }
}
public ObservationType ObservationType
{
get { return m_ObservationType; }
set { m_ObservationType = value; }
}
/// <summary>
/// Creates and returns the camera sensor.

/// <param name="name">The name of the camera sensor.</param>
/// <param name="compression">The compression to apply to the generated image.</param>
public CameraSensor(
Camera camera, int width, int height, bool grayscale, string name, SensorCompressionType compression)
Camera camera, int width, int height, bool grayscale, string name, SensorCompressionType compression,
ObservationType observationType = ObservationType.Default)
{
m_Camera = camera;
m_Width = width;

m_Shape = GenerateShape(width, height, grayscale);
m_CompressionType = compression;
m_ObservationType = observationType;
}
/// <summary>

public string GetName()
{
return m_Name;
}
/// <inheritdoc/>
public virtual ObservationType GetObservationType()
{
return m_ObservationType;
}
/// <summary>

15
com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs


set { m_SensorName = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("observationType")]
ObservationType m_ObservationType;
[HideInInspector, SerializeField, FormerlySerializedAs("width")]
int m_Width = 84;

}
/// <summary>
/// The type of the observation.
/// </summary>
public ObservationType SensorObservationType
{
get { return m_ObservationType; }
set { m_ObservationType = value; UpdateSensor(); }
}
/// <summary>
m_Sensor = new CameraSensor(m_Camera, m_Width, m_Height, Grayscale, m_SensorName, m_Compression);
m_Sensor = new CameraSensor(m_Camera, m_Width, m_Height, Grayscale, m_SensorName, m_Compression, m_ObservationType);
if (ObservationStacks != 1)
{

{
m_Sensor.Camera = m_Camera;
m_Sensor.CompressionType = m_Compression;
m_Sensor.ObservationType = m_ObservationType;
}
}
}

4
com.unity.ml-agents/Runtime/Sensors/ITypedSensor.cs


/// <summary>
/// The ObservationType enum of the Sensor.
/// </summary>
internal enum ObservationType
public enum ObservationType
{
// Collected observations are generic.
Default = 0,

/// <summary>
/// Sensor interface for sensors with variable types.
/// </summary>
internal interface ITypedSensor
public interface ITypedSensor
{
/// <summary>
/// Returns the ObservationType enum corresponding to the type of the sensor.

12
com.unity.ml-agents/Runtime/Sensors/VectorSensor.cs


/// <summary>
/// A sensor implementation for vector observations.
/// </summary>
public class VectorSensor : ISensor, IBuiltInSensor
public class VectorSensor : ISensor, ITypedSensor, IBuiltInSensor
{
// TODO use float[] instead
// TODO allow setting float[]

ObservationType m_ObservationType;
/// <summary>
/// Initializes the sensor.

public VectorSensor(int observationSize, string name = null)
public VectorSensor(int observationSize, string name = null, ObservationType observationType = ObservationType.Default)
{
if (name == null)
{

m_Observations = new List<float>(observationSize);
m_Name = name;
m_Shape = new[] { observationSize };
m_ObservationType = observationType;
}
/// <inheritdoc/>

public int[] GetObservationShape()
{
return m_Shape;
}
/// <inheritdoc/>
public virtual ObservationType GetObservationType()
{
return m_ObservationType;
}
/// <inheritdoc/>

42
com.unity.ml-agents/Editor/VectorSensorComponentEditor.cs


using UnityEditor;
using Unity.MLAgents.Sensors;
namespace Unity.MLAgents.Editor
{
[CustomEditor(typeof(VectorSensorComponent))]
[CanEditMultipleObjects]
internal class VectorSensorComponentEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
var so = serializedObject;
so.Update();
// Drawing the VectorSensorComponent
EditorGUI.BeginChangeCheck();
EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties());
{
// These fields affect the sensor order or observation size,
// So can't be changed at runtime.
EditorGUILayout.PropertyField(so.FindProperty("m_observationSize"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_ObservationType"), true);
}
EditorGUI.EndDisabledGroup();
var requireSensorUpdate = EditorGUI.EndChangeCheck();
so.ApplyModifiedProperties();
if (requireSensorUpdate)
{
UpdateSensor();
}
}
void UpdateSensor()
{
var sensorComponent = serializedObject.targetObject as VectorSensorComponent;
sensorComponent?.UpdateSensor();
}
}
}

11
com.unity.ml-agents/Editor/VectorSensorComponentEditor.cs.meta


fileFormatVersion: 2
guid: 4e900f9ce9be541c69e45ae94956932f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

62
com.unity.ml-agents/Runtime/Sensors/VectorSensorComponent.cs


using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.MLAgents.Sensors
{
[AddComponentMenu("ML Agents/Vector Sensor", (int)MenuGroup.Sensors)]
public class VectorSensorComponent : SensorComponent
{
public int ObservationSize
{
get { return m_observationSize; }
set { m_observationSize = value; }
}
[HideInInspector, SerializeField]
int m_observationSize;
[HideInInspector, SerializeField]
ObservationType m_ObservationType;
VectorSensor m_sensor;
public ObservationType ObservationType
{
get { return m_ObservationType; }
set { m_ObservationType = value; }
}
/// <summary>
/// Creates a VectorSensor.
/// </summary>
/// <returns></returns>
public override ISensor CreateSensor()
{
m_sensor = new VectorSensor(m_observationSize, observationType: m_ObservationType);
return m_sensor;
}
/// <inheritdoc/>
public override int[] GetObservationShape()
{
return new[] { m_observationSize };
}
public VectorSensor GetSensor()
{
return m_sensor;
}
/// <summary>
/// Update fields that are safe to change on the Sensor at runtime.
/// </summary>
internal void UpdateSensor()
{
if (m_sensor != null)
{
}
}
}
}

11
com.unity.ml-agents/Runtime/Sensors/VectorSensorComponent.cs.meta


fileFormatVersion: 2
guid: 58239d05cfbbd4ac1b22ba12865beca3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存