浏览代码

read/write bools, unit tests

/bug-failed-api-check
Chris Elion 5 年前
当前提交
158fcae2
共有 5 个文件被更改,包括 103 次插入19 次删除
  1. 26
      com.unity.ml-agents/Runtime/SideChannels/FloatPropertiesChannel.cs
  2. 20
      com.unity.ml-agents/Runtime/SideChannels/SideChannel.cs
  3. 55
      com.unity.ml-agents/Tests/Editor/SideChannelTests.cs
  4. 8
      ml-agents-envs/mlagents_envs/side_channel/side_channel.py
  5. 13
      ml-agents-envs/mlagents_envs/tests/test_side_channel.py

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


var value = msg.ReadFloat32();
m_FloatProperties[key] = value;
if (m_RegisteredActions.ContainsKey(key))
{
m_RegisteredActions[key].Invoke(value);
}
Action<float> action;
m_RegisteredActions.TryGetValue(key, out action);
action?.Invoke(value);
}
/// <inheritdoc/>

QueueMessageToSend(msgOut);
}
if (m_RegisteredActions.ContainsKey(key))
{
m_RegisteredActions[key].Invoke(value);
}
Action<float> action;
m_RegisteredActions.TryGetValue(key, out action);
action?.Invoke(value);
if (m_FloatProperties.ContainsKey(key))
{
return m_FloatProperties[key];
}
else
{
return defaultValue;
}
float valueOut;
bool hasKey = m_FloatProperties.TryGetValue(key, out valueOut);
return hasKey ? valueOut : defaultValue;
}
/// <inheritdoc/>

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


/// of each type. Ensure the Unity side channels will be linked to their Python equivalent.
/// </summary>
/// <returns> The integer identifier of the SideChannel.</returns>
public Guid ChannelId{
public Guid ChannelId
{
get;
protected set;
}

m_Reader = new BinaryReader(m_Stream);
}
public bool ReadBoolean()
{
return m_Reader.ReadBoolean();
}
public int ReadInt32()
{
return m_Reader.ReadInt32();

return str;
}
public IList<float> ReadFloatArray()
public IList<float> ReadFloatList()
{
var len = ReadInt32();
var output = new float[len];

m_Stream?.Dispose();
}
public void WriteBoolean(bool b)
{
m_Writer.Write(b);
}
public void WriteInt32(int i)
{
m_Writer.Write(i);

public void SetRawBytes(byte[] data)
{
// Reset first.
m_Stream.SetLength(0);
// Then append the data
m_Stream.Capacity = (m_Stream.Capacity < data.Length) ? data.Length : m_Stream.Capacity;
m_Stream.Write(data, 0, data.Length);
}

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


{
public List<int> messagesReceived = new List<int>();
public TestSideChannel() {
public TestSideChannel()
{
ChannelId = new Guid("6afa2c06-4f82-11ea-b238-784f4387d1f7");
}

fakeData = RpcCommunicator.GetSideChannelMessage(dictSender);
RpcCommunicator.ProcessSideChannelData(dictReceiver, fakeData);
Assert.AreEqual(wasCalled, 1);
var keysA = propA.ListProperties();
Assert.AreEqual(2, keysA.Count);
Assert.IsTrue(keysA.Contains(k1));
Assert.IsTrue(keysA.Contains(k2));
var keysB = propA.ListProperties();
Assert.AreEqual(2, keysB.Count);
Assert.IsTrue(keysB.Contains(k1));
Assert.IsTrue(keysB.Contains(k2));
}
[Test]
public void TestOutgoingMessageRawBytes()
{
var msg = new OutgoingMessage();
msg.WriteInt32(42);
msg.WriteFloat32(1.0f);
var data = new byte[] { 1, 2, 3, 4 };
msg.SetRawBytes(data);
var result = msg.ToByteArray();
Assert.AreEqual(data, result);
}
[Test]
public void TestMessageReadWrites()
{
var boolVal = true;
var intVal = 1337;
var floatVal = 4.2f;
var floatListVal = new float[] { 1001, 1002 };
var stringVal = "mlagents!";
IncomingMessage incomingMsg;
using (var outgoingMsg = new OutgoingMessage())
{
outgoingMsg.WriteBoolean(boolVal);
outgoingMsg.WriteInt32(intVal);
outgoingMsg.WriteFloat32(floatVal);
outgoingMsg.WriteFloatList(floatListVal);
outgoingMsg.WriteString(stringVal);
incomingMsg = new IncomingMessage(outgoingMsg.ToByteArray());
}
Assert.AreEqual(boolVal, incomingMsg.ReadBoolean());
Assert.AreEqual(intVal, incomingMsg.ReadInt32());
Assert.AreEqual(floatVal, incomingMsg.ReadFloat32());
Assert.AreEqual(floatListVal, incomingMsg.ReadFloatList());
Assert.AreEqual(stringVal, incomingMsg.ReadString());
}
}
}

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


def __init__(self):
self.buffer = bytearray()
def write_bool(self, b: bool) -> None:
self.buffer += struct.pack("<?", b)
def write_int32(self, i: int) -> None:
self.buffer += struct.pack("<i", i)

def __init__(self, buffer: bytes, offset: int = 0):
self.buffer = buffer
self.offset = offset
def read_bool(self) -> bool:
val = struct.unpack_from("<?", self.buffer, self.offset)[0]
self.offset += 1
return val
def read_int32(self) -> int:
val = struct.unpack_from("<i", self.buffer, self.offset)[0]

13
ml-agents-envs/mlagents_envs/tests/test_side_channel.py


assert len(messages) == 0
def test_message_bool():
vals = [True, False]
msg_out = OutgoingMessage()
for v in vals:
msg_out.write_bool(v)
msg_in = IncomingMessage(msg_out.buffer)
read_vals = []
for _ in range(len(vals)):
read_vals.append(msg_in.read_bool())
assert vals == read_vals
def test_message_int32():
val = 1337
msg_out = OutgoingMessage()

正在加载...
取消
保存