#ML Agents train to avoid walls

1 messages · Page 1 of 1 (latest)

fallen stag
#

I've been trying to make my agent avoid walls and reach a target. However after training It doesn't seem to get much smarter with a mean reward of close to or slightly below 0. Either it wanders around, gets stuck near a wall in place not knowing what to do or simply walks into the wall

#
    public override void Initialize()
    {
        rBody = GetComponent<Rigidbody>();
        initialPosition = transform.localPosition;
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Wall"))
        {       
            AddReward(-3f);
            EndEpisode();
        }
    }
    
    public override void OnEpisodeBegin()
    {
        transform.localPosition = new Vector3(Random.Range(-15f, 15f), 0.5f, Random.Range(-15f, 15f));
        Target.localPosition = new Vector3(Random.Range(-15f, 15f), 0.5f, Random.Range(-15f, 15f));
        previousDistanceToTarget = Vector3.Distance(transform.localPosition, Target.localPosition);
    }
    public override void CollectObservations(VectorSensor sensor)
    {

        Vector3 relativeTargetPos = transform.InverseTransformPoint(Target.position);
        sensor.AddObservation(relativeTargetPos.normalized);
        sensor.AddObservation(relativeTargetPos.magnitude / 40f);

        Vector3 localVelocity = transform.InverseTransformDirection(rBody.linearVelocity);
        sensor.AddObservation(localVelocity.x / MoveSpeed);
        sensor.AddObservation(localVelocity.z / MoveSpeed);
    }
#
 public override void OnActionReceived(ActionBuffers actions)
 {
 
     float moveX = actions.ContinuousActions[0];
     float moveZ = actions.ContinuousActions[1];

     Vector3 movement = new Vector3(moveX, 0f, moveZ);

     transform.position += movement * MoveSpeed * Time.fixedDeltaTime;


     if (movement.magnitude > 0.01f)
     {
         Quaternion targetRotation = Quaternion.LookRotation(movement);
         float rotationSpeed = 10f;
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.fixedDeltaTime);
     }

     EvaluateMovement();
 }

 private void EvaluateMovement()
 {
     if (Target != null)
     {
         distanceToTarget = Vector3.Distance(
         new Vector3(transform.localPosition.x, 0, transform.localPosition.z),
         new Vector3(Target.localPosition.x, 0, Target.localPosition.z));

         float diff = previousDistanceToTarget - distanceToTarget;
        
         AddReward(diff * 0.01f);
         previousDistanceToTarget = distanceToTarget;

         AddReward(-0.0005f);

         if (distanceToTarget < PickupRange)
         {
             AddReward(5f);
             EndEpisode();
         }

     }
 }
undone crater
#

Should probably ask in #1202574086115557446 instead.
The only thing that I can assume is that the issue is because you're not providing the model with any info about the walls. You basically make it blind and expect to walk towards a target while avoiding obstacles, but it doesn't ever learn that there are walls.