文章日志
日志类别:Unity3d
2023-03-23 13:30

首先,环境的搭建


安装anaconda

anaconda新建单独环境,建议python3.6

打开anaconda prompt切换到单独环境activate <环境名>

执行pop install mlagents -i <镜像位置> 安装mlagents

指定版本的 pip install mlagents==0.18.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/


conda list 查看是否有安装好


unity项目packeage manager 安装 Barracuda

下载 unity ml-agents

https://github.com/Unity-Technologies/ml-agents

复制3个文件夹(Editor,Plugins,Runtime)到本地项目中



ml-agents:0.18.0

ml-agents-envs:0.18.0

Communicator API:1.0.0

TensorFlow:2.2.0




简单的开始

建立一个类,继承Agent



using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using UnityEngine;
using UnityEngine.UI;

public class RollerAgent : Agent
{
    public Transform target;

    Rigidbody m_rigidbody;

    float speed = 10;


    // Start is called before the first frame update
    void Start()
    {
        m_rigidbody = GetComponent<Rigidbody>();
        //MaxStep = 600;       //60为1秒,多少秒没结果重新一轮
    }

    //进入新一轮时调用的函数
    public override void OnEpisodeBegin()
    {
        //print("OnEpisodeBegin");
        if (transform.position.y < 0)
        {
            //坐标调整
            transform.position = new Vector3(0, 0.5f, 0);
            //重置移动力
            m_rigidbody.velocity = Vector3.zero;
            m_rigidbody.angularVelocity = Vector3.zero;
        }

        //重置目标位置
        target.position = new Vector3(UnityEngine.Random.value * 8 - 4, 0.5f, UnityEngine.Random.value * 8 - 4);
    }

    //收集观察的结果
    public override void CollectObservations(VectorSensor sensor)
    {
        //2个位置
        sensor.AddObservation(target.position);
        sensor.AddObservation(transform.position);
        //2个速度
        sensor.AddObservation(m_rigidbody.velocity.x);
        sensor.AddObservation(m_rigidbody.velocity.z);
    }

    //接收动作,是否给奖励
    public override void OnActionReceived(float[] vectorAction)
    {
        //print(vectorAction);
        Vector3 force = Vector3.zero;
        force.x = vectorAction[0];
        force.z = vectorAction[1];

        m_rigidbody.AddForce(force * speed);

        if (transform.position.y < 0)
        {
            EndEpisode();       //出界,此轮结束
        }

        if (Vector3.Distance(target.position, transform.position) < 1.5f)
        {
            SetReward(1f);
            EndEpisode();
        }
    }

    //手动操作智能体
    public override void Heuristic(float[] actionsOut)
    {
        //监控了2个float值,所以VectorAction SpaceSize是2
        actionsOut[0] = Input.GetAxis("Horizontal");
        actionsOut[1] = Input.GetAxis("Vertical");
    }
}



添加组件Behavior Parameters

Behavior Parameters属性说明

Behavior Name:指定该行为参数组件应与一个代理代表的行为关联的名称。因此,该组件应该分配给代理的同名Brain。

    可以取名例子:RollBall

Vector Observation Space Size:指定观测值空间的大小(如向量长度、离散状态数等)。

    有多少个值被检测就填多少

Vector Observation Space Vectors:指定观测值维度数量。

    填1好了

Vector Action Space Type:指定智能体输出动作的空间类型(如连续值、离散值等)。

    数值的变化是一定范围的就填连续的好了,Continuous

Vector Action Space Size:指定动作空间的大小。

    OnActionReceived 会收到 Heuristic 几个,2个就填2

Behavior Type:指定智能体使用何种类型的决策算法进行训练,包括Heuristic Only、Default、Inference Only等等。

    人控制训练的话选择Heuristic,一般由AI控制选择Default

TeamId:未知。

    默认好了

Use Child Sensors:未知。

    默认好了

Observable Attribute:未知。

    默认好了

这些属性可用于自定义智能体行为并优化其训练效果。



添加组件Decision Requester

属性说明:

    Decision Period

    Take Actions Between



写好相关代码,相关控件配置好

打开Anaconda Prompt,目录到项目目录

输入指令 mlagents-learn config.yaml ,等待unity开始运行

8.unity点击运行,即开始训练

.....



0    0
昵称: