Skip to content

Commit 0a8ca89

Browse files
salim4nclaude
andcommitted
fix(demo-car-circuit): lap bonus fires only once per crossing
The reward function was giving +50 every step near the start line after completing a lap, causing the agent to learn to circle at the start. Now uses a justCompletedLap flag that fires only on the exact step the car crosses from progress > 0.9 to < 0.1. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d51d444 commit 0a8ca89

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎packages/demo-car-circuit/src/circuit-env.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class CircuitEnv implements TrainingEnv {
1919
laps = 0;
2020
private lastProgress = 0;
2121
private offTrack = false;
22+
private justCompletedLap = false;
2223

2324
constructor(straightLen = 10, radius = 4, halfWidth = 2) {
2425
this.track = new OvalTrack(straightLen, radius, halfWidth);
@@ -57,10 +58,12 @@ export class CircuitEnv implements TrainingEnv {
5758
// Check track status
5859
this.offTrack = !this.track.isOnTrack(this.carX, this.carY);
5960

60-
// Check lap completion
61+
// Check lap completion — fire bonus only once per lap crossing
62+
this.justCompletedLap = false;
6163
const result = this.track.nearestPoint(this.carX, this.carY);
6264
if (result.progress < 0.1 && this.lastProgress > 0.9) {
6365
this.laps++;
66+
this.justCompletedLap = true;
6467
}
6568
this.lastProgress = result.progress;
6669

@@ -69,7 +72,7 @@ export class CircuitEnv implements TrainingEnv {
6972

7073
reward(): number {
7174
if (this.offTrack) return OFF_TRACK_PENALTY;
72-
if (this.laps > 0 && this.lastProgress < 0.1) return LAP_BONUS;
75+
if (this.justCompletedLap) return LAP_BONUS;
7376
return 1; // +1 per step on track
7477
}
7578

@@ -88,6 +91,7 @@ export class CircuitEnv implements TrainingEnv {
8891
this.laps = 0;
8992
this.lastProgress = 0;
9093
this.offTrack = false;
94+
this.justCompletedLap = false;
9195
}
9296

9397
private normalizeAngle(a: number): number {

0 commit comments

Comments
 (0)