Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Not avoiding walls...

Discussion in 'ML-Agents' started by doneda, Jul 23, 2020.

  1. doneda

    doneda

    Joined:
    Nov 4, 2019
    Posts:
    1
    Hello,
    I'm using Unity 2019.4.3f1 and ml-agents release-3 and I know I'm doing something wrong but I can't find it out....
    My goal is to train a Gameobject to go from its starting point to a determined area, without colliding with a wall.
    help.PNG

    The walls are just cubes tagged as "canal".
    I`ve also created RayPerseptionSensor 3D.
    My script is:

    ========================================
    Code (CSharp):
    1. public class navio_agent : Agent
    2. {
    3.     private Rigidbody rBody;
    4.     public GameObject begin;
    5.     public GameObject end;
    6.     private Vector3 startingPosition;
    7.     public bool useVectorObs;
    8.     public float speed = 15f;
    9.     public float ROT = 10f;
    10.     public float beginCourse = 0f;
    11.     private float timeAgent;
    12.     public event Action OnReset;
    13.     private float velocinicial = 15f;
    14.  
    15.     public override void Initialize()
    16.     {
    17.         rBody = GetComponent<Rigidbody>();
    18.         startingPosition = new Vector3(UnityEngine.Random.Range(begin.transform.position.x - 50, begin.transform.position.x + 50), 0f, UnityEngine.Random.Range(begin.transform.position.z - 50, begin.transform.position.z + 50));
    19.         rBody.MoveRotation(Quaternion.Euler(0f, UnityEngine.Random.Range(beginCourse-20f, beginCourse+20f), 0f));
    20.        
    21.     }
    22.  
    23.     public override void CollectObservations(VectorSensor sensor) //12
    24.     {
    25.         sensor.AddObservation(transform.position); //3
    26.         sensor.AddObservation(end.transform.position);//3
    27.         sensor.AddObservation(transform.rotation); //3
    28.         sensor.AddObservation(rBody.velocity);//3
    29.  
    30.     }
    31.    
    32.     public override void Heuristic(float[] actionsOut)
    33.     {
    34.         actionsOut[0] = 0;
    35.         if (Input.GetKey(KeyCode.D))
    36.         {
    37.             actionsOut[0] = 1;
    38.         }
    39.  
    40.         else if (Input.GetKey(KeyCode.A))
    41.         {
    42.             actionsOut[0] = 2;
    43.         }
    44.         else if (Input.GetKey(KeyCode.X))
    45.         {
    46.             actionsOut[0] = 3;
    47.         }
    48.         else if (Input.GetKey(KeyCode.W))
    49.         {
    50.             actionsOut[0] = 4;
    51.         }
    52.         else if (Input.GetKey(KeyCode.S))
    53.         {
    54.             actionsOut[0] = 5;
    55.         }
    56.     }
    57.  
    58.  
    59.     public override void OnActionReceived(float[] vectorAction)
    60.     {
    61.         AddReward(-0.05f);
    62.         Move(vectorAction);      
    63.     }
    64.  
    65.     public void FixedUpdate()
    66.     {
    67.          RequestDecision();
    68.     }
    69.  
    70.     public override void OnEpisodeBegin()
    71.     {
    72.         Reset();
    73.     }
    74.  
    75.     public void Move(float[] act)
    76.     {
    77.         float dirToGo = 0f;
    78.         var rotateDir = Vector3.zero;
    79.  
    80.         var action = Mathf.FloorToInt(act[0]);
    81.  
    82.         switch (action)
    83.         {
    84.             case 1:
    85.                 rotateDir = transform.up * 1f;
    86.                 rBody.AddRelativeTorque(rotateDir * ROT, ForceMode.Acceleration);
    87.                 break;
    88.             case 2:
    89.                 rotateDir = transform.up * -1f;
    90.                 rBody.AddRelativeTorque(rotateDir * ROT, ForceMode.Acceleration);
    91.                 break;
    92.             case 3:
    93.                 break;
    94.             case 4:
    95.                 dirToGo = 1f * speed;
    96.                 rBody.AddRelativeForce(new Vector3(0, 0, dirToGo), ForceMode.Acceleration);
    97.                 break;
    98.             case 5:
    99.                 dirToGo = -1f * speed;
    100.                 rBody.AddRelativeForce(new Vector3(0,0,dirToGo), ForceMode.Acceleration);
    101.                 break;
    102.         }    
    103.     }
    104.  
    105.     private void Reset()
    106.     {
    107.         transform.position = startingPosition;      
    108.     }
    109.  
    110.    
    111.     public void OnCollisionEnter(Collision collision)
    112.     {
    113.         if (collision.gameObject.CompareTag("area") && collision.gameObject.name == end.name)
    114.         {
    115.             AddReward(+1f);
    116.             EndEpisode();
    117.  
    118.         } else if (collision.gameObject.CompareTag("canal"))
    119.         {
    120.             AddReward(-0.5f);
    121.         }              
    122.     }
    123.  
    124. }
    ========================================
    I've also set the Behavior Parameters like this.
    upload_2020-7-22_20-25-41.png
    But I don't see the GameObject trying to avoid the wall. What could be happening? Thank you all!
     
  2. awjuliani

    awjuliani

    Unity Technologies

    Joined:
    Mar 1, 2017
    Posts:
    69
    Hello,

    I would recommend starting off with a much closer goal, and then progressively moving it further away. The. issue right now is that the only positive reward the agent gets is for reaching the goal, and because that. is hard for the agent to do, it does not learn any meaningful behavior.
     
  3. dani_kal

    dani_kal

    Joined:
    Mar 25, 2020
    Posts:
    52
    Hi!!!
    I am not sure, but perhaps you have to include the distance to the wall also, into your observations.