Unity ML-Agents Release 1 のチュートリアル (2)
前回
1. はじめに
「Unity ML-Agents」で、強化学習の学習環境を作成する手順を説明します。ボール(RollerAgent)が立方体(Target)に向かって転がるように訓練する学習環境になります。
今回の学習環境の要素は次のとおりです。
・観察
・Vector Observation (サイズ8)
0 : TargetのX座標
1 : TargetのY座標
2 : TargetのZ座標
3 : RollerAgentのX座標
4 : RollerAgentのY座標
5 : RollerAgentのZ座標
6 : RollerAgentのX速度
7 : RollerAgentのZ速度
・行動
・Continuous (サイズ2)
0: RollerAgentのX方向に加える力
1: RollerAgentのZ方向に加える力
・報酬 :
・RollerAgentがTargetの位置に到着 : +1.0 (エピソード完了)
・RollerAgentが落下 : +0.0 (エピソード完了)
・決定
・10フレーム毎
2. 開発環境のインストール
「Unity ML-Agents Release 1 のチュートリアル (1)」と同様になります。
3. プロジェクトの準備
「Unity ML-Agents Release 1 のチュートリアル (1)」と同様になります。
4. 学習環境の作成
◎ カメラの位置と向きの調整
(1) Hierarchyウィンドウで「Main Camera」を選択。
(2) Inspectorウィンドウで以下を設定。
Position = (0, 4, -10)
Rotation = (20, 0, 0)
Scale = (1, 1, 1)
◎ マテリアルの作成
(1) Projectウィンドウで「Create → Material」で「Material」を作成し、名前に「Gray」を指定。
(2) 「Gray」を選択し、Inspectorウィンドウの「Main Maps → Albedo」(テクスチャ色)で灰色(168,168,168)を指定。
(3)同様に茶色(212,154,33)のマテリアル「Brown」と、青色(0,35,255)のマテリアル「Blue」を作成。
◎ Floorの追加
(1) Hierarchyウィンドウの「Create → 3D Object → Plane」で「Plane」を追加し、名前に「Floor」を指定。
(2) 「Floor」を選択し、Inspectorウィンドウで以下を設定。
Position = (0, 0, 0)
Rotation = (0, 0, 0)
Scale = (1, 1, 1)
(3) 「Floor」の「Mesh Renderer」の「Materials → Element 0」に灰色のマテリアル「Gray」を指定。
◎ Targetの追加
(1) Hierarchyウィンドウの「Create → 3D Object → Cube」で「Cube」を追加し、名前に「Target」を指定。
(2) 「Target」を選択し、Inspectorウィンドウで以下を設定。
Position = (3, 0.5, 3)
Rotation = (0, 0, 0)
Scale = (1, 1, 1)
(3) 「Target」の「Mesh Renderer」の「Materials → Element 0」に茶色のマテリアル「Brown」を指定。
◎ RollerAgentの追加
(1) Hierarchyウィンドウの「Create → 3D Object → Sphere」で「Sphere」を追加し、名前に「RollerAgent」を指定。
(2) 「RollerAgent」を選択し、Inspectorウィンドウで以下を設定。
Position = (0, 0.5, 0)
Rotation = (0, 0, 0)
Scale = (1, 1, 1)
(3) 「Mesh Renderer」の「Materials → Element 0」に青のマテリアル「Blue」を指定。
(4) 「Add Component」で「Rigidbody」を追加。
5. Behavior Parametersの追加
作成した学習環境の「RollerAgent」に、「Behavior Parameters」を追加します。「Behavior Parameters」は、エージェントの「観察」と「行動」のデータ型を設定するコンポーネントです。
(1) 「RollerAgent」に「Add Component」で「Behavior Parameters」を追加。
(2) 「RollerAgent」を選択し、Inspectorウィンドウで以下を設定。
Behavior Name = RollerBall
Vector Observation → Space Size = 8
Vector Observation → Stacked Vectors = 1
Vector Action → Space Type = Continuous
Vector Action → Space Size = 2
6. Agentクラスのスクリプトの追加
作成した学習環境のエージェント(RollerAgent)に、「Agentクラスのスクリプト」を追加します。
(1) コンポーネント「RollerAgent」を選択して、「Add Component」で新規スクリプト「RollerAgent」を追加。
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
// RollerAgent
public class RollerAgent : Agent
{
public Transform target;
Rigidbody rBody;
// 初期化時に呼ばれる
public override void Initialize()
{
rBody = GetComponent<Rigidbody>();
}
// エピソード開始時に呼ばれる
public override void OnEpisodeBegin()
{
// RollerAgentが床から落下している時
if (this.transform.localPosition.y < 0)
{
// RollerAgentの位置と速度をリセット
this.rBody.angularVelocity = Vector3.zero;
this.rBody.velocity = Vector3.zero;
this.transform.localPosition = new Vector3(0.0f, 0.5f, 0.0f);
}
// Targetの位置のリセット
target.localPosition = new Vector3(
Random.value*8-4, 0.5f, Random.value*8-4);
}
// 観察取得時に呼ばれる
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(target.localPosition); //TargetのXYZ座標
sensor.AddObservation(this.transform.localPosition); //RollerAgentのXYZ座標
sensor.AddObservation(rBody.velocity.x); // RollerAgentのX速度
sensor.AddObservation(rBody.velocity.z); // RollerAgentのZ速度
}
// 行動実行時に呼ばれる
public override void OnActionReceived(float[] vectorAction)
{
// RollerAgentに力を加える
Vector3 controlSignal = Vector3.zero;
controlSignal.x = vectorAction[0];
controlSignal.z = vectorAction[1];
rBody.AddForce(controlSignal * 10);
// RollerAgentがTargetの位置に到着した時
float distanceToTarget = Vector3.Distance(
this.transform.localPosition, target.localPosition);
if (distanceToTarget < 1.42f)
{
AddReward(1.0f);
EndEpisode();
}
// RollerAgentが床から落下した時
if (this.transform.localPosition.y < 0)
{
EndEpisode();
}
}
// ヒューリスティックモードの行動決定時に呼ばれる
public override void Heuristic(float[] actionsOut)
{
actionsOut[0] = Input.GetAxis("Horizontal");
actionsOut[1] = Input.GetAxis("Vertical");
}
}
(2) Hierarchyウィンドウで「RollerAgent」を選択し、Inspectorウィンドウで以下を設定。
Targetには、HierarchyウィンドウのTargetを「RollerAgent」のTargetにドラッグ&ドロップします。
Max Step = 1000
Target = Target
7. Decision Requesterの追加
作成した学習環境の「RollerAgent」に、「Decision Requester」を追加します。「Decision Requester」は、何ステップ毎に「決定」を要求するかを設定するコンポーネントです。
(1) 「RollerAgent」に「Add Component」で「Decision Requester」を追加。
(2) 「RollerAgent」を選択し、Inspectorウィンドウで以下を設定。
Decision Period = 10
8. 学習環境ので動作確認
Unity Editorでシーンを実行することで、学習前の学習環境の動作確認ができます。方向キーでボールを動かし、立方体に衝突すると立方体が移動し、ボールが落下すると床の上に戻ることを確認してください。
Unity Editorでシーンを実行した時に、適用されるモードの条件は次のとおりです。
(1) 学習を行うPythonスクリプトが実行中 → 学習
(2) (1)以外で、Behavior ParametersのModelが存在 → 推論
(3) (1)(2)以外で、Heuristic()を定義 → ヒューリスティック
9. Pythonスクリプトによる学習
「Pythonスクリプト」を使って「サンプルの学習環境」を学習を行います。学習が完了すると「推論モデル」が生成されます。
(1) 訓練設定ファイルの設定。
訓練設定ファイル(ml-agents/config/trainer_config.yaml)に以下のセクションを追加。
RollerBall:
summary_freq: 1000
batch_size: 10
buffer_size: 100
normalize: true
(2) mlagents-learnの実行。
$ mlagents-learn config/trainer_config.yaml --run-id=RollerBall-ppo-1
(3) Unity EditorのPlayボタン(▶)を押して学習開始。
20,000ステップ未満で学習できます。
【おまけ】 0.15.1 からの変更点
◎ 名前空間
using MLAgents;
using MLAgents.Sensors;
↓
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
◎ ヒューリスティックの変更
// ヒューリスティックモードで行動決定時に呼ばれる
public override float[] Heuristic()
{
var action = new float[2];
action[0] = Input.GetAxis("Horizontal");
action[1] = Input.GetAxis("Vertical");
return action;
}
↓
// ヒューリスティックモードの行動決定時に呼ばれる
public override void Heuristic(float[] actionsOut)
{
action[0] = Input.GetAxis("Horizontal");
action[1] = Input.GetAxis("Vertical");
}
◎ mlagents-learnのパラメータ
$ mlagents-learn config/trainer_config.yaml --run-id=RollerBall-ppo-1 --train
↓
$ mlagents-learn config/trainer_config.yaml --run-id=RollerBall-ppo-1
次回
この記事が気に入ったらサポートをしてみませんか?