Unity/ML-Agents

02. ML-Agents - position,rigidbody ๊ด€์ธก

๐Ÿ”ท ์‹ค์Šต2

- Agent๊ฐ€ Target์„ ํ–ฅํ•ด ์ด๋™

 

- position, rigidbody ๊ฐ’ ๊ด€์ธก

- ์—ฐ์† : actions.ContinuousActions

 

 

 

๐Ÿ”ถ ๊ธฐ๋ณธ ์„ธํŒ…

- ํด๋” ์ •๋ฆฌ

 

 

 

https://assetstore.unity.com/packages/3d/characters/free-mummy-monster-134212

 

Free Mummy Monster | 3D ์บ๋ฆญํ„ฐ | Unity Asset Store

Elevate your workflow with the Free Mummy Monster asset from amusedART. Find this & other ์บ๋ฆญํ„ฐ options on the Unity Asset Store.

assetstore.unity.com

 

- ํŒจํ‚ค์ง€ ๋‹ค์šด๋กœ๋“œ ํ›„ ์ž„ํฌํŠธ

 

 

 

๐Ÿ”ถ Agent ์„ธํŒ…

- Mummy_Monํ”„๋ฆฌํŒน์„ Agent๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ

 

 

 

- ๋จธํ‹ฐ๋ฆฌ์–ผ ์—ฐ๊ฒฐ > Apply

 

 

 

- ๋นˆ๊ฒŒ์ž„์˜ค๋ธŒ์ ํŠธ(Stage)

- ํ๋ธŒ(Floor)

 

 

 

- ๋นˆ๊ฒŒ์ž„์˜ค๋ธŒ์ ํŠธ(DeadZone)

- ํ๋ธŒ(Wall) ์ƒ์„ฑ

- Mesh Renderer ๋น„ํ™œ์„ฑํ™”

- Tag(DEAD_ZONE) ์ƒ์„ฑ > ์ ์šฉ

 

 

 

- ํ๋ธŒ(Target) ์ƒ์„ฑ

- Tag(TARGET) ์ƒ์„ฑ > ์ ์šฉ

 

 

 

- Agent ์ถ”๊ฐ€

 

 

 

- Capsule Collider ์ถ”๊ฐ€ > ์กฐ์ •

- Rigidbody ์ถ”๊ฐ€ > Freeze Rotation

 

 

 

- Animator Controller ์—ฐ๊ฒฐ

 

 

 

๐Ÿ”ถ Script ์ž‘์„ฑ : ML-Agent ๊ธฐ๋ณธ ํ•จ์ˆ˜

- MummyAgent.cs ์ƒ์„ฑ

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;   // ML_Agents ๋„ค์ž„์ŠคํŽ˜์ด์Šค

/*
    ์—์ด์ „ํŠธ์˜ ์—ญํ• 
    1. ์ฃผ๋ณ€ํ™˜๊ฒฝ์„ ๊ด€์ธก(Observations)
    2. ์ •์ฑ…์— ์˜ํ•ด ํ–‰๋™(Action)
    3. ๋ณด์ƒ(Reward)
*/

public class MummyAgent : Agent
{

}

 

 

 

- Agent์˜ค๋ธŒ์ ํŠธ์— MummyAgent.cs ์—ฐ๊ฒฐ

 

 

 

- Max Step : ํ•ด๋‹น ๊ฐ’๋งŒํผ ์›€์ง์˜€์œผ๋‚˜ ๋ณด์ƒ์ด ์—†์„ ๋•Œ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•จ

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;   // ML_Agents ๋„ค์ž„์ŠคํŽ˜์ด์Šค
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;

/*
    ์—์ด์ „ํŠธ์˜ ์—ญํ• 
    1. ์ฃผ๋ณ€ํ™˜๊ฒฝ์„ ๊ด€์ธก(Observations)
    2. ์ •์ฑ…์— ์˜ํ•ด ํ–‰๋™(Action)
    3. ๋ณด์ƒ(Reward)
*/

public class MummyAgent : Agent
{
    // ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ
    public override void Initialize()
    {

    }

    // ์—ํ”ผ์†Œ๋“œ(ํ•™์Šต์˜ ๋‹จ์œ„)๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnEpisodeBegin()
    {

    }

    // ์ฃผ๋ณ€ ํ™˜๊ฒฝ์„ ๊ด€์ธกํ•˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์†Œ๋“œ
    public override void CollectObservations(VectorSensor sensor)
    {

    }

