Search Unity

Question OnActionRecieved isn't being called

Discussion in 'ML-Agents' started by smoemnk, Aug 23, 2023.

  1. smoemnk

    smoemnk

    Joined:
    Nov 1, 2021
    Posts:
    6
    I added a debug log to check whether my OnActionRecieved() method was being called, because when I was recording my demonstration for behaviour cloning (I am exclusively using behaviour cloning) the demo file recorded 1 step and 2 episodes, and the debug log never showed in console. The episode thing is an issue in and of itself but I'll figure that out later, probably. Heres the code I'm working with, it has a lot of useless stuff but trust me removing even one component renders the entire code useless for some reason:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.MLAgents;
    4. using Unity.MLAgents.Sensors;
    5. using UnityEngine;
    6. using Unity.MLAgents.Actuators;
    7.  
    8. public class CarParkingAgent : Agent
    9. {
    10.     public Transform targetParkingSpot;
    11.     public CarController carController;
    12.     public float maxRaycastDistance = 10f;
    13.     private Rigidbody rb;
    14.     public float maxSpeed = 10f;
    15.     public float maxAngularVelocity = 10f;
    16.     private bool isCarParked = false;
    17.     public Vector3 tensor;
    18.     private bool lastFrameParkedStatus = false;
    19.     private int currentStep = 0;
    20.  
    21.     void Start()
    22.     {
    23.         rb = GetComponent<Rigidbody>();
    24.         rb.inertiaTensor = tensor;
    25.         rb.inertiaTensor = new Vector3(1829.532f, 1974.514f, 391.8728f);
    26.  
    27.         Debug.Log("Initial Car Position: " + transform.localPosition);
    28.     }
    29.  
    30.     private void Update()
    31.     {
    32.         // Check if the car is parked
    33.         if (!isCarParked)
    34.         {
    35.             Debug.Log("car aint parked");
    36.             isCarParked = IsCarOnParkingSpot();
    37.         }
    38.     }
    39.     private void FixedUpdate()
    40.     {
    41.         //idk what this does but if i remove it my entire IsCarOnParkingSpot() stops working
    42.         if (!lastFrameParkedStatus)
    43.         {
    44.             isCarParked = IsCarOnParkingSpot();
    45.         }
    46.         lastFrameParkedStatus = isCarParked;
    47.     }
    48.  
    49.     public override void CollectObservations(VectorSensor sensor)
    50.     {
    51.        
    52.         sensor.AddObservation(transform.localPosition);
    53.         sensor.AddObservation(transform.rotation);
    54.         sensor.AddObservation(carController.GetVelocity());
    55.  
    56.         Vector3 toTarget = targetParkingSpot.localPosition - transform.localPosition;
    57.         Vector3 normalizedToTarget = toTarget.normalized;
    58.         sensor.AddObservation(toTarget.magnitude);
    59.         sensor.AddObservation(normalizedToTarget);
    60.         sensor.AddObservation(rb.velocity.magnitude / maxSpeed);
    61.         sensor.AddObservation(rb.angularVelocity.magnitude / maxAngularVelocity);
    62.  
    63.         sensor.AddObservation(targetParkingSpot.localPosition);
    64.         sensor.AddObservation(targetParkingSpot.rotation);
    65.  
    66.         Vector3[] raycastDirections = { transform.forward, transform.right, -transform.right, transform.forward + transform.right, transform.forward - transform.right };
    67.  
    68.         foreach (Vector3 direction in raycastDirections)
    69.         {
    70.             if (Physics.Raycast(transform.localPosition, direction, out RaycastHit hit, maxRaycastDistance))
    71.             {
    72.                 float normalizedDistance = hit.distance / maxRaycastDistance;
    73.                 sensor.AddObservation(normalizedDistance);
    74.             }
    75.             else
    76.             {
    77.                 sensor.AddObservation(-1f);
    78.             }
    79.         }
    80.     }
    81.  
    82.     public override void OnActionReceived(ActionBuffers actions)
    83.     {
    84.         Debug.Log("OnActionReceived called, current step = " + currentStep);
    85.         currentStep++;
    86.         float accelerate = actions.ContinuousActions[0];
    87.         float brake = actions.ContinuousActions[1];
    88.         float steering = actions.ContinuousActions[2];
    89.  
    90.         carController.HandleMotor(accelerate, brake);
    91.         carController.HandleSteering(steering);
    92.     }
    93.  
    94.     public override void OnEpisodeBegin()
    95.     {
    96.         currentStep = 0;
    97.         Debug.Log("new episode begun");
    98.         ResetCarPosition();
    99.         isCarParked = false;
    100.         lastFrameParkedStatus = false;
    101.     }
    102.  
    103.     public void OnTriggerEnter(Collider collision)
    104.     {
    105.         if (collision.gameObject.CompareTag("Obstacle"))
    106.         {
    107.             ResetCarPosition();
    108.             EndEpisode();
    109.             isCarParked = false;
    110.         }
    111.         else if (collision.gameObject.CompareTag("ParkingSpot") && !isCarParked)
    112.         {
    113.             IsCarOnParkingSpot();
    114.         }
    115.     }
    116.  
    117.     public bool IsCarOnParkingSpot()
    118.     {
    119.         Vector3 toParkingSpot = targetParkingSpot.position - transform.position;
    120.         //Debug.Log("Distance from parking spot: " + toParkingSpot.magnitude);
    121.         if (toParkingSpot.magnitude < 1.5f)
    122.         {
    123.             isCarParked = true;
    124.             ResetCarPosition();
    125.             EndEpisode();
    126.             return true;
    127.         }
    128.         isCarParked = false;
    129.  
    130.         return false;
    131.     }
    132.  
    133.     private void ResetCarPosition()
    134.     {
    135.         rb.velocity = Vector3.zero;
    136.         rb.angularVelocity = Vector3.zero;
    137.  
    138.         Vector3 localPosition = new Vector3(
    139.             UnityEngine.Random.Range(-7f, -4f),
    140.             0.1f,
    141.             UnityEngine.Random.Range(-7f, 2f));
    142.  
    143.         //Debug.Log("Calculated Local Position: " + localPosition);
    144.  
    145.         transform.localPosition = localPosition;
    146.         transform.localRotation = Quaternion.Euler(Vector3.zero);
    147.        
    148.     }
    149. }
    150.  
    151.  
    Any help is greatly appreciated as ChatGPT has been quite useless when it comes to this for some reason. Thankyou in advance.
     
  2. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    147
    i assume you have a decision requester on your agent?
    also demo recordings record heuristics actions, not the agent so without a heuristics method set there won't be anything to record
     
  3. smoemnk

    smoemnk

    Joined:
    Nov 1, 2021
    Posts:
    6
    yeah forgot about that, however I added a method, everything seems to work fine, but im still going through the same issue, OnActionRecieved method isnt getting called.
     
  4. smoemnk

    smoemnk

    Joined:
    Nov 1, 2021
    Posts:
    6
    In my case what fixed it was adding an Awake() method, I assume for some an Initialise() method would work too but it didn't for me when I tried.
    Code (CSharp):
    1. void Awake()
    2.     {
    3.         Debug.Log("Awake called");
    4.         rb = GetComponent<Rigidbody>();
    5.         rb.inertiaTensor = tensor;
    6.         rb.inertiaTensor = new Vector3(1829.532f, 1974.514f, 391.8728f);
    7.         useHeuristic = true;
    8.     }