Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Sidechannel in gym environment

Discussion in 'ML-Agents' started by Maxhilde, Apr 9, 2020.

  1. Maxhilde

    Maxhilde

    Joined:
    Sep 4, 2019
    Posts:
    2
    Hi,

    I've been moving my project from 0.10 to 0.15.0 ML-agents, and one of the key differences is that time-scaling and width/height setup has been removed from the editor, as the academy object has been removed. I now understand that you can still change these, but as a parameter when using mlagents-learn, or implement sidechannel when making UnityEnvironment.

    However, when making UnityEnv, because i'm using the gym_wrapper and stable baselines, i am unable to do so. Am i missing something, can someone direct me to a place where i can read more on how to pass these arguments in a gym environment?
     
  2. Maxhilde

    Maxhilde

    Joined:
    Sep 4, 2019
    Posts:
    2
    To clarify i have it running as such:
    In envs\__init.py
    Add:
    from mlagents_envs.side_channel.engine_configuration_channel import EngineConfigurationChannel
    Change:
    def __init__(
    self,
    environment_filename: str,
    dimensions: int = [], #added
    timescale: int = 1, #added
    worker_id: int = 0,
    use_visual: bool = False,
    uint8_visual: bool = False,
    multiagent: bool = False,
    flatten_branched: bool = False,
    no_graphics: bool = False,
    allow_multiple_visual_obs: bool = False,
    set_config: bool = True, #added
    ):

    base_port = 5005
    if environment_filename is None:
    base_port = UnityEnvironment.DEFAULT_EDITOR_PORT

    #Added
    channel = EngineConfigurationChannel()
    if set_config==True:
    channel.set_configuration_parameters(
    time_scale = timescale,
    width = dimensions[0],
    height = dimensions[1])
    #Added



    self._env = UnityEnvironment(
    environment_filename,
    worker_id,
    base_port=base_port,
    no_graphics=no_graphics,
    side_channels=[channel], #added
    )

    Was just wondering if there was an established solution other than this.
     
  3. sohojoe

    sohojoe

    Joined:
    Feb 13, 2015
    Posts:
    21
    I ended up doing the same workaround
     
  4. celion_unity

    celion_unity

    Unity Technologies

    Joined:
    Jun 12, 2019
    Posts:
    289
  5. celion_unity

    celion_unity

    Unity Technologies

    Joined:
    Jun 12, 2019
    Posts:
    289
  6. SickOrean

    SickOrean

    Joined:
    May 18, 2020
    Posts:
    4
    I'm trying the same work around in 0.14.1.

    I changed the file ml-agents\gym-unity\gym_unity\envs\__init__.py of the gym wrapper according to your suggestion. However the following notebook still gives me errors:

    1: from gym_unity.envs import UnityEnv
    2: from mlagents_envs.side_channel.engine_configuration_channel import EngineConfigurationChannel
    3:
    4: channel1 = EngineConfigurationChannel()
    5: env = UnityEnv(env_name, worker_id=1, use_visual=False, multiagent=True, side_channels=[channel1])

    TypeError: __init__() got an unexpected keyword argument 'side_channels'

    Could it be that I applied the changes incorrectly?
    Or did I use the UnityEnv afterwards wrongly?
     
  7. mhildebrand123

    mhildebrand123

    Joined:
    May 18, 2020
    Posts:
    1
    - Did you add the side_channels argument in self._env as per the final bit in my original post? - In the init file.
     
  8. SickOrean

    SickOrean

    Joined:
    May 18, 2020
    Posts:
    4
    From my perspective I added everything as suggested.

    1. The import in the header
    2. dimensions, timescale, and set_config in the __init__()
    3. the channel = EngineConfigurationChannel()
    4. the if (set_config == True)
    5. The side_channels in self._env()

    Could it be that I use the class wrongly in my notebook?

    Following code copied from my file:

    Code (Python):
    1.  
    2. class UnityEnv(gym.Env):
    3.     """
    4.     Provides Gym wrapper for Unity Learning Environments.
    5.     Multi-agent environments use lists for object types, as done here:
    6.     https://github.com/openai/multiagent-particle-envs
    7.     """
    8.  
    9.     def __init__(
    10.         self,
    11.         environment_filename: str,
    12.         dimensions: int = [],   #Added
    13.         timescale: int = 1,     #Added
    14.         worker_id: int = 0,
    15.         use_visual: bool = False,
    16.         uint8_visual: bool = False,
    17.         multiagent: bool = False,
    18.         flatten_branched: bool = False,
    19.         no_graphics: bool = False,
    20.         allow_multiple_visual_obs: bool = False,
    21.         set_config: bool = True,    #Added
    22.     ):
    23.         """
    24.         Environment initialization
    25.         :param environment_filename: The UnityEnvironment path or file to be wrapped in the gym.
    26.         :param worker_id: Worker number for environment.
    27.         :param use_visual: Whether to use visual observation or vector observation.
    28.         :param uint8_visual: Return visual observations as uint8 (0-255) matrices instead of float (0.0-1.0).
    29.         :param multiagent: Whether to run in multi-agent mode (lists of obs, reward, done).
    30.         :param flatten_branched: If True, turn branched discrete action spaces into a Discrete space rather than
    31.             MultiDiscrete.
    32.         :param no_graphics: Whether to run the Unity simulator in no-graphics mode
    33.         :param allow_multiple_visual_obs: If True, return a list of visual observations instead of only one.
    34.         """
    35.         base_port = 5005
    36.         if environment_filename is None:
    37.             base_port = UnityEnvironment.DEFAULT_EDITOR_PORT
    38.  
    39.         channel = EngineConfigurationChannel()        # Added
    40.  
    41.  
    42.         #Added
    43.         if set_config == True:
    44.             channel.set_configuration_parameters(time_scale=timescale, width=dimensions[0], height=dimensions[1])
    45.         #Added
    46.  
    47.         self._env = UnityEnvironment(
    48.             environment_filename,
    49.             worker_id,
    50.             base_port=base_port,
    51.             no_graphics=no_graphics,
    52.             side_channels=[channel],        # Added
    53.         )
    54.  
     
  9. SickOrean

    SickOrean

    Joined:
    May 18, 2020
    Posts:
    4
    Ok, I found my foolish mistake. Of course, in my notebook I have to create my gym environment like this:

    env = UnityEnv(env_name, worker_id=1, use_visual=False, multiagent=True, timescale = 1)

    Guess my python is quite rusted. :D

    Now I just have to figure out, how the timescale actually scales.
     
    mhildebrand123 likes this.