基于unity ml-agents 去训练一个小游戏 ---智能体吃球

发布时间:2026/6/2 17:37:43

基于unity ml-agents 去训练一个小游戏 ---智能体吃球 1.在unity里面创建游戏环境创建scripts-c#脚本 编写#代码针智能体以及操作对象创建Materials-{materials,physice materisls} 另外add components 添加rigidbody创建Prefabs预制体 可以直接鼠标拖着dongame到这个文件下 作用Prefabs允许你创建一个包含多个组件的对象并在多个场景中重复使用这个对象而无需每次都重新设置这些组件2.编写C#脚本代码里面不能有任何报错 否则训练不能正常运行using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Sensors; using Unity.MLAgents.Actuators; //智能体狗吃target球的实验 public class DogAgent : Agent { //目标坐标(Goal)的Transform public Transform target; //智能体狗的刚体 Rigidbody rBody; public float speed 50; public override void Initialize() { base.Initialize(); rBody GetComponentRigidbody(); // 确保刚体不会进入睡眠状态以响应物理力 rBody.WakeUp(); } //进入新的一轮调用的函数 public override void OnEpisodeBegin() { //UnityEngine.Debug.Log(DogAgent Start); //base.OnEpisodeBegin(); //只有当智能体狗掉落的时候才去重置智能体的位置目的是让智能体一直可以吃到小球 if(this.transform.position.y 0) { //重新开始设置智能体狗的初始位置 this.transform.position new Vector3(3.5f, 0.5f, 0); //智能体狗的速度与旋转 this.rBody.velocity Vector3.zero; this.rBody.angularVelocity Vector3.zero; } //随机Target的位置 target.position new Vector3(UnityEngine.Random.value * 8 - 4, 0.5f, UnityEngine.Random.value * 8 - 4); } //搜集观察的结果 public override void CollectObservations(VectorSensor sensor) { // 收集目标位置 sensor.AddObservation(target.position); // 收集智能体当前位置 sensor.AddObservation(this.transform.position); // 收集智能体在x轴和z轴方向上的速度 sensor.AddObservation(rBody.velocity.x); sensor.AddObservation(rBody.velocity.z); } // 应用力到球上的函数 //接收动作 是否给予奖励 public override void OnActionReceived(ActionBuffers actions) { //base.OnActionReceived(actions); //拿到水平和垂直的输入 float horizontal actions.ContinuousActions[0]; float vertical actions.ContinuousActions[1]; //Debug.Log(Horizontal: horizontal); //根据输入的值来移动 Vector3 movement new(horizontal, 0, vertical); // // 根据动作计算要施加的力 rBody.AddForce(movement * speed); //智能体出界了 使用y坐标来判断 if (this.transform.position.y 0) { SetReward(-1.0f); //结束这一轮的测试 EndEpisode(); } //狗子吃到了东西 _ Vector3.Distance(this.transform.position, target.position); if (Vector3.Distance(this.transform.position, target.position) 1.42f) { SetReward(1.0f); EndEpisode(); } } //手动操作智能体 public override void Heuristic(in ActionBuffers actionsOut) { //base.Heuristic(actionsOut); //拿到水平和垂直的输入 var continuousActionsOut actionsOut.ContinuousActions; continuousActionsOut[0] Input.GetAxis(Horizontal); continuousActionsOut[1] Input.GetAxis(Vertical); // 打印动作输出以便于调试 //Debug.Log(Heuristic - Horizontal: continuousActionsOut[0]); //Debug.Log(Heuristic - Vertical: continuousActionsOut[1]); } }3.在ml-agents环境下训练4.注意事项参考文件CSDN编程社区其中在Behavior Parameters中Behavior Name中的名字必须要和第二行的那个名字一致如果想设置不同的智能体使用不同的配置同时进行训练只需要在下面加上不同名字的配置然后再相应的智能体的Behavior Name中使用那个名字即可。训练时文件路径要写对mlagents-learn config/ppo/3DBall.yaml --run-id3DBallTest --force加force是强制执行 防止当前文件夹有同名的 覆盖之前的数据登录后复制如果想继续上次的训练mlagents-learn config/ppo/3DBall.yaml --run-id3DBallTest --rusume登录后复制TensorBoard的使用在上面的控制台环境下输入下列命令训练过程中可以另外开一个Anaconda Prompttensorboard --logdir .\results\ --port 6006登录后复制在浏览器中的网址栏输入localhost:6006就可以看到tensorboard的界面了。这样就能把数据进行可视化。奖励和Loss的变化一清二楚。加速训练在编辑器中的训练是需要消耗非常多性能的因此我们需要先把它打包成exe文件具体操作是File–Build Settings–Build。打包好后把配置文件yaml文件也放入文件夹中。cd到该文件夹中输入以下命令mlagents-learn 配置文件名.yaml --run-id自己随意起名 --env执行文件名.exe --num-envs9 --force参考文档的链接https://www.bilibili.com/video/BV1Yu4y1D7n7/https://yebd1h.smartapps.cn/pages/blog/index?blogId121115480_swebfr1_swebFromHostbdlite

相关新闻