Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Agent dont use ray perceprion sensor 3d

Discussion in 'ML-Agents' started by Barden_20, Jul 28, 2023.

  1. Barden_20

    Barden_20

    Joined:
    Mar 21, 2021
    Posts:
    3
    I have a scene with a track and a car, and I want the car to learn how to pass the track itself. Everything works correctly, only the car learns strangely. It seems that she does not use ray perception sensor 3d. There are also checkpoints, if the car passes them in the right direction, then it receives a reward of +0.01, and if in the wrong direction, then -0.01. Attached screenshots of the agent settings.
    screenshot_2.png screenshot_1.png

    Agent script:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Unity.MLAgents;
    6. using Unity.MLAgents.Actuators;
    7. using Unity.MLAgents.Sensors;
    8. using System.Runtime.CompilerServices;
    9. using UnityEngine.UI;
    10. using TMPro;
    11. using System;
    12. using Random = UnityEngine.Random;
    13. public class Car : Agent
    14. {
    15.     [SerializeField] private TrackCheckpoints trackCheckpoints;
    16.     [SerializeField] private Transform spwanPosition;
    17.     private MSVehicleControllerFree carDriver;
    18.     private MSSceneControllerFree sceneController;
    19.     public GameObject scene;
    20.     public Text distance_text;
    21.     double distanceTravelled = 0;
    22.     Vector3 lastPosition;
    23.  
    24.     IEnumerator distance_reward()
    25.     {
    26.         while (true)
    27.         {
    28.             yield return new WaitForSeconds(1.0f);
    29.             AddReward((float)(0.001 * distanceTravelled));
    30.             distanceTravelled = 0;
    31.         }
    32.     }
    33.     public void Awake()
    34.     {
    35.         carDriver = GetComponent<MSVehicleControllerFree>();
    36.         lastPosition = transform.position;
    37.         sceneController = scene.GetComponent<MSSceneControllerFree>();
    38.         StartCoroutine(distance_reward());
    39.     }
    40.     private void FixedUpdate()
    41.     {
    42.        
    43.         if (sceneController.car_speed < 10) {
    44.             AddReward(-0.00001f);
    45.         }
    46.        
    47.         if (sceneController.car_speed >= 0) {
    48.             distanceTravelled += Vector3.Distance(transform.position, lastPosition);
    49.         }
    50.         else if (sceneController.car_speed < 0) {
    51.             distanceTravelled -= Vector3.Distance(transform.position, lastPosition);
    52.         }
    53.         lastPosition = transform.position;
    54.         distanceTravelled = Math.Round(distanceTravelled, 2);
    55.         distance_text.text = "Distance: " + distanceTravelled;
    56.         AddReward((float)(0.000002 * sceneController.car_speed));
    57.     }
    58.     public override void OnEpisodeBegin()
    59.     {
    60.        
    61.         transform.position = spwanPosition.position + new Vector3 (Random.Range(-1f,+1f), 0, Random.Range(-1f, +1f));
    62.         transform.forward = spwanPosition.forward;
    63.         trackCheckpoints.ResetCheckpoints(transform);
    64.         carDriver.GetComponent<Rigidbody>().velocity = Vector3.zero;
    65.      
    66.        
    67.         lastPosition = transform.position;
    68.         distanceTravelled = 0;
    69.         StartCoroutine(distance_reward());
    70.     }
    71.     public override void CollectObservations(VectorSensor sensor)
    72.     {
    73.         Vector3 checkpointForward = trackCheckpoints.GetNextCheckpoint(transform).transform.forward;
    74.         float directionDot = Vector3.Dot(transform.forward, checkpointForward);
    75.      
    76.        
    77.         sensor.AddObservation(directionDot);
    78.         sensor.AddObservation(sceneController.car_speed);
    79.         sensor.AddObservation((float)distanceTravelled);
    80.     }
    81.     public override void OnActionReceived(ActionBuffers actions)
    82.     {
    83.         float forwardAmount = 0f;
    84.         float turnAmount = 0f;
    85.         switch (actions.DiscreteActions[0])
    86.         {
    87.             case 0: forwardAmount = 0f; break;
    88.             case 1: forwardAmount = +1f; break;
    89.             case 2: forwardAmount = -1f; break;
    90.         }
    91.         switch (actions.DiscreteActions[1])
    92.         {
    93.             case 0: turnAmount = 0f; break;
    94.             case 1: turnAmount = -1f; break;
    95.             case 2: turnAmount = +1f; break;
    96.         }
    97.         sceneController.Update_Controls(forwardAmount, turnAmount);
    98.         carDriver.GetComponent<MSVehicleControllerFree>().Update_Controls(forwardAmount, turnAmount);
    99.      
    100.        
    101.         AddReward(-0.000007f);
    102.     }
    103.     public override void Heuristic(in ActionBuffers actionsOut)
    104.     {
    105.         int forwardAction = 0;
    106.         if(Input.GetKey(KeyCode.Keypad8)) forwardAction = 1;
    107.         if (Input.GetKey(KeyCode.Keypad5)) forwardAction = 2;
    108.         int turnAction = 0;
    109.         if (Input.GetKey(KeyCode.Keypad4)) turnAction = 1;
    110.         if (Input.GetKey(KeyCode.Keypad6)) turnAction = 2;
    111.         ActionSegment<int> discreteActions = actionsOut.DiscreteActions;
    112.         discreteActions[0] = forwardAction;
    113.         discreteActions[1] = turnAction;
    114.     }
    115.  
    116.    
    117.     private void OnTriggerEnter(Collider other)
    118.     {
    119.         if (other.gameObject.TryGetComponent<Wall>(out Wall wall))
    120.         {
    121.             AddReward(-0.01f);
    122.             EndEpisode();
    123.         }
    124.     }
    125. }
     
    Last edited: Jul 28, 2023
  2. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    144
    your sensors need unique names, what makes you say they're not working?
    you can output the results from the rays to check in your collectobservations function
     
  3. Barden_20

    Barden_20

    Joined:
    Mar 21, 2021
    Posts:
    3
    I edit sensors names. But while train car, you can see that it does not use sensors. And how i can get otput from rays?
     
  4. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    144
  5. Barden_20

    Barden_20

    Joined:
    Mar 21, 2021
    Posts:
    3
    I check, ray sensor works fine. But car still not train. Output from console: screen.png My scene: screen2.jpg
     
  6. Energymover

    Energymover

    Joined:
    Mar 28, 2023
    Posts:
    33