Skip to content

Commit c149f6e

Browse files
vincentpierreeshvk
authored andcommitted
Changing the API, not working as fast for now
1 parent 04b2f4f commit c149f6e

File tree

12 files changed

+291
-202
lines changed

12 files changed

+291
-202
lines changed

Assets/ECS_MLAgents_v0/Core/AgentSystem.cs

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Unity.Entities;
66
using Unity.Jobs;
77
using UnityEngine;
8+
using Unity.Burst;
89

910
namespace ECS_MLAgents_v0.Core
1011
{
@@ -35,29 +36,27 @@ namespace ECS_MLAgents_v0.Core
3536
* data in batch and generate a new NativeArray<float> that will be used to populate the
3637
* Actuator data of all compatible Entities.
3738
*/
38-
public abstract class AgentSystem<TS, TA> : JobComponentSystem, IAgentSystem
39+
public abstract class AgentSystem<TS, TA> : JobComponentSystem, IAgentSystem<TS, TA>
3940
where TS : struct, IComponentData
4041
where TA : struct, IComponentData
4142
{
4243
private const int INITIAL_MEMORY_SIZE = 1024;
43-
private const int SIZE_OF_FLOAT_IN_MEMORY = 4;
4444

45-
private int _sensorMemorySize = INITIAL_MEMORY_SIZE;
46-
private int _actuatorMemorySize = INITIAL_MEMORY_SIZE;
45+
private int _currentNAgents = INITIAL_MEMORY_SIZE;
4746

4847
public IDecisionRequester DecisionRequester { get; set; }
4948
private int _phase;
5049

51-
public IAgentDecision Decision { get; set; }
50+
public IAgentDecision<TS, TA> Decision { get; set; }
5251

5352
private ComponentGroup _componentGroup;
5453
private int _sensorSize;
5554
private int _actuatorSize;
5655
// TODO : Make sure there is not extra cost for memory allocation here and when copying
57-
private NativeArray<float> _sensorTensor =
58-
new NativeArray<float>(INITIAL_MEMORY_SIZE, Allocator.Persistent);
59-
private NativeArray<float> _actuatorTensor =
60-
new NativeArray<float>(INITIAL_MEMORY_SIZE, Allocator.Persistent);
56+
private NativeArray<TS> _sensorTensor =
57+
new NativeArray<TS>(INITIAL_MEMORY_SIZE, Allocator.Persistent);
58+
private NativeArray<TA> _actuatorTensor =
59+
new NativeArray<TA>(INITIAL_MEMORY_SIZE, Allocator.Persistent);
6160

6261
// TODO : Decide if we want to keep at all
6362
private Logger _logger;
@@ -138,17 +137,13 @@ protected override JobHandle OnUpdate(JobHandle inputDeps)
138137
* If there was more agents than allowed by the memory allocation of the sensor or
139138
* actuator, then the size is updated to the required size.
140139
*/
141-
if (nAgents * _sensorSize / SIZE_OF_FLOAT_IN_MEMORY > _sensorMemorySize)
140+
if (nAgents > _currentNAgents)
142141
{
143-
_sensorMemorySize = nAgents * _sensorSize / SIZE_OF_FLOAT_IN_MEMORY;
142+
_currentNAgents = nAgents;
144143
_sensorTensor.Dispose();
145-
_sensorTensor = new NativeArray<float>(_sensorMemorySize, Allocator.Persistent);
146-
}
147-
if (nAgents * _actuatorSize / SIZE_OF_FLOAT_IN_MEMORY > _actuatorMemorySize)
148-
{
149-
_actuatorMemorySize = nAgents * _actuatorSize / SIZE_OF_FLOAT_IN_MEMORY;
150144
_actuatorTensor.Dispose();
151-
_actuatorTensor = new NativeArray<float>(_actuatorMemorySize, Allocator.Persistent);
145+
_sensorTensor = new NativeArray<TS>(_currentNAgents, Allocator.Persistent);
146+
_actuatorTensor = new NativeArray<TA>(_currentNAgents, Allocator.Persistent);
152147
}
153148

154149
/*
@@ -167,48 +162,39 @@ protected override JobHandle OnUpdate(JobHandle inputDeps)
167162
{
168163
Sensors = sensors,
169164
SensorTensor = _sensorTensor,
170-
SensorSize = _sensorSize
171165
};
172166
handle = copySensorsJob.Schedule(nAgents, 64, handle);
173-
167+
174168
handle.Complete();
175-
176-
/*
177-
* The Decision is called here to populate the NativeArray<float> of Actuators.
178-
*/
179-
handle = Decision.DecideBatch(ref _sensorTensor,
180-
ref _actuatorTensor,
181-
_sensorSize / SIZE_OF_FLOAT_IN_MEMORY,
182-
_actuatorSize / SIZE_OF_FLOAT_IN_MEMORY,
183-
nAgents,
184-
handle);
185-
186-
/*
169+
170+
Decision.BatchProcess(ref _sensorTensor, ref _actuatorTensor);
171+
172+
/*
187173
* Copy the data from the actuator NativeArray<float> to the actuators of each entity.
188174
*/
189175
var copyActuatorsJob = new CopyActuatorsJob
190176
{
191177
ActuatorTensor = _actuatorTensor,
192178
Actuators = actuators,
193-
ActuatorSize = _actuatorSize
179+
194180
};
195181

196182
return copyActuatorsJob.Schedule(nAgents, 64, handle);
183+
197184
}
198185

199186
/*
200187
* This IJobParallelFor copied the Sensor data into a NativeArray<float>
201188
*/
202-
// [BurstCompile]
189+
[BurstCompile]
203190
private struct CopySensorsJob : IJobParallelFor
204191
{
205192
[ReadOnly] public ComponentDataArray<TS> Sensors;
206-
public NativeArray<float> SensorTensor;
207-
[ReadOnly] public int SensorSize;
193+
public NativeArray<TS> SensorTensor;
208194

209195
public void Execute(int i)
210196
{
211-
TensorUtility.CopyToNativeArray(Sensors[i], SensorTensor, i * SensorSize);
197+
SensorTensor[i] = Sensors[i];
212198
}
213199
}
214200

@@ -218,17 +204,12 @@ public void Execute(int i)
218204
// [BurstCompile]
219205
private struct CopyActuatorsJob : IJobParallelFor
220206
{
221-
222207
public ComponentDataArray<TA> Actuators;
223-
public NativeArray<float> ActuatorTensor;
224-
[ReadOnly] public int ActuatorSize;
208+
public NativeArray<TA> ActuatorTensor;
225209

226210
public void Execute(int i)
227211
{
228-
var tmp = Actuators[i];
229-
// TODO : Make sure there is no extra cost here
230-
TensorUtility.CopyFromNativeArray(ActuatorTensor, out tmp, i * ActuatorSize);
231-
Actuators[i] = tmp;
212+
Actuators[i] = ActuatorTensor[i];
232213
}
233214
}
234215
}

