55using Unity . Entities ;
66using Unity . Jobs ;
77using UnityEngine ;
8+ using Unity . Burst ;
89
910namespace 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 }
0 commit comments