Unity/ML-Agents

07. ML-Agents - Soccer (2)

๐Ÿ”ท ์‹ค์Šต 6 (2)

- ์ถ•๊ตฌ

- ์ด์‚ฐ๊ฐ’

 

 

 

๐Ÿ”ท ํŠธ๋ ˆ์ด๋‹ ํ™˜๊ฒฝ ๊ตฌ์ถ•

- ํƒœ๊ทธ ์ถ”๊ฐ€

- ๊ฐ ์˜ค๋ธŒ์ ํŠธ์— ํƒœ๊ทธ ๋ถ€์—ฌ

 

 

 

- ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€ : Ray Perception Sensor 3D

 

 

 

- Sensor Name : RayFront : ์„ผ์„œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ด๋ฆ„์ด ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋จ

- Detectable Tags : ์ธ์‹ํ•  ์˜ค๋ธŒ์ ํŠธ์˜ ํƒœ๊ทธ ์ถ”๊ฐ€

- Ray per Director : 4 : ๋ ˆ์ด์˜ ๊ฐœ์ˆ˜ : 4 * 2 + 1 = 9๊ฐœ

- Max Ray Degrees : 80 : ๋ ˆ์ด์˜ ๊ฐ„๊ฒฉ

- Ray Length : 40 : Agent๊ฐ€ ๊ฒฝ๊ธฐ์žฅ ๋์— ์žˆ์„๋•Œ๋„ ๋ฐ˜๋Œ€ํŽธ๊นŒ์ง€ ๋ ˆ์ด๊ฐ€ ๋‹ฟ๋„๋ก, ์‚ฌ๊ฐ์ง€๋Œ€๊ฐ€ ์—†๋„๋ก ์กฐ์ ˆ

- ์ปดํฌ๋„ŒํŠธ copy

 

 

 

- Agent ์•„๋ž˜์— ๋นˆ ๊ฒŒ์ž„์˜ค๋ธŒ์ ํŠธ ์ถ”๊ฐ€ : BackRay

- ์ปดํฌ๋„ŒํŠธ paste : Ray Perception Sensor 3D

    - Rotation : 0, 180, 0

    - Sensor Name : RayBack

    - Ray per Director : 2

    - Max Ray Degrees : 90

    - Ray Hit Color : Green

- Apply All

 

 

- Agent > Agent_Blue ์ด๋ฆ„๋ณ€๊ฒฝ

- ๋ณต์‚ฌ > Agent_Red ์ด๋ฆ„๋ณ€๊ฒฝ

 

 

 

- Team : Red

 

 

 

- TMP_Pro ์ž„ํฌํŠธ 

 

 

 

- ์บ”๋ฒ„์Šค ์ถ”๊ฐ€

- Render Mode : World Space

- Rect Transform : Reset

    - Pos Y : 0.2

    - Width : 30

    - Height : 15

    - Rotation : 90, 0, 0

 

 

 

- ์บ”๋ฒ„์Šค ์•„๋ž˜์— TMP_Text ์˜ค๋ธŒ์ ํŠธ ์ถ”๊ฐ€ > Text - Blue Score๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ

- ์ •๋ ฌ : Alt + ์ขŒ์ธกํ•˜๋‹จ์— ์žˆ๋Š” ๊ฒƒ

- Pos X : 5

- Width : 10

- Font Size : 3

- Alignment : ์ขŒ์šฐ ๊ฐ€์šด๋ฐ, ์ƒํ•˜ ๊ฐ€์šด๋ฐ

 

 

 

- Blue Score ๋ณต์‚ฌ > Text - Red Score๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ

- ์ •๋ ฌ : Alt + ์šฐ์ธกํ•˜๋‹จ

 

 

 

๐Ÿ”ถ ๊ณจ๋Œ€์— ๊ณต์ด ๋‹ฟ์œผ๋ฉด ๋“์  ์ฒ˜๋ฆฌ

- ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ : BallCtrl.cs

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.MLAgents;

public class BallCtrl : MonoBehaviour
{
    public Agent[] players;

    // ๊ฐ ํŒ€์˜ ์ ์ˆ˜
    private int blueScore, redScore;
    public TMP_Text blueScoreText, redScoreText;

    void Start()
    {

    }
}

 

 

 

- Soccer Ball์— BallCtrl ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€

    - players : Blue, Red ์ˆœ์„œ ๋งž์ถฐ์„œ ์—ฐ๊ฒฐ

    - TMP_Text๋„ ์—ฐ๊ฒฐ

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.MLAgents;

public class BallCtrl : MonoBehaviour
{
    public Agent[] players;
    private new Rigidbody rigidbody;

