mirror of
https://github.com/wassname/ray.git
synced 2026-08-05 13:21:03 +08:00
[rllib] Native support for Dict and Tuple spaces; fix Tuple action spaces; add prev a, r to LSTM (#3051)
This commit is contained in:
@@ -2,7 +2,7 @@ import unittest
|
||||
import traceback
|
||||
|
||||
import gym
|
||||
from gym.spaces import Box, Discrete, Tuple
|
||||
from gym.spaces import Box, Discrete, Tuple, Dict
|
||||
from gym.envs.registration import EnvSpec
|
||||
import numpy as np
|
||||
import sys
|
||||
@@ -14,33 +14,28 @@ from ray.tune.registry import register_env
|
||||
|
||||
ACTION_SPACES_TO_TEST = {
|
||||
"discrete": Discrete(5),
|
||||
"vector": Box(0.0, 1.0, (5, ), dtype=np.float32),
|
||||
"simple_tuple": Tuple([
|
||||
Box(0.0, 1.0, (5, ), dtype=np.float32),
|
||||
Box(0.0, 1.0, (5, ), dtype=np.float32)
|
||||
]),
|
||||
"mixed_tuple": Tuple(
|
||||
"vector": Box(-1.0, 1.0, (5, ), dtype=np.float32),
|
||||
"tuple": Tuple(
|
||||
[Discrete(2),
|
||||
Discrete(3),
|
||||
Box(0.0, 1.0, (5, ), dtype=np.float32)]),
|
||||
Box(-1.0, 1.0, (5, ), dtype=np.float32)]),
|
||||
}
|
||||
|
||||
OBSERVATION_SPACES_TO_TEST = {
|
||||
"discrete": Discrete(5),
|
||||
"vector": Box(0.0, 1.0, (5, ), dtype=np.float32),
|
||||
"image": Box(0.0, 1.0, (84, 84, 1), dtype=np.float32),
|
||||
"atari": Box(0.0, 1.0, (210, 160, 3), dtype=np.float32),
|
||||
"atari_ram": Box(0.0, 1.0, (128, ), dtype=np.float32),
|
||||
"simple_tuple": Tuple([
|
||||
Box(0.0, 1.0, (5, ), dtype=np.float32),
|
||||
Box(0.0, 1.0, (5, ), dtype=np.float32)
|
||||
]),
|
||||
"mixed_tuple": Tuple(
|
||||
[Discrete(10), Box(0.0, 1.0, (5, ), dtype=np.float32)]),
|
||||
"vector": Box(-1.0, 1.0, (5, ), dtype=np.float32),
|
||||
"image": Box(-1.0, 1.0, (84, 84, 1), dtype=np.float32),
|
||||
"atari": Box(-1.0, 1.0, (210, 160, 3), dtype=np.float32),
|
||||
"tuple": Tuple([Discrete(10),
|
||||
Box(-1.0, 1.0, (5, ), dtype=np.float32)]),
|
||||
"dict": Dict({
|
||||
"task": Discrete(10),
|
||||
"position": Box(-1.0, 1.0, (5, ), dtype=np.float32),
|
||||
}),
|
||||
}
|
||||
|
||||
|
||||
def make_stub_env(action_space, obs_space):
|
||||
def make_stub_env(action_space, obs_space, check_action_bounds):
|
||||
class StubEnv(gym.Env):
|
||||
def __init__(self):
|
||||
self.action_space = action_space
|
||||
@@ -52,16 +47,23 @@ def make_stub_env(action_space, obs_space):
|
||||
return sample
|
||||
|
||||
def step(self, action):
|
||||
if check_action_bounds and not self.action_space.contains(action):
|
||||
raise ValueError("Illegal action for {}: {}".format(
|
||||
self.action_space, action))
|
||||
if (isinstance(self.action_space, Tuple)
|
||||
and len(action) != len(self.action_space.spaces)):
|
||||
raise ValueError("Illegal action for {}: {}".format(
|
||||
self.action_space, action))
|
||||
return self.observation_space.sample(), 1, True, {}
|
||||
|
||||
return StubEnv
|
||||
|
||||
|
||||
def check_support(alg, config, stats):
|
||||
def check_support(alg, config, stats, check_bounds=False):
|
||||
for a_name, action_space in ACTION_SPACES_TO_TEST.items():
|
||||
for o_name, obs_space in OBSERVATION_SPACES_TO_TEST.items():
|
||||
print("=== Testing", alg, action_space, obs_space, "===")
|
||||
stub_env = make_stub_env(action_space, obs_space)
|
||||
stub_env = make_stub_env(action_space, obs_space, check_bounds)
|
||||
register_env("stub_env", lambda c: stub_env())
|
||||
stat = "ok"
|
||||
a = None
|
||||
@@ -105,8 +107,13 @@ class ModelSupportedSpaces(unittest.TestCase):
|
||||
"num_sgd_iter": 1,
|
||||
"train_batch_size": 10,
|
||||
"sample_batch_size": 10,
|
||||
"sgd_minibatch_size": 1
|
||||
}, stats)
|
||||
"sgd_minibatch_size": 1,
|
||||
"model": {
|
||||
"squash_to_range": True
|
||||
},
|
||||
},
|
||||
stats,
|
||||
check_bounds=True)
|
||||
check_support(
|
||||
"ES", {
|
||||
"num_workers": 1,
|
||||
|
||||
Reference in New Issue
Block a user