Farama-Foundation / Gymnasium

An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)
https://gymnasium.farama.org
MIT License
7.43k stars 839 forks source link

[Bug Report] Box2d car racing environment fails to take integer action #1233

Open brseong opened 4 weeks ago

brseong commented 4 weeks ago

Describe the bug

https://github.com/Farama-Foundation/Gymnasium/commit/8161d7d0a025ef53598c7c845ad583c168e6db36#diff-b2657ab36b5d46d0f112f0c7d20e3026e4d50c8a0e260e5669002d229e5f027cR544

the newly added line 543 of box2d/car_racing.py always fails to run non-continuous action.

Code example

Replacing the type casting may be helpful:

#line 540
def step(self, action: Union[np.ndarray, int]):
    assert self.car is not None
        if action is not None:
            if self.continuous:
                action = action.astype(np.float64)
                self.car.steer(-action[0])
                self.car.gas(action[1])
                self.car.brake(action[2])
            else:
                if not self.action_space.contains(action):
                    raise InvalidAction(
                        f"you passed the invalid action `{action}`. "
                        f"The supported action_space is `{self.action_space}`"
                    )
                self.car.steer(-0.6 * (action == 1) + 0.6 * (action == 2))
                self.car.gas(0.2 * (action == 3))
                self.car.brake(0.8 * (action == 4))

System info

No response

Additional context

No response

Checklist

pseudo-rnd-thoughts commented 3 weeks ago

Could you make a PR fixing this along with a test about it?