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

Fix discrete CarRacing-v3 #1253

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion gymnasium/envs/box2d/car_racing.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ def reset(
def step(self, action: Union[np.ndarray, int]):
assert self.car is not None
if action is not None:
action = action.astype(np.float64)
if self.continuous:
action = action.astype(np.float64)
self.car.steer(-action[0])
self.car.gas(action[1])
self.car.brake(action[2])
Expand Down
22 changes: 22 additions & 0 deletions tests/envs/test_env_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from gymnasium.envs.box2d.lunar_lander import demo_heuristic_lander
from gymnasium.envs.toy_text import CliffWalkingEnv, TaxiEnv
from gymnasium.envs.toy_text.frozen_lake import generate_random_map
from gymnasium.error import InvalidAction


def test_lunar_lander_heuristics():
Expand Down Expand Up @@ -343,3 +344,24 @@ def test_cartpole_vector_equiv():

env.close()
envs.close()


@pytest.mark.parametrize("env_id", ["CarRacing-v3", "LunarLander-v3"])
def test_discrete_action_validation(env_id):
# get continuous action
continuous_env = gym.make(env_id, continuous=True)
continuous_action = continuous_env.action_space.sample()
continuous_env.close()

# create discrete env
discrete_env = gym.make(env_id, continuous=False)
discrete_env.reset()

# expect InvalidAction (caused by CarRacing) or AssertionError (caused by LunarLander)
with pytest.raises((InvalidAction, AssertionError)):
discrete_env.step(continuous_action)

# expect no error
discrete_action = discrete_env.action_space.sample()
discrete_env.step(discrete_action)
discrete_env.close()
Loading