    // ์ •์ฑ…์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ–‰๋™์„ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnActionReceived(ActionBuffers actions)
    {

    }

    // ๊ฐœ๋ฐœ์ž์˜ ํ…Œ์ŠคํŠธ ์šฉ๋„ / ๋ชจ๋ฐฉํ•™์Šต
    public override void Heuristic(in ActionBuffers actionsOut)
    {

    }

}

- ๊ธฐ๋ณธ ํ•จ์ˆ˜๋“ค

 

 

 

๐Ÿ”ถ Script ์ž‘์„ฑ : 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;   // ML_Agents ๋„ค์ž„์ŠคํŽ˜์ด์Šค
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;

/*
    ์—์ด์ „ํŠธ์˜ ์—ญํ• 
    1. ์ฃผ๋ณ€ํ™˜๊ฒฝ์„ ๊ด€์ธก(Observations)
    2. ์ •์ฑ…์— ์˜ํ•ด ํ–‰๋™(Action)
    3. ๋ณด์ƒ(Reward)
*/

public class MummyAgent : Agent
{
    private new Rigidbody rigidbody;
    private new Transform transform;
    private Transform targetTr;

    // ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ
    public override void Initialize()
    {
        rigidbody = GetComponent<Rigidbody>();
        transform = GetComponent<Transform>();
        targetTr = transform.parent.Find("Target");
    }

    // ์—ํ”ผ์†Œ๋“œ(ํ•™์Šต์˜ ๋‹จ์œ„)๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnEpisodeBegin()
    {
        // ๋ฌผ๋ฆฌ๋ ฅ์„ ์ดˆ๊ธฐํ™”
        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;

        // ์—์ด์ „ํŠธ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        transform.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.05f, Random.Range(-4.0f, 4.0f));

        // ํƒ€๊ฒŸ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        targetTr.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.55f, Random.Range(-4.0f, 4.0f));
    }

    // ์ฃผ๋ณ€ ํ™˜๊ฒฝ์„ ๊ด€์ธกํ•˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์†Œ๋“œ
    public override void CollectObservations(VectorSensor sensor)
    {
        // ์ด 8๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€์ธก
        sensor.AddObservation(targetTr.localPosition);  // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(transform.localPosition); // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.x);    // (x)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.z);    // (z)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
    }

    // ์ •์ฑ…์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ–‰๋™์„ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnActionReceived(ActionBuffers actions)
    {

    }

    // ๊ฐœ๋ฐœ์ž์˜ ํ…Œ์ŠคํŠธ ์šฉ๋„ / ๋ชจ๋ฐฉํ•™์Šต
    public override void Heuristic(in ActionBuffers actionsOut)
    {

    }

}

- ๊ด€์ธกํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ์•„์•ผํ•จ

 

 

 

- Behavior Name ๋ช…์‹œ

- Space Size : ๊ด€์ธกํ•˜๋Š” ๊ฐ’ ๊ฐœ์ˆ˜

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;   // ML_Agents ๋„ค์ž„์ŠคํŽ˜์ด์Šค
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;

/*
    ์—์ด์ „ํŠธ์˜ ์—ญํ• 
    1. ์ฃผ๋ณ€ํ™˜๊ฒฝ์„ ๊ด€์ธก(Observations)
    2. ์ •์ฑ…์— ์˜ํ•ด ํ–‰๋™(Action)
    3. ๋ณด์ƒ(Reward)
*/

public class MummyAgent : Agent
{
    private new Rigidbody rigidbody;
    private new Transform transform;
    private Transform targetTr;

    // ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ
    public override void Initialize()
    {
        rigidbody = GetComponent<Rigidbody>();
        transform = GetComponent<Transform>();
        targetTr = transform.parent.Find("Target");
    }

    // ์—ํ”ผ์†Œ๋“œ(ํ•™์Šต์˜ ๋‹จ์œ„)๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnEpisodeBegin()
    {
        // ๋ฌผ๋ฆฌ๋ ฅ์„ ์ดˆ๊ธฐํ™”
        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;

        // ์—์ด์ „ํŠธ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        transform.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.05f, Random.Range(-4.0f, 4.0f));

        // ํƒ€๊ฒŸ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        targetTr.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.55f, Random.Range(-4.0f, 4.0f));
    }

