더보기
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()
{
this.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 outAction = actionsOut.ContinuousActions;
outAction[0] = Input.GetAxis("Horizontal");
outAction[1] = Input.GetAxis("Vertical");
}
}
더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
public Button btn;
public RollerAgent agent;
void Start()
{
btn.onClick.AddListener(() =>
{
this.agent.Init();
});
}
}
더보기
behaviors:
RollerBall:
trainer_type: ppo
hyperparameters:
batch_size: 10
buffer_size: 100
learning_rate: 3.0e-4
beta: 5.0e-4
epsilon: 0.2
lambd: 0.99
num_epoch: 3
learning_rate_schedule: linear
network_settings:
normalize: false
hidden_units: 128
num_layers: 2
vis_encode_type: simple
reward_signals:
extrinsic:
gamma: 0.99
strength: 1.0
max_steps: 500000
time_horizon: 64
summary_freq: 10000
'유니티 > 수업 내용' 카테고리의 다른 글
게임인공지능 Ml-Agents - RollerBall 환경 바꾸기 (벽 세우기) (0) | 2022.01.18 |
---|---|
게임인공지능 - Ml-Agents - Penguin (0) | 2022.01.18 |
[UGUI] Scrollview (0) | 2022.01.18 |
[UGUI] Joystick (0) | 2022.01.18 |
[UGUI] UIStage (0) | 2022.01.18 |