浏览代码

Making gym a dependency of ml-agents-envs

/develop/gym-wrapper
vincentpierre 5 年前
当前提交
419611f4
共有 4 个文件被更改,包括 59 次插入73 次删除
  1. 34
      ml-agents-envs/mlagents_envs/gym_to_unity_wrapper.py
  2. 1
      ml-agents-envs/setup.py
  3. 40
      ml-agents-envs/mlagents_envs/tests/test_gym_to_unity_wrapper.py
  4. 57
      ml-agents-envs/mlagents_envs/tests/test_gym_wrapper.py

34
ml-agents-envs/mlagents_envs/gym_to_unity_wrapper.py


)
from mlagents_envs.exception import UnityActionException, UnityObservationException
from typing import List, Tuple
from typing import List, Tuple, Union, Optional
try:
import gym
_GYM_IMPORTED = True
except ImportError:
_GYM_IMPORTED = False
import gym
class GymToUnityWrapper(BaseEnv):

def __init__(self, gym_env, name=None):
if not _GYM_IMPORTED:
raise RuntimeError(
"gym is not installed, gym required to use the GymToUnityWrapper"
)
def __init__(self, gym_env: gym.Env, name: Optional[str] = None):
"""
Wrapper construction. Creates an implementation of a Unity BaseEnv from a gym
environment.
:gym.Env gym_env: The gym environment that will be wrapped.
:str name: [Optional] The name of the gym environment. This will become the
name of the behavior for the BaseEnv.
"""
self._behavior_name = name
if self._behavior_name is None:
if name is None:
else:
self._behavior_name = name
action_shape = 0
action_shape: Union[Tuple[int, ...], int] = 0
if isinstance(self._gym_env.action_space, gym.spaces.Box):
action_type = ActionType.CONTINUOUS
action_shape = np.prod(self._gym_env.action_space.shape)

action_shape=action_shape,
)
self._g_action: np.ndarray = None
self._current_steps: Tuple[DecisionSteps, TerminalSteps] = (None, None)
self._current_steps: Tuple[DecisionSteps, TerminalSteps] = (
DecisionSteps.empty(self._behavior_specs),
TerminalSteps.empty(self._behavior_specs),
)
def step(self) -> None:
if self._first_message:

1
ml-agents-envs/setup.py


packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),
zip_safe=False,
install_requires=[
"gym",
"cloudpickle",
"grpcio>=1.11.0",
"numpy>=1.14.1,<2.0",

40
ml-agents-envs/mlagents_envs/tests/test_gym_to_unity_wrapper.py


from mlagents_envs.gym_to_unity_wrapper import GymToUnityWrapper
from mlagents_envs.base_env import ActionType
import gym
import pytest
GYM_ENVS = ["CartPole-v1", "MountainCar-v0"]
@pytest.mark.parametrize("name", GYM_ENVS, ids=GYM_ENVS)
def test_creation(name):
env = GymToUnityWrapper(gym.make(name), name)
env.close()
@pytest.mark.parametrize("name", GYM_ENVS, ids=GYM_ENVS)
def test_specs(name):
gym_env = gym.make(name)
env = GymToUnityWrapper(gym_env, name)
assert env.get_behavior_names()[0] == name
if isinstance(gym_env.action_space, gym.spaces.Box):
assert env.get_behavior_spec(name).action_type == ActionType.CONTINUOUS
elif isinstance(gym_env.action_space, gym.spaces.Discrete):
assert env.get_behavior_spec(name).action_type == ActionType.DISCRETE
else:
raise NotImplementedError("Test for this action space type not implemented")
env.close()
@pytest.mark.parametrize("name", GYM_ENVS, ids=GYM_ENVS)
def test_steps(name):
env = GymToUnityWrapper(gym.make(name), name)
spec = env.get_behavior_spec(name)
env.reset()
for _ in range(200):
d_steps, t_steps = env.get_steps(name)
env.set_actions(name, spec.create_empty_action(len(d_steps)))
env.step()
env.close()

57
ml-agents-envs/mlagents_envs/tests/test_gym_wrapper.py


from mlagents_envs.gym_to_unity_wrapper import GymToUnityWrapper
from mlagents_envs.base_env import ActionType
try:
import gym
_GYM_IMPORTED = True
except ImportError:
_GYM_IMPORTED = False
import pytest
GYM_ENVS = ["CartPole-v1", "MountainCar-v0"]
@pytest.mark.parametrize("name", GYM_ENVS, ids=GYM_ENVS)
def test_creation(name):
if not _GYM_IMPORTED:
raise RuntimeError(
"gym is not installed, gym required to test the GymToUnityWrapper"
)
env = GymToUnityWrapper(gym.make(name), name)
env.close()
@pytest.mark.parametrize("name", GYM_ENVS, ids=GYM_ENVS)
def test_specs(name):
if not _GYM_IMPORTED:
raise RuntimeError(
"gym is not installed, gym required to test the GymToUnityWrapper"
)
gym_env = gym.make(name)
env = GymToUnityWrapper(gym_env, name)
assert env.get_behavior_names()[0] == name
if isinstance(gym_env.action_space, gym.spaces.Box):
assert env.get_behavior_spec(name).action_type == ActionType.CONTINUOUS
elif isinstance(gym_env.action_space, gym.spaces.Discrete):
assert env.get_behavior_spec(name).action_type == ActionType.DISCRETE
else:
raise NotImplementedError("Test for this action space type not implemented")
env.close()
@pytest.mark.parametrize("name", GYM_ENVS, ids=GYM_ENVS)
def test_steps(name):
if not _GYM_IMPORTED:
raise RuntimeError(
"gym is not installed, gym required to test the GymToUnityWrapper"
)
env = GymToUnityWrapper(gym.make(name), name)
spec = env.get_behavior_spec(name)
env.reset()
for _ in range(200):
d_steps, t_steps = env.get_steps(name)
env.set_actions(name, spec.create_empty_action(len(d_steps)))
env.step()
env.close()
正在加载...
取消
保存