    // ์ฃผ๋ณ€ ํ™˜๊ฒฝ์„ ๊ด€์ธกํ•˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์†Œ๋“œ
    public override void CollectObservations(VectorSensor sensor)
    {
        // ์ด 8๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€์ธก
        sensor.AddObservation(targetTr.localPosition);  // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(transform.localPosition); // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.x);    // (x)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.z);    // (z)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
    }

    // ์ •์ฑ…์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ–‰๋™์„ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnActionReceived(ActionBuffers actions)
    {
        // ์—ฐ์† : actions.ContinuousActions
        // ์ด์‚ฐ : actions.DiscreteActions
        var action = actions.ContinuousActions;

        // [0] Up, Down
        // [1] Left, Right
        Vector3 dir = (Vector3.forward * action[0]) + (Vector3.right * action[1]);
        rigidbody.AddForce(dir.normalized * 50.0f);

        // ๊ฐ€๋งŒํžˆ ์žˆ์„ ๋•Œ ๋งˆ์ด๋„ˆ์Šค ํŽ˜๋„ํ‹ฐ ๋ถ€์—ฌ
        SetReward(-0.001f);
    }

    // ๊ฐœ๋ฐœ์ž์˜ ํ…Œ์ŠคํŠธ ์šฉ๋„ / ๋ชจ๋ฐฉํ•™์Šต
    public override void Heuristic(in ActionBuffers actionsOut)
    {

    }

}

 

 

 

 

- ์—ฐ์†์ ์ธ ๊ฐ’ 2๊ฐœ : Up,Down / Left, Right

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;   // ML_Agents ๋„ค์ž„์ŠคํŽ˜์ด์Šค
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;

/*
    ์—์ด์ „ํŠธ์˜ ์—ญํ• 
    1. ์ฃผ๋ณ€ํ™˜๊ฒฝ์„ ๊ด€์ธก(Observations)
    2. ์ •์ฑ…์— ์˜ํ•ด ํ–‰๋™(Action)
    3. ๋ณด์ƒ(Reward)
*/

public class MummyAgent : Agent
{
    private new Rigidbody rigidbody;
    private new Transform transform;
    private Transform targetTr;

    // ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ
    public override void Initialize()
    {
        rigidbody = GetComponent<Rigidbody>();
        transform = GetComponent<Transform>();
        targetTr = transform.parent.Find("Target");
    }

    // ์—ํ”ผ์†Œ๋“œ(ํ•™์Šต์˜ ๋‹จ์œ„)๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnEpisodeBegin()
    {
        // ๋ฌผ๋ฆฌ๋ ฅ์„ ์ดˆ๊ธฐํ™”
        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;

        // ์—์ด์ „ํŠธ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        transform.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.05f, Random.Range(-4.0f, 4.0f));

        // ํƒ€๊ฒŸ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        targetTr.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.55f, Random.Range(-4.0f, 4.0f));
    }

    // ์ฃผ๋ณ€ ํ™˜๊ฒฝ์„ ๊ด€์ธกํ•˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์†Œ๋“œ
    public override void CollectObservations(VectorSensor sensor)
    {
        // ์ด 8๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€์ธก
        sensor.AddObservation(targetTr.localPosition);  // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(transform.localPosition); // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.x);    // (x)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.z);    // (z)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
    }

    // ์ •์ฑ…์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ–‰๋™์„ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnActionReceived(ActionBuffers actions)
    {
        // ์—ฐ์† : actions.ContinuousActions
        // ์ด์‚ฐ : actions.DiscreteActions
        var action = actions.ContinuousActions;

        // [0] Up, Down
        // [1] Left, Right
        Vector3 dir = (Vector3.forward * action[0]) + (Vector3.right * action[1]);
        rigidbody.AddForce(dir.normalized * 50.0f);

        // ๊ฐ€๋งŒํžˆ ์žˆ์„ ๋•Œ ๋งˆ์ด๋„ˆ์Šค ํŽ˜๋„ํ‹ฐ ๋ถ€์—ฌ
        SetReward(-0.001f);
    }

    // ๊ฐœ๋ฐœ์ž์˜ ํ…Œ์ŠคํŠธ ์šฉ๋„ / ๋ชจ๋ฐฉํ•™์Šต
    public override void Heuristic(in ActionBuffers actionsOut)
    {
        var action = actionsOut.ContinuousActions;
        action[0] = Input.GetAxis("Vertical");
        action[1] = Input.GetAxis("Horizontal");
    }
}

 

 

 

