Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scaling for PositionBonus #433

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions minigrid/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def step(self, action):

class PositionBonus(Wrapper):
"""
Adds an exploration bonus based on which positions
Adds a scaled exploration bonus based on which positions
are visited on the grid.

Note:
Expand All @@ -144,7 +144,7 @@ class PositionBonus(Wrapper):
>>> _, reward, _, _, _ = env.step(1)
>>> print(reward)
0
>>> env_bonus = PositionBonus(env)
>>> env_bonus = PositionBonus(env, scale=1)
>>> obs, _ = env_bonus.reset(seed=0)
>>> obs, reward, terminated, truncated, info = env_bonus.step(1)
>>> print(reward)
Expand All @@ -154,14 +154,15 @@ class PositionBonus(Wrapper):
0.7071067811865475
"""

def __init__(self, env):
def __init__(self, env, scale=1):
"""A wrapper that adds an exploration bonus to less visited positions.

Args:
env: The environment to apply the wrapper
"""
super().__init__(env)
self.counts = {}
self.scale = 1

def step(self, action):
"""Steps through the environment with `action`."""
Expand All @@ -173,16 +174,14 @@ def step(self, action):
tup = tuple(env.agent_pos)

# Get the count for this key
pre_count = 0
if tup in self.counts:
pre_count = self.counts[tup]
pre_count = self.counts.get(tup, 0)

# Update the count for this key
new_count = pre_count + 1
self.counts[tup] = new_count

bonus = 1 / math.sqrt(new_count)
reward += bonus
reward += bonus * self.scale

return obs, reward, terminated, truncated, info

Expand Down