浏览代码

csharp cleanup (#3392) (#3395)

* clean up examples and tests

* more cleanup

* m_UseChildSensors
/asymm-envs
GitHub 5 年前
当前提交
d20bda06
共有 26 个文件被更改,包括 60 次插入85 次删除
  1. 2
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs
  2. 10
      Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
  3. 7
      Project/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerFieldArea.cs
  4. 26
      com.unity.ml-agents/Runtime/Academy.cs
  5. 1
      com.unity.ml-agents/Runtime/Agent.cs
  6. 11
      com.unity.ml-agents/Runtime/DecisionRequester.cs
  7. 4
      com.unity.ml-agents/Runtime/EpisodeIdCounter.cs
  8. 1
      com.unity.ml-agents/Runtime/Grpc/GrpcExtensions.cs
  9. 12
      com.unity.ml-agents/Runtime/Grpc/RpcCommunicator.cs
  10. 3
      com.unity.ml-agents/Runtime/ICommunicator.cs
  11. 4
      com.unity.ml-agents/Runtime/InferenceBrain/ApplierImpl.cs
  12. 2
      com.unity.ml-agents/Runtime/InferenceBrain/GeneratorImpl.cs
  13. 1
      com.unity.ml-agents/Runtime/InferenceBrain/TensorApplier.cs
  14. 2
      com.unity.ml-agents/Runtime/InferenceBrain/TensorGenerator.cs
  15. 2
      com.unity.ml-agents/Runtime/Policy/BarracudaPolicy.cs
  16. 15
      com.unity.ml-agents/Runtime/Policy/BehaviorParameters.cs
  17. 3
      com.unity.ml-agents/Runtime/Policy/IPolicy.cs
  18. 1
      com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs
  19. 2
      com.unity.ml-agents/Runtime/Sensor/SensorShapeValidator.cs
  20. 1
      com.unity.ml-agents/Runtime/SideChannel/EngineConfigurationChannel.cs
  21. 8
      com.unity.ml-agents/Runtime/SideChannel/FloatPropertiesChannel.cs
  22. 4
      com.unity.ml-agents/Runtime/SideChannel/RawBytesChannel.cs
  23. 8
      com.unity.ml-agents/Tests/Editor/EditModeTestInternalBrainTensorApplier.cs
  24. 1
      com.unity.ml-agents/Tests/Editor/EditModeTestInternalBrainTensorGenerator.cs
  25. 11
      com.unity.ml-agents/Tests/Editor/SideChannelTests.cs
  26. 3
      com.unity.ml-agents/Tests/Editor/TimerTest.cs

2
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs


var nnModel = GetModelForBehaviorName(bp.behaviorName);
Debug.Log($"Overriding behavior {bp.behaviorName} for agent with model {nnModel?.name}");
// This might give a null model; that's better because we'll fall back to the Heuristic
m_Agent.GiveModel($"Override_{bp.behaviorName}", nnModel, InferenceDevice.CPU);
m_Agent.GiveModel($"Override_{bp.behaviorName}", nnModel);
}
}

10
Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs


[HideInInspector]
public Rigidbody agentRb;
SoccerSettings m_SoccerSettings;
Renderer m_AgentRenderer;
BehaviorParameters m_BP;
BehaviorParameters m_BehaviorParameters;
m_BP = gameObject.GetComponent<BehaviorParameters>();
if (m_BP.m_TeamID == (int)Team.Blue)
m_BehaviorParameters = gameObject.GetComponent<BehaviorParameters>();
if (m_BehaviorParameters.m_TeamID == (int)Team.Blue)
{
team = Team.Blue;
m_Transform = new Vector3(transform.position.x - 4f, .5f, transform.position.z);

team = Team.Purple;
m_Transform = new Vector3(transform.position.x + 4f, .5f, transform.position.z);
}
m_AgentRenderer = GetComponentInChildren<Renderer>();
m_SoccerSettings = FindObjectOfType<SoccerSettings>();
agentRb = GetComponent<Rigidbody>();
agentRb.maxAngularVelocity = 500;

