Search Unity

Question Direction and Distance KeyValuePair List as an Observation

Discussion in 'ML-Agents' started by Hsgngr, Jun 29, 2020.

  1. Hsgngr

    Hsgngr

    Joined:
    Dec 28, 2015
    Posts:
    61
    I have an agent which is trying to avoid others. I'm thinking to give a list of things which is inside of agent's trigger collider with their distance and direction as follows:
    Code (CSharp):
    1. //The list of n-number agents' directions and distance to this agent inside of the exposure radius.
    2.     List<KeyValuePair<Vector3, float>> directions = new List<KeyValuePair<Vector3, float>>();
    How can I do the implementation of update function of this ? Is it a problem that sometimes when there is no one in the collider the list is going to be empty and size will always change ? Is it logical thing to use this list system ?

    My purpose is to teach avoiding others by using this list as an observation.
     
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    If you want to observe a list of detected objects rather than using raycasts, you'll need to allocate a fixed amount of float observations for the maximum number of objects your agent can detect. Try sorting the list objects first in a way that's somewhat consistent, e.g. by distance or angle or type etc, so their order makes sense to the agent and isn't wildly different at each decision step. Set neutral values if less objects were detected than the number of float observations you've allocated.
    Let's say you want to observe a maximum of 10 objects that you've sorted by distance from the agent, starting with the closest. If your list contains 12 objects, you set observations for the first 10 ignoring the remaining 2. If it contains only 8 objects, you assign their values and fill the 2 remaining observation slots with 0 or -1 or whatever your neutral value is.
    I found this approach to be working reasonably ok if detected objects are more or less stationary, or if the agent doesn't need to discriminate them by type.
     
    Hsgngr likes this.
  3. Hsgngr

    Hsgngr

    Joined:
    Dec 28, 2015
    Posts:
    61
    Thanks for the reccomendations @mbaske, I am trying to implement a social distancing, Thought it may be easier for agents to learn with the List then a Lidar. Do you think raycasts should be much better for this ?
     
  4. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    I think raycasts are more straight-forward, because there's a clear relation between ray directions and observations, which doesn't ever change. I would only use the list approach if the number of detectable objects is much smaller than the number of raycasts needed to cover the observable area. In that case, a smallish list might be better than a ton of raycasts with regard to model size and training time.
     
    Hsgngr likes this.