Skip to content

Commit

Permalink
Fix CarRacing termination
Browse files Browse the repository at this point in the history
  • Loading branch information
araffin committed Jun 14, 2022
1 parent 053ee80 commit 3e37382
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions gym/envs/box2d/car_racing.py
Expand Up @@ -88,7 +88,7 @@ def _contact(self, contact, begin):
and self.env.tile_visited_count / len(self.env.track)
> self.lap_complete_percent
):
self.env_new_lap = True
self.env.new_lap = True
else:
obj.tiles.remove(tile)

Expand Down Expand Up @@ -484,6 +484,7 @@ def step(self, action: Union[np.ndarray, int]):

step_reward = 0
done = False
info = {}
if action is not None: # First step without action, called from reset()
self.reward -= 0.1
# We actually don't want to count fuel spent, we want car to be faster.
Expand All @@ -493,13 +494,17 @@ def step(self, action: Union[np.ndarray, int]):
self.prev_reward = self.reward
if self.tile_visited_count == len(self.track) or self.new_lap:
done = True
# Termination due to finishing lap
# This should not be treated as a failure
# but like a timeout
info["TimeLimit.truncated"] = True
x, y = self.car.hull.position
if abs(x) > PLAYFIELD or abs(y) > PLAYFIELD:
done = True
step_reward = -100

self.renderer.render_step()
return self.state, step_reward, done, {}
return self.state, step_reward, done, info

def render(self, mode: str = "human"):
if self.render_mode is not None:
Expand Down

0 comments on commit 3e37382

Please sign in to comment.