質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

強化学習

強化学習とは、ある環境下のエージェントが現状を推測し行動を決定することで報酬を獲得するという見解から、その報酬を最大限に得る方策を学ぶ機械学習のことを指します。問題解決時に得る報酬が選択結果によって変化することで、より良い行動を選択しようと学習する点が特徴です。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

4519閲覧

ML-Agentsで強化学習を始めたい

退会済みユーザー

退会済みユーザー

総合スコア0

Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

強化学習

強化学習とは、ある環境下のエージェントが現状を推測し行動を決定することで報酬を獲得するという見解から、その報酬を最大限に得る方策を学ぶ機械学習のことを指します。問題解決時に得る報酬が選択結果によって変化することで、より良い行動を選択しようと学習する点が特徴です。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/09/03 17:17

編集2021/09/03 17:49

前提・実現したいこと

こんにちは。1-2か月前pythonのプログラミングを始めた超初心者です。UnityのML-Agentsのサンプル集を見て強化学習に興味を持ったので、とりあえずサンプルを使って学習をやってみよう!と思ったのですが、うまくいっておりません。
最初の方は30個くらいエラーがあったのですが、パッケージマネージャーからInput systemなるものを導入することで5個まで減りました。
コメントいただけたらすぐにでも返信いたしますので、どうぞよろしくお願いいたします。
問題のスクリプトの内容も追記しておきますね。

発生している問題・エラーメッセージ

Assets\ML-Agents\Examples\PushBlockWithInput\Scripts\PushBlockWithInputPlayerController.cs(1,33): error CS0234: The type or namespace name 'Input' does not exist in the namespace 'Unity.MLAgents.Extensions' (are you missing an assembly reference?)

Assets\ML-Agents\Examples\PushBlockWithInput\Scripts\PushBlockWithInputPlayerController.cs(21,66): error CS0246: The type or namespace name 'IInputActionAssetProvider' could not be found (are you missing a using directive or an assembly reference?)

Assets\ML-Agents\Examples\PushBlockWithInput\Scripts\PushBlockActions.cs(18,42): error CS0246: The type or namespace name 'IInputActionCollection2' could not be found (are you missing a using directive or an assembly reference?)

Assets\ML-Agents\Examples\PushBlockWithInput\Scripts\PushBlockWithInputPlayerController.cs(109,31): error CS0246: The type or namespace name 'IInputActionCollection2' could not be found (are you missing a using directive or an assembly reference?)

Assets\ML-Agents\Examples\PushBlockWithInput\Scripts\PushBlockActions.cs(237,17): error CS0540: 'PushBlockActions.IEnumerable.GetEnumerator()': containing type does not implement interface 'IEnumerable'

###スクリプトの内容
(PushBlockWithInputPlayerController)

using Unity.MLAgents.Extensions.Input;
using UnityEngine;
using UnityEngine.InputSystem;

/// <summary>
/// This class handles the input for the PushBlock Cube character in the PushBlock scene.
/// Note that the only ML-Agents code here is the implementation of the <see cref="IInputActionAssetProvider"/>.
/// The <see cref="InputActuatorComponent"/> looks for a component that implements that interface in order to
/// rebind actions to virtual controllers when training agents or running inference. This means that you can
/// keep your input handling code separate from ML-Agents, and have your agent's action space defined by the
/// actions defined in your project's <see cref="GetInputActionAsset"/>.
///
/// If you don't implement <see cref="IInputActionAssetProvider"/> the <see cref="InputActuatorComponent"/> will
/// look for a <see cref="PlayerInput"/> component on the GameObject it live on. It will rebind the actions of that
/// instance of the asset.
///
/// It is important to note that if you have multiple components on the same GameObject handling input, you will
/// need to share the instance of the generated C# <see cref="IInputActionCollection2"/> (named <see cref="m_PushBlockActions"/>
/// here) in order to ensure that all of your actions are bound correctly for ml-agents training and inference.
/// </summary>
public class PushBlockWithInputPlayerController : MonoBehaviour, IInputActionAssetProvider
{

PushBlockWithInputSettings m_PushBlockSettings; public float JumpTime = 0.5f; float m_JumpTimeRemaining; Rigidbody m_PlayerRb; //cached on initialization PushBlockActions m_PushBlockActions; float m_JumpCoolDownStart; void Awake() { m_PushBlockSettings = FindObjectOfType<PushBlockWithInputSettings>(); LazyInitializeActions(); // Cache the agent rigidbody m_PlayerRb = GetComponent<Rigidbody>(); } void LazyInitializeActions() { if (m_PushBlockActions != null) { return; } m_PushBlockActions = new PushBlockActions(); m_PushBlockActions.Enable(); // You can listen to C# events. m_PushBlockActions.Movement.jump.performed += JumpOnperformed; } void JumpOnperformed(InputAction.CallbackContext callbackContext) { InnerJump(gameObject.transform); } void FixedUpdate() { // Or you can poll the action itself like we do here. InnerMove(gameObject.transform, m_PushBlockActions.Movement.movement.ReadValue<Vector2>()); if (m_JumpTimeRemaining < 0) { m_PlayerRb.AddForce(-transform.up * (m_PushBlockSettings.agentJumpForce * 3), ForceMode.Acceleration); } m_JumpTimeRemaining -= Time.fixedDeltaTime; } void InnerJump(Transform t) { if (Time.realtimeSinceStartup - m_JumpCoolDownStart > m_PushBlockSettings.agentJumpCoolDown) { m_JumpTimeRemaining = JumpTime; m_PlayerRb.AddForce(t.up * m_PushBlockSettings.agentJumpForce, ForceMode.VelocityChange); m_JumpCoolDownStart = Time.realtimeSinceStartup; } } void InnerMove(Transform t, Vector2 v) { var forward = CreateForwardVector(v); var up = CreateUpVector(v); var dirToGo = t.forward * forward; var rotateDir = t.up * up; t.Rotate(rotateDir, Time.deltaTime * 200f); m_PlayerRb.AddForce(dirToGo * m_PushBlockSettings.agentRunSpeed, ForceMode.VelocityChange); } static float CreateUpVector(Vector2 move) { return Mathf.Abs(move.x) > Mathf.Abs(move.y) ? move.x : 0f; } static float CreateForwardVector(Vector2 move) { return Mathf.Abs(move.y) > Mathf.Abs(move.x) ? move.y : 0f; } /// <summary> /// This is the implementation of the <see cref="IInputActionAssetProvider"/> for this class. We need /// both the <see cref="GetInputActionAsset"/> and the <see cref="IInputActionCollection2"/> if you are /// listening to C# events, Unity Events, or receiving Messages from the Input System Package as those callbacks /// are set up through the generated <see cref="IInputActionCollection2"/>. /// </summary> /// <returns></returns> public (InputActionAsset, IInputActionCollection2) GetInputActionAsset() { LazyInitializeActions(); return (m_PushBlockActions.asset, m_PushBlockActions); }

}

