Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question ML-Agents, I can't use heuristic only. So I can't use ML-Agents

Discussion in 'ML-Agents' started by kuraytunc123, May 18, 2024.

  1. kuraytunc123

    kuraytunc123

    Joined:
    Sep 12, 2023
    Posts:
    1
    Hello, I was trying for MLAgents it worked on 2D but it doesn't work on 3D could someone please help me ? 2.PNG 3.PNG 1.PNG


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Unity.MLAgents;
    4. using Unity.MLAgents.Actuators;
    5. using Unity.MLAgents.Sensors;
    6.  
    7. public class MoveToTargetAgent : Agent
    8. {
    9.     //[SerializeField] private Transform environment; // Environment object
    10.     [SerializeField] private Transform target; // Target object
    11.     [SerializeField] private Material rewardMaterial; // Reward material
    12.     [SerializeField] private Material penaltyMaterial; // Penalty material
    13.  
    14.     public override void OnEpisodeBegin()
    15.     {
    16.         // Randomly set the positions of the target and the agent
    17.         target.localPosition = new Vector3(Random.Range(-4.4f, 4.665f), 0.22f, Random.Range(4.516f, -4.648f));
    18.         transform.localPosition = new Vector3(Random.Range(0f, 4.665f), 0.22f, Random.Range(4.516f, -4.648f));
    19.  
    20.         // Rotate the environment (optional)
    21.         // environment.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
    22.     }
    23.  
    24.     public override void CollectObservations(VectorSensor sensor)
    25.     {
    26.         // Add the positions of the agent and the target as observations
    27.         sensor.AddObservation(transform.localPosition);
    28.         sensor.AddObservation(target.localPosition);
    29.     }
    30.  
    31.     public override void OnActionReceived(ActionBuffers actions)
    32.     {
    33.         float moveX = actions.ContinuousActions[0]; // Movement along the X-axis (right-left)
    34.         float moveZ = actions.ContinuousActions[1]; // Movement along the Z-axis (forward-backward)
    35.  
    36.         float movementSpeed = 10f;
    37.  
    38.         // Create a movement vector
    39.         Vector3 movement = new Vector3(moveX, 0f, moveZ) * Time.deltaTime * movementSpeed;
    40.  
    41.         // Update the agent's position
    42.         transform.localPosition += movement;
    43.     }
    44.  
    45.     public override void Heuristic(in ActionBuffers actionsOut)
    46.     {
    47.         ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
    48.  
    49.         // Log detected inputs
    50.         Debug.Log("Horizontal Input: " + Input.GetAxis("Horizontal"));
    51.         Debug.Log("Vertical Input: " + Input.GetAxis("Vertical"));
    52.  
    53.         // Map keyboard inputs to continuous actions
    54.         continuousActions[0] = Input.GetAxis("Horizontal"); // X-axis (right-left)
    55.         continuousActions[1] = 0; // No movement along the Y-axis (up-down)
    56.         continuousActions[2] = Input.GetAxis("Vertical"); // Z-axis (forward-backward)
    57.     }
    58.  
    59.     private void OnTriggerEnter(Collider other)
    60.     {
    61.         if (other.CompareTag("Target")) // Compare with the target object's tag
    62.         {
    63.             AddReward(10f);
    64.             ChangeMaterial(rewardMaterial);
    65.             EndEpisode();
    66.         }
    67.         else if (other.CompareTag("Wall")) // Compare with the wall object's tag
    68.         {
    69.             AddReward(-2f);
    70.             ChangeMaterial(penaltyMaterial);
    71.             EndEpisode();
    72.         }
    73.     }
    74.  
    75.     private void ChangeMaterial(Material material)
    76.     {
    77.         // Change the material of the target (or another visual element)
    78.         target.GetComponent<MeshRenderer>().material = material;
    79.     }
    80. }
     
  2. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    154
    you only have 2 branches set in the inspector and your code is trying to access 3 branches (0, 1 & 2)