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.

Can 2 agent use same grid sensor or ray perception?

Discussion in 'ML-Agents' started by Glolut, Jan 27, 2022.

  1. Glolut

    Glolut

    Joined:
    Jun 30, 2021
    Posts:
    7
    Let's say I have a bot whose task is shooting enemies. It has 2 different weapons, and each weapon is controlled by a policy. The first agent collects observations (enemies' location) through a grid sensor. So can the second agent, which I made to be the parent gameObject of the first agent, also collect observations through the child grid sensor?
     
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    As far as I can tell, you'll need two distinct grid sensor components for this.

    Agents store their associated sensors in a list - so in theory it might be possible for multiple agents to reference the same sensors. However, each agent invokes the sensor's Update method, so there wouldn't really be a performance gain, because the detection logic would be run multiple times per observation step anyway. Also, I think there could be a missing reference issue with this, since GridSensorComponent creates its own list of sensors, which would be overriden during multiple CreateSensors calls coming from different agents (never tried that though).

    If you're willing to dig into ML-Agents' sensor code, you can try writing a proxy sensor. I did something like this for my audio sensor. The way this works is that there's a regular sensor running detection logic. The proxy sensor merely contains a reference to the regular sensor (which updates first and buffers its observations), forwarding the necessary method calls.
    https://github.com/mbaske/ml-audio-sensor/blob/main/Assets/Scripts/Sensors/Audio/AudioSensorProxy.cs
    https://github.com/mbaske/ml-audio-...ts/Sensors/Audio/AudioSensorComponentProxy.cs
     
    Last edited: Jan 27, 2022
    Glolut likes this.
  3. WaxyMcRivers

    WaxyMcRivers

    Joined:
    May 9, 2016
    Posts:
    59
    I could see this being useful in the scenario where there are many types of weapons on some character where each weapon is autonomous - and you don't want to have multiple iterations of some sensor. I know that mbaske has much more experience with the grid sensor component, so here's how I would go about it with ray perception sensors.

    The requirements for instancing and updating the ray sensors are:
    • Calling CreateSensors() for the RayPersceptionSensorComponent at the start,
    • Using sensor.RaySensor.RayPerceptionOutput.RayOutputs to actually get the data,
    • And calling sensor.RaySensor.Update at whatever frequency you'd like (ie the decision request frequency)
    Using these you can store the data in memory and have each agent (on its own policy) access this script/data on their CollectObservations call; so you'd add the data through vector observations rather than through the traditional ray perception sensor component.

    As long as the agents are children of this script and/or you have "use child sensors" turned off just in case in your behavior parameters, I think it should work out fine. Otherwise, you could achieve the same thing by instancing your own rays and doing the data collection that way.
     
    Glolut likes this.