Unity-Technologies / ml-agents

The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinforcement learning and imitation learning.
https://unity.com/products/machine-learning-agents
Other
17.12k stars 4.15k forks source link

Defining 'spec' as @property in UnityEnvironment() prevents from registering environment #4963

Closed julienroyd closed 2 years ago

julienroyd commented 3 years ago

Hi,

I am using gym==0.18.0 and gym-unity==0.23.0.

I know that gym.make() is not officially supported by unity_gym. However, I was wondering why the attribute spec was defined as a @property in gym_unity/envs/__init__:UnityToGymWrapper?

Having spec be a regular attribute rather than a @property would allow users to register themselves their unity environments and have their ML code simply call gym.make("MyUnityEnv-v0") for example, like the other RL benchmarks. For exemple, the user could simply implement something as simple as this in the repository containing the environments:

unity_env_wrapper.py:

from gym.envs.registration import register

from mlagents_envs.environment import UnityEnvironment
from gym_unity.envs import UnityToGymWrapper

class MyUnityEnv(UnityToGymWrapper):
    def __init__(self, **kwargs):
        unity_env = UnityEnvironment(file_name="<path-to-executable>")
        super().__init__(unity_env=unity_env, uint8_visual=False, flatten_branched=False, allow_multiple_obs=False)

register(
    id='MyUnityEnv-v0',
    entry_point='unity_env_wrapper:MyUnityEnv'
)

rl_script.py:

import unity_env_wrapper
import gym

env = gym.make("MyUnityEnv-v0")

Having spec being a @property triggers AttributeError: can't set attribute in gym/envs/registration.py

Ademord commented 3 years ago

@julienroyd I tried to register my environment following your code but i get an error when trying to use vectorized environments

Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ros/.local/lib/python3.8/site-packages/stable_baselines3/common/vec_env/subproc_vec_env.py", line 24, in _worker
    env = env_fn_wrapper.var()
  File "unity_test_vectrorized.py", line 19, in _init
    env = gym.make(env_id)
  File "/home/ros/.local/lib/python3.8/site-packages/gym/envs/registration.py", line 145, in make
    return registry.make(id, **kwargs)
  File "/home/ros/.local/lib/python3.8/site-packages/gym/envs/registration.py", line 89, in make
    spec = self.spec(path)
  File "/home/ros/.local/lib/python3.8/site-packages/gym/envs/registration.py", line 131, in spec
    raise error.UnregisteredEnv('No registered env with id: {}'.format(id))
gym.error.UnregisteredEnv: No registered env with id: BirdSingleAgentNoStackedNoVisual-v1
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had activity in the last 28 days. It will be closed in the next 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

This issue has been automatically closed because it has not had activity in the last 42 days. If this issue is still valid, please ping a maintainer. Thank you for your contributions.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.