Unity 机器学习代理工具包 (ML-Agents) 是一个开源项目,它使游戏和模拟能够作为训练智能代理的环境。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

73 行
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallAgent : AreaAgent
{
public GameObject goalHolder;
public GameObject block;
public GameObject wall;
public override void InitializeAgent()
{
base.InitializeAgent();
}
public override List<float> CollectState()
{
List<float> state = new List<float>();
Vector3 velocity = GetComponent<Rigidbody>().velocity;
state.Add((transform.position.x - area.transform.position.x));
state.Add((transform.position.y - area.transform.position.y));
state.Add((transform.position.z + 5 - area.transform.position.z));
state.Add((goalHolder.transform.position.x - area.transform.position.x));
state.Add((goalHolder.transform.position.y - area.transform.position.y));
state.Add((goalHolder.transform.position.z + 5 - area.transform.position.z));
state.Add((block.transform.position.x - area.transform.position.x));
state.Add((block.transform.position.y - area.transform.position.y));
state.Add((block.transform.position.z + 5 - area.transform.position.z));
state.Add(wall.transform.localScale.y);
state.Add(velocity.x);
state.Add(velocity.y);
state.Add(velocity.z);
Vector3 blockVelocity = block.GetComponent<Rigidbody>().velocity;
state.Add(blockVelocity.x);
state.Add(blockVelocity.y);
state.Add(blockVelocity.z);
return state;
}
public override void AgentStep(float[] act)
{
reward = -0.01f;
int movement = Mathf.FloorToInt(act[0]);
MoveAgent(movement);
if (gameObject.transform.position.y < 0.0f ||
Mathf.Abs(gameObject.transform.position.x - area.transform.position.x) > 8f ||
Mathf.Abs(gameObject.transform.position.z + 5 - area.transform.position.z) > 8)
{
done = true;
reward = -1f;
}
}
public override void AgentReset()
{
transform.position = new Vector3(Random.Range(-3.5f, 3.5f), 1.1f, -8f) + area.transform.position;
GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
area.GetComponent<Area>().ResetArea();
}
public override void AgentOnDone()
{
}
}