Assets/ECS_MLAgents_v0/Core/ExternalDecision.cs

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
using Unity.Collections.LowLevel.Unsafe;
77
using UnityEngine;
88
using UnityEngine.Profiling;
9+
using Unity.Entities;
910

1011

1112
namespace ECS_MLAgents_v0.Core{
12-
public class ExternalDecision : IAgentDecision
13+
public class ExternalDecision<TS, TA> : IAgentDecision<TS, TA>
14+
where TS : struct, IComponentData
15+
where TA : struct, IComponentData
1316
{
1417

1518
// [ Unity Ready (1) , nAgents (4) , sensorSize (4) , actuatorSize (4) , Data
@@ -27,6 +30,14 @@ public class ExternalDecision : IAgentDecision
2730

2831

2932
private float[] actuatorData = new float[0];
33+
34+
35+
36+
System.Type _sensorType;
37+
System.Type _actuatorType;
38+
39+
private int _sensorSize;
40+
private int _actuatorSize;
3041

3142
// This is a temporary test file
3243
// TODO : Replace with a file creation system
@@ -47,36 +58,35 @@ public ExternalDecision()
4758
Debug.Log("Is Ready to Communicate");
4859
}
4960

50-
public JobHandle DecideBatch(
51-
ref NativeArray<float> sensor,
52-
ref NativeArray<float> actuator,
53-
int sensorSize,
54-
int actuatorSize,
55-
int nAgents,
56-
JobHandle handle)
61+
public void BatchProcess(ref NativeArray<TS> sensors, ref NativeArray<TA> actuators )
5762
{
5863
Profiler.BeginSample("Communicating");
59-
if (sensor.Length > 4 * 50000)
64+
65+
VerifySensor(typeof(TS));
66+
VerifyActuator(typeof(TA));
67+
68+
int batch = sensors.Length;
69+
if (batch != actuators.Length)
6070
{
61-
throw new Exception("TOO much data to send");
71+
throw new Exception("Error in the length of the sensors and actuators");
6272
}
63-
64-
if (actuator.Length > 4 * 50000)
73+
74+
if (batch > 50000)
6575
{
6676
throw new Exception("TOO much data to send");
6777
}
6878

69-
if (actuatorData.Length < actuator.Length)
79+
if (actuatorData.Length < _actuatorSize* batch)
7080
{
71-
actuatorData = new float[actuator.Length];
81+
actuatorData = new float[_actuatorSize * batch];
7282
}
7383

7484

75-
accessor.Write(NUMBER_AGENTS_POSITION, nAgents);
76-
accessor.Write(SENSOR_SIZE_POSITION, sensorSize);
77-
accessor.Write(ACTUATOR_SIZE_POSITION, actuatorSize);
85+
accessor.Write(NUMBER_AGENTS_POSITION, batch);
86+
accessor.Write(SENSOR_SIZE_POSITION, _sensorSize);
87+
accessor.Write(ACTUATOR_SIZE_POSITION, _actuatorSize);
7888

79-
accessor.WriteArray(SENSOR_DATA_POSITION, sensor.ToArray(), 0, sensor.Length);
89+
accessor.WriteArray(SENSOR_DATA_POSITION, sensors.ToArray(), 0, batch);
8090

8191
accessor.Write(PYTHON_READY_POSITION, false);
8292

@@ -96,11 +106,36 @@ public JobHandle DecideBatch(
96106
}
97107
}
98108

99-
accessor.ReadArray(ACTUATOR_DATA_POSITION, actuatorData, 0, actuator.Length);
100-
actuator.CopyFrom(actuatorData);
109+
accessor.ReadArray(ACTUATOR_DATA_POSITION, actuatorData, 0, batch * _actuatorSize);
110+
111+
// actuator.CopyFrom(actuatorData);
112+
113+
var tmpA = new NativeArray<float>(batch * _actuatorSize, Allocator.Persistent);
114+
tmpA.CopyFrom(actuatorData);
115+
for(var i = 0; i< batch; i++){
116+
var act = new TA();
117+
TensorUtility.CopyFromNativeArray(tmpA, out act, i * _sensorSize * 4);
118+
actuators[i] = act;
119+
}
120+
tmpA.Dispose();
121+
101122

102123
Profiler.BeginSample("Communicating");
103-
return handle;
124+
}
125+
126+
private void VerifySensor(System.Type t){
127+
if (! t.Equals(_sensorType)){
128+
TensorUtility.DebugCheckStructure(t);
129+
_sensorSize = System.Runtime.InteropServices.Marshal.SizeOf(t) / 4;
130+
_sensorType = t;
131+
}
132+
}
133+
private void VerifyActuator(System.Type t){
134+
if (! t.Equals(_actuatorType)){
135+
TensorUtility.DebugCheckStructure(t);
136+
_actuatorSize = System.Runtime.InteropServices.Marshal.SizeOf(t) / 4;
137+
_actuatorType = t;
138+
}
104139
}
105140
}
106141
}
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
using Unity.Collections;
2-
using Unity.Jobs;
2+
using Unity.Entities;
33

