Search Unity

No suitable method found to override

Discussion in 'ML-Agents' started by Yuulis04, May 6, 2021.

  1. Yuulis04

    Yuulis04

    Joined:
    Apr 4, 2021
    Posts:
    28
    Hello. I tried to make a new environment for ML-Agents, but after writing a code, the errors happened :
    'AgentControl.Heuristic(float[])': no suitable method found to override
    'AgentControl.OnActionReceived(float[])': no suitable method found to override

    I'm using Unity 2019.4, ML-Agents 2.0.0 exp-1.
    My code is below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.MLAgents;
    5. using Unity.MLAgents.Sensors;
    6. using Unity.MLAgents.Actuators;
    7.  
    8. public class AgentControl : Agent
    9. {
    10.     public Transform target1;
    11.     public Transform target2;
    12.     public Transform target3;
    13.     public Transform target4;
    14.     Rigidbody rBody;
    15.     private bool onEpisode = false;
    16.     private string closestTarget;
    17.  
    18.     public override void Initialize()
    19.     {
    20.         this.rBody = GetComponent<Rigidbody>();
    21.         onEpisode = false;
    22.     }
    23.  
    24.     public override void OnEpisodeBegin()
    25.     {
    26.         this.rBody.angularVelocity = Vector3.zero;
    27.         this.rBody.velocity = Vector3.zero;
    28.         this.transform.rotation = Quaternion.identity;
    29.  
    30.         this.transform.localPosition = new Vector3(Random.Range(-14.0f, 14.0f), 0.5f, Random.Range(-9.0f, 9.0f));
    31.         onEpisode = true;
    32.  
    33.         float dis1 = Vector3.Distance(this.transform.localPosition, target1.localPosition);
    34.         float dis2 = Vector3.Distance(this.transform.localPosition, target2.localPosition);
    35.         float dis3 = Vector3.Distance(this.transform.localPosition, target3.localPosition);
    36.         float dis4 = Vector3.Distance(this.transform.localPosition, target4.localPosition);
    37.  
    38.         float leastDis = Mathf.Min(dis1, dis2, dis3, dis4);
    39.  
    40.         if (leastDis == dis1) closestTarget = "Target1";
    41.         if (leastDis == dis2) closestTarget = "Target2";
    42.         if (leastDis == dis3) closestTarget = "Target3";
    43.         if (leastDis == dis4) closestTarget = "Target4";
    44.     }
    45.  
    46.     public override void OnActionReceived(float[] vectorAction)
    47.     {
    48.         Vector3 dirToGo = Vector3.zero;
    49.         Vector3 rotateDir = Vector3.zero;
    50.  
    51.         int action = (int)vectorAction[0];
    52.         if (action == 1) dirToGo = transform.forward;
    53.         if (action == 2) dirToGo = transform.forward * -1.0f;
    54.         if (action == 3) rotateDir = transform.up;
    55.         if (action == 4) rotateDir = transform.up * -1.0f;
    56.  
    57.         this.transform.Rotate(rotateDir, Time.deltaTime * 200f);
    58.         this.rBody.AddForce(dirToGo * 0.5f, ForceMode.VelocityChange);
    59.  
    60.         if (this.transform.localPosition.y < 0)
    61.         {
    62.             AddReward(-0.3f);
    63.             EndEpisode();
    64.         }
    65.  
    66.         AddReward(-1.0f / MaxStep);
    67.     }
    68.  
    69.     public void OnCollisionEnter(Collision collision)
    70.     {
    71.         if (collision.gameObject.tag == "target")
    72.         {
    73.             if (onEpisode)
    74.             {
    75.                 if (collision.gameObject.name == closestTarget)
    76.                 {
    77.                     AddReward(1.0f);
    78.                     EndEpisode();
    79.                 }
    80.  
    81.                 else
    82.                 {
    83.                     AddReward(0.3f);
    84.                     EndEpisode();
    85.                 }
    86.  
    87.             }
    88.             else
    89.             {
    90.                 this.transform.localPosition = new Vector3(Random.Range(-14.0f, 14.0f), 0.5f, Random.Range(-9.0f, 9.0f));
    91.             }
    92.         }
    93.     }
    94.  
    95.     public override void Heuristic(float[] actionsOut)
    96.     {
    97.         actionsOut[0] = 0;
    98.         if (Input.GetKey(KeyCode.UpArrow)) actionsOut[0] = 1;
    99.         if (Input.GetKey(KeyCode.DownArrow)) actionsOut[0] = 2;
    100.         if (Input.GetKey(KeyCode.RightArrow)) actionsOut[0] = 3;
    101.         if (Input.GetKey(KeyCode.LeftArrow)) actionsOut[0] = 4;
    102.     }
    103. }
    Could you help me?
     
  2. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    Please read the migration guide when upgrading the ml-agents version. You will see that these method signatures have changed on Agent:

    • Agent.CollectDiscreteActionMasks(DiscreteActionMasker actionMasker) --> Agent.WriteDiscreteActionMask(IDiscreteActionMask actionMask)
    • Agent.Heuristic(float[] actionsOut) --> Agent.Heuristic(in ActionBuffers actionsOut)
    • Agent.OnActionReceived(float[] vectorAction) --> Agent.OnActionReceived(ActionBuffers actions)
    • Agent.GetAction() --> Agent.GetStoredActionBuffers()
     
  3. Yuulis04

    Yuulis04

    Joined:
    Apr 4, 2021
    Posts:
    28
    Thank you! I understand "override" is no longer used in this method.
    This is the first time ML-Agents upgrade for me, so I will check there next time.
     
    christophergoy likes this.
  4. Jiheneh

    Jiheneh

    Joined:
    Dec 17, 2021
    Posts:
    2
    hello, i've changed 2 lines
    • Agent.Heuristic(float[] actionsOut) --> Agent.Heuristic(in ActionBuffers actionsOut)
    • Agent.OnActionReceived(float[] vectorAction) --> Agent.OnActionReceived(ActionBuffers actions)th
    these 2 errors appear:

    Assets\Hummingbird\Scripts\HummingbirdAgent.cs(116,43): error CS0246: The type or namespace name 'ActionBuffers' could not be found (are you missing a using directive or an assembly reference?)


    Assets\Hummingbird\Scripts\HummingbirdAgent.cs(192,39): error CS0246: The type or namespace name 'ActionBuffers' could not be found (are you missing a using directive or an assembly reference?)

    Any help please?







     
    BorjaRiera likes this.
  5. jrupert-unity

    jrupert-unity

    Unity Technologies

    Joined:
    Oct 20, 2021
    Posts:
    12
    Those specific errors should be fixed by adding this using directive at the top of the file:
    Code (CSharp):
    1. using Unity.MLAgents.Actuators;
    Are you migrating the Hummingbird project to ML-Agents 2.0? There's probably quite a bit of work required to do that, you might be better off using ML-Agents 1.0 if you want to use that project.
     
    Jiheneh likes this.
  6. Jiheneh

    Jiheneh

    Joined:
    Dec 17, 2021
    Posts:
    2
    Thank you
     
  7. klinsc

    klinsc

    Joined:
    Oct 4, 2020
    Posts:
    1

    Thank you!
    I just faced the same issues, and just fixed it by downgrading the package to https://github.com/Unity-Technologies/ml-agents/releases/tag/release_1
     
  8. BorjaRiera

    BorjaRiera

    Joined:
    Aug 16, 2022
    Posts:
    1
    it says "Cannot apply indexing with [] to an expression of type 'ActionBuffers'" I only used [] in actionsOut[0] and actionsOut[1]. send help please.
     
  9. SneakySasquatch_

    SneakySasquatch_

    Joined:
    Sep 1, 2022
    Posts:
    2
    Hello everyone!

    Versions:
    ml-agents (python package): 0.28.0
    ml-agents-envs (python package): 0.28.0
    Unity Package Manager package: ML Agents 2.01
    ======================================
    Errors #1:
    Assets\Scripts\BounceAgent.cs(30,25): error CS0115: 'BounceAgent.OnActionReceived(float[])': no suitable method found to override

    &

    Assets\Scripts\BounceAgent.cs(49,25): error CS0115: 'BounceAgent.Heuristic(float[])': no suitable method found to override
    =========================================================================================
    I understand that this code is deprecated - but when I change it to OnActionReceived(ActionBuffers actions) or Heuristic(in ActionBuffers actionsOut), I get the following error:

    Error CS0021 Cannot apply indexing with [] to an expression of type 'ActionBuffers'

    This is because I have not declared an array - but when I do declare an array, I get the following errors:

    Error CS1503 Argument 1: cannot convert from 'Unity.MLAgents.Actuators.ActionBuffers' to 'float'

    &

    Error CS0029 Cannot implicitly convert type 'Unity.MLAgents.Actuators.ActionBuffers' to 'float'

    &

    Error CS0019 Operator '*' cannot be applied to operands of type 'ActionBuffers' and 'ActionBuffers'

    I don't know how to change ActionBuffers to a float...(or if that would help)...

    I tried downgrading my packages, but after 3 days I ended up getting everything back, so I'm not going to use that option. Here is a screenshot of the problematic code...


    CodeDoesn'tWork.PNG


    Any help would be much appreciated!
     
  10. sachinside

    sachinside

    Joined:
    Mar 30, 2023
    Posts:
    1
  11. Olek820

    Olek820

    Joined:
    Feb 16, 2023
    Posts:
    8
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class MainMenu : MonoBehaviour
    5. {
    6.     public void Play()
    7.     {
    8.         SceneManager.LoadScene(1);
    9.     }
    10.  
    11.     public void Options()
    12.     {
    13.         SceneManager.LoadScene(2);
    14.         Debug.Log("You Open Setings");
    15.     }
    16.  
    17.     public void Exit()
    18.     {
    19.         Application.Quit();
    20.     }
    21.  
    22.     public void ExitSettings()
    23.     {
    24.         SceneManager.LoadScene(0);
    25.     }
    26.      
    27.     public void Esc()
    28.     {
    29.         if(Input.GetKeyDown(Esc))
    30.         SceneManager.LoadScene(3);
     
  12. Olek820

    Olek820

    Joined:
    Feb 16, 2023
    Posts:
    8
    hello my code see like this and error see like this:
     
  13. Olek820

    Olek820

    Joined:
    Feb 16, 2023
    Posts:
    8
    Assets\_Scripts\MainMenu.cs(29,29): error CS1503: Argument 1: cannot convert from 'method group' to 'KeyCode'
     
  14. Olek820

    Olek820

    Joined:
    Feb 16, 2023
    Posts:
    8
    PLease Help
     
  15. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    147
  16. thouserr

    thouserr

    Joined:
    Mar 28, 2024
    Posts:
    1
    Go to your project --> open project manager --> press the "Add" button on the top left (+ sign) --> press "Add package from git URL" --> paste github link