벽을 하나 세우고 큐브가 생성되는 위치를 매번 현재 공의 위치와 반대사이드가 되도록 바꿔봤다.
그외엔 떨어지면 페널티로 -1점, 타겟과 일정거리 이상가까워졌을때의 보상은 5점으로 바꿨다.
더보기
using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;
public class RollerAgent : Agent
{
[SerializeField]
private Transform target;
private Rigidbody rBody;
[SerializeField]
private float force = 10;
private void Start()
{
this.rBody = this.GetComponent<Rigidbody>();
}
public override void OnEpisodeBegin()
{
this.Init();
}
public void Init()
{
if (this.transform.localPosition.y < 0) //떨어지면
{
this.rBody.angularVelocity = Vector3.zero; //회전속도를 0으로
this.rBody.velocity = Vector3.zero; //움직이는 속도를 0으로
this.transform.localPosition = new Vector3(-3, 0.5f, 0); //위치 초기화
if (this.target.localPosition.x < -0.7f)
{
this.TargetInit();
}
}
}
public void TargetInit()
{
//cube를 랜덤한 위치로 이동
//var randX = Random.value * 8 - 4; //-4 ~ 4
if (this.target.localPosition.x > 0) //타겟이 오른쪽이면
{
var randX = Random.value * -4;
if (randX > -0.8f)
{
randX = -0.8f; //왼쪽으로 보내고
}
this.target.localPosition = new Vector3(randX, 0.5f, Random.value * 8 - 4);
}
else //타겟이 왼쪽이면
{
var randX = Random.value * 4;
if (randX < 0.8f)
{
randX = 0.8f; //오른쪽으로 보낸다
}
this.target.localPosition = new Vector3(randX, 0.5f, Random.value * 8 - 4);
}
}
//네임스페이스에 using Unity.MLAgents.Sensors; 자동으로 추가됨
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(this.target.localPosition); //x,y,z 위치값 3개
sensor.AddObservation(this.transform.localPosition); //x,y,z 위치값 3개
sensor.AddObservation(this.rBody.velocity.x); //x축 이동속도 1개
sensor.AddObservation(this.rBody.velocity.z); //y축 이동속도 1개
}
//네임스페이스에 using Unity.MLAgents.Actuators; 자동으로 추가됨
public override void OnActionReceived(ActionBuffers actions)
{
Vector3 dir = Vector3.zero; //초기화
dir.x = actions.ContinuousActions[0];
dir.z = actions.ContinuousActions[1];
this.rBody.AddForce(dir * this.force);
var distacne = Vector3.Distance(this.target.localPosition, this.transform.localPosition);
if (distacne < 1.42f)
{
this.SetReward(5.0f);
this.TargetInit();
this.EndEpisode();
}
else if (this.transform.localPosition.y < 0)
{
this.SetReward(-1.0f);
this.EndEpisode();
}
}
public override void Heuristic(in ActionBuffers actionsOut)
{
var outAction = actionsOut.ContinuousActions;
outAction[0] = Input.GetAxis("Horizontal");
outAction[1] = Input.GetAxis("Vertical");
}
}
5만번 훈련결과
최단거리로 가지 않고 굳이 빙글 돌아가려는 움직임이 보이지만
훈련은 잘되는거같다
'유니티 > 수업 내용' 카테고리의 다른 글
ML Agent - DinoRun (0) | 2022.01.18 |
---|---|
게임인공지능 MLAgent RollerBall 복습 (0) | 2022.01.18 |
게임인공지능 - Ml-Agents - Penguin (0) | 2022.01.18 |
게임인공지능 Ml-Agents - rollerball_config (0) | 2022.01.18 |
[UGUI] Scrollview (0) | 2022.01.18 |