{
var dirToGo = Vector3.zero;
var rotateDir = Vector3.zero;
var action = Mathf.FloorToInt(act[0]);
m_KickPower = 0f;

7
Project/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerFieldArea.cs


public GameObject goalTextUI;
[HideInInspector]
public bool canResetBall;
Material m_GroundMaterial;
Renderer m_GroundRenderer;
SoccerSettings m_SoccerSettings;
m_SoccerSettings = FindObjectOfType<SoccerSettings>();
m_GroundRenderer = centerPitch.GetComponent<Renderer>();
m_GroundMaterial = m_GroundRenderer.material;
canResetBall = true;
if (goalTextUI) { goalTextUI.SetActive(false); }
ballRb = ball.GetComponent<Rigidbody>();

26
com.unity.ml-agents/Runtime/Academy.cs


const int k_EditorTrainingPort = 5004;
// Lazy initializer pattern, see https://csharpindepth.com/articles/singleton#lazy
static Lazy<Academy> lazy = new Lazy<Academy>(() => new Academy());
static Lazy<Academy> s_Lazy = new Lazy<Academy>(() => new Academy());
get { return lazy.IsValueCreated; }
get { return s_Lazy.IsValueCreated; }
public static Academy Instance { get { return lazy.Value; } }
public static Academy Instance { get { return s_Lazy.Value; } }
public IFloatProperties FloatProperties;

// Signals to all the Agents at each environment step so they can use
// their Policy to decide on their next action.
internal event System.Action DecideAction;
internal event Action DecideAction;
internal event System.Action DestroyAction;
internal event Action DestroyAction;
internal event System.Action<int> AgentSetStatus;
internal event Action<int> AgentSetStatus;
internal event System.Action AgentSendState;
internal event Action AgentSendState;
internal event System.Action AgentAct;
internal event Action AgentAct;
internal event System.Action AgentForceReset;
internal event Action AgentForceReset;
public event System.Action OnEnvironmentReset;
public event Action OnEnvironmentReset;
AcademyFixedUpdateStepper m_FixedUpdateStepper;
GameObject m_StepperObject;

//environment must use Inference.
try
{
var unityRLInitParameters = Communicator.Initialize(
var unityRlInitParameters = Communicator.Initialize(
UnityEngine.Random.InitState(unityRLInitParameters.seed);
UnityEngine.Random.InitState(unityRlInitParameters.seed);
}
catch
{

m_Initialized = false;
// Reset the Lazy instance
lazy = new Lazy<Academy>(() => new Academy());
s_Lazy = new Lazy<Academy>(() => new Academy());
}
}
}

1
com.unity.ml-agents/Runtime/Agent.cs


/// List of sensors used to generate observations.
/// Currently generated from attached SensorComponents, and a legacy VectorSensor
/// </summary>
[FormerlySerializedAs("m_Sensors")]
internal List<ISensor> sensors;
/// <summary>

11
com.unity.ml-agents/Runtime/DecisionRequester.cs


using System;
using System.Collections.Generic;
using Barracuda;
using UnityEngine.Serialization;
namespace MLAgents
{

[Tooltip("Whether or not Agent decisions should start at a random offset.")]
public bool offsetStep;
private Agent m_Agent;
private int offset;
Agent m_Agent;
int m_Offset;
offset = offsetStep ? gameObject.GetInstanceID() : 0;
m_Offset = offsetStep ? gameObject.GetInstanceID() : 0;
m_Agent = gameObject.GetComponent<Agent>();
Academy.Instance.AgentSetStatus += MakeRequests;
}

void MakeRequests(int count)
{
if ((count + offset) % DecisionPeriod == 0)
if ((count + m_Offset) % DecisionPeriod == 0)
{
m_Agent?.RequestDecision();
}

4
com.unity.ml-agents/Runtime/EpisodeIdCounter.cs


{
internal static class EpisodeIdCounter
{
private static int Counter;
static int s_Counter;
return Counter++;
return s_Counter++;
}
}
}

1
com.unity.ml-agents/Runtime/Grpc/GrpcExtensions.cs


using System.Collections.Generic;
using System.Linq;
using Google.Protobuf;
using Google.Protobuf.Collections;
using MLAgents.CommunicatorObjects;
using UnityEngine;
using System.Runtime.CompilerServices;

12
com.unity.ml-agents/Runtime/Grpc/RpcCommunicator.cs


/// Sends the observations of one Agent.
/// </summary>
/// <param name="behaviorName">Batch Key.</param>
/// <param name="agent">Agent info.</param>
/// <param name="info">Agent info.</param>
/// <param name="sensors">Sensors that will produce the observations</param>
public void PutObservations(string behaviorName, AgentInfo info, List<ISensor> sensors)
{
# if DEBUG

/// <param name="sideChannel"> The side channel to be registered.</param>
public void RegisterSideChannel(SideChannel sideChannel)
{
if (m_SideChannels.ContainsKey(sideChannel.ChannelType()))
var channelType = sideChannel.ChannelType();
if (m_SideChannels.ContainsKey(channelType))
"A side channel with type index {} is already registered. You cannot register multiple " +
"side channels of the same type."));
"A side channel with type index {0} is already registered. You cannot register multiple " +
"side channels of the same type.", channelType));
m_SideChannels.Add(sideChannel.ChannelType(), sideChannel);
m_SideChannels.Add(channelType, sideChannel);
}
/// <summary>

