浏览代码

rename to SideChannelManager +backcompat (#4137)

/MLA-1734-demo-provider
GitHub 4 年前
当前提交
8e8a508b
共有 12 个文件被更改,包括 62 次插入36 次删除
  1. 1
      com.unity.ml-agents/CHANGELOG.md
  2. 6
      com.unity.ml-agents/Runtime/Academy.cs
  3. 4
      com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs
  4. 4
      com.unity.ml-agents/Runtime/EnvironmentParameters.cs
  5. 4
      com.unity.ml-agents/Runtime/SideChannels/SideChannel.cs
  6. 31
      com.unity.ml-agents/Runtime/SideChannels/SideChannelManager.cs
  7. 4
      com.unity.ml-agents/Runtime/StatsRecorder.cs
  8. 18
      com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs
  9. 10
      com.unity.ml-agents/Tests/Editor/SamplerTests.cs
  10. 16
      com.unity.ml-agents/Tests/Editor/SideChannelTests.cs
  11. 0
      /com.unity.ml-agents/Runtime/SideChannels/SideChannelManager.cs.meta
  12. 0
      /com.unity.ml-agents/Runtime/SideChannels/SideChannelManager.cs

1
com.unity.ml-agents/CHANGELOG.md


### Minor Changes
#### com.unity.ml-agents (C#)
- `SideChannelsManager` was renamed to `SideChannelManager`. The old name is still supported, but deprecated. (#4137)
- `RayPerceptionSensor.Perceive()` now additionally store the GameObject that was hit by the ray. (#4111)
- The Barracuda dependency was upgraded to 1.0.0 (#4118)
#### ml-agents / ml-agents-envs / gym-unity (Python)

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


EnableAutomaticStepping();
SideChannelsManager.RegisterSideChannel(new EngineConfigurationChannel());
SideChannelManager.RegisterSideChannel(new EngineConfigurationChannel());
m_EnvironmentParameters = new EnvironmentParameters();
m_StatsRecorder = new StatsRecorder();

// If the communicator is not on, we need to clear the SideChannel sending queue
if (!IsCommunicatorOn)
{
SideChannelsManager.GetSideChannelMessage();
SideChannelManager.GetSideChannelMessage();
}
using (TimerStack.Instance.Scoped("AgentAct"))

m_EnvironmentParameters.Dispose();
m_StatsRecorder.Dispose();
SideChannelsManager.UnregisterAllSideChannels(); // unregister custom side channels
SideChannelManager.UnregisterAllSideChannels(); // unregister custom side channels
if (m_ModelRunners != null)
{

4
com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs


void UpdateEnvironmentWithInput(UnityRLInputProto rlInput)
{
SideChannelsManager.ProcessSideChannelData(rlInput.SideChannel.ToArray());
SideChannelManager.ProcessSideChannelData(rlInput.SideChannel.ToArray());
SendCommandEvent(rlInput.Command);
}

message.RlInitializationOutput = tempUnityRlInitializationOutput;
}
byte[] messageAggregated = SideChannelsManager.GetSideChannelMessage();
byte[] messageAggregated = SideChannelManager.GetSideChannelMessage();
message.RlOutput.SideChannel = ByteString.CopyFrom(messageAggregated);
var input = Exchange(message);

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


internal EnvironmentParameters()
{
m_Channel = new EnvironmentParametersChannel();
SideChannelsManager.RegisterSideChannel(m_Channel);
SideChannelManager.RegisterSideChannel(m_Channel);
}
/// <summary>

internal void Dispose()
{
SideChannelsManager.UnregisterSideChannel(m_Channel);
SideChannelManager.UnregisterSideChannel(m_Channel);
}
}
}

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


/// To create your own, you'll need to create two, new mirrored classes, one in Unity (by
/// extending <see cref="SideChannel"/>) and another in Python by extending a Python class
/// also called SideChannel. Then, within your project, use
/// <see cref="SideChannelsManager.RegisterSideChannel"/> and
/// <see cref="SideChannelsManager.UnregisterSideChannel"/> to register and unregister your
/// <see cref="SideChannelManager.RegisterSideChannel"/> and
/// <see cref="SideChannelManager.UnregisterSideChannel"/> to register and unregister your
/// custom side channel.
/// </summary>
public abstract class SideChannel

31
com.unity.ml-agents/Runtime/SideChannels/SideChannelManager.cs


/// Collection of static utilities for managing the registering/unregistering of
/// <see cref="SideChannels"/> and the sending/receiving of messages for all the channels.
/// </summary>
public static class SideChannelsManager
public static class SideChannelManager
{
static Dictionary<Guid, SideChannel> s_RegisteredChannels = new Dictionary<Guid, SideChannel>();

/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
internal static T GetSideChannel<T>() where T: SideChannel
internal static T GetSideChannel<T>() where T : SideChannel
return (T) sc;
return (T)sc;
}
}
return null;

}
}
}
}
}
/// <summary>
/// Deprecated, use <see cref="SideChannelManager"/> instead.
/// </summary>
[Obsolete("Use SideChannelManager instead.")]
public static class SideChannelsManager
{
/// <summary>
/// Deprecated, use <see cref="SideChannelManager.RegisterSideChannel"/> instead.
/// </summary>
/// <param name="sideChannel"></param>
public static void RegisterSideChannel(SideChannel sideChannel)
{
SideChannelManager.RegisterSideChannel(sideChannel);
}
/// <summary>
/// Deprecated, use <see cref="SideChannelManager.UnregisterSideChannel"/> instead.
/// </summary>
/// <param name="sideChannel"></param>
public static void UnregisterSideChannel(SideChannel sideChannel)
{
SideChannelManager.UnregisterSideChannel(sideChannel);
}
}
}

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


