forked from openai/gym
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Wrappers]: add ClipReward (openai#1484)
* Create clip_reward.py * Update __init__.py * Create test_clip_reward.py * Update clip_reward.py
- Loading branch information
1 parent
c8c4449
commit bf12bf9
Showing
3 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
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) |
This file contains 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
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 |