Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Heuristic function not called.

Discussion in 'ML-Agents' started by JPauloesact, Jul 19, 2023.

  1. JPauloesact

    JPauloesact

    Joined:
    Nov 1, 2013
    Posts:
    4
    I installed MLagents on macox and I can run mlagents-learn without errors. I added the following script to the agent and in the Behavior Parameters I set Heuristic Only. However, the Heuristic function is not invoked. Someone can help me?

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.MLAgents;
    5. using Unity.MLAgents.Actuators;
    6. using Unity.MLAgents.Sensors;
    7. using UnityEngine;
    8.  
    9. public class MoveToGoalAgent : Agent
    10. {
    11.     [SerializeField] private Transform targetTransform;
    12.     [SerializeField] private float speed = 3f;
    13.  
    14.     public override void CollectObservations(VectorSensor sensor)
    15.     {
    16.         Debug.Log("CollectObservations");
    17.  
    18.         sensor.AddObservation(transform.position);
    19.         sensor.AddObservation(targetTransform);
    20.     }
    21.  
    22.     public override void OnActionReceived(ActionBuffers actions)
    23.     {
    24.         Debug.Log("OnActionReceived");
    25.         float moveX = actions.ContinuousActions[0];
    26.         float moveZ = actions.ContinuousActions[1];
    27.  
    28.         transform.position += new Vector3(moveX, 0, moveZ) * Time.deltaTime * speed;
    29.     }
    30.  
    31.     public void OnTriggerEnter(Collider other)
    32.     {
    33.        
    34.         Debug.Log("OnTriggerEnter");
    35.         if (other.TryGetComponent<Goal>(out Goal goal))
    36.         {
    37.             SetReward(1f);
    38.             EndEpisode();
    39.         }
    40.         if (other.TryGetComponent<Wall>(out Wall wall))
    41.         {
    42.             SetReward(-1f);
    43.             EndEpisode();
    44.         }
    45.     }
    46.    
    47.     public override void Heuristic(in ActionBuffers actionsOut)
    48.     {
    49.         Debug.Log("Heuristic");
    50.         var continuousActionsOut = actionsOut.ContinuousActions;
    51.         continuousActionsOut[0] = Input.GetAxisRaw("Horizontal") ;
    52.         continuousActionsOut[1] = Input.GetAxisRaw("Vertical");
    53.     }
    54.  
    55.    
    56.     public override void OnEpisodeBegin()
    57.     {
    58.         Debug.Log("OnEpisodeBegin");
    59.         transform.position = Vector3.zero;
    60.     }
    61. }
    62.  
     

    Attached Files:

  2. Energymover

    Energymover

    Joined:
    Mar 28, 2023
    Posts:
    29
    Just a guess, but do you have a decision requester on the agent?