Skip to content

Commit

Permalink
[Wrappers]: add ClipReward (openai#1484)
Browse files Browse the repository at this point in the history
* Create clip_reward.py

* Update __init__.py

* Create test_clip_reward.py

* Update clip_reward.py
  • Loading branch information
zuoxingdong authored and pzhokhov committed May 24, 2019
1 parent c8c4449 commit bf12bf9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gym/wrappers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from gym.wrappers.time_limit import TimeLimit
from gym.wrappers.dict import FlattenDictWrapper
from gym.wrappers.atari_preprocessing import AtariPreprocessing
from gym.wrappers.sign_reward import SignReward
from gym.wrappers.clip_reward import ClipReward
from gym.wrappers.sign_reward import SignReward
14 changes: 14 additions & 0 deletions gym/wrappers/clip_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np

from gym import RewardWrapper


class ClipReward(RewardWrapper):
r""""Clip reward to [min, max]. """
def __init__(self, env, min_r, max_r):
super(ClipReward, self).__init__(env)
self.min_r = min_r
self.max_r = max_r

def reward(self, reward):
return np.clip(reward, self.min_r, self.max_r)
21 changes: 21 additions & 0 deletions gym/wrappers/test_clip_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

import gym
from gym.wrappers import ClipReward


@pytest.mark.parametrize('env_id', ['CartPole-v1', 'Pendulum-v0', 'MountainCar-v0'])
def test_clip_reward(env_id):
env = gym.make(env_id)
wrapped_env = ClipReward(env, -0.0005, 0.0002)

env.reset()
wrapped_env.reset()

action = env.action_space.sample()

_, reward, _, _ = env.step(action)
_, wrapped_reward, _, _ = wrapped_env.step(action)

assert abs(wrapped_reward) < abs(reward)
assert wrapped_reward == -0.0005 or wrapped_reward == 0.0002

0 comments on commit bf12bf9

Please sign in to comment.