Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ML-Agents-Examples: add two extra legs to crawler -> IndexOutOfRangeException

Discussion in 'ML-Agents' started by picardlindeloef, Apr 13, 2020.

  1. picardlindeloef

    picardlindeloef

    Joined:
    Apr 13, 2020
    Posts:
    2
    Hey guys,

    I want to add two extra legs to the crawler and then train it. I added the legs in the Unity-Editor.

    Bildschirmfoto 2020-04-13 um 15.37.19.png

    Bildschirmfoto 2020-04-13 um 15.46.02.png

    Then I added the following lines of code:

    Code (CSharp):
    1. [Header("Body Parts")] [Space(10)] public Transform body;
    2.     public Transform leg0Upper;
    3.     public Transform leg0Lower;
    4.     public Transform leg1Upper;
    5.     public Transform leg1Lower;
    6.     public Transform leg2Upper;
    7.     public Transform leg2Lower;
    8.     public Transform leg3Upper;
    9.     public Transform leg3Lower;
    10.     public Transform leg4mUpper;
    11.     public Transform leg4mLower;
    12.     public Transform leg5mUpper;
    13.     public Transform leg5mLower;
    14.  
    Code (CSharp):
    1.     public override void Initialize()
    2.     {
    3.         m_JdController = GetComponent<JointDriveController>();
    4.         m_DirToTarget = target.position - body.position;
    5.  
    6.  
    7.         //Setup each body part
    8.         m_JdController.SetupBodyPart(body);
    9.         m_JdController.SetupBodyPart(leg0Upper);
    10.         m_JdController.SetupBodyPart(leg0Lower);
    11.         m_JdController.SetupBodyPart(leg1Upper);
    12.         m_JdController.SetupBodyPart(leg1Lower);
    13.         m_JdController.SetupBodyPart(leg2Upper);
    14.         m_JdController.SetupBodyPart(leg2Lower);
    15.         m_JdController.SetupBodyPart(leg3Upper);
    16.         m_JdController.SetupBodyPart(leg3Lower);
    17.         m_JdController.SetupBodyPart(leg4mUpper);
    18.         m_JdController.SetupBodyPart(leg4mLower);
    19.         m_JdController.SetupBodyPart(leg5mUpper);
    20.         m_JdController.SetupBodyPart(leg5mLower);
    21.     }
    The code executed fine and the crawler was also still able to still walk a bit (with the pre-learned net for four legs). But when I added that:

    Code (CSharp):
    1.     public override void OnActionReceived(float[] vectorAction)
    2.     {
    3.         // The dictionary with all the body parts in it are in the jdController
    4.         var bpDict = m_JdController.bodyPartsDict;
    5.  
    6.         var i = -1;
    7.         // Pick a new target joint rotation
    8.         bpDict[leg0Upper].SetJointTargetRotation(vectorAction[++i], vectorAction[++i], 0);
    9.         bpDict[leg1Upper].SetJointTargetRotation(vectorAction[++i], vectorAction[++i], 0);
    10.         bpDict[leg2Upper].SetJointTargetRotation(vectorAction[++i], vectorAction[++i], 0);
    11.         bpDict[leg3Upper].SetJointTargetRotation(vectorAction[++i], vectorAction[++i], 0);
    12.         bpDict[leg4mUpper].SetJointTargetRotation(vectorAction[++i], vectorAction[++i], 0);
    13.         bpDict[leg5mUpper].SetJointTargetRotation(vectorAction[++i], vectorAction[++i], 0);
    14.         bpDict[leg0Lower].SetJointTargetRotation(vectorAction[++i], 0, 0);
    15.         bpDict[leg1Lower].SetJointTargetRotation(vectorAction[++i], 0, 0);
    16.         bpDict[leg2Lower].SetJointTargetRotation(vectorAction[++i], 0, 0);
    17.         bpDict[leg3Lower].SetJointTargetRotation(vectorAction[++i], 0, 0);
    18.         bpDict[leg4mLower].SetJointTargetRotation(vectorAction[++i], 0, 0);
    19.         bpDict[leg5mLower].SetJointTargetRotation(vectorAction[++i], 0, 0);
    20.  
    21.         // Update joint strength
    22.         bpDict[leg0Upper].SetJointStrength(vectorAction[++i]);
    23.         bpDict[leg1Upper].SetJointStrength(vectorAction[++i]);
    24.         bpDict[leg2Upper].SetJointStrength(vectorAction[++i]);
    25.         bpDict[leg3Upper].SetJointStrength(vectorAction[++i]);
    26.         bpDict[leg4mUpper].SetJointStrength(vectorAction[++i]);
    27.         bpDict[leg5mUpper].SetJointStrength(vectorAction[++i]);
    28.         bpDict[leg0Lower].SetJointStrength(vectorAction[++i]);
    29.         bpDict[leg1Lower].SetJointStrength(vectorAction[++i]);
    30.         bpDict[leg2Lower].SetJointStrength(vectorAction[++i]);
    31.         bpDict[leg3Lower].SetJointStrength(vectorAction[++i]);
    32.         bpDict[leg4mLower].SetJointStrength(vectorAction[++i]);
    33.         bpDict[leg5mLower].SetJointStrength(vectorAction[++i]);
    34.     }
    I get the following error:

    IndexOutOfRangeException: Index was outside the bounds of the array.
    CrawlerAgent.OnActionReceived (System.Single[] vectorAction) (at Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs:181)
    MLAgents.Agent.AgentStep () (at /Users/Johannes/ml-agents/com.unity.ml-agents/Runtime/Agent.cs:793)
    MLAgents.Academy.EnvironmentStep () (at /Users/Johannes/ml-agents/com.unity.ml-agents/Runtime/Academy.cs:472)
    MLAgents.AcademyFixedUpdateStepper.FixedUpdate () (at /Users/Johannes/ml-agents/com.unity.ml-agents/Runtime/Academy.cs:34)

    (line 181 in CrawlerAgent refers to the line where the 17th element is added)

    I think that the bpDict is too small, but I do not understand, where I can change that.
    If anybody knows, please let me know! :)
    (Also if you there is some other misconception)

    Greetings,
    Johannes
     
    Last edited: Apr 13, 2020
  2. andrewcoh_unity

    andrewcoh_unity

    Unity Technologies

    Joined:
    Sep 5, 2019
    Posts:
    162
    Did you increase the size of the action space in the behavior parameters script accordingly?
     
    picardlindeloef likes this.
  3. picardlindeloef

    picardlindeloef

    Joined:
    Apr 13, 2020
    Posts:
    2
    I just did, the crawler is learning now. Thank you!