internal StatsRecorder()
{
m_Channel = new StatsSideChannel();
SideChannelsManager.RegisterSideChannel(m_Channel);
SideChannelManager.RegisterSideChannel(m_Channel);
}
/// <summary>

internal void Dispose()
{
SideChannelsManager.UnregisterSideChannel(m_Channel);
SideChannelManager.UnregisterSideChannel(m_Channel);
}
}
}

18
com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs


Assert.AreEqual(0, aca.EpisodeCount);
Assert.AreEqual(0, aca.StepCount);
Assert.AreEqual(0, aca.TotalStepCount);
Assert.AreNotEqual(null, SideChannelsManager.GetSideChannel<EnvironmentParametersChannel>());
Assert.AreNotEqual(null, SideChannelsManager.GetSideChannel<EngineConfigurationChannel>());
Assert.AreNotEqual(null, SideChannelsManager.GetSideChannel<StatsSideChannel>());
Assert.AreNotEqual(null, SideChannelManager.GetSideChannel<EnvironmentParametersChannel>());
Assert.AreNotEqual(null, SideChannelManager.GetSideChannel<EngineConfigurationChannel>());
Assert.AreNotEqual(null, SideChannelManager.GetSideChannel<StatsSideChannel>());
// Check that Dispose is idempotent
aca.Dispose();

[Test]
public void TestAcademyDispose()
{
var envParams1 = SideChannelsManager.GetSideChannel<EnvironmentParametersChannel>();
var engineParams1 = SideChannelsManager.GetSideChannel<EngineConfigurationChannel>();
var statsParams1 = SideChannelsManager.GetSideChannel<StatsSideChannel>();
var envParams1 = SideChannelManager.GetSideChannel<EnvironmentParametersChannel>();
var engineParams1 = SideChannelManager.GetSideChannel<EngineConfigurationChannel>();
var statsParams1 = SideChannelManager.GetSideChannel<StatsSideChannel>();
var envParams2 = SideChannelsManager.GetSideChannel<EnvironmentParametersChannel>();
var engineParams2 = SideChannelsManager.GetSideChannel<EngineConfigurationChannel>();
var statsParams2 = SideChannelsManager.GetSideChannel<StatsSideChannel>();
var envParams2 = SideChannelManager.GetSideChannel<EnvironmentParametersChannel>();
var engineParams2 = SideChannelManager.GetSideChannel<EngineConfigurationChannel>();
var statsParams2 = SideChannelManager.GetSideChannel<StatsSideChannel>();
Academy.Instance.Dispose();
Assert.AreNotEqual(envParams1, envParams2);

10
com.unity.ml-agents/Tests/Editor/SamplerTests.cs


public SamplerTests()
{
m_Channel = SideChannelsManager.GetSideChannel<EnvironmentParametersChannel>();
m_Channel = SideChannelManager.GetSideChannel<EnvironmentParametersChannel>();
SideChannelsManager.RegisterSideChannel(m_Channel);
SideChannelManager.RegisterSideChannel(m_Channel);
}
}
[Test]