試したこと

参考にさせていただいておりますサイトはこちらです。
https://www.suzu6.net/posts/270-isntall-unity-ml-agents/
一度上記の方法でサンプルを使った強化学習までは行うことができました。その際resultsにonnxファイルが生成されたのですが、それをAgentsに貼り付けても動作せず、いろいろ調べたところML-Agentsのバージョンが悪い?といった情報が出てきたので、pythonやunity、ml-agentsのバージョンをできる限り最新にした上で新たに挑戦したとところ、行き詰った次第です。

補足情報(FW/ツールのバージョンなど)(どこまで書いたらよいのかわからないので、必要な情報があればすぐに追記いたします。)

Windows10
Unity 2021.1.19

使用しているファイル
ml-agents-release_18

パッケージマネージャー
ML Agents バージョン 2.1.0-exp.1
ML Agents Extensions バージョン 0.5.0-preview
Input System バージョン 1.0.2 - January 21, 2021
JetBrains Rider Editor バージョン 3.0.7 - May 21, 2021
Test Framework バージョン 1.1.29 - August 17, 2021
TextMeshPro バージョン 3.0.6 - April 22, 2021
Timeline バージョン 1.5.6 - July 08, 2021
Unity UI バージョン 1.0.0 - August 24, 2021
Version Control バージョン 1.9.0 - August 05, 2021
Visual Scripting バージョン 1.6.1 - April 22, 2021
Visual Studio Code Editor バージョン 1.2.3 - October 28, 2020
Visual Studio Editor バージョン 2.0.11 - July 08, 2021

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

エラー内容から判断しますと、どうやらcom.unity.ml-agents.extensionsパッケージの機能を使用しようとしているように見受けられますね。ご提示の「Unity ML-Agentsのインストールから学習まで」の記事には言及がないようですので、もしかしてこちらのパッケージがインストールされていないのではないでしょうか?

Mac版の記事ではありますが、「M1 MacでUnity ML-Agents Release 18を動かす - 創庵」には...

Unity Hubから直接ml-agents/Pojectフォルダを開く。(従来のように、ml-agents/Project/Assets/ML-AgentsフォルダをUnityのAssetsフォルダーにドラックする方法だとエラー)

との記載もありました。出来合いのml-agents/Projectをベースにする(既存のプロジェクトにML-Agentsを追加するのではなく)のでもかまわなければ、こちらの方法を試してみてもいいかもしれません。

投稿2021/09/03 18:04

Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/09/03 18:14

迅速な回答、ありがとうございます! ご指摘の通り?私が参考にしたサイトにはExtensionをパッケージとして取得することは書いていないのですが、一度目の挑戦ではextensionをrelease18のml agents extensionからパッケージマネージャーへ追加する(ここではML Agents Extensions バージョン 0.5.0-previewという奴になっていますね)ことでサンプルを実行することができました。 マックとは少し画面が異なりますが、開く、というタブからML-Agents release18/ Project を直接開いてみればよい、ということでしょうか。2019のUnityのバージョンじゃないと開けないよと言われたので、再installして、試みております。
退会済みユーザー

退会済みユーザー

2021/09/04 05:38

ありがとうございます!サンプルの動作と、学習したonnxの動作を確認できました。 Bongo様の迅速なご回答に感謝しております。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問