Skip to content

Commit

Permalink
fix: reset grace when init score changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Chianelli committed Jan 16, 2025
1 parent 2c0f1e1 commit 1294d64
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ private static <Score_ extends Score<Score_>> double softImprovementOrNaNForHard
// vast majority of comparisons
return 0.0;
}
if (start.initScore() != end.initScore()) {
// init score improved
return Double.NaN;
}
var scoreDiffs = end.subtract(start).toLevelDoubles();
var softestLevel = scoreDiffs.length - 1;
for (int i = 0; i < softestLevel; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,49 @@ void testTerminatesWhenImprovementDoesNotMeetCriteria() {
assertThat(termination.isTerminated(31 * NANOS_PER_MILLISECOND, score)).isTrue();
}

@Test
void testImprovementInInitScoreResetsGrace() {
var termination = new AdaptiveTermination<Object, SimpleScore>(10, 1);

var score = SimpleScore.ofUninitialized(-1, 0);
termination.start(0L, score);

score = SimpleScore.ofUninitialized(-1, 1);
assertThat(termination.isTerminated(10 * NANOS_PER_MILLISECOND, score)).isFalse();
// Y_0 is 1 - 0 = 1

score = SimpleScore.ofUninitialized(-1, 2);
termination.step(11 * NANOS_PER_MILLISECOND, score);

// These will compare as 2 - 1 = 1, 1 / 1 >= 1
assertThat(termination.isTerminated(11 * NANOS_PER_MILLISECOND, score)).isFalse();
assertThat(termination.isTerminated(12 * NANOS_PER_MILLISECOND, score)).isFalse();
assertThat(termination.isTerminated(15 * NANOS_PER_MILLISECOND, score)).isFalse();
assertThat(termination.isTerminated(19 * NANOS_PER_MILLISECOND, score)).isFalse();
assertThat(termination.isTerminated(20 * NANOS_PER_MILLISECOND, score)).isFalse();

score = SimpleScore.of(2);
termination.step(21 * NANOS_PER_MILLISECOND, score);

// This will reset the grace period
assertThat(termination.isTerminated(21 * NANOS_PER_MILLISECOND, score)).isFalse();
assertThat(termination.isTerminated(30 * NANOS_PER_MILLISECOND, score)).isFalse();

score = SimpleScore.of(4);
termination.step(31 * NANOS_PER_MILLISECOND, score);

// Y_0 is 4 - 2 = 2

// These will compare as 4 - 2 = 2, 2 / 2 >= 1
assertThat(termination.isTerminated(31 * NANOS_PER_MILLISECOND, score)).isFalse();
assertThat(termination.isTerminated(40 * NANOS_PER_MILLISECOND, score)).isFalse();

score = SimpleScore.of(5);
termination.step(31 * NANOS_PER_MILLISECOND, score);
// This will compare as 5 - 4 = 1, 1 / 2 < 1, so terminate
assertThat(termination.isTerminated(41 * NANOS_PER_MILLISECOND, score)).isTrue();
}

@Test
void testImprovementInHardScoreResetsGrace() {
var termination = new AdaptiveTermination<Object, HardSoftScore>(10, 1);
Expand Down Expand Up @@ -142,4 +185,32 @@ void testImprovementInHardScoreDuringGrace() {
// These will compare as 1 - 0 = 1, 1 / 2 < 2, so terminate
assertThat(termination.isTerminated(20 * NANOS_PER_MILLISECOND, score)).isTrue();
}

@Test
void testImprovementInInitScoreDuringGrace() {
var termination = new AdaptiveTermination<Object, SimpleScore>(10, 1);

var score = SimpleScore.ofUninitialized(-1, 0);
termination.start(0, score);

score = SimpleScore.of(-1);
termination.step(5 * NANOS_PER_MILLISECOND, score);
assertThat(termination.isTerminated(5 * NANOS_PER_MILLISECOND, score)).isFalse();

score = SimpleScore.of(0);
termination.step(10 * NANOS_PER_MILLISECOND, score);
assertThat(termination.isTerminated(10 * NANOS_PER_MILLISECOND, score)).isFalse();

score = SimpleScore.of(1);
termination.step(15 * NANOS_PER_MILLISECOND, score);
assertThat(termination.isTerminated(15 * NANOS_PER_MILLISECOND, score)).isFalse();

// Y_0 is 1 - (-1) = 2
// These will compare as 1 - (-1) = 2, 2 / 2 >= 2
assertThat(termination.isTerminated(16 * NANOS_PER_MILLISECOND, score)).isFalse();
assertThat(termination.isTerminated(19 * NANOS_PER_MILLISECOND, score)).isFalse();

// These will compare as 1 - 0 = 1, 1 / 2 < 2, so terminate
assertThat(termination.isTerminated(20 * NANOS_PER_MILLISECOND, score)).isTrue();
}
}

0 comments on commit 1294d64

Please sign in to comment.