Remove/add sensors before training through code

Hi,
I'm using 4 different types of sensors with my agents. I would like to test some combinations of using/not-using those sensors. For example:
| S1 | S2 | S3 | S4
comb1 | X | X | X | X
comb2 | X | | X |
comb3 | | X | X | X
, where combination 1 uses the 4 sensors, the 2nd one uses only first and third sensors and combination 3 uses sensors 2, 3 and 4.
So, to make testing easier i implement the following code

public class SensorManager : Monobehaviour {
    public DictionarySensorBool sensors;

    void Awake() {
        foreach (SensorComponent sensor in sensors.Keys) {
            bool useIt = sensors[sensor];
            if (!useIt) {
                Destroy(sensor);
            }
        }
    }
}

5980874--642422--sensormanager.jpg
This script removes the unchecked sensors from the agent (the agent have this sensors attached and the dictionary keys are references to those sensors).
However, i ran the training from jupyter notebook to check if the sensors where removed and it doesn't. I'm still getting observations from the sensors even when they where removed from the agent object. So,

  • How could i remove the sensors (or add the ones i want) through code and not get their observations.
  • Which other mechanism could i use to avoid remove/add the sensors manually, or having an agent for each posible combination and enable/disable them.

Thanks in advance, sorry for my bad english.

This might simply be a timing issue, where ML-Agents initialization code runs prior to your code that's removing sensors. Take a look at script execution order settings:
https://docs.unity3d.com/Manual/class-MonoManager.html

1 Like


Thanks and yes, that might be one of the problems, however, i realised that the main problem was that the Destroy function destroys the object at the end of the frame. So, a solution could be to use DestroyInmediate instead. But, it is not recommended to use it. Another solution could be to add the sensors copying them from a prefab with this code https://gist.github.com/mattatz/1c039dce5ef251dcef7b628c878bea32 instead of removing them. I post this in case anyone has the same problem.