Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help - Version 0.24 - OnActionReceived not firing

Discussion in 'ML-Agents' started by jchambers, Mar 16, 2021.

  1. jchambers

    jchambers

    Joined:
    Feb 4, 2013
    Posts:
    7
    Windows 8.1 x64
    Card: GTX960
    Python 3.79

    [from command line with unity logo]
    Version information:
    ml-agents: 0.24.0,
    ml-agents-envs: 0.24.0,
    Communicator API: 1.4.0,
    PyTorch: 1.7.1+cu110

    unity-addons.jpg


    untiy-ml-settings.jpg

    Below
    OnActionReceived not firing

    unity-console.jpg

    I tried this will build 12 and got a basic setup to work, though I would like to get this newer version with ActionBuffers working.

    Just using Debug.Log for now.

    Code (CSharp):
    1. using Unity.MLAgents;
    2. using Unity.MLAgents.Actuators;
    3. using Unity.MLAgents.Sensors;
    4. using UnityEngine;
    5.  
    6. public class AI_NPC_Warrior : Agent
    7. {
    8.  
    9.     public override void OnEpisodeBegin()
    10.     {
    11.         Debug.Log("OnEpisodeBegin");
    12.     }
    13.  
    14.  
    15.     public override void CollectObservations(VectorSensor sensor)
    16.     {
    17.         sensor.AddObservation(transform.position);
    18.        
    19.         for (int i = 1; i <= 5; i++)
    20.         {
    21.             sensor.AddObservation(0);
    22.             Debug.Log("CollectObservations : " + i);
    23.         }
    24.     }
    25.    
    26.  
    27.     public override void OnActionReceived(ActionBuffers actionBuffers)
    28.     {
    29.         Debug.Log("OnActionReceived");
    30.         Debug.Log(actionBuffers.DiscreteActions[0]);
    31.     }
    32. }

    Any ideas would be great.
     
  2. christophergoy

    christophergoy

    Joined:
    Sep 16, 2015
    Posts:
    735
    Do you have a decision requester component or ever call RequestDecision anywhere?
     
    jchambers likes this.
  3. jchambers

    jchambers

    Joined:
    Feb 4, 2013
    Posts:
    7
    Hi Christopher and thank you for the reply.

    I did not know about this, helped a bunch getting me on the right track.
    I can get the OnActionReceived firing now.
    Still have more to learn about Max size, Space size and Stacked vectors.

    I'll post my code once I get something that may help others.
     
  4. christophergoy

    christophergoy

    Joined:
    Sep 16, 2015
    Posts:
    735
    by max size do you mean max step? If so, maxStep is the maximum number of steps the agent would take before it being done, resetting, and starting over.

    Space size in the BehaviorParameters Component tells ML-Agents how many observations you want to make. From the code you posted above, you would want to set that to 8:

    transform.Position is a Vector3 which is 3 floats. (size of 3)
    Your loop adds 5 zeros as observations (size of 5)
    Total number of observations is 3 + 5 = 8.

    Stacked vectors give the Neural network a look back at previous observations. So let's say you set Stack Vectors to 5, this means the neural network would get your observations from the current frame plus the previous 4 observations.

    Let me know if that helps clarify anything. Also, all of these things are explained in the documentation if you'd like to read through it.