Search Unity

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

Discussion in 'ML-Agents' started by unity_ZZovixQiIOD01g, Oct 5, 2020.

  1. unity_ZZovixQiIOD01g

    unity_ZZovixQiIOD01g

    Joined:
    Oct 5, 2020
    Posts:
    2
    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.
    Code (CSharp):
    1. public static class RunGame {
    2.  
    3.      public static bool isBatchMode = false;
    4.  
    5.      public static void PlayGame()
    6.      {
    7.          isBatchMode = true;
    8.          EditorSceneManager.OpenScene("Assets/Scenes/Main.unity");
    9.          EditorApplication.EnterPlaymode();
    10.      }
    11.  
    12.      public static void ExitGame(int errorCode=0)
    13.      {
    14.         if (Application.isBatchMode)
    15.             EditorApplication.Exit(errorCode);
    16.  
    17.         isBatchMode = false;
    18.       }
    19. }
    Thank you for your answers!
     
  2. henrypeteet

    henrypeteet

    Unity Technologies

    Joined:
    Aug 19, 2020
    Posts:
    37
    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.
     
    ItzMeStellar likes this.
  3. unity_ZZovixQiIOD01g

    unity_ZZovixQiIOD01g

    Joined:
    Oct 5, 2020
    Posts:
    2

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