3
com.unity.ml-agents/Runtime/ICommunicator.cs


using System;
using System.Collections.Generic;
using UnityEngine;
using MLAgents.CommunicatorObjects;
namespace MLAgents
{

/// <summary>
/// Delegate for handling reset parameter updates sent from the communicator.
/// </summary>
/// <param name="resetParams"></param>
internal delegate void ResetCommandHandler();
/// <summary>

/// <param name="brainKey">Batch Key.</param>
/// <param name="info">Agent info.</param>
/// <param name="sensors">The list of ISensors of the Agent.</param>
/// <param name="action">The action that will be called once the next AgentAction is ready.</param>
void PutObservations(string brainKey, AgentInfo info, List<ISensor> sensors);
/// <summary>

4
com.unity.ml-agents/Runtime/InferenceBrain/ApplierImpl.cs


var memorySize = (int)tensorProxy.shape[tensorProxy.shape.Length - 1];
foreach (int agentId in actionIds)
{
List<float> memory = null;
List<float> memory;
if (!m_Memories.TryGetValue(agentId, out memory)
|| memory.Count < memorySize)
{

foreach (int agentId in actionIds)
{
List<float> memory = null;
List<float> memory;
if (!m_Memories.TryGetValue(agentId, out memory)
|| memory.Count < memorySize * m_MemoriesCount)
{

2
com.unity.ml-agents/Runtime/InferenceBrain/GeneratorImpl.cs


/// </summary>
internal class RecurrentInputGenerator : TensorGenerator.IGenerator
{
private readonly ITensorAllocator m_Allocator;
readonly ITensorAllocator m_Allocator;
Dictionary<int, List<float>> m_Memories;
public RecurrentInputGenerator(

1
com.unity.ml-agents/Runtime/InferenceBrain/TensorApplier.cs


/// </param>
/// <param name="actionIds"> List of Agents Ids that will be updated using the tensor's data</param>
/// <param name="lastActions"> Dictionary of AgentId to Actions to be updated</param>
/// </param>
void Apply(TensorProxy tensorProxy, IEnumerable<int> actionIds, Dictionary<int, float[]> lastActions);
}

2
com.unity.ml-agents/Runtime/InferenceBrain/TensorGenerator.cs


/// <param name="tensors"> Enumerable of tensors that will be modified.</param>
/// <param name="currentBatchSize"> The number of agents present in the current batch
/// </param>
/// <param name="agents"> List of Agents that contains the
/// <param name="infos"> List of AgentsInfos and Sensors that contains the
/// data that will be used to modify the tensors</param>
/// <exception cref="UnityAgentsException"> One of the tensor does not have an
/// associated generator.</exception>

2
com.unity.ml-agents/Runtime/Policy/BarracudaPolicy.cs


{
protected ModelRunner m_ModelRunner;
private int m_AgentId;
int m_AgentId;
/// <summary>
/// Sensor shapes for the associated Agents. All Agents must have the same shapes for their Sensors.

15
com.unity.ml-agents/Runtime/Policy/BehaviorParameters.cs


using Barracuda;
using System;
using System.Collections.Generic;
using UnityEngine.Serialization;
namespace MLAgents
{

public class BehaviorParameters : MonoBehaviour
{
[Serializable]
private enum BehaviorType
enum BehaviorType
{
Default,
HeuristicOnly,

string m_BehaviorName = "My Behavior";
[HideInInspector]
[SerializeField]
public int m_TeamID = 0;
public int m_TeamID;
[FormerlySerializedAs("m_useChildSensors")]
bool m_useChildSensors = true;
bool m_UseChildSensors = true;
public BrainParameters brainParameters
{

public bool useChildSensors
{
get { return m_useChildSensors; }
get { return m_UseChildSensors; }
}
public string behaviorName

}
public void GiveModel(
string behaviorName,
string newBehaviorName,
m_BehaviorName = behaviorName;
m_BehaviorName = newBehaviorName;
}
}
}

3
com.unity.ml-agents/Runtime/Policy/IPolicy.cs


/// will make the decision at a later time to allow possible
/// batching of requests.
/// </summary>
/// <param name="agent"></param>
/// <param name="info"></param>
/// <param name="sensors"></param>
void RequestDecision(AgentInfo info, List<ISensor> sensors);
/// <summary>

1
com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs


/// <param name="transform">Transform of the GameObject</param>
/// <param name="castType">Whether to perform the casts in 2D or 3D.</param>
/// <param name="perceptionBuffer">Output array of floats. Must be (num rays) * (num tags + 2) in size.</param>
/// <param name="layerMask">Filtering options for the casts</param>
/// <param name="debugInfo">Optional debug information output, only used by RayPerceptionSensor.</param>
///
public static void PerceiveStatic(float unscaledRayLength,

2
com.unity.ml-agents/Runtime/Sensor/SensorShapeValidator.cs


{
internal class SensorShapeValidator
{
private List<int[]> m_SensorShapes;
List<int[]> m_SensorShapes;
/// <summary>
/// Check that the List Sensors are the same shape as the previous ones.

1
com.unity.ml-agents/Runtime/SideChannel/EngineConfigurationChannel.cs


using System.Collections.Generic;
using System.IO;
using UnityEngine;

8
com.unity.ml-agents/Runtime/SideChannel/FloatPropertiesChannel.cs


public class FloatPropertiesChannel : SideChannel, IFloatProperties
{
private Dictionary<string, float> m_FloatProperties = new Dictionary<string, float>();
private Dictionary<string, Action<float>> m_RegisteredActions = new Dictionary<string, Action<float>>();
Dictionary<string, float> m_FloatProperties = new Dictionary<string, float>();
Dictionary<string, Action<float>> m_RegisteredActions = new Dictionary<string, Action<float>>();
public override int ChannelType()
{

return new List<string>(m_FloatProperties.Keys);
}
private static KeyValuePair<string, float> DeserializeMessage(byte[] data)
static KeyValuePair<string, float> DeserializeMessage(byte[] data)
{
using (var memStream = new MemoryStream(data))
{

}
}
private static byte[] SerializeMessage(string key, float value)
static byte[] SerializeMessage(string key, float value)
{
using (var memStream = new MemoryStream())
{

4
com.unity.ml-agents/Runtime/SideChannel/RawBytesChannel.cs


{
public class RawBytesChannel : SideChannel
{
private List<byte[]> m_MessagesReceived = new List<byte[]>();
private int m_ChannelId;
List<byte[]> m_MessagesReceived = new List<byte[]>();
int m_ChannelId;
/// <summary>
/// RawBytesChannel provides a way to exchange raw byte arrays between Unity and Python.

8
com.unity.ml-agents/Tests/Editor/EditModeTestInternalBrainTensorApplier.cs


using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using System.Reflection;
using Barracuda;
using MLAgents.InferenceBrain;
using System;

{
class TestAgent : Agent
{
public AgentAction GetAction()
{
var f = typeof(Agent).GetField(
"m_Action", BindingFlags.Instance | BindingFlags.NonPublic);
return (AgentAction)f.GetValue(this);
}
}
[Test]

1
com.unity.ml-agents/Tests/Editor/EditModeTestInternalBrainTensorGenerator.cs


using NUnit.Framework;
using UnityEngine;
using MLAgents.InferenceBrain;
using System.Reflection;
namespace MLAgents.Tests

11
com.unity.ml-agents/Tests/Editor/SideChannelTests.cs


using System;
using NUnit.Framework;
using MLAgents;
using System.Collections.Generic;
using System.Text;

// This test side channel only deals in integers
public class TestSideChannel : SideChannel
{
public List<int> m_MessagesReceived = new List<int>();
public List<int> messagesReceived = new List<int>();
m_MessagesReceived.Add(BitConverter.ToInt32(data, 0));
messagesReceived.Add(BitConverter.ToInt32(data, 0));
}
public void SendInt(int data)

byte[] fakeData = RpcCommunicator.GetSideChannelMessage(dictSender);
RpcCommunicator.ProcessSideChannelData(dictReceiver, fakeData);
Assert.AreEqual(intReceiver.m_MessagesReceived[0], 4);
Assert.AreEqual(intReceiver.m_MessagesReceived[1], 5);
Assert.AreEqual(intReceiver.m_MessagesReceived[2], 6);
Assert.AreEqual(intReceiver.messagesReceived[0], 4);
Assert.AreEqual(intReceiver.messagesReceived[1], 5);
Assert.AreEqual(intReceiver.messagesReceived[2], 6);
}
[Test]

3
com.unity.ml-agents/Tests/Editor/TimerTest.cs


using NUnit.Framework;
using UnityEditor.Graphs;
using UnityEngine;
namespace MLAgents.Tests

{
using (myTimer.Scoped("bar"))
{
myTimer.SetGauge("my_gauge", (float)i);
myTimer.SetGauge("my_gauge", i);
}
}
}

正在加载...
取消
保存