Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved How to set time-scale of the engine when training with env as executable

Discussion in 'ML-Agents' started by julienroyd, Feb 18, 2021.

  1. julienroyd

    julienroyd

    Joined:
    Feb 7, 2021
    Posts:
    6
    Hi,

    I have seen that `mlagents-learn` accepts the `time-scale` argument to control the engine's time-scale. I am training with the env as executable and with my own training codebase rather than the mlagents python package. However, when I create the environment, neither `UnityEnvironment` nor `UnityToGymWrapper` accepts the `time_scale` argument.

    How can I set the time-scale when training without mlagents and with an executable?
     
  2. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    Unity player builds don't accept a time scale parameter so you'll need to imbed your own.

    It's pretty easy though just put something like the following in your project -
    Code (CSharp):
    1. // Helper function for getting the command line arguments
    2. private static string GetArg(string name)
    3. {
    4.     var args = System.Environment.GetCommandLineArgs();
    5.     for (int i = 0; i < args.Length; i++)
    6.     {
    7.         if (args[i] == name && args.Length > i + 1)
    8.         {
    9.             return args[i + 1];
    10.         }
    11.     }
    12.     return null;
    13. }
    Call that
    GetArg(double_time)
    method somewhere and use it to set the timescale within unity.

    Then when you launch the environment executable just pass in the timescale
    open environment.app/ --args -double_time


    This example will give you true/false params, if you want something more like ml-agents time_scale argument you'll have to parse the command and value out of the arg but you probably get the idea.
     
    Sira- likes this.
  3. julienroyd

    julienroyd

    Joined:
    Feb 7, 2021
    Posts:
    6
    @Luke-Houlihan thanks a lot for your answer!

    I think that could work but even better would be to be able to communicate the timeScale directly from the python side rather than the commandline. I have seen that there is a EngineConfigurationChannel in mlagents_envs/side_channel but I am not quite sure how to use it?
     
    Last edited: Feb 22, 2021
  4. julienroyd

    julienroyd

    Joined:
    Feb 7, 2021
    Posts:
    6
    Ok got it! I needed to pass the EngineConfigurationChannel as argument to the UnityEnvironment. Here is a minimal working example:


    Code (python):
    1.  
    2. from mlagents_envs.environment import UnityEnvironment
    3. from mlagents_envs.side_channel.engine_configuration_channel import EngineConfigurationChannel
    4. from gym_unity.envs import UnityToGymWrapper
    5.  
    6. engineConfigChannel = EngineConfigurationChannel()
    7.  
    8. unity_env = UnityEnvironment(file_name=exec_path, no_graphics=False, seed=0, side_channels=[engineConfigChannel])
    9. engineConfigChannel.set_configuration_parameters(width=250, height=250, quality_level=1, time_scale=20.,
    10.                                                  target_frame_rate=-1, capture_frame_rate=60)
    11.  
    12. env = UnityToGymWrapper(unity_env, uint8_visual=False, flatten_branched=False, allow_multiple_obs=False)
    13.  
    14. for _ in range(100):
    15.     done = False
    16.     state = env.reset()
    17.     while not done:
    18.         action = env.action_space.sample()
    19.         next_state, reward, done, _ = env.step(action)
    20.         state = next_state
    21.  
     
    ditlevrisdahl and OrSimhon like this.
  5. OrSimhon

    OrSimhon

    Joined:
    Nov 23, 2020
    Posts:
    16
    Thanks!! it's definitely what I needed.
    There is somewhere explanations about every parameters? for example 'quality_level'
     
    Kapitalstocken likes this.