Skip to content

Commit 0f23c03

Browse files
Merge pull request #6 from Unity-Technologies/experiment-reflection
Experiment reflection
2 parents 1f8459a + 37f0a1c commit 0f23c03

File tree

17 files changed

+327
-66
lines changed

17 files changed

+327
-66
lines changed

Assets/ECS_MLAgents_v0/Core/ExternalDecision.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ public class ExternalDecision<TS, TA> : IAgentDecision<TS, TA>
4343
// TODO : Replace with a file creation system
4444
// TODO : Implement the communication in a separate class
4545
// TODO : Have separate files for sensor and actuators
46-
private string filenameWrite = "Assets/shared_communication_file.txt";
47-
46+
// private string filenameWrite = "Assets/shared_communication_file.txt";
47+
private string filenameWrite = "shared_communication_file.txt";
48+
49+
4850
private MemoryMappedViewAccessor accessor;
4951

5052
public ExternalDecision()
@@ -115,7 +117,7 @@ public void BatchProcess(ref NativeArray<TS> sensors, ref NativeArray<TA> actuat
115117
}
116118
Profiler.EndSample();
117119

118-
Profiler.BeginSample("__Read");
120+
Profiler.BeginSample("__Read");
119121
accessor.ReadArray(ACTUATOR_DATA_POSITION, actuatorData, 0, batch);
120122

121123
actuators.Slice(offset, batch).CopyFrom(actuatorData);
@@ -124,7 +126,7 @@ public void BatchProcess(ref NativeArray<TS> sensors, ref NativeArray<TA> actuat
124126
// actuators[i] = actuatorData[i];
125127
// }
126128

127-
Profiler.EndSample();
129+
Profiler.EndSample();
128130
Profiler.EndSample();
129131

130132
}

Assets/ECS_MLAgents_v0/Core/SensorAttribute.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.

Assets/ECS_MLAgents_v0/Data.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Unity.Entities;
2+
using Unity.Collections;
3+
using Unity.Collections.LowLevel.Unsafe;
4+
using System.Runtime.InteropServices;
5+
using System;
6+
namespace ECS_MLAgents_v0.Data
7+
{
8+
9+
/*
10+
11+
[StructLayout(LayoutKind.Sequential)]
12+
struct char<N> {
13+
private int size;
14+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = <N>)]
15+
private byte[] bytes;
16+
17+
public char<N>(string s){
18+
this.bytes=new byte[<N>];
19+
this.size = s.Length;
20+
if (this.size > <N>){
21+
throw new NotSupportedException(
22+
"Cannot create a char<N> object with more than <N> characters"
23+
);
24+
}
25+
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
26+
}
27+
28+
public string GetString(){
29+
return System.Text.Encoding.UTF8.GetString(bytes, 0, size);
30+
}
31+
}
32+
33+
*/
34+
35+
36+
[StructLayout(LayoutKind.Sequential)]
37+
public struct char64 {
38+
39+
private int size; // TODO we could use an end of line stopper rather than keeping track of the size
40+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
41+
private byte[] bytes;
42+
43+
public char64(string s){
44+
this.bytes=new byte[64];
45+
this.size = s.Length;
46+
if (this.size > 64){
47+
throw new NotSupportedException(
48+
"Cannot create a char64 object with more than 64 characters"
49+
);
50+
}
51+
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
52+
}
53+
54+
public string GetString(){
55+
return System.Text.Encoding.UTF8.GetString(bytes, 0, size);
56+
}
57+
}
58+
59+
[StructLayout(LayoutKind.Sequential)]
60+
public struct char256 {
61+
62+
private int size;
63+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
64+
private byte[] bytes;
65+
66+
public char256(string s){
67+
this.bytes=new byte[256];
68+
this.size = s.Length;
69+
if (this.size > 256){
70+
throw new NotSupportedException(
71+
"Cannot create a char256 object with more than 256 characters"
72+
);
73+
}
74+
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
75+
}
76+
77+
public string GetString(){
78+
return System.Text.Encoding.UTF8.GetString(bytes, 0, size);
79+
}
80+
}
81+
82+
}

