Search Unity

Question Heuristic is called when in inference only

Discussion in 'ML-Agents' started by Shachartz0, Mar 25, 2021.

  1. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    Hi there
    I am new in Unity and trying some ml-agents-based path-finding game.
    I trained my agent for some time and got good results (watched the cumulative reward reaches his top value on TensorBoard). But when I implemented my trained weights as the agent's model, nothing happens.
    I set the behavior to 'Inference only' and to 'Default' and nothing happens, and when I press the errows the agent moves (meaning it uses Heuristic method).
    Please help me understand where I go wrong

    I also attach the agent's code
    Thank you

    upload_2021-3-25_18-57-27.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.MLAgents;
    5. using Unity.MLAgents.Sensors;
    6.  
    7. public class PlayerAgentBasic : Agent
    8. {
    9.     [SerializeField]
    10.     private float speed = 5.0f;
    11.  
    12.     [SerializeField]
    13.     private float distanceRequired = 1.5f;
    14.  
    15.     [SerializeField]
    16.     private GameObject target;
    17.  
    18.     [SerializeField]
    19.     private Material successMateial;
    20.  
    21.     [SerializeField]
    22.     private Material failMateial;
    23.  
    24.     [SerializeField]
    25.     private Material defaultMateial;
    26.  
    27.     [SerializeField]
    28.     private MeshRenderer groundMeshRenderer;
    29.  
    30.     #region Private Instance Variables
    31.     private Rigidbody playerRigidBody;
    32.     private Vector3 originalPosition;
    33.     private Vector3 originalTargetPosition;
    34.     #endregion
    35.  
    36.     public override void Initialize()
    37.     {
    38.         playerRigidBody = GetComponent<Rigidbody>();
    39.         originalPosition = transform.localPosition;
    40.         originalTargetPosition = target.transform.localPosition;
    41.     }
    42.  
    43.     public override void OnEpisodeBegin()
    44.     {
    45.         transform.LookAt(transform.transform);
    46.         target.transform.localPosition = new Vector3(originalTargetPosition.x, originalTargetPosition.y, Random.Range(-4, 4));
    47.         transform.localPosition = originalPosition;
    48.         transform.localPosition = new Vector3(originalPosition.x, originalPosition.y, Random.Range(-4, 4));
    49.     }
    50.     public override void CollectObservations(VectorSensor sensor)
    51.     {
    52.         sensor.AddObservation(transform.localPosition); // x y z (3 dim)
    53.         sensor.AddObservation(target.transform.localPosition); // x y z (3 dim)
    54.         sensor.AddObservation(playerRigidBody.velocity.x); // one dimention
    55.         sensor.AddObservation(playerRigidBody.velocity.z); // one dimention
    56.  
    57.     }
    58.  
    59.     public override void OnActionReceived(float[] vectorAction)
    60.     {
    61.         var vectorForce = new Vector3();
    62.         vectorForce.x = vectorAction[0];
    63.         vectorForce.z = vectorAction[1];
    64.  
    65.         playerRigidBody.AddForce(vectorForce * speed);
    66.  
    67.         var distanceFromTarget = Vector3.Distance(transform.localPosition, target.transform.localPosition);
    68.  
    69.         if(distanceFromTarget < distanceRequired) // we are doing good
    70.         {
    71.             SetReward(1.0f);
    72.             EndEpisode();
    73.             StartCoroutine(swapGroundMaterial(successMateial, 0.5f));
    74.  
    75.         }
    76.         if (transform.localPosition.y < 0) // falling of the floor
    77.         {
    78.             AddReward(-.5f);
    79.             EndEpisode();
    80.  
    81.             StartCoroutine(swapGroundMaterial(failMateial, 0.5f));
    82.         }
    83.         // go back and punish the agent for falling
    84.  
    85.     }
    86.  
    87.     public override void Heuristic(float[] actionsOut) // telling the agent how to move
    88.     {
    89.  
    90.         actionsOut[0] = Input.GetAxis("Horizontal"); // x
    91.         actionsOut[1] = Input.GetAxis("Vertical"); // z
    92.     }
    93.  
    94.     private IEnumerator swapGroundMaterial(Material mat, float time)
    95.     {
    96.         groundMeshRenderer.material = mat;
    97.         yield return new WaitForSeconds(time);
    98.         groundMeshRenderer.material = defaultMateial;
    99.  
    100.     }
    101. }
    102.  
     
  2. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    Can someone please take a look?
    I am stuck on this for a week now..:(
     
  3. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
    I'll flag for the team to have a look. Which version of ML Agents are you using?
     
  4. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    Hi, thank you!
    I'm using version 1.0.7
     
  5. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    Hi @Shachartz0,
    Sorry I missed your thread. When you hit play in the editor, what does your BehaviorParameters component look like? It does sound like it is in Heuristic mode instead of running inference.
     
  6. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    Hi thank you for your reply.
    I set the behavior type to 'Inference only' but when I hit play heuristic method is called and I don't understand why
     
  7. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    Yes, I understand that. Can you take a screen shot of the BehaviorParameters component of your agent after you hit play? I'd like to see if it was set back to Heuristic only.
     
  8. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    It stays in inference only...
    upload_2021-3-30_20-12-12.png
     
  9. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    what does your decision requester component look like?
     
  10. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
  11. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    And "PlayerMaze" is the model you want to use? Not PlayerMaze1 or PlayerMaze2?
     
  12. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    would you be willing to share your project in some way? Is it on github or some other repo hosting service where I could take a look?