Search Unity

Question Agent ignores negative reward

Discussion in 'ML-Agents' started by GabrielVianna, Apr 30, 2022.

  1. GabrielVianna

    GabrielVianna

    Joined:
    Nov 19, 2021
    Posts:
    2
    I am trying to train an agent to be able to cross the street avoiding hitting cars, in the image below an example.
    forum.gif

    I set a positive reward for the agent as soon as he manages to cross the street and a negative reward when he hits a car.
    The problem is that the agent seems to ignore the negative reward and takes no action to avoid hitting the cars. I would like it to stop going foward (Wait until
    car gets out of the way) or to move backwards to dodge a car.

    Can anyone help me?

    This is part of my code that I think may help to unsderstand how I'm doing it:

    Code (CSharp):
    1. public override void OnActionReceived(ActionBuffers actionBuffers)
    2.     {
    3.         Vector3 controlSignal = Vector3.zero;
    4.         controlSignal.y = actionBuffers.ContinuousActions[1];
    5.         transform.Translate(controlSignal * speed * Time.deltaTime);
    6.  
    7.         // I call this function when agent crosses the street, I set a positive reward and end the episode
    8.         if (transform.position.y > 6.0f)
    9.         {
    10.             SetReward(1.0f);
    11.             EndEpisode();
    12.         }
    13.     }
    14.  
    15.     // I call this function when the agent hits a car, I set a negative reward and end the episode
    16.     public void finish() {
    17.         SetReward(-1.0f);
    18.         EndEpisode();
    19.     }
    20.     }
     
    Last edited: Apr 30, 2022