@@ -101,20 +101,30 @@ public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable<AgentIn
101
101
var agentIndex = 0 ;
102
102
foreach ( var info in infos )
103
103
{
104
- var tensorOffset = 0 ;
105
- // Write each sensor consecutively to the tensor
106
- foreach ( var sensorIndex in m_SensorIndices )
104
+ if ( info . agentInfo . done )
107
105
{
108
- var sensor = info . sensors [ sensorIndex ] ;
109
- m_WriteAdapter . SetTarget ( tensorProxy , agentIndex , tensorOffset ) ;
110
- var numWritten = sensor . Write ( m_WriteAdapter ) ;
111
- tensorOffset += numWritten ;
106
+ // If the agent is done, we might have a stale reference to the sensors
107
+ // e.g. a dependent object might have been disposed.
108
+ // To avoid this, just fill observation with zeroes instead of calling sensor.Write.
109
+ TensorUtils . FillTensorBatch ( tensorProxy , agentIndex , 0.0f ) ;
110
+ }
111
+ else
112
+ {
113
+ var tensorOffset = 0 ;
114
+ // Write each sensor consecutively to the tensor
115
+ foreach ( var sensorIndex in m_SensorIndices )
116
+ {
117
+ var sensor = info . sensors [ sensorIndex ] ;
118
+ m_WriteAdapter . SetTarget ( tensorProxy , agentIndex , tensorOffset ) ;
119
+ var numWritten = sensor . Write ( m_WriteAdapter ) ;
120
+ tensorOffset += numWritten ;
121
+ }
122
+ Debug . AssertFormat (
123
+ tensorOffset == vecObsSizeT ,
124
+ "mismatch between vector observation size ({0}) and number of observations written ({1})" ,
125
+ vecObsSizeT , tensorOffset
126
+ ) ;
112
127
}
113
- Debug . AssertFormat (
114
- tensorOffset == vecObsSizeT ,
115
- "mismatch between vector observation size ({0}) and number of observations written ({1})" ,
116
- vecObsSizeT , tensorOffset
117
- ) ;
118
128
119
129
agentIndex ++ ;
120
130
}
@@ -356,8 +366,19 @@ public void Generate(TensorProxy tensorProxy, int batchSize, IEnumerable<AgentIn
356
366
foreach ( var infoSensorPair in infos )
357
367
{
358
368
var sensor = infoSensorPair . sensors [ m_SensorIndex ] ;
359
- m_WriteAdapter . SetTarget ( tensorProxy , agentIndex , 0 ) ;
360
- sensor . Write ( m_WriteAdapter ) ;
369
+ if ( infoSensorPair . agentInfo . done )
370
+ {
371
+ // If the agent is done, we might have a stale reference to the sensors
372
+ // e.g. a dependent object might have been disposed.
373
+ // To avoid this, just fill observation with zeroes instead of calling sensor.Write.
374
+ TensorUtils . FillTensorBatch ( tensorProxy , agentIndex , 0.0f ) ;
375
+ }
376
+ else
377
+ {
378
+ m_WriteAdapter . SetTarget ( tensorProxy , agentIndex , 0 ) ;
379
+ sensor . Write ( m_WriteAdapter ) ;
380
+
381
+ }
361
382
agentIndex ++ ;
362
383
}
363
384
}
0 commit comments