ML agents python DOES NOT WANT TO WORK AT ALL

Before I start, I just want to mention how DIFFICULT it was to set up the ML-agents python package. I had to install different an older version of python, use a specific version of different things etc. I don’t think I have ever had such a headache just to install a python package, jesus!

Anyways, When I don’t run the CMD as admin it does a PermissionError: [WinError 5] Access is denied, so I run it as a admin. But when i finally got to training my agents, i got the error: ValueError: semaphore or lock released too many times.

I can’t fix this. I’m using python v 3.7.2, MLAgents package is 0.29.0, mlagentenvs 0.29.0.

Not sure why it should make a huge difference but here is my c# agent code (runs fine in heuristic):

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

public enum Team
{
    Red,
    Blue
}

public class Bean : Agent
{
    [SerializeField] private float moveSpeed = 5f;
    private CharacterController characterController;
    private Vector3 moveDirection;

    [SerializeField] private Team team;
    [SerializeField] private Transform TeamMate;
    [SerializeField] private Transform OpposingBean1;
    [SerializeField] private Transform OpposingBean2;
    [SerializeField] private Transform ball;
    [SerializeField] private Transform goal1;
    [SerializeField] private Transform goal2;

    [SerializeField] private Vector3 AgentStartPos;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
        Debug.Log("Start position: " + ball.transform.position);
    }

    public override void OnEpisodeBegin()
    {
        Debug.Log("Episode started.");
        transform.position = AgentStartPos;
        ball.position = new Vector3(0, 1, 0);
    }

    // Collect observations from the environment
    public override void CollectObservations(VectorSensor sensor)
    {
        Debug.Log("Collecting observations.");
        sensor.AddObservation(transform.position);
        sensor.AddObservation(TeamMate.transform.position);
        sensor.AddObservation(OpposingBean1.transform.position);
        sensor.AddObservation(OpposingBean2.transform.position);
        sensor.AddObservation(ball.transform.position);
        sensor.AddObservation(goal1.transform.position);
        sensor.AddObservation(goal2.transform.position);
    }

    // Process actions received from the agent
    public override void OnActionReceived(ActionBuffers actions)
    {
        float horizontalMovement = actions.ContinuousActions[0];
        float verticalMovement = actions.ContinuousActions[1];

        moveDirection = transform.TransformDirection(new Vector3(horizontalMovement, 0f, verticalMovement).normalized);

        // Move the agent using the CharacterController (ensure characterController is not null)
        if (characterController != null)
        {
            characterController.SimpleMove(moveDirection * moveSpeed);
            Debug.Log("Moving: " + moveDirection);
        }
        else
        {
            Debug.LogWarning("CharacterController is null. Make sure it's assigned in the Inspector.");
        }
    }

    public void AddGoalReward(Team rewardTeam)
    {
        if (rewardTeam == Team.Blue)
        {
            if (team == Team.Blue)
            {
                AddReward(2f);
                EndEpisode();
                Debug.Log("Blue team scored. Reward: +2");
            }
            else
            {
                AddReward(-2f);
                EndEpisode();
                Debug.Log("Red team scored. Reward: -2");
            }
        }
        else if (rewardTeam == Team.Red)
        {
            if (team == Team.Red)
            {
                AddReward(2f);
                EndEpisode();
                Debug.Log("Red team scored. Reward: +2");
            }
            else
            {
                AddReward(-2f);
                EndEpisode();
                Debug.Log("Blue team scored. Reward: -2");
            }
        }
    }

    // Heuristic method for manual control (using arrow keys)
    public override void Heuristic(in ActionBuffers actionsOut)
    {
        ActionSegment<float> continuousActions = actionsOut.ContinuousActions;

        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        continuousActions[0] = horizontalInput;
        continuousActions[1] = verticalInput;

        Debug.Log("Heuristic - Horizontal: " + horizontalInput + ", Vertical: " + verticalInput);
    }
}

Thanks in advance.

1 Like

Hello!
I feel you, it was very tedious to intall it for me too.

I use Windows 11, release 20 of the mlagents package
Version information:
ml-agents: 0.30.0,
ml-agents-envs: 0.30.0,
Communicator API: 1.5.0,
PyTorch: 2.0.1+cu118

and Python 3.8.16 with Anaconda

I had to deinstall and install some things manually too (the barracuda liberay broke for me + reinstall some libraries)

I dont know how to fix your issue though, most simple fix would be just to reinstall it.