Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Assets/ECS_MLAgents_v0/Core/ExternalDecision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public class ExternalDecision<TS, TA> : IAgentDecision<TS, TA>
// TODO : Replace with a file creation system
// TODO : Implement the communication in a separate class
// TODO : Have separate files for sensor and actuators
private string filenameWrite = "Assets/shared_communication_file.txt";

// private string filenameWrite = "Assets/shared_communication_file.txt";
private string filenameWrite = "shared_communication_file.txt";


private MemoryMappedViewAccessor accessor;

public ExternalDecision()
Expand Down Expand Up @@ -115,7 +117,7 @@ public void BatchProcess(ref NativeArray<TS> sensors, ref NativeArray<TA> actuat
}
Profiler.EndSample();

Profiler.BeginSample("__Read");
Profiler.BeginSample("__Read");
accessor.ReadArray(ACTUATOR_DATA_POSITION, actuatorData, 0, batch);

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

Profiler.EndSample();
Profiler.EndSample();
Profiler.EndSample();

}
Expand Down
58 changes: 0 additions & 58 deletions Assets/ECS_MLAgents_v0/Core/SensorAttribute.cs

This file was deleted.

8 changes: 8 additions & 0 deletions Assets/ECS_MLAgents_v0/Data.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions Assets/ECS_MLAgents_v0/Data/CharStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Unity.Entities;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using System.Runtime.InteropServices;
using System;
namespace ECS_MLAgents_v0.Data
{

/*

[StructLayout(LayoutKind.Sequential)]
struct char<N> {
private int size;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = <N>)]
private byte[] bytes;

public char<N>(string s){
this.bytes=new byte[<N>];
this.size = s.Length;
if (this.size > <N>){
throw new NotSupportedException(
"Cannot create a char<N> object with more than <N> characters"
);
}
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
}

public string GetString(){
return System.Text.Encoding.UTF8.GetString(bytes, 0, size);
}
}

*/


[StructLayout(LayoutKind.Sequential)]
public struct char64 {

private int size; // TODO we could use an end of line stopper rather than keeping track of the size
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
private byte[] bytes;

public char64(string s){
this.bytes=new byte[64];
this.size = s.Length;
if (this.size > 64){
throw new NotSupportedException(
"Cannot create a char64 object with more than 64 characters"
);
}
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
}

public string GetString(){
return System.Text.Encoding.UTF8.GetString(bytes, 0, size);
}
}

[StructLayout(LayoutKind.Sequential)]
public struct char256 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the generic approach not work? You commented out the code..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic does not work because the size must be known at compile time.


private int size;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
private byte[] bytes;

public char256(string s){
this.bytes=new byte[256];
this.size = s.Length;
if (this.size > 256){
throw new NotSupportedException(
"Cannot create a char256 object with more than 256 characters"
);
}
System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0);
}

public string GetString(){
return System.Text.Encoding.UTF8.GetString(bytes, 0, size);
}
}

}
11 changes: 11 additions & 0 deletions Assets/ECS_MLAgents_v0/Data/CharStruct.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions Assets/ECS_MLAgents_v0/Data/SensorAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Collections;
using Unity.Mathematics;

namespace ECS_MLAgents_v0.Data
{
public enum SensorDataType : int {
FLOAT,
// INT,
// BOOL,
// ENUM,
// TEXTURE,

}

public enum SensorType : int {
DATA,
REWARD,
DONE,
ID,

}

public class SensorAttribute : Attribute{
public string Description;
public SensorType SensorType;
public SensorAttribute(SensorType Type, string Description = ""){
this.Description = Description;
this.SensorType = Type;
}

}



public class AttributeUtility
{
// Note : This is non blittable
public struct SensorMetadata
{
public char64 Name;
public char256 Description;
// public int4 Dimension;
// public int Offset;
// public SensorDataType DataType;
public SensorType SensorType;
}

public static SensorMetadata[] GetSensorMetaData(Type t)
{
var fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
// var fields = t.GetFields();
var result = new SensorMetadata[fields.Length];
for(var i =0; i<fields.Length; i++)
{
result[i] = GetMetaDataFromField(fields[i]);
}
return result;
}

public static SensorMetadata GetMetaDataFromField(FieldInfo field)
{
//TODO : Run some checks on the field to make sure the data is right


var attribute = field.GetCustomAttribute<SensorAttribute>();
if (attribute == null){
attribute = new SensorAttribute(SensorType.DATA);
}

return new SensorMetadata{
Name = new char64(field.Name),
SensorType = attribute.SensorType,
Description = new char256(attribute.Description),
};
}



}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/ECS_MLAgents_v0/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/ECS_MLAgents_v0/Editor/Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Assets/ECS_MLAgents_v0/Editor/Tests/CharStructTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using NUnit.Framework;
using ECS_MLAgents_v0;
using ECS_MLAgents_v0.Data;
using System.Runtime.InteropServices;
using UnityEngine;
using System;

namespace ECS_MLAgents_v0.Editor.Tests{
public class char64Test{

[Test]
public void TestFromStringChar64(){
foreach(string s in new string[]{"", " ", "TestString"}){
var tmp = new char64(s);
Assert.AreEqual(s, tmp.GetString());
Assert.AreEqual(4 + 64, Marshal.SizeOf(tmp));
}
}

[Test]
public void TestLongStringChar64(){
Assert.That(() => new char64("0000000001000000000200000000030000000004000000000500000000060000XXXXX"),
Throws.TypeOf<NotSupportedException>());
}
}

public class char256Test{

[Test]
public void TestFromStringChar256(){
foreach(string s in new string[]{"", " ", "TestString"}){
var tmp = new char256(s);
Assert.AreEqual(s, tmp.GetString());
Assert.AreEqual(4 + 256, Marshal.SizeOf(tmp));
}
}
}



}
11 changes: 11 additions & 0 deletions Assets/ECS_MLAgents_v0/Editor/Tests/CharStructTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading