Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
48 changes: 43 additions & 5 deletions gym/envs/box2d/lunar_lander.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SIDE_ENGINE_POWER = 0.6

INITIAL_RANDOM = 1000.0 # Set 1500 to make game harder
WIND_POWER = 15.0

LANDER_POLY = [(-14, +17), (-17, 0), (-17, -10), (+17, -10), (+17, 0), (+14, +17)]
LEG_AWAY = 20
Expand Down Expand Up @@ -151,12 +152,26 @@ class LunarLander(gym.Env, EzPickle):

metadata = {"render_modes": ["human", "rgb_array"], "render_fps": FPS}

def __init__(self, continuous: bool = False):
def __init__(
self,
continuous: bool = False,
gravity: float = -10.0,
enable_wind: bool = False,
):
EzPickle.__init__(self)

assert (
-12.0 < gravity and gravity < 0.0
), f"gravity (current value: {gravity}) must be between -12 and 0"
self.gravity = gravity

self.enable_wind = enable_wind
self.wind_idx = np.random.randint(-9999, 9999)

self.screen = None
self.clock = None
self.isopen = True
self.world = Box2D.b2World()
self.world = Box2D.b2World(gravity=(0, gravity))
self.moon = None
self.lander = None
self.particles = []
Expand Down Expand Up @@ -330,6 +345,25 @@ def _clean_particles(self, all):
self.world.DestroyBody(self.particles.pop(0))

def step(self, action):
# Update wind
if self.enable_wind and not (
self.legs[0].ground_contact or self.legs[1].ground_contact
):
# the function used for wind is tanh(sin(2 k x) + sin(pi k x)),
# which is proven to never be periodic, k = 0.01
wind_mag = (
math.tanh(
math.sin(0.02 * self.wind_idx)
+ (math.sin(math.pi * 0.01 * self.wind_idx))
)
* WIND_POWER
)
self.wind_idx += 1
self.lander.ApplyForceToCenter(
(wind_mag, 0.0),
True,
)

if self.continuous:
action = np.clip(action, -1, +1).astype(np.float32)
else:
Expand All @@ -352,9 +386,8 @@ def step(self, action):
assert m_power >= 0.5 and m_power <= 1.0
else:
m_power = 1.0
ox = (
tip[0] * (4 / SCALE + 2 * dispersion[0]) + side[0] * dispersion[1]
) # 4 is move a bit downwards, +-2 for randomness
# 4 is move a bit downwards, +-2 for randomness
ox = +tip[0] * (4 / SCALE + 2 * dispersion[0]) + side[0] * dispersion[1]
oy = -tip[1] * (4 / SCALE + 2 * dispersion[0]) - side[1] * dispersion[1]
impulse_pos = (self.lander.position[0] + ox, self.lander.position[1] + oy)
p = self._create_particle(
Expand Down Expand Up @@ -618,6 +651,11 @@ def heuristic(env, s):


def demo_heuristic_lander(env, seed=None, render=False):

# wind power must be reduced for heuristic landing
global WIND_POWER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big no-no, all caps suggests that this should be a global constant, so changing this in the code is a bad pattern.

If the wind power is something that users are meant to change, maybe make it a constructor argument?

Copy link
Contributor Author

@jjshoots jjshoots Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Ariel, the wind power isn't something that user's are meant to change.

The only reason it's here is that the heuristic landing won't work with the default wind setting, and this causes it to fail tests. So for heuristic landings, I've reduced the wind power to be 1/15th of the original magnitude (so that the wind code is still tested in tests, but still solvable with heuristic landing).

I'm aware this is not the most optimal solution, I'll think of something soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made it a kwarg in the latest commit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want users to not change it (I don't know why that'd be the case, but I trust your judgement), wouldn't the standard be to name it _wind_power and not have it be a kwarg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkterry1 done!

WIND_POWER = 1.0

total_reward = 0
steps = 0
s = env.reset(seed=seed)
Expand Down
10 changes: 10 additions & 0 deletions tests/envs/test_lunar_lander.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def test_lunar_lander_continuous():
_test_lander(LunarLander(continuous=True), seed=0)


@pytest.mark.skipif(Box2D is None, reason="Box2D not installed")
def test_lunar_lander_wind():
_test_lander(LunarLander(enable_wind=True), seed=0)


@pytest.mark.skipif(Box2D is None, reason="Box2D not installed")
def test_lunar_lander_wind_continuous():
_test_lander(LunarLander(continuous=True, enable_wind=True), seed=0)


@pytest.mark.skipif(Box2D is None, reason="Box2D not installed")
def _test_lander(env, seed=None, render=False):
total_reward = demo_heuristic_lander(env, seed=seed, render=render)
Expand Down