Assets/ECS_MLAgents_v0/Data/CharStruct.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using UnityEngine;
5+
using Unity.Collections.LowLevel.Unsafe;
6+
using Unity.Collections;
7+
using Unity.Mathematics;
8+
9+
namespace ECS_MLAgents_v0.Data
10+
{
11+
public enum SensorDataType : int {
12+
FLOAT,
13+
// INT,
14+
// BOOL,
15+
// ENUM,
16+
// TEXTURE,
17+
18+
}
19+
20+
public enum SensorType : int {
21+
DATA,
22+
REWARD,
23+
DONE,
24+
ID,
25+
26+
}
27+
28+
public class SensorAttribute : Attribute{
29+
public string Description;
30+
public SensorType SensorType;
31+
public SensorAttribute(SensorType Type, string Description = ""){
32+
this.Description = Description;
33+
this.SensorType = Type;
34+
}
35+
36+
}
37+
38+
39+
40+
public class AttributeUtility
41+
{
42+
// Note : This is non blittable
43+
public struct SensorMetadata
44+
{
45+
public char64 Name;
46+
public char256 Description;
47+
// public int4 Dimension;
48+
// public int Offset;
49+
// public SensorDataType DataType;
50+
public SensorType SensorType;
51+
}
52+
53+
public static SensorMetadata[] GetSensorMetaData(Type t)
54+
{
55+
var fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
56+
// var fields = t.GetFields();
57+
var result = new SensorMetadata[fields.Length];
58+
for(var i =0; i<fields.Length; i++)
59+
{
60+
result[i] = GetMetaDataFromField(fields[i]);
61+
}
62+
return result;
63+
}
64+
65+
public static SensorMetadata GetMetaDataFromField(FieldInfo field)
66+
{
67+
//TODO : Run some checks on the field to make sure the data is right
68+
69+
70+
var attribute = field.GetCustomAttribute<SensorAttribute>();
71+
if (attribute == null){
72+
attribute = new SensorAttribute(SensorType.DATA);
73+
}
74+
75+
return new SensorMetadata{
76+
Name = new char64(field.Name),
77+
SensorType = attribute.SensorType,
78+
Description = new char256(attribute.Description),
79+
};
80+
}
81+
82+
83+
84+
}
85+
}

Assets/ECS_MLAgents_v0/Core/SensorAttribute.cs.meta renamed to Assets/ECS_MLAgents_v0/Data/SensorAttribute.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/ECS_MLAgents_v0/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/ECS_MLAgents_v0/Editor/Tests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using NUnit.Framework;
2+
using ECS_MLAgents_v0;
3+
using ECS_MLAgents_v0.Data;
4+
using System.Runtime.InteropServices;
5+
using UnityEngine;
6+
using System;
7+
8+
namespace ECS_MLAgents_v0.Editor.Tests{
9+
public class char64Test{
10+
11+
[Test]
12+
public void TestFromStringChar64(){
13+
foreach(string s in new string[]{"", " ", "TestString"}){
14+
var tmp = new char64(s);
15+
Assert.AreEqual(s, tmp.GetString());
16+
Assert.AreEqual(4 + 64, Marshal.SizeOf(tmp));
17+
}
18+
}
19+
20+
[Test]
21+
public void TestLongStringChar64(){
22+
Assert.That(() => new char64("0000000001000000000200000000030000000004000000000500000000060000XXXXX"),
23+
Throws.TypeOf<NotSupportedException>());
24+
}
25+
}
26+
27+
public class char256Test{
28+
29+
[Test]
30+
public void TestFromStringChar256(){
31+
foreach(string s in new string[]{"", " ", "TestString"}){
32+
var tmp = new char256(s);
33+
Assert.AreEqual(s, tmp.GetString());
34+
Assert.AreEqual(4 + 256, Marshal.SizeOf(tmp));
35+
}
36+
}
37+
}
38+
39+
40+
41+
}

0 commit comments

Comments
 (0)