Search Unity

Question Heuristics always gives same value

Discussion in 'ML-Agents' started by dragonlord7337, Aug 26, 2020.

  1. dragonlord7337

    dragonlord7337

    Joined:
    Mar 21, 2019
    Posts:
    2
    Hi. I'm working on a project involving an object using a neural network to try and traverse a maze. I am trying to use WASD for the movement of the agent

    I have attached a Decision Requester with a decision period of 1 to my agent and written up my code, but the value my Heurist function gets given seems to always be the same.

    The following is my code for the Heuristic function

    public override void Heuristic(float[] actionsOut)
    {
    actionsOut[0] = 0;
    if (Input.GetKey(KeyCode.W))
    {
    actionsOut[0] = 1;
    }
    else if (Input.GetKey(KeyCode.S))
    {
    actionsOut[0] = 2;
    }
    else if (Input.GetKey(KeyCode.A))
    {
    actionsOut[0] = 3;
    }
    else if (Input.GetKey(KeyCode.D))
    {
    actionsOut[0] = 4;
    }
    Debug.Log(actionsOut[0]);
    }

    The debug log I added to find what actionsOut[0] was returning and it's always 0 so the agent ends up not moving

    I am also showing my behaviour parameters to show what I have for my Vector Actions

    I have tried this with the space type being continuous as well but no change in results

    I am utterly confused what else can be done to fix it. It feels like my decision requester is not delivering decisions to my heuristic function. Any help would be appreciated
     
  2. ReinierJ

    ReinierJ

    Joined:
    Jul 10, 2020
    Posts:
    10
    Since you say
    it sounds like the Debug.Log statement at the end of the Heuristics function is working. In that case, your assumption that the decision requester is not asking your heuristic function for decisions is incorrect. If you are unsure, you can add a Debug.Log("test") statement to the beginning of the Heuristic function and see if anything is being printed. Make sure that Log level messages are enabled in the console.

    If the Heuristic function is being called and you press one of the keys from your code, then most likely the Input.GetKey calls are returning false. This could be for a couple of reasons:
    • Are you pressing WASD from the Game view? Otherwise, it might not work
    • Your keyboard is broken ;)
    • Some other reason. Have you tried restarting Unity?
     
  3. dragonlord7337

    dragonlord7337

    Joined:
    Mar 21, 2019
    Posts:
    2
    I actually wanted the Neural Network to move around the agent itself (or... well... as it's being trained) instead of being the one controlling it. I ended up realising I need to set the behaviour type to Default and run it with the Python config file to get it to do so

    Thank you very much for the help anyway :)