๐ท ์ค์ต 6 (1)
- ์ถ๊ตฌ
- ์ด์ฐ๊ฐ
๐ถ ํ๋ก์ ํธ ์ธํ
- ํ๋ก์ ํธ ์์ฑ : MLSoccer
- ํจํค์ง ๋ค์ด๋ก๋ ๋ฐ ์ํฌํธ : Soccer Field
- ํด๋ ์ ๋ฆฌ
- Add package from disk
- mlagents, mlagents extentions ํจํค์ง ์ค์น
- ๊ฒฝ๋ก
- ml-agents\com.unity.ml-agents\package.json
- ml-agents\com.unity.ml-agents.extensions\package.json
- ํจํค์ง 2๊ฐ๊ฐ ์ค์น๋ ๊ฒ์ ํ์ธ
- Regenerate project files
- Ctrl + Shift + P
- OmniSharp : Restart OmniSharp
๐ท ํธ๋ ์ด๋ ํ๊ฒฝ ๊ตฌ์ถ
๐ถ ์ฌ ์ธํ
- SoccerFieldTwos : ํ์ด์ด๋ผํค์ฐฝ์ ๋์ด๋ค๋๊ธฐ
- Stage๋ก ์ด๋ฆ ๋ณ๊ฒฝ
- Stage ์๋์ AgentCube_Blue ํ๋ฆฌํน ์ถ๊ฐ
- AgentCube_Blue -> Agent๋ก ์ด๋ฆ ๋ณ๊ฒฝ
- Stage : Unpack
- Agent ํ๋ฆฌํนํ
๐ถ PlayerAgent.cs : agent team, ์์ ์ค์
- PlayerAgent.cs ์์ฑ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies; // Behavior Parameter ์ปดํฌ๋ํธ๋ฅผ ์ ๊ธดํ๊ธฐ ์ํ ๋ค์์คํ์ด์ค
[RequireComponent(typeof(DecisionRequester))]
public class PlayerAgent : Agent
{
public enum TEAM
{
BLUE, RED
}
// ํ๋ ์ด์ด์ ํ
public TEAM team = TEAM.BLUE;
// Behavior Parameters
private BehaviorParameters bps;
public override void Initialize()
{
bps = GetComponent<BehaviorParameters>();
// ํ ์์ด๋ ์ค์
}
}
- PlayerAgent ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- rotation : 0, 0, 0
- behavior Name : Soccer
- Space Size : 0
- Team Id : Team Id๊ฐ ๊ฐ์ ํ๋ ์ด์ด๋ผ๋ฆฌ ๊ฒฝํ์ ๊ณต์ ํ๊ณ ๊ฐ์ Brain์ ์ฌ์ฉํจ
- 0 : Blue
- 1 : Red
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies; // Behavior Parameter ์ปดํฌ๋ํธ๋ฅผ ์ ๊ธดํ๊ธฐ ์ํ ๋ค์์คํ์ด์ค
[RequireComponent(typeof(DecisionRequester))]
public class PlayerAgent : Agent
{
public enum TEAM
{
BLUE, RED
}
// ํ๋ ์ด์ด์ ํ
public TEAM team = TEAM.BLUE;
// ํ๋ ์ด์ด์ ์์
public Material[] materials;
// Behavior Parameters
private BehaviorParameters bps;
public override void Initialize()
{
bps = GetComponent<BehaviorParameters>();
// ํ ์์ด๋ ์ค์
bps.TeamId = (int)team;
// ํ๋ ์ด์ด์ ์์๋ณ๊ฒฝ
GetComponent<Renderer>().material = materials[(int)team];
// step ์ค์
MaxStep = 10000;
}
}
- ํ์์ด๋ ์ค์ , ์์ ๋ณ๊ฒฝ
- ๋จธํฐ๋ฆฌ์ผ Blue, Red์์ผ๋ก ์ฐ๊ฒฐ
๐ถ ์ด๊ธฐ ์์น
- ์ ์ด ์ฐํ์๋ ์์น๋ก ์ด๊ธฐํ๋๋๋ก ์ค์ ํ ์์
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies; // Behavior Parameter ์ปดํฌ๋ํธ๋ฅผ ์ ๊ธดํ๊ธฐ ์ํ ๋ค์์คํ์ด์ค
[RequireComponent(typeof(DecisionRequester))]
public class PlayerAgent : Agent
{
public enum TEAM
{
BLUE, RED
}
// ํ๋ ์ด์ด์ ํ
public TEAM team = TEAM.BLUE;
// ํ๋ ์ด์ด์ ์์
public Material[] materials;
// Behavior Parameters
private BehaviorParameters bps;
// ํ๋ ์ด์ด์ ์ด๊ธฐ ์์น, ๊ฐ๋
private Vector3 initPosBlue = new Vector3(-5.5f, 0.5f, 0.0f);
private Vector3 initPosRed = new Vector3(5.5f, 0.5f, 0.0f);
private Quaternion initRotBlue = Quaternion.Euler(Vector3.up * 90.0f);
private Quaternion initRotRed = Quaternion.Euler(Vector3.up * -90.0f);
void InitPlayer()
{
transform.localPosition = (team == TEAM.BLUE) ? initPosBlue : initPosRed;
transform.localRotation = (team == TEAM.BLUE) ? initRotBlue : initRotRed;
}
public override void Initialize()
{
bps = GetComponent<BehaviorParameters>();
// ํ ์์ด๋ ์ค์
bps.TeamId = (int)team;
// ํ๋ ์ด์ด์ ์์๋ณ๊ฒฝ
GetComponent<Renderer>().material = materials[(int)team];
// step ์ค์
MaxStep = 10000;
}
public override void OnEpisodeBegin()
{
// ํ๋ ์ด์ด ์์น, ๊ฐ๋ ์ด๊ธฐํ
InitPlayer();
}
}
- ์ด๊ธฐ ์์น, ๊ฐ๋ ์ธํ
๐ถ rigidbody ๋ฌผ๋ฆฌ๋ ฅ ์ด๊ธฐํ
- ์ปดํฌ๋ํธ ์ถ๊ฐ : rigidbody
- mass : 10 : ๊ณต์ด 3์ด๋๊น ๋ ๋ฌด๊ฒ๊ฒ ์ค์
- Freeze Rotation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies; // Behavior Parameter ์ปดํฌ๋ํธ๋ฅผ ์ ๊ธดํ๊ธฐ ์ํ ๋ค์์คํ์ด์ค
[RequireComponent(typeof(DecisionRequester))]
public class PlayerAgent : Agent
{
public enum TEAM
{
BLUE, RED
}
// ํ๋ ์ด์ด์ ํ
public TEAM team = TEAM.BLUE;
// ํ๋ ์ด์ด์ ์์
public Material[] materials;
// Behavior Parameters
private BehaviorParameters bps;
private new Rigidbody rigidbody;
// ํ๋ ์ด์ด์ ์ด๊ธฐ ์์น, ๊ฐ๋
private Vector3 initPosBlue = new Vector3(-5.5f, 0.5f, 0.0f);
private Vector3 initPosRed = new Vector3(5.5f, 0.5f, 0.0f);
private Quaternion initRotBlue = Quaternion.Euler(Vector3.up * 90.0f);
private Quaternion initRotRed = Quaternion.Euler(Vector3.up * -90.0f);
void InitPlayer()
{
transform.localPosition = (team == TEAM.BLUE) ? initPosBlue : initPosRed;
transform.localRotation = (team == TEAM.BLUE) ? initRotBlue : initRotRed;
}
public override void Initialize()
{
bps = GetComponent<BehaviorParameters>();
rigidbody = GetComponent<Rigidbody>();
// ํ ์์ด๋ ์ค์
bps.TeamId = (int)team;
// ํ๋ ์ด์ด์ ์์๋ณ๊ฒฝ
GetComponent<Renderer>().material = materials[(int)team];
// step ์ค์
MaxStep = 10000;
}
public override void OnEpisodeBegin()
{
// ํ๋ ์ด์ด ์์น, ๊ฐ๋ ์ด๊ธฐํ
InitPlayer();
// ๋ฌผ๋ฆฌ๋ ฅ ์ด๊ธฐํ
rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
}
}
- ๋ฌผ๋ฆฌ๋ ฅ ์ด๊ธฐํ
๐ถ ์ด๋ ๋ฐ ํ์ ๋ก์ง : ์ด์ฐ๊ฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies; // Behavior Parameter ์ปดํฌ๋ํธ๋ฅผ ์ ๊ธดํ๊ธฐ ์ํ ๋ค์์คํ์ด์ค
[RequireComponent(typeof(DecisionRequester))]
public class PlayerAgent : Agent
{
public enum TEAM
{
BLUE, RED
}
// ํ๋ ์ด์ด์ ํ
public TEAM team = TEAM.BLUE;
// ํ๋ ์ด์ด์ ์์
public Material[] materials;
// Behavior Parameters
private BehaviorParameters bps;
private new Rigidbody rigidbody;
// ํ๋ ์ด์ด์ ์ด๊ธฐ ์์น, ๊ฐ๋
private Vector3 initPosBlue = new Vector3(-5.5f, 0.5f, 0.0f);
private Vector3 initPosRed = new Vector3(5.5f, 0.5f, 0.0f);
private Quaternion initRotBlue = Quaternion.Euler(Vector3.up * 90.0f);
private Quaternion initRotRed = Quaternion.Euler(Vector3.up * -90.0f);
void InitPlayer()
{
transform.localPosition = (team == TEAM.BLUE) ? initPosBlue : initPosRed;
transform.localRotation = (team == TEAM.BLUE) ? initRotBlue : initRotRed;
}
public override void Initialize()
{
bps = GetComponent<BehaviorParameters>();
rigidbody = GetComponent<Rigidbody>();
// ํ ์์ด๋ ์ค์
bps.TeamId = (int)team;
// ํ๋ ์ด์ด์ ์์๋ณ๊ฒฝ
GetComponent<Renderer>().material = materials[(int)team];
// step ์ค์
MaxStep = 10000;
}
public override void OnEpisodeBegin()
{
// ํ๋ ์ด์ด ์์น, ๊ฐ๋ ์ด๊ธฐํ
InitPlayer();
// ๋ฌผ๋ฆฌ๋ ฅ ์ด๊ธฐํ
rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
}
public override void OnActionReceived(ActionBuffers actions)
{
var action = actions.DiscreteActions;
Debug.Log($"[0]={action[0]}, [1]={action[1]}, [2]={action[2]}");
}
public override void Heuristic(in ActionBuffers actionsOut)
{
var actions = actionsOut.DiscreteActions;
// ํ๋ผ๋ฏธํฐ ๊ฐ์ ์ด๊ธฐํ
actions.Clear();
// ์ ์ง, ์ ์ง, ํ์ง (0, 1, 2)
if (Input.GetKey(KeyCode.W)) actions[0] = 1;
if (Input.GetKey(KeyCode.S)) actions[0] = 2;
// ์ ์ง, ์ข, ์ฐ ์ด๋ (0, 1, 2)
if (Input.GetKey(KeyCode.Q)) actions[1] = 1;
if (Input.GetKey(KeyCode.E)) actions[1] = 2;
// ์ ์ง, ์ข, ์ฐ ํ์ (0, 1, 2)
if (Input.GetKey(KeyCode.A)) actions[2] = 1;
if (Input.GetKey(KeyCode.D)) actions[2] = 2;
}
}
- ์ด๋ ๋ฐ ํ์ ์ ๋ ฅ๊ฐ ์ธํ
- Discrete Branches : 3
- Branch 0 : 3
- Branch 1 : 3
- Branch 2 : 3
- box collider ์ถ๊ฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies; // Behavior Parameter ์ปดํฌ๋ํธ๋ฅผ ์ ๊ธดํ๊ธฐ ์ํ ๋ค์์คํ์ด์ค
[RequireComponent(typeof(DecisionRequester))]
public class PlayerAgent : Agent
{
public enum TEAM
{
BLUE, RED
}
// ํ๋ ์ด์ด์ ํ
public TEAM team = TEAM.BLUE;
// ํ๋ ์ด์ด์ ์์
public Material[] materials;
// Behavior Parameters
private BehaviorParameters bps;
private new Rigidbody rigidbody;
// ํ๋ ์ด์ด์ ์ด๊ธฐ ์์น, ๊ฐ๋
private Vector3 initPosBlue = new Vector3(-5.5f, 0.5f, 0.0f);
private Vector3 initPosRed = new Vector3(5.5f, 0.5f, 0.0f);
private Quaternion initRotBlue = Quaternion.Euler(Vector3.up * 90.0f);
private Quaternion initRotRed = Quaternion.Euler(Vector3.up * -90.0f);
void InitPlayer()
{
transform.localPosition = (team == TEAM.BLUE) ? initPosBlue : initPosRed;
transform.localRotation = (team == TEAM.BLUE) ? initRotBlue : initRotRed;
}
public override void Initialize()
{
bps = GetComponent<BehaviorParameters>();
rigidbody = GetComponent<Rigidbody>();
// ํ ์์ด๋ ์ค์
bps.TeamId = (int)team;
// ํ๋ ์ด์ด์ ์์๋ณ๊ฒฝ
GetComponent<Renderer>().material = materials[(int)team];
// step ์ค์
MaxStep = 10000;
}
public override void OnEpisodeBegin()
{
// ํ๋ ์ด์ด ์์น, ๊ฐ๋ ์ด๊ธฐํ
InitPlayer();
// ๋ฌผ๋ฆฌ๋ ฅ ์ด๊ธฐํ
rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
}
public override void OnActionReceived(ActionBuffers actions)
{
var action = actions.DiscreteActions;
// Debug.Log($"[0]={action[0]}, [1]={action[1]}, [2]={action[2]}");
// ์ด๋ ๋ฒกํฐ
Vector3 dir = Vector3.zero;
// ํ์ ๋ฒกํฐ
Vector3 rot = Vector3.zero;
int forward = action[0]; // noen, W, S : 0, 1, 2
int right = action[1]; // noen, Q, E : 0, 1, 2
int rotate = action[2]; // noen, A, D : 0, 1, 2
switch (forward)
{
case 1: dir = transform.forward; break;
case 2: dir = -transform.forward; break;
}
switch (right)
{
case 1: dir = -transform.right; break;
case 2: dir = transform.right; break;
}
switch (rotate)
{
case 1: rot = -transform.up; break;
case 2: rot = transform.up; break;
}
rigidbody.AddForce(dir * 1.0f, ForceMode.VelocityChange);
transform.Rotate(rot, Time.fixedDeltaTime * 100.0f);
}
public override void Heuristic(in ActionBuffers actionsOut)
{
var actions = actionsOut.DiscreteActions;
// ํ๋ผ๋ฏธํฐ ๊ฐ์ ์ด๊ธฐํ
actions.Clear();
// ์ ์ง, ์ ์ง, ํ์ง (0, 1, 2)
if (Input.GetKey(KeyCode.W)) actions[0] = 1;
if (Input.GetKey(KeyCode.S)) actions[0] = 2;
// ์ ์ง, ์ข, ์ฐ ์ด๋ (0, 1, 2)
if (Input.GetKey(KeyCode.Q)) actions[1] = 1;
if (Input.GetKey(KeyCode.E)) actions[1] = 2;
// ์ ์ง, ์ข, ์ฐ ํ์ (0, 1, 2)
if (Input.GetKey(KeyCode.A)) actions[2] = 1;
if (Input.GetKey(KeyCode.D)) actions[2] = 2;
}
}
- ์ด๋ ๋ฐ ํ์ ์ฒ๋ฆฌ
๐ถ Kick ์ฒ๋ฆฌ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Policies; // Behavior Parameter ์ปดํฌ๋ํธ๋ฅผ ์ ๊ธดํ๊ธฐ ์ํ ๋ค์์คํ์ด์ค
[RequireComponent(typeof(DecisionRequester))]
public class PlayerAgent : Agent
{
public enum TEAM
{
BLUE, RED
}
// ํ๋ ์ด์ด์ ํ
public TEAM team = TEAM.BLUE;
// ํ๋ ์ด์ด์ ์์
public Material[] materials;
// Behavior Parameters
private BehaviorParameters bps;
private new Rigidbody rigidbody;
// ํ๋ ์ด์ด์ ์ด๊ธฐ ์์น, ๊ฐ๋
private Vector3 initPosBlue = new Vector3(-5.5f, 0.5f, 0.0f);
private Vector3 initPosRed = new Vector3(5.5f, 0.5f, 0.0f);
private Quaternion initRotBlue = Quaternion.Euler(Vector3.up * 90.0f);
private Quaternion initRotRed = Quaternion.Euler(Vector3.up * -90.0f);
void InitPlayer()
{
transform.localPosition = (team == TEAM.BLUE) ? initPosBlue : initPosRed;
transform.localRotation = (team == TEAM.BLUE) ? initRotBlue : initRotRed;
}
public override void Initialize()
{
bps = GetComponent<BehaviorParameters>();
rigidbody = GetComponent<Rigidbody>();
// ํ ์์ด๋ ์ค์
bps.TeamId = (int)team;
// ํ๋ ์ด์ด์ ์์๋ณ๊ฒฝ
GetComponent<Renderer>().material = materials[(int)team];
// step ์ค์
MaxStep = 10000;
}
public override void OnEpisodeBegin()
{
// ํ๋ ์ด์ด ์์น, ๊ฐ๋ ์ด๊ธฐํ
InitPlayer();
// ๋ฌผ๋ฆฌ๋ ฅ ์ด๊ธฐํ
rigidbody.velocity = rigidbody.angularVelocity = Vector3.zero;
}
public override void OnActionReceived(ActionBuffers actions)
{
var action = actions.DiscreteActions;
// Debug.Log($"[0]={action[0]}, [1]={action[1]}, [2]={action[2]}");
// ์ด๋ ๋ฒกํฐ
Vector3 dir = Vector3.zero;
// ํ์ ๋ฒกํฐ
Vector3 rot = Vector3.zero;
int forward = action[0]; // noen, W, S : 0, 1, 2
int right = action[1]; // noen, Q, E : 0, 1, 2
int rotate = action[2]; // noen, A, D : 0, 1, 2
switch (forward)
{
case 1: dir = transform.forward; break;
case 2: dir = -transform.forward; break;
}
switch (right)
{
case 1: dir = -transform.right; break;
case 2: dir = transform.right; break;
}
switch (rotate)
{
case 1: rot = -transform.up; break;
case 2: rot = transform.up; break;
}
rigidbody.AddForce(dir * 1.0f, ForceMode.VelocityChange);
transform.Rotate(rot, Time.fixedDeltaTime * 100.0f);
}
public override void Heuristic(in ActionBuffers actionsOut)
{
var actions = actionsOut.DiscreteActions;
// ํ๋ผ๋ฏธํฐ ๊ฐ์ ์ด๊ธฐํ
actions.Clear();
// ์ ์ง, ์ ์ง, ํ์ง (0, 1, 2)
if (Input.GetKey(KeyCode.W)) actions[0] = 1;
if (Input.GetKey(KeyCode.S)) actions[0] = 2;
// ์ ์ง, ์ข, ์ฐ ์ด๋ (0, 1, 2)
if (Input.GetKey(KeyCode.Q)) actions[1] = 1;
if (Input.GetKey(KeyCode.E)) actions[1] = 2;
// ์ ์ง, ์ข, ์ฐ ํ์ (0, 1, 2)
if (Input.GetKey(KeyCode.A)) actions[2] = 1;
if (Input.GetKey(KeyCode.D)) actions[2] = 2;
}
private void OnCollisionEnter(Collision coll)
{
if (coll.collider.CompareTag("ball"))
{
// ๋ณผ ํฐ์น : + ๋ฆฌ์๋
AddReward(0.2f);
// ํฅ ๋ก์ง
Vector3 kickDir = coll.GetContact(0).point - transform.position;
coll.gameObject.GetComponent<Rigidbody>().AddForce(kickDir * 800.0f);
}
}
}
- ํ๋ ์ด์ด์ ๊ณต์ด ๋ถ๋ชํ๋ฉด
- ๊ณต์ ์ฐฌ ๋ฐฉํฅ์ผ๋ก AddForce
'Unity > ML-Agents' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
07. ML-Agents - Soccer (2) (0) | 2021.08.03 |
---|---|
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 |