44
namespace ECS_MLAgents_v0.Core
55
{
66
/*
77
* The Interface to define a Decision process by which a bach of agent updates its actuator
88
* based on the information present in the sensor.
99
*/
10-
public interface IAgentDecision
10+
public interface IAgentDecision<TS, TA>
11+
where TS : struct, IComponentData
12+
where TA : struct, IComponentData
1113
{
1214
/// <summary>
13-
/// DecideBatch updates the aggregated actuators of the agents present in the batch from
14-
/// the aggregated actuators.
15+
/// DecideBatch updates the actuators of the agents present in the batch from
16+
/// the data present in the sensors.
1517
/// </summary>
16-
/// <param name="sensor">The aggregated data for the sensor information present in the
17-
/// batch. The sensor data is linearly arranged.</param>
18-
/// <param name="actuator">The aggregated data for the actuator information present in the
19-
/// batch. The sensor data is linearly arranged.</param>
20-
/// <param name="sensorSize">The number of float values present in a sensor for one agent
21-
/// </param>
22-
/// <param name="actuatorSize">The number of float values present in an actuator
23-
/// for one agent</param>
24-
/// <param name="nAgents">The number of agents present in the batch</param>
25-
/// <param name="handle">The JobHandle for the input dependencies.</param>
26-
/// <returns>The Job Handle for the output dependencies.</returns>
27-
JobHandle DecideBatch(ref NativeArray<float> sensor,
28-
ref NativeArray<float> actuator,
29-
int sensorSize,
30-
int actuatorSize,
31-
int nAgents,
32-
JobHandle handle);
18+
/// <param name="sensors">The aggregated data for the sensor information present in the
19+
/// batch. T.</param>
20+
/// <param name="actuators">The aggregated data for the actuator information present in the
21+
/// batch. </param>
22+
void BatchProcess(ref NativeArray<TS> sensors, ref NativeArray<TA> actuators);
23+
3324
}
25+
3426
}

Assets/ECS_MLAgents_v0/Core/IAgentSystem.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace ECS_MLAgents_v0.Core
44
{
5-
public interface IAgentSystem
5+
public interface IAgentSystem<TS, TA>
6+
where TS : struct , IComponentData
7+
where TA:struct, IComponentData
68
{
79
/// <summary>
810
/// If true, the AgentSystem will perform on the agents
@@ -12,7 +14,7 @@ public interface IAgentSystem
1214
/// <summary>
1315
/// The IAgentDecision that will be used to update the Actuators of compatible Entities.
1416
/// </summary>
15-
IAgentDecision Decision { get; set; }
17+
IAgentDecision<TS, TA> Decision { get; set; }
1618

1719
/// <summary>
1820
/// This method defines what are the required ComponentType that are needed on an Entity

0 commit comments

Comments
 (0)