Created
July 22, 2018 22:41
-
-
Save ericl/1ea2e5f31bdd0087b6183f737a189163 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Example of a custom gym environment. Run this for a demo.""" | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import numpy as np | |
import gym | |
from gym.spaces import Discrete, Box, Tuple | |
from gym.envs.registration import EnvSpec | |
import ray | |
from ray.tune import run_experiments | |
from ray.tune.registry import register_env | |
# action space = n tuple of m choices each | |
n = 100 | |
m = 10 | |
class SimpleCorridor(gym.Env): | |
"""Example of a custom env in which you have to walk down a corridor. | |
You can configure the length of the corridor via the env config.""" | |
def __init__(self, config): | |
self.end_pos = config["corridor_length"] | |
self.cur_pos = 0 | |
self.action_space = Tuple([Discrete(n) for _ in range(m)]) | |
self.observation_space = Box( | |
0.0, self.end_pos, shape=(1,), dtype=np.float32) | |
self._spec = EnvSpec("SimpleCorridor-{}-v0".format(self.end_pos)) | |
def reset(self): | |
self.cur_pos = 0 | |
return [self.cur_pos] | |
def step(self, action): | |
action = action[0][0] % 2 # hack to ignore rest | |
assert action in [0, 1] | |
if action == 0 and self.cur_pos > 0: | |
self.cur_pos -= 1 | |
elif action == 1: | |
self.cur_pos += 1 | |
done = self.cur_pos >= self.end_pos | |
return [self.cur_pos], 1 if done else 0, done, {} | |
if __name__ == "__main__": | |
env_creator_name = "corridor" | |
register_env(env_creator_name, lambda config: SimpleCorridor(config)) | |
ray.init() | |
run_experiments({ | |
"demo": { | |
"run": "PPO", | |
"env": "corridor", | |
"config": { | |
"env_config": { | |
"corridor_length": 5, | |
}, | |
}, | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment