2020-05-01 22:59:34 +02:00
|
|
|
import gym
|
|
|
|
from gym.spaces import Box, Discrete
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
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."""
|
|
|
|
|
2021-04-20 08:46:05 +02:00
|
|
|
def __init__(self, config=None):
|
|
|
|
config = config or {}
|
|
|
|
self.end_pos = config.get("corridor_length", 10)
|
2020-05-01 22:59:34 +02:00
|
|
|
self.cur_pos = 0
|
|
|
|
self.action_space = Discrete(2)
|
2020-05-08 08:26:32 +02:00
|
|
|
self.observation_space = Box(0.0, 999.0, shape=(1,), dtype=np.float32)
|
2020-05-01 22:59:34 +02:00
|
|
|
|
|
|
|
def set_corridor_length(self, length):
|
|
|
|
self.end_pos = length
|
|
|
|
print("Updated corridor length to {}".format(length))
|
|
|
|
|
|
|
|
def reset(self):
|
2020-05-08 08:26:32 +02:00
|
|
|
self.cur_pos = 0.0
|
2020-05-01 22:59:34 +02:00
|
|
|
return [self.cur_pos]
|
|
|
|
|
|
|
|
def step(self, action):
|
|
|
|
assert action in [0, 1], action
|
|
|
|
if action == 0 and self.cur_pos > 0:
|
2020-05-08 08:26:32 +02:00
|
|
|
self.cur_pos -= 1.0
|
2020-05-01 22:59:34 +02:00
|
|
|
elif action == 1:
|
2020-05-08 08:26:32 +02:00
|
|
|
self.cur_pos += 1.0
|
2020-05-01 22:59:34 +02:00
|
|
|
done = self.cur_pos >= self.end_pos
|
|
|
|
return [self.cur_pos], 1 if done else 0, done, {}
|