Train Agent in Unity with rendering turned off using ml-agents

I created a game in Unity and I’m trying to train an agent in this game with ml-agents tool. I implemented algorithms in Python to train agent and for interacting with unity environment I’m using API from official documentation (env = UnityEnvironment(file_name=None, seed=1, side_channels=[ ])). Now I want to turn off rendering to train agent as fast as possible. I found that --batchmode option could be possible solution but when I tried it, Unity couldn’t connect to ml-agents Python API (without --batchmode option it works, it connects to address and port and starts training).

Can you please help me why it doesn’t work with this option and how to solve this or is there another way how to do it?

Command to run Unity which I used:

"PathToUnityExecutable\Unity.exe" -batchmode -logfile -projectPath "pathToProject" -executeMethod RunGame.PlayGame

Code for “RunGame” class with “PlayGame” method is below.

public static class RunGame {

     public static bool isBatchMode = false;

     public static void PlayGame()
     {
         isBatchMode = true;
         EditorSceneManager.OpenScene("Assets/Scenes/Main.unity");
         EditorApplication.EnterPlaymode();
     }

     public static void ExitGame(int errorCode=0)
     {
        if (Application.isBatchMode)
            EditorApplication.Exit(errorCode);

        isBatchMode = false;
      } 
}

Thank you for your answers!

I am not sure if this helps but you can build your game in headless mode and run training on the executable. This is commonly used so you can not only get the best single-instance speed but also use the –num-envs argument for even more parallel gains. Please let me know if this isn’t what you are looking for.

1 Like

Yes, this is what I need. Thank you very much!