前提・実現したいこと
こんにちは。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

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/09/03 18:14
退会済みユーザー
2021/09/04 05:38