outgoingMsg.WriteFloat32(min_value);
outgoingMsg.WriteFloat32(max_value);
byte[] message = GetByteMessage(m_Channel, outgoingMsg);
SideChannelsManager.ProcessSideChannelData(message);
SideChannelManager.ProcessSideChannelData(message);
}
Assert.AreEqual(1.208888f, m_Channel.GetWithDefault(parameter, 1.0f), k_Epsilon);
Assert.AreEqual(1.118017f, m_Channel.GetWithDefault(parameter, 1.0f), k_Epsilon);

outgoingMsg.WriteFloat32(mean);
outgoingMsg.WriteFloat32(stddev);
byte[] message = GetByteMessage(m_Channel, outgoingMsg);
SideChannelsManager.ProcessSideChannelData(message);
SideChannelManager.ProcessSideChannelData(message);
}
Assert.AreEqual(2.936162f, m_Channel.GetWithDefault(parameter, 1.0f), k_Epsilon);
Assert.AreEqual(2.951348f, m_Channel.GetWithDefault(parameter, 1.0f), k_Epsilon);

outgoingMsg.WriteInt32((int)SamplerType.MultiRangeUniform);
outgoingMsg.WriteFloatList(intervals);
byte[] message = GetByteMessage(m_Channel, outgoingMsg);
SideChannelsManager.ProcessSideChannelData(message);
SideChannelManager.ProcessSideChannelData(message);
}
Assert.AreEqual(3.387999f, m_Channel.GetWithDefault(parameter, 1.0f), k_Epsilon);
Assert.AreEqual(1.294413f, m_Channel.GetWithDefault(parameter, 1.0f), k_Epsilon);

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


intSender.SendInt(5);
intSender.SendInt(6);
byte[] fakeData = SideChannelsManager.GetSideChannelMessage(dictSender);
SideChannelsManager.ProcessSideChannelData(dictReceiver, fakeData);
byte[] fakeData = SideChannelManager.GetSideChannelMessage(dictSender);
SideChannelManager.ProcessSideChannelData(dictReceiver, fakeData);
Assert.AreEqual(intReceiver.messagesReceived[0], 4);
Assert.AreEqual(intReceiver.messagesReceived[1], 5);

strSender.SendRawBytes(Encoding.ASCII.GetBytes(str1));
strSender.SendRawBytes(Encoding.ASCII.GetBytes(str2));
byte[] fakeData = SideChannelsManager.GetSideChannelMessage(dictSender);
SideChannelsManager.ProcessSideChannelData(dictReceiver, fakeData);
byte[] fakeData = SideChannelManager.GetSideChannelMessage(dictSender);
SideChannelManager.ProcessSideChannelData(dictReceiver, fakeData);
var messages = strReceiver.GetAndClearReceivedMessages();

tmp = propB.GetWithDefault(k2, 3.0f);
Assert.AreEqual(tmp, 1.0f);
byte[] fakeData = SideChannelsManager.GetSideChannelMessage(dictSender);
SideChannelsManager.ProcessSideChannelData(dictReceiver, fakeData);
byte[] fakeData = SideChannelManager.GetSideChannelMessage(dictSender);
SideChannelManager.ProcessSideChannelData(dictReceiver, fakeData);
tmp = propA.GetWithDefault(k2, 3.0f);
Assert.AreEqual(tmp, 1.0f);

Assert.AreEqual(wasCalled, 0);
fakeData = SideChannelsManager.GetSideChannelMessage(dictSender);
SideChannelsManager.ProcessSideChannelData(dictReceiver, fakeData);
fakeData = SideChannelManager.GetSideChannelMessage(dictSender);
SideChannelManager.ProcessSideChannelData(dictReceiver, fakeData);
Assert.AreEqual(wasCalled, 1);
var keysA = propA.Keys();

/com.unity.ml-agents/Runtime/SideChannels/SideChannelsManager.cs.meta → /com.unity.ml-agents/Runtime/SideChannels/SideChannelManager.cs.meta

/com.unity.ml-agents/Runtime/SideChannels/SideChannelsManager.cs → /com.unity.ml-agents/Runtime/SideChannels/SideChannelManager.cs

正在加载...
取消
保存