Skip to content

Commit

Permalink
Calculating transitions inside Corridor env
Browse files Browse the repository at this point in the history
  • Loading branch information
khozzy committed Mar 15, 2020
1 parent 89f25c5 commit d20d24d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ For some usage examples look at [examples/](examples) directory.
## Development

conda create --name openai-envs python=3.7
source activate openai-envs
conda activate openai-envs

pip install -r requirements.txt
pip install -e ".[testing]"

python setup.py test
25 changes: 25 additions & 0 deletions gym_corridor/corridor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Corridor(gym.Env):
def __init__(self, size=20):
self._size = size
self._position = None
self._transitions = self._calculate_transitions()

self.observation_space = Discrete(1)
self.action_space = Discrete(2)
Expand Down Expand Up @@ -47,8 +48,32 @@ def render(self, mode='human'):
else:
raise ValueError('Unknown visualisation mode')

def get_transitions(self):
return self._transitions

def _visualize(self):
corridor = ["" for _ in range(0, self._size - 1)]
corridor[self._position - 1] = "X"
corridor[self._size - 2] = "$"
return "[" + ".".join(corridor) + "]"

def _calculate_transitions(self):
START, END = 1, self._size
LEFT, RIGHT = 0, 1

def _handle_state(state):
moves = []
if state == START:
moves.append((state, RIGHT, state + 1))
else:
moves.append((state, LEFT, state - 1))
moves.append((state, RIGHT, state + 1))

return moves

transitions = []

for state in range(START, END):
transitions += _handle_state(state)

return transitions
10 changes: 10 additions & 0 deletions gym_corridor/tests/test_corridor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ def test_should_move_in_both_directions(self):

p2, _, _, _ = corr.step(MOVE_RIGHT)
assert int(p2) == int(p0)

def test_should_calculate_transitions(self):
# given
corr = gym.make('corridor-20-v0')

# when
transitions = corr.env.get_transitions()

# then
assert len(transitions) == 37
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
]

setup(name='parrotprediction-openai-envs',
version='2.2.1',
version='2.2.2',
description='Custom environments for OpenAI Gym',
keywords='acs lcs machine-learning reinforcement-learning openai',
url='https://github.com/ParrotPrediction/openai-envs',
Expand Down

0 comments on commit d20d24d

Please sign in to comment.