浏览代码

C# versions of the python utils

/bug-failed-api-check
Chris Elion 5 年前
当前提交
8ba73c57
共有 7 个文件被更改,包括 170 次插入68 次删除
  1. 16
      com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs
  2. 30
      com.unity.ml-agents/Runtime/SideChannels/EngineConfigurationChannel.cs
  3. 49
      com.unity.ml-agents/Runtime/SideChannels/FloatPropertiesChannel.cs
  4. 10
      com.unity.ml-agents/Runtime/SideChannels/RawBytesChannel.cs
  5. 119
      com.unity.ml-agents/Runtime/SideChannels/SideChannel.cs
  6. 12
      com.unity.ml-agents/Tests/Editor/SideChannelTests.cs
  7. 2
      ml-agents-envs/mlagents_envs/side_channel/side_channel.py

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


"side channels of the same id.", channelId));
}
// Process any messages that we've already received for this channel ID.
var numMessages = m_CachedMessages.Count;
for (int i = 0; i < numMessages; i++)
{

sideChannel.OnMessageReceived(cachedMessage.Message);
using (var incomingMsg = new IncomingMessage(cachedMessage.Message))
{
sideChannel.OnMessageReceived(incomingMsg);
}
}
else
{

var cachedMessage = m_CachedMessages.Dequeue();
if (sideChannels.ContainsKey(cachedMessage.ChannelId))
{
sideChannels[cachedMessage.ChannelId].OnMessageReceived(cachedMessage.Message);
using (var incomingMsg = new IncomingMessage(cachedMessage.Message))
{
sideChannels[cachedMessage.ChannelId].OnMessageReceived(incomingMsg);
}
}
else
{

}
if (sideChannels.ContainsKey(channelId))
{
sideChannels[channelId].OnMessageReceived(message);
using (var incomingMsg = new IncomingMessage(message))
{
sideChannels[channelId].OnMessageReceived(incomingMsg);
}
}
else
{

30
com.unity.ml-agents/Runtime/SideChannels/EngineConfigurationChannel.cs


}
/// <inheritdoc/>
public override void OnMessageReceived(byte[] data)
public override void OnMessageReceived(IncomingMessage msg)
using (var memStream = new MemoryStream(data))
{
using (var binaryReader = new BinaryReader(memStream))
{
var width = binaryReader.ReadInt32();
var height = binaryReader.ReadInt32();
var qualityLevel = binaryReader.ReadInt32();
var timeScale = binaryReader.ReadSingle();
var targetFrameRate = binaryReader.ReadInt32();
var width = msg.ReadInt32();
var height = msg.ReadInt32();
var qualityLevel = msg.ReadInt32();
var timeScale = msg.ReadFloat32();
var targetFrameRate = msg.ReadInt32();
timeScale = Mathf.Clamp(timeScale, 1, 100);
timeScale = Mathf.Clamp(timeScale, 1, 100);
Screen.SetResolution(width, height, false);
QualitySettings.SetQualityLevel(qualityLevel, true);
Time.timeScale = timeScale;
Time.captureFramerate = 60;
Application.targetFrameRate = targetFrameRate;
}
}
Screen.SetResolution(width, height, false);
QualitySettings.SetQualityLevel(qualityLevel, true);
Time.timeScale = timeScale;
Time.captureFramerate = 60;
Application.targetFrameRate = targetFrameRate;
}
}
}

49
com.unity.ml-agents/Runtime/SideChannels/FloatPropertiesChannel.cs


}
/// <inheritdoc/>
public override void OnMessageReceived(byte[] data)
public override void OnMessageReceived(IncomingMessage msg)
var kv = DeserializeMessage(data);
m_FloatProperties[kv.Key] = kv.Value;
if (m_RegisteredActions.ContainsKey(kv.Key))
var key = msg.ReadString();
var value = msg.ReadFloat32();
m_FloatProperties[key] = value;
if (m_RegisteredActions.ContainsKey(key))
m_RegisteredActions[kv.Key].Invoke(kv.Value);
m_RegisteredActions[key].Invoke(value);
}
}

m_FloatProperties[key] = value;
QueueMessageToSend(SerializeMessage(key, value));
using (var msgOut = new OutgoingMessage())
{
msgOut.WriteString(key);
msgOut.WriteFloat32(value);
QueueMessageToSend(msgOut);
}
if (m_RegisteredActions.ContainsKey(key))
{
m_RegisteredActions[key].Invoke(value);

public IList<string> ListProperties()
{
return new List<string>(m_FloatProperties.Keys);
}
static KeyValuePair<string, float> DeserializeMessage(byte[] data)
{
using (var memStream = new MemoryStream(data))
{
using (var binaryReader = new BinaryReader(memStream))
{
var keyLength = binaryReader.ReadInt32();
var key = Encoding.ASCII.GetString(binaryReader.ReadBytes(keyLength));
var value = binaryReader.ReadSingle();
return new KeyValuePair<string, float>(key, value);
}
}
}
static byte[] SerializeMessage(string key, float value)
{
using (var memStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(memStream))
{
var stringEncoded = Encoding.ASCII.GetBytes(key);
binaryWriter.Write(stringEncoded.Length);
binaryWriter.Write(stringEncoded);
binaryWriter.Write(value);
return memStream.ToArray();
}
}
}
}
}