- Decision Requester ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€

// Decision Requester ์ปดํฌ๋„ŒํŠธ : ๊ฒฐ์ •์„ ์š”์ฒญํ•˜๋Š” ์š”์ฒญ์ž

// Decision Period : 5๋ฒˆ์— ํ•œ ๋ฒˆ ๊ฒฐ์ •์„ ์š”์ฒญํ•จ

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;   // ML_Agents ๋„ค์ž„์ŠคํŽ˜์ด์Šค
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;

/*
    ์—์ด์ „ํŠธ์˜ ์—ญํ• 
    1. ์ฃผ๋ณ€ํ™˜๊ฒฝ์„ ๊ด€์ธก(Observations)
    2. ์ •์ฑ…์— ์˜ํ•ด ํ–‰๋™(Action)
    3. ๋ณด์ƒ(Reward)
*/

public class MummyAgent : Agent
{
    private new Rigidbody rigidbody;
    private new Transform transform;
    private Transform targetTr;

    public Material goodMAT;
    public Material badMAT;
    private Material originMAT;

    private Renderer floorRd;

    // ์ดˆ๊ธฐํ™” ๋ฉ”์†Œ๋“œ
    public override void Initialize()
    {
        rigidbody = GetComponent<Rigidbody>();
        transform = GetComponent<Transform>();
        targetTr = transform.parent.Find("Target");
        floorRd = transform.parent.Find("Floor").GetComponent<Renderer>();
        originMAT = floorRd.material;
    }

    // ์—ํ”ผ์†Œ๋“œ(ํ•™์Šต์˜ ๋‹จ์œ„)๊ฐ€ ์‹œ์ž‘๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnEpisodeBegin()
    {
        // ๋ฌผ๋ฆฌ๋ ฅ์„ ์ดˆ๊ธฐํ™”
        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;

        // ์—์ด์ „ํŠธ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        transform.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.05f, Random.Range(-4.0f, 4.0f));

        // ํƒ€๊ฒŸ์˜ ์œ„์น˜๋ฅผ ๋ถˆ๊ทœ์น™ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
        targetTr.localPosition = new Vector3(Random.Range(-4.0f, 4.0f), 0.55f, Random.Range(-4.0f, 4.0f));

        StartCoroutine(ReverMaterial());
    }

    IEnumerator ReverMaterial()
    {
        yield return new WaitForSeconds(0.2f);
        floorRd.material = originMAT;
    }

    // ์ฃผ๋ณ€ ํ™˜๊ฒฝ์„ ๊ด€์ธกํ•˜๋Š” ์ฝœ๋ฐฑ ๋ฉ”์†Œ๋“œ
    public override void CollectObservations(VectorSensor sensor)
    {
        // ์ด 8๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€์ธก
        sensor.AddObservation(targetTr.localPosition);  // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(transform.localPosition); // (x,y,z)  3๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.x);    // (x)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
        sensor.AddObservation(rigidbody.velocity.z);    // (z)      1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
    }

    // ์ •์ฑ…์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ–‰๋™์„ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    public override void OnActionReceived(ActionBuffers actions)
    {
        // ์—ฐ์† : actions.ContinuousActions
        // ์ด์‚ฐ : actions.DiscreteActions
        var action = actions.ContinuousActions;

        // [0] Up, Down
        // [1] Left, Right
        Vector3 dir = (Vector3.forward * action[0]) + (Vector3.right * action[1]);
        rigidbody.AddForce(dir.normalized * 50.0f);

        // ๊ฐ€๋งŒํžˆ ์žˆ์„ ๋•Œ ๋งˆ์ด๋„ˆ์Šค ํŽ˜๋„ํ‹ฐ ๋ถ€์—ฌ
        SetReward(-0.001f);
    }

    // ๊ฐœ๋ฐœ์ž์˜ ํ…Œ์ŠคํŠธ ์šฉ๋„ / ๋ชจ๋ฐฉํ•™์Šต
    public override void Heuristic(in ActionBuffers actionsOut)
    {
        var action = actionsOut.ContinuousActions;
        action[0] = Input.GetAxis("Vertical");
        action[1] = Input.GetAxis("Horizontal");
    }

    // ๋ณด์ƒ ์ฒ˜๋ฆฌ ๋กœ์ง
    void OnCollisionEnter(Collision coll)
    {
        if (coll.collider.CompareTag("DEAD_ZONE"))
        {
            floorRd.material = badMAT;
            SetReward(-1.0f);
            EndEpisode();   // ํ•™์Šต ์ข…๋ฃŒ
        }

        if (coll.collider.CompareTag("TARGET"))
        {
            floorRd.material = goodMAT;
            SetReward(1.0f);
            EndEpisode();
        }
    }
}

- ๋ฆฌ์›Œ๋“œ ์ถ”๊ฐ€

- ํŠธ๋ ˆ์ด๋‹์ด ์ž˜ ๋˜๊ณ  ์žˆ๋Š”์ง€ ์‹œ๊ฐํ™” : ์ถฉ๋Œ ์‹œ floor ์ƒ‰์ƒ ๋ณ€ํ™”

 

 

 

- Stage ํ”„๋ฆฌํŒนํ™”

 

 

 

- ์Šคํ…Œ์ด์ง€ ๋ณต์‚ฌ

 

 

 

๐Ÿ”ถ ํŠธ๋ ˆ์ด๋‹ ์‹œํ‚ค๊ธฐ

- 3DBall.yamlํŒŒ์ผ์„ ๋ณต์‚ฌ > ์ด๋ฆ„์„ Mummy.yaml๋กœ

 

 

 

- Mummy.yaml ํŒŒ์ผ์ด ์ƒ์„ฑ๋์Œ

 

 

 

- Mummy.yamlํŒŒ์ผ open

 

 

 

behaviors:
  Mummy:  #Unity Behavior Parameters ์ปดํฌ๋„ŒํŠธ์˜ Behavior ์ด๋ฆ„๊ณผ ๋™์ผํ•ด์•ผํ•จ
    trainer_type: ppo
    hyperparameters:
      batch_size: 64
      buffer_size: 12000
      learning_rate: 0.0003
      beta: 0.001
      epsilon: 0.2
      lambd: 0.99
      num_epoch: 3
      learning_rate_schedule: linear
    network_settings:
      normalize: true
      hidden_units: 128
      num_layers: 2
      vis_encode_type: simple
    reward_signals:
      extrinsic:
        gamma: 0.99
        strength: 1.0
    keep_checkpoints: 5
    max_steps: 300000   #300000๋ฒˆ ์‹คํ–‰ ํ›„ ํ•™์Šต ์ข…๋ฃŒ
    time_horizon: 1000
    summary_freq: 10000 #10000๋ฒˆ์— ํ•œ๋ฒˆ์”ฉ summary

- Mummy.yaml

- behavior ์ด๋ฆ„ ์ฃผ์˜ํ•˜๊ธฐ

 

 

 

- Mummy.yaml ํŒŒ์ผ์ด ์žˆ๋Š” ppoํด๋” ์•ˆ์—์„œ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰

 

 

 

- ์œ ๋‹ˆํ‹ฐ์—์„œ๋„ ์‹คํ–‰

- 30000๋ฒˆ ๋Œ๋ฉด ํ•™์Šต ์ข…๋ฃŒ๋จ

 

 

 

๐Ÿ”ถ ํ•™์Šต์‹œํ‚จ ๋ชจ๋ธ ์ ์šฉ์‹œํ‚ค๊ธฐ

- ํ•™์Šตํ•œ ๋ชจ๋ธ(.onnx)์„ ํ”„๋กœ์ ํŠธ ์ฐฝ์— ๋Œ์–ด๋‹ค ๋†“๊ธฐ

 

 

 

- Agent์˜ model ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ•™์Šตํ•œ ๋ชจ๋ธ(.onnx) ๋Œ์–ด๋‹ค๋†“๊ธฐ

- apply to prefab

 

 

 

- playํ•ด ๋ณด๋ฉด ์ž˜ ํ•™์Šต๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Œ

'Unity > ML-Agents' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

06. ML-Agents - Soccer (1)  (1) 2021.08.02
05. ML-Agents - Imitation Learning  (0) 2021.08.02
04. ML-Agents - Camera Sensor  (0) 2021.08.01
03. ML-Agents - Ray Perception Sensor 3D  (0) 2021.07.30
01. ML-Agents - ์„ค์น˜ ๋ฐ ๊ฐ„๋‹จํ•œ ์‹ค์Šต  (1) 2021.07.30