    // ๊ฐ ํŒ€์˜ ์ ์ˆ˜
    private int blueScore, redScore;
    public TMP_Text blueScoreText, redScoreText;

    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }

    void InitBall()
    {
        rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
        // ์ถ•๊ตฌ๊ณต์˜ ์œ„์น˜๋ฅผ ์ดˆ๊ธฐํ™”
        transform.localPosition = new Vector3(0.0f, 1.0f, 0.0f);
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.collider.CompareTag("BLUE_GOAL"))
        {
            // ๋ ˆ๋“œํŒ€ ์Šค์ฝ”์–ด +1์ 
            redScoreText.text = (++redScore).ToString();
            // ๋ ˆ๋“œํŒ€ ๋ฆฌ์›Œ๋“œ +1.0f
            // ๋ธ”๋ฃจํŒ€ ๋ฆฌ์›Œ๋“œ -1.0f

            InitBall();
        }

        if (other.collider.CompareTag("RED_GOAL"))
        {
            // ๋ธ”๋ฃจํŒ€ ์Šค์ฝ”์–ด +1์ 
            blueScoreText.text = (++blueScore).ToString();
            // ๋ ˆ๋“œํŒ€ ๋ฆฌ์›Œ๋“œ -1.0f
            // ๋ธ”๋ฃจํŒ€ ๋ฆฌ์›Œ๋“œ +1.0f

            InitBall();
        }
    }
}

- ๊ณจ๋Œ€์— ๊ณต์„ ๋„ฃ์œผ๋ฉด ์Šค์ฝ”์–ด ๋“์ 

 

 

 

๐Ÿ”ถ ๋ฆฌ์›Œ๋“œ ๋ถ€์—ฌ ๋ฐ ํ•™์Šต์ข…๋ฃŒ ์„ธํŒ…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.MLAgents;

public class BallCtrl : MonoBehaviour
{
    public Agent[] players;
    private new Rigidbody rigidbody;

    // ๊ฐ ํŒ€์˜ ์ ์ˆ˜
    private int blueScore, redScore;
    public TMP_Text blueScoreText, redScoreText;

    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }

    void InitBall()
    {
        rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
        // ์ถ•๊ตฌ๊ณต์˜ ์œ„์น˜๋ฅผ ์ดˆ๊ธฐํ™”
        transform.localPosition = new Vector3(0.0f, 1.0f, 0.0f);
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.collider.CompareTag("BLUE_GOAL"))
        {
            // ๋ ˆ๋“œํŒ€ ์Šค์ฝ”์–ด +1์ 
            redScoreText.text = (++redScore).ToString();
            // ๋ ˆ๋“œํŒ€ ๋ฆฌ์›Œ๋“œ +1.0f
            players[1].AddReward(+1.0f);
            // ๋ธ”๋ฃจํŒ€ ๋ฆฌ์›Œ๋“œ -1.0f
            players[0].AddReward(-1.0f);
            // ๋ณผ ์ดˆ๊ธฐํ™”
            InitBall();

            players[0].EndEpisode();    // ๋ธ”๋ฃจํŒ€ ํ•™์Šต์ข…๋ฃŒ
            players[1].EndEpisode();    // ๋ ˆ๋“œํŒ€ ํ•™์Šต์ข…๋ฃŒ
        }

        if (other.collider.CompareTag("RED_GOAL"))
        {
            // ๋ธ”๋ฃจํŒ€ ์Šค์ฝ”์–ด +1์ 
            blueScoreText.text = (++blueScore).ToString();
            // ๋ ˆ๋“œํŒ€ ๋ฆฌ์›Œ๋“œ -1.0f
            players[1].AddReward(-1.0f);
            // ๋ธ”๋ฃจํŒ€ ๋ฆฌ์›Œ๋“œ +1.0f
            players[0].AddReward(+1.0f);
            // ๋ณผ ์ดˆ๊ธฐํ™”
            InitBall();

            players[0].EndEpisode();    // ๋ธ”๋ฃจํŒ€ ํ•™์Šต์ข…๋ฃŒ
            players[1].EndEpisode();    // ๋ ˆ๋“œํŒ€ ํ•™์Šต์ข…๋ฃŒ
        }
    }
}

 

 

 

๐Ÿ”ท ํŠธ๋ ˆ์ด๋‹

- poca ํด๋” ์•ˆ์— ์žˆ๋Š” SoccerTwos.yaml ๋ณต์‚ฌ

 

 

 

- behavior ์ด๋ฆ„ ๋™์ผํ•˜๊ฒŒ ๋ณ€๊ฒฝ

 

 

 

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

 

 

 

- ๋ณต์‚ฌ

 

 

 

- ํŠธ๋ ˆ์ด๋‹ ์‹œ์ž‘

 

 

 

- ์œ ๋‹ˆํ‹ฐ 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
02. ML-Agents - position,rigidbody ๊ด€์ธก  (0) 2021.07.30