10
com.unity.ml-agents/Runtime/SideChannels/RawBytesChannel.cs


}
/// <inheritdoc/>
public override void OnMessageReceived(byte[] data)
public override void OnMessageReceived(IncomingMessage msg)
m_MessagesReceived.Add(data);
m_MessagesReceived.Add(msg.ReadRawBytes());
}
/// <summary>

/// <param name="data"> The byte array of data to send to Python.</param>
public void SendRawBytes(byte[] data)
{
QueueMessageToSend(data);
using (var msg = new OutgoingMessage())
{
msg.SetRawBytes(data);
QueueMessageToSend(msg);
}
}
/// <summary>

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


using System.Collections.Generic;
using System;
using System.IO;
using System.Text;
namespace MLAgents.SideChannels
{

/// Is called by the communicator every time a message is received from Python by the SideChannel.
/// Can be called multiple times per simulation step if multiple messages were sent.
/// </summary>
/// <param name="data"> the payload of the message.</param>
public abstract void OnMessageReceived(byte[] data);
/// <param name="msg">The incoming message.</param>
public abstract void OnMessageReceived(IncomingMessage msg);
protected void QueueMessageToSend(byte[] data)
protected void QueueMessageToSend(OutgoingMessage msg)
{
MessageQueue.Add(msg.ToByteArray());
}
}
public class IncomingMessage : IDisposable
{
byte[] m_Data;
Stream m_Stream;
BinaryReader m_Reader;
public IncomingMessage(byte[] data)
{
m_Data = data;
m_Stream = new MemoryStream(data);
m_Reader = new BinaryReader(m_Stream);
}
public int ReadInt32()
{
return m_Reader.ReadInt32();
}
public float ReadFloat32()
{
return m_Reader.ReadSingle();
}
public string ReadString()
{
var strLength = ReadInt32();
var str = Encoding.ASCII.GetString(m_Reader.ReadBytes(strLength));
return str;
}
public IList<float> ReadFloatArray()
{
var len = ReadInt32();
var output = new float[len];
for (var i = 0; i < len; i++)
{
output[i] = ReadFloat32();
}
return output;
}
public byte[] ReadRawBytes()
{
return m_Data;
}
public void Dispose()
{
m_Reader?.Dispose();
m_Stream?.Dispose();
}
}
public class OutgoingMessage : IDisposable
{
BinaryWriter m_Writer;
MemoryStream m_Stream;
public OutgoingMessage()
{
m_Stream = new MemoryStream();
m_Writer = new BinaryWriter(m_Stream);
}
public void Dispose()
{
m_Writer?.Dispose();
m_Stream?.Dispose();
}
public void WriteInt32(int i)
{
m_Writer.Write(i);
}
public void WriteFloat32(float f)
{
m_Writer.Write(f);
}
public void WriteString(string s)
{
var stringEncoded = Encoding.ASCII.GetBytes(s);
m_Writer.Write(stringEncoded.Length);
m_Writer.Write(stringEncoded);
}
public void WriteFloatList(IList<float> floatList)
{
WriteInt32(floatList.Count);
foreach (var f in floatList)
{
WriteFloat32(f);
}
}
public void SetRawBytes(byte[] data)
MessageQueue.Add(data);
m_Stream.Seek(0, SeekOrigin.Begin);
m_Stream.Write(data, 0, data.Length);
}
internal byte[] ToByteArray()
{
return m_Stream.ToArray();
}
}
}

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


ChannelId = new Guid("6afa2c06-4f82-11ea-b238-784f4387d1f7");
}
public override void OnMessageReceived(byte[] data)
public override void OnMessageReceived(IncomingMessage msg)
messagesReceived.Add(BitConverter.ToInt32(data, 0));
messagesReceived.Add(msg.ReadInt32());
public void SendInt(int data)
public void SendInt(int value)
QueueMessageToSend(BitConverter.GetBytes(data));
using (var msg = new OutgoingMessage())
{
msg.WriteInt32(value);
QueueMessageToSend(msg);
}
}
}

2
ml-agents-envs/mlagents_envs/side_channel/side_channel.py


to the Env object at construction.
"""
def __init__(self, channel_id):
def __init__(self, channel_id: uuid.UUID):
self._channel_id: uuid.UUID = channel_id
self.message_queue: List[bytearray] = []

正在加载...
取消
保存