2020.3.21 버전으로 프로젝트 생성 하여 project안에 폴더들을 패키지로 넣는다
3D -> Plane 오브젝트 생성
floor로 이름 변경
◆타겟이 되는 큐브를 만들자
3D -> cube를 생성
position : 3, 0.5, 3
Target으로 이름 변경
3D -> Sphere 오브젝트 생성
Position : 0, 0.5, 0
rigidbody를 붙인다
RollerAgent로 이름 변경
오브젝트를 만들고
이름 : TraininArea
Position : 0, 0, 0
TrainingArea 안에 오브젝트 들을 자식으로 넣어준다
스크립트 폴더 만들고 RollerAgent 스크립트 생성
재료 폴더를 만들고 폴더에서 Material을 2개 생성
각각 오브젝트에 끌어다 넣어주고, 이름을 변경, 색상도 변경해준다
RollerAgent 스크립트에서 클래스를 Agent로 변경
◆코드작성
using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using UnityEngine;
public class RollerAgent : Agent
{
[SerializeField]
private Transform target;
private Rigidbody rBody;
private void Start()
{
this.rBody = this.GetComponent<Rigidbody>();
}
public override void OnEpisodeBegin()
{
if(this.transform.localPosition.y < 0)
{
this.rBody.angularVelocity = Vector3.zero;
this.rBody.velocity = Vector3.zero;
this.transform.localPosition = new Vector3(0, 0.5f, 0);
}
this.target.localPosition = new Vector3(Random.value * 8 - 4, 0.5f, Random.value * 8 - 4); ;
}
}
RollerAgent 오브젝트에 스크립트 붙여주고
Target오브젝트 넣어준다
UI -> Button 생성
Main 스크립트 생성
◆코드작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
public Button btn;
public RollerAgent agent;
// Start is called before the first frame update
void Start()
{
btn.onClick.AddListener(() =>
{
this.agent.Init();
});
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using UnityEngine;
public class RollerAgent : Agent
{
[SerializeField]
private Transform target;
private Rigidbody rBody;
private void Start()
{
this.rBody = this.GetComponent<Rigidbody>();
}
public override void OnEpisodeBegin()
{
}
public void Init()
{
if (this.transform.localPosition.y < 0)
{
this.rBody.angularVelocity = Vector3.zero;
this.rBody.velocity = Vector3.zero;
this.transform.localPosition = new Vector3(0, 0.5f, 0);
}
this.target.localPosition = new Vector3(Random.value * 8 - 4, 0.5f, Random.value * 8 - 4); ;
}
}
btn 과 Agent에 각각 오브젝트 삽입
출력 추가
public override void OnEpisodeBegin()
{
Debug.Log("OnEpisodeBegin");
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(this.transform.localPosition); //3
sensor.AddObservation(this.target.localPosition); //3
sensor.AddObservation(this.rBody.velocity.x); //1
sensor.AddObservation(this.rBody.velocity.y); //1
}
코드 수정
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;
[SerializeField]
private float force = 10;
private Rigidbody rBody;
private void Start()
{
this.rBody = this.GetComponent<Rigidbody>();
}
public override void OnEpisodeBegin()
{
Debug.Log("OnEpisodeBegin");
}
public void Init()
{
if (this.transform.localPosition.y < 0)
{
this.rBody.angularVelocity = Vector3.zero;
this.rBody.velocity = Vector3.zero;
this.transform.localPosition = new Vector3(0, 0.5f, 0);
}
this.target.localPosition = new Vector3(Random.value * 8 - 4, 0.5f, Random.value * 8 - 4); ;
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(this.transform.localPosition); //3
sensor.AddObservation(this.target.localPosition); //3
sensor.AddObservation(this.rBody.velocity.x); //1
sensor.AddObservation(this.rBody.velocity.y); //1
}
public override void OnActionReceived(ActionBuffers actions)
{
Vector3 dir = Vector3.zero;
dir.x = actions.ContinuousActions[0];
this.rBody.AddForce(dir * this.force);
var distance = Vector3.Distance(this.target.localPosition, this.transform.localPosition);
if (distance < 1.42f)
{
this.SetReward(1.0f);
this.EndEpisode();
}
else if (this.transform.localPosition.y < 0)
{
this.EndEpisode();
}
}
}
RollerAgent 오브젝트에
Force -> 10
Decision Requester 컴포넌트 해준다
이름을 RollerBall로 변경
Space Size : 8
Countinious Action : 2
코드추가
public override void Heuristic(in ActionBuffers actionsOut)
{
var continuousActionsOut = actionsOut.ContinuousActions;
continuousActionsOut[0] = Input.GetAxis("Horizontal");
continuousActionsOut[1] = Input.GetAxis("vertical");
}
Type 변경
★오류
ArgumentException: Input Axis Vertican is not setup.
To change the input settings use: Edit -> Settings -> Input
RollerAgent.Heuristic
해결 = 코드에서 vatical 스펠링이 달라서 그랬다;;
코드수정
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;
[SerializeField]
private float force = 10;
private Rigidbody rBody;
private void Start()
{
this.rBody = this.GetComponent<Rigidbody>();
}
public override void OnEpisodeBegin()
{
Debug.Log("OnEpisodeBegin");
Init();
}
public void Init()
{
if (this.transform.localPosition.y < 0)
{
this.rBody.angularVelocity = Vector3.zero;
this.rBody.velocity = Vector3.zero;
this.transform.localPosition = new Vector3(0, 0.5f, 0);
}
this.target.localPosition = new Vector3(Random.value * 8 - 4, 0.5f, Random.value * 8 - 4); ;
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(this.transform.localPosition); //3
sensor.AddObservation(this.target.localPosition); //3
sensor.AddObservation(this.rBody.velocity.x); //1
sensor.AddObservation(this.rBody.velocity.z); //1
}
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 distance = Vector3.Distance(this.target.localPosition, this.transform.localPosition);
if (distance < 1.42f)
{
this.SetReward(1.0f);
this.EndEpisode();
}
else if (this.transform.localPosition.y < 0)
{
this.EndEpisode();
}
}
public override void Heuristic(in ActionBuffers actionsOut)
{
var continuousActionsOut = actionsOut.ContinuousActions;
continuousActionsOut[0] = Input.GetAxis("Horizontal");
continuousActionsOut[0] = Input.GetAxis("Vertical");
}
}
yenal 파일 찾기 -> 복사 -> 새로 만들기 -> 이름 롤러볼
비쥬얼 스튜디오에 끌어다 넣기
자료 수정
'Unity > 수업 내용' 카테고리의 다른 글
[게임인공지능] - ML-Agents (해석) (0) | 2021.11.15 |
---|---|
[게임인공지능] - ML-Agents(펭귄) (0) | 2021.11.12 |
[게임인공지능] - ML-Agents(밸런스 볼) (0) | 2021.11.09 |
[게임 인공지능] 프로그래밍 설치 (0) | 2021.11.08 |
[UGUI] StudyUI - Skill (0) | 2021.11.05 |