Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question mlagents problem: Unexpected exception when trying to initialize communication: System.IO.IOExceptio

Discussion in 'ML-Agents' started by dong5455993, Sep 4, 2023.

  1. dong5455993

    dong5455993

    Joined:
    Dec 24, 2022
    Posts:
    1
    I am learning mlagents right now, but no idea why when I try to run it, agent not moving when I pressed key and Heuristic() not calling and not working, and I checked the console it show:

    "C:\Users\dong5\OneDrive\Desktop\study\ml_project\test1\Library\PackageCache\com.unity.ml-agents@2.0.1\Plugins\ProtoBuffer\runtimes/win/native\grpc_csharp_ext.x64.dll"
    at Grpc.Core.Internal.UnmanagedLibrary..ctor (System.String[] libraryPathAlternatives) [0x00063] in <2f154ad39ec14cfea604815989d96352>:0
    at Grpc.Core.Internal.NativeExtension.Load () [0x000d7] in <2f154ad39ec14cfea604815989d96352>:0

    Heres my Script:

    using UnityEngine;
    using Unity.MLAgents;
    using Unity.MLAgents.Sensors;
    using Unity.MLAgents.Actuators;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    public class ml_test : Agent
    {
    //[SerializeField] private Transform enemy;
    //private Rigidbody rBody;
    public List<Transform> enemies;
    [SerializeField] private float speed = 3f;
    private float totalReward = 0f;
    private int currentEnemyIndex = 0;
    //public spawn_manager spawner;
    /*
    public void Awake()
    {
    rBody = GetComponent<Rigidbody>();
    }
    */
    public override void OnEpisodeBegin()
    {
    transform.position = new Vector3(0f, 1.73f, 0f);
    currentEnemyIndex = 0;
    totalReward = 0f;
    DestroyAllEnemies();
    }
    public override void CollectObservations(VectorSensor sensor)
    {
    Vector3 combinedObservation = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    combinedObservation += enemies[currentEnemyIndex].position;
    sensor.AddObservation(combinedObservation);
    }
    public override void OnActionReceived(ActionBuffers actions)
    {
    float moveX = actions.ContinuousActions[0];
    float moveZ = actions.ContinuousActions[1];
    Vector3 moveDirection = new Vector3(moveX, 0, moveZ);
    //transform.Translate(moveDirection * Time.deltaTime * speed);
    transform.position += new Vector3(moveX, 0, moveZ) * Time.deltaTime * speed;
    float distanceToEnemy = Vector3.Distance(transform.position, enemies[currentEnemyIndex].position);
    if (distanceToEnemy < 1.0f)
    {
    AddReward(-0.1f);
    }
    AddReward(0.01f);
    totalReward += GetCumulativeReward();
    }
    public override void Heuristic(in ActionBuffers actionsOut)
    {
    Debug.Log("called");
    ActionSegment<float> Caction = actionsOut.ContinuousActions;
    Caction[0] = Input.GetAxisRaw("Horizontal");
    Caction[1] = Input.GetAxisRaw("Vertical");
    Debug.Log("Caction[0]: " + Caction[0]);
    Debug.Log("Caction[1]: " + Caction[1]);
    }
    public void addEnemy()
    {
    currentEnemyIndex++;
    }
    private void OnCollisionEnter(Collision other)
    {
    Debug.Log("touch");
    if (other.gameObject.CompareTag("enemy"))
    {
    Debug.Log("touch enemy");
    AddReward(-1f);
    EndEpisode();
    }
    }
    public void DestroyAllEnemies()
    {
    foreach (Transform enemy in enemies)
    {
    if (enemy != null)
    {
    Destroy(enemy.gameObject);
    }
    }
    enemies.Clear();
    }
    }