Search Unity

Proper Usage of Buffer Sensor?

Discussion in 'ML-Agents' started by LastSpark, Jun 14, 2021.

  1. LastSpark

    LastSpark

    Joined:
    May 23, 2021
    Posts:
    8
    Hi all,

    I am currently trying to use a buffer sensor in a dogfighting game for the AI to learn to avoid enemy projectiles (amongst other things). One thing I'm not sure of is if I had set it up correctly.

    I added the buffer sensor to the gameobject which has all of the ML-Agent scripts and settings (I have attached an image showing how that looks).
    Then inside **CollectObservations**:

    Code (CSharp):
    1.  public override void CollectObservations(VectorSensor sensor)
    2.     {
    3.        //omitted code about other basic observations
    4.         for (int i = 0; i < bulletRadar.bullets.Count; i++)
    5.         {
    6.  
    7.             Transform tempTran = bulletRadar.bullets[i].GetComponent<Transform>();
    8.             Rigidbody2D tempRigid = bulletRadar.bullets[i].GetComponent<Rigidbody2D>();
    9.             //normalized: x, y, velX, velY rotation around Z
    10.             float[] temp = { tempTran.localPosition.x / xLength, tempTran.localPosition.y / yLength,    tempRigid.velocity.x / maxVel, tempRigid.velocity.y / maxVel, (transform.rotation.z % 360f) / 360f };
    11.             bufferSensor.AppendObservation(temp);
    12.         }
    13.     }
    So my question, do I just need to add the bullets into the bufferSensor on each CollectObservation call and that's it? Will the output of that bufferSensor be properly handled by ML-agents stuff?
     

    Attached Files:

  2. LastSpark

    LastSpark

    Joined:
    May 23, 2021
    Posts:
    8
    Hopefully, it's not against rules, I will try bumping this post just once instead of reposting...
     
  3. jonhillman192

    jonhillman192

    Joined:
    Aug 22, 2013
    Posts:
    2
    I'm working with a similar setup, and it does appear this is all you need to do. I'm not sure how to think about the buffer itself, how often it fills, what happens to old observations and at what rate. Any insights?
     
  4. WaxyMcRivers

    WaxyMcRivers

    Joined:
    May 9, 2016
    Posts:
    59
    I believe the buffer is cleared every ML-Agents step and that you have to refill it each time collect observations is called. For your given problem, ordering the entries w.r.t distance to your agent would help it understand "threat" as well as the attention mechanism (that the NN uses with buffer sensor).

    You can also use the forum search function to find threads that are related to buffer sensor questions. It also helped me reading the source code for buffer sensor.
     
  5. macsimilian

    macsimilian

    Joined:
    Sep 19, 2020
    Posts:
    25
    Doesn't order not matter for the BufferSensor?
     
  6. Thorce

    Thorce

    Joined:
    Jul 3, 2019
    Posts:
    41
    Order does not matter
     
  7. Energymover

    Energymover

    Joined:
    Mar 28, 2023
    Posts:
    33
    Order doesn't matter and yes you would need to fill the buffer each time. If you want to carry observations from one step to another you would need to use the Stacked Vectors (or maybe a recurring neural network).
     
  8. WaxyMcRivers

    WaxyMcRivers

    Joined:
    May 9, 2016
    Posts:
    59
    Yes, ordering doesn't matter unless the network contains convolutions. My apologies.