我一直在研究一个自我平衡的代理,它努力保持腰部在某个高度。最近,我升级了“大腿”部分,允许其在三个轴上旋转,而不是之前的两个轴。完成这些修改后,我还调整了ml agents代码以支持子传感器,现在这些代理似乎无法再在多个代理/区域中正常工作。我不知道为什么会这样。为了说明清楚,唯一工作的代理表现得比平时更加“爆炸”。当它独自行动时,它在尝试平衡时会更加平静。可能我在过程中搞错了其他东西?如果有人有任何想法,我都愿意听取。谢谢!
代理脚本:
using MLAgents;using System;using System.Collections;using System.Collections.Generic;using UnityEngine;using MLAgents.Sensor;using Random = UnityEngine.Random;public class BalanceAgent : Agent{ private BalancingArea area; public GameObject floor; public GameObject sensor; public GameObject waist; public GameObject wFront; //Used to check balance of waist. public GameObject wBack; //Used to check balance of waist. public GameObject hipR; public GameObject hipL; public GameObject buttR; public GameObject buttL; public GameObject thighR; public GameObject thighL; public GameObject legR; public GameObject legL; public GameObject footR; public GameObject footL; public float bodyMoveSensitivity = 3f; public static GameObject[] bodyParts = new GameObject[11]; public static HingeJoint[] hingeParts = new HingeJoint[11]; public static JointLimits[] jntLimParts = new JointLimits[11]; public static Vector3[] posStart = new Vector3[11]; public static Vector3[] eulerStart = new Vector3[11]; public void Start() { bodyParts = new GameObject[] { waist /*0*/, buttR /*1*/, buttL /*2*/, thighR /*3*/, thighL /*4*/, legR /*5*/, legL /*6*/, footR /*7*/, footL /*8*/, hipR /*9*/, hipL /*10*/}; for (int i = 0; i < bodyParts.Length; i++) { posStart[i] = bodyParts[i].transform.position; eulerStart[i] = bodyParts[i].transform.eulerAngles; if (bodyParts[i].GetComponent<HingeJoint>() != null) { hingeParts[i] = bodyParts[i].GetComponent<HingeJoint>(); hingeParts[i].limits = jntLimParts[i]; } } } public override void InitializeAgent() { base.InitializeAgent(); area = GetComponentInParent<BalancingArea>(); } public override void AgentReset() { floor.transform.eulerAngles = new Vector3(Random.Range(-15, 15), 0, Random.Range(-15, 15)); //Floor rotation for (int i = 0; i < bodyParts.Length; i++) { bodyParts[i].transform.position = posStart[i]; bodyParts[i].transform.eulerAngles = eulerStart[i]; bodyParts[i].GetComponent<Rigidbody>().velocity = Vector3.zero; bodyParts[i].GetComponent<Rigidbody>().angularVelocity = Vector3.zero; } waist.transform.eulerAngles = new Vector3(0, Random.Range(0, 360), 0); jntLimParts[1].max = 0; jntLimParts[1].min = jntLimParts[2].max - 1; hingeParts[1].limits = jntLimParts[2]; jntLimParts[2].max = 0; jntLimParts[2].min = jntLimParts[2].max - 1; hingeParts[2].limits = jntLimParts[2]; jntLimParts[3].max = 15; jntLimParts[3].min = jntLimParts[3].max - 1; hingeParts[3].limits = jntLimParts[3]; jntLimParts[4].max = 15; jntLimParts[4].min = jntLimParts[4].max - 1; hingeParts[4].limits = jntLimParts[4]; jntLimParts[5].max = -15; jntLimParts[5].min = jntLimParts[5].max - 1; hingeParts[5].limits = jntLimParts[5]; jntLimParts[6].max = -15; jntLimParts[6].min = jntLimParts[6].max - 1; hingeParts[6].limits = jntLimParts[6]; jntLimParts[7].max = 15; jntLimParts[7].min = jntLimParts[7].max - 1; hingeParts[7].limits = jntLimParts[7]; jntLimParts[8].max = 15; jntLimParts[8].min = jntLimParts[8].max - 1; hingeParts[8].limits = jntLimParts[8]; jntLimParts[9].max = 0; jntLimParts[9].min = jntLimParts[9].max - 1; hingeParts[9].limits = jntLimParts[9]; jntLimParts[10].max = 0; jntLimParts[10].min = jntLimParts[10].max - 1; hingeParts[10].limits = jntLimParts[10]; } public override void AgentAction(float[] vectorAction) { float buttRDir = 0; int buttRVec = (int)vectorAction[0]; switch (buttRVec) { case 1: buttRDir = 0; break; case 2: buttRDir = bodyMoveSensitivity; break; case 3: buttRDir = -bodyMoveSensitivity; break; } if (jntLimParts[1].max < 60 && jntLimParts[1].min > -60) { jntLimParts[1].max += buttRDir; jntLimParts[1].min = jntLimParts[1].max - 1; hingeParts[1].limits = jntLimParts[1]; } else { if (jntLimParts[1].min <= -60) { jntLimParts[1].max = -58; } else if (jntLimParts[1].max >= 60) { jntLimParts[1].max = 59; } jntLimParts[1].min = jntLimParts[1].max - 1; } float buttLDir = 0; int buttLVec = (int)vectorAction[1]; switch (buttLVec) { case 1: buttLDir = 0; break; case 2: buttLDir = bodyMoveSensitivity; break; case 3: buttLDir = -bodyMoveSensitivity; break; } if (jntLimParts[2].max < 60 && jntLimParts[2].min > -60) { jntLimParts[2].max += buttLDir; jntLimParts[2].min = jntLimParts[2].max - 1; hingeParts[2].limits = jntLimParts[2]; } else { if (jntLimParts[2].min <= -60) { jntLimParts[2].max = -58; } else if (jntLimParts[2].max >= 60) { jntLimParts[2].max = 59; } jntLimParts[2].min = jntLimParts[2].max - 1; } }}
回答:
遗憾的是,我不得不从头开始才能解决这个问题。完成后,我确保使用了更新版本的MLAgents,一切又恢复正常了。