Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Remove/add sensors before training through code

Discussion in 'ML-Agents' started by MiguelCoK, Jun 15, 2020.

  1. MiguelCoK

    MiguelCoK

    Joined:
    Aug 22, 2017
    Posts:
    12
    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
    Code (CSharp):
    1. public class SensorManager : Monobehaviour {
    2.     public DictionarySensorBool sensors;
    3.  
    4.     void Awake() {
    5.         foreach (SensorComponent sensor in sensors.Keys) {
    6.             bool useIt = sensors[sensor];
    7.             if (!useIt) {
    8.                 Destroy(sensor);
    9.             }
    10.         }
    11.     }
    12. }
    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,
    1. How could i remove the sensors (or add the ones i want) through code and not get their observations.
    2. 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.
     
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    vincentgao88 likes this.
  3. MiguelCoK

    MiguelCoK

    Joined:
    Aug 22, 2017
    Posts:
    12
    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.