LoginSignup
umworldorder
@umworldorder (とん らい)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Unity MLAgents-Learnが動作しません。助けてください!

解決したいこと

UnityでML-Agentsを使ってリバーシ(オセロ)的なゲームの人工知能を作っているのですが作動しません。
Anacondaでmlagents-learnを起動させたのですが全くオブジェクトが動きません
おそらく ControllAgent に問題があると思うのですが原因がわかりません
どなたか分かる方いらっしゃるでしょうか
コンパイルエラーはないです

該当するソースコード

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;

public class ReversiAgent : Agent
{
    public int agentId;
    GameObject[] tagObjects;
    public GameObject Field;
    public Agent[] agents;
    public GameDirector gd;

    public override void Initialize()
    {
        GameObject CursoleB = GameObject.Find("CursoleCubeBlack");
        CursoleB.GetComponent<Renderer>().material.color = new Color(0, 1, 0.75f, 0.6f);
        CursoleB.GetComponent<Renderer>().material.SetFloat("_Metallic", 0f);
        CursoleB.GetComponent<Renderer>().material.SetFloat("_Glossiness", 1.0f);
        CursoleB.GetComponent<Renderer>().material.SetFloat("_SpecularHighlights", 0);
        CursoleB.GetComponent<Renderer>().material.SetFloat("_GlossyReflections", 0);
        
        GameObject CursoleW = GameObject.Find("CursoleCubeWhite");
        CursoleW.GetComponent<Renderer>().material.color = new Color(0, 1, 0.75f, 0.6f);
        CursoleW.GetComponent<Renderer>().material.SetFloat("_Metallic", 0f);
        CursoleW.GetComponent<Renderer>().material.SetFloat("_Glossiness", 1.0f);
        CursoleW.GetComponent<Renderer>().material.SetFloat("_SpecularHighlights", 0);
        CursoleW.GetComponent<Renderer>().material.SetFloat("_GlossyReflections", 0);
    }

    public override void CollectObservations(VectorSensor sensor)
    {
        sensor.AddObservation(GameObject.FindGameObjectsWithTag("White").Length);
        sensor.AddObservation(GameObject.FindGameObjectsWithTag("Black").Length);
    }

    public void ControllAgent(ActionSegment<int> act)
    {
        GameObject CursoleWhite = GameObject.Find("CursoleCubeWhite");
        GameObject CWinField = GameObject.Find("CursoleCubeWhiteInField");
        GameObject CursoleBlack = GameObject.Find("CursoleCubeBlack");
        GameObject CBinField = GameObject.Find("CursoleCubeBlackInField");
        var ForwardBackAction = act[0];
        var SideAction = act[1];
        var UpDownAction = act[2];
        var PutAction = act[3];

            if (ForwardBackAction == 1){
             CursoleBlack.transform.Translate(0, 0, -2);
             CBinField.transform.Translate(0, 0, -2);
             Debug.Log("done");
            }
            else if (ForwardBackAction == 2){
             CursoleBlack.transform.Translate(0, 0, 2);
             CBinField.transform.Translate(0, 0, 2);
            }
            if (SideAction == 1){
             CursoleBlack.transform.Translate(2, 0, 0);
             CBinField.transform.Translate(2, 0, 0);
            }
            else if (SideAction == 2){
             CursoleBlack.transform.Translate(-2, 0, 0);
             CBinField.transform.Translate(-2, 0, 0);
            }
            if (UpDownAction == 1){
             CursoleBlack.transform.Translate(0, 2, 0);
             CBinField.transform.Translate(0, 2, 0);
            }
            else if (UpDownAction == 2){
             CursoleBlack.transform.Translate(0, -2, 0);
             CBinField.transform.Translate(0, -2, 0);
            }
            if (PutAction == 1){
              if (gd.nowTurn == 1)
              {
                gd.blackCube();
              }
              if (gd.nowTurn == 2)
              {
                gd.reverseCheck();
              }
            }
            
    }

    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        ControllAgent(actionBuffers.DiscreteActions);
    }

    public void EndEpisode(int agentId)
    {
        if (GameObject.FindGameObjectsWithTag("White").Length 
          > GameObject.FindGameObjectsWithTag("Black").Length)
        {
            agents[0].AddReward(-1.0f); 
            agents[1].AddReward(1.0f);
        }
        if (GameObject.FindGameObjectsWithTag("Black").Length 
          > GameObject.FindGameObjectsWithTag("White").Length)
        {
            agents[0].AddReward(1.0f); 
            agents[1].AddReward(-1.0f); 
        }
        agents[0].EndEpisode();
        agents[1].EndEpisode();
    }
}

下記の通りにBehavior Parametersを設定しました。

スクリーンショット 2022-09-30 1.12.20.png

自分で試したこと

ここに問題・エラーに対して試したことを記載してください。

0

No Answers yet.

Your answer might help someone💌