Skip to content

Commit

Permalink
chore: address sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
zepfred committed Nov 28, 2024
1 parent 67f5006 commit 615461f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public UnimprovedBestSolutionTermination(Double stopFlatLineDetectionRatio, Doub
}
}

public long getMinimalExecutionTimeMillis() {
return minimalExecutionTimeMillis;
}

public double getStopFlatLineDetectionRatio() {
return stopFlatLineDetectionRatio;
}

public double getNoStopFlatLineDetectionRatio() {
return noStopFlatLineDetectionRatio;
}

// ************************************************************************
// Lifecycle methods
// ************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ void childWithTimeSpentLimitShouldNotInheritTimeSpentLimitFromParent() {
assertThat(child.getMinutesSpentLimit()).isNull();
}

@Test
void childWithUnimprovedPropertiesFromParent() {
TerminationConfig child = new TerminationConfig();
TerminationConfig parent = new TerminationConfig()
.withStopFlatLineDetectionRatio(0.5)
.withNoStopFlatLineDetectionRatio(0.1)
.withMinimalExecutionTimeSeconds(10L);
child.inherit(parent);
assertThat(child.getStopFlatLineDetectionRatio()).isEqualTo(0.5);
assertThat(child.getNoStopFlatLineDetectionRatio()).isEqualTo(0.1);
assertThat(child.getMinimalExecutionTimeSeconds()).isEqualTo(10L);
}

@Test
void checkMoveCountMetrics() {
TerminationConfig parent = new TerminationConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,55 @@ void bestScoreFeasible_requiresAtLeastOneFeasibleLevel() {
.isThrownBy(() -> terminationFactory.buildTermination(heuristicConfigPolicy))
.withMessageContaining("can only be used with a score type that has at least 1 feasible level");
}

@Test
void buildBestScoreFeasible() {
var heuristicConfigPolicy = mock(HeuristicConfigPolicy.class);
when(heuristicConfigPolicy.getScoreDefinition()).thenReturn(new HardSoftScoreDefinition());
var terminationConfig = new TerminationConfig();
terminationConfig.setBestScoreFeasible(true);
var termination = TerminationFactory.create(terminationConfig).buildTermination(heuristicConfigPolicy);
assertThat(termination)
.isInstanceOf(BestScoreFeasibleTermination.class);
}

@Test
void buildStepCountLimit() {
var heuristicConfigPolicy = mock(HeuristicConfigPolicy.class);
when(heuristicConfigPolicy.getScoreDefinition()).thenReturn(new HardSoftScoreDefinition());
var terminationConfig = new TerminationConfig();
terminationConfig.setStepCountLimit(1);
var termination = TerminationFactory.create(terminationConfig).buildTermination(heuristicConfigPolicy);
assertThat(termination)
.isInstanceOf(StepCountTermination.class);
}

@Test
void buildUnimprovedStepCountLimit() {
var heuristicConfigPolicy = mock(HeuristicConfigPolicy.class);
when(heuristicConfigPolicy.getScoreDefinition()).thenReturn(new HardSoftScoreDefinition());
var terminationConfig = new TerminationConfig();
terminationConfig.withUnimprovedStepCountLimit(1);
var termination = TerminationFactory.create(terminationConfig).buildTermination(heuristicConfigPolicy);
assertThat(termination)
.isInstanceOf(UnimprovedStepCountTermination.class);
}

@Test
void buildUnimprovedBestScoreRatio() {
var terminationConfig = new TerminationConfig();
terminationConfig.setStopFlatLineDetectionRatio(0.5);
terminationConfig.setNoStopFlatLineDetectionRatio(0.1);
terminationConfig.setMinimalExecutionTimeSeconds(10L);
var termination = TerminationFactory.create(terminationConfig)
.buildTermination(mock(HeuristicConfigPolicy.class));
assertThat(termination)
.isInstanceOf(UnimprovedBestSolutionTermination.class);
assertThat(((UnimprovedBestSolutionTermination<?>) termination).getStopFlatLineDetectionRatio())
.isEqualTo(0.5);
assertThat(((UnimprovedBestSolutionTermination<?>) termination).getNoStopFlatLineDetectionRatio())
.isEqualTo(0.1);
assertThat(((UnimprovedBestSolutionTermination<?>) termination).getMinimalExecutionTimeMillis())
.isEqualTo(10000L);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void testTermination() {
when(clock.millis()).thenReturn(currentTime + 14_000);
assertThat(termination.isPhaseTerminated(phaseScope)).isFalse();

assertThat(termination.calculatePhaseTimeGradient(phaseScope)).isEqualTo(-1.0);
}

@Test
Expand Down Expand Up @@ -125,5 +126,8 @@ void invalidTermination() {
.isThrownBy(() -> new UnimprovedBestSolutionTermination<TestdataSolution>(0.0, 1.0, 1L));
assertThatIllegalArgumentException()
.isThrownBy(() -> new UnimprovedBestSolutionTermination<TestdataSolution>(1.0, 1.0, 0L));
assertThatIllegalArgumentException()
.isThrownBy(() -> new UnimprovedBestSolutionTermination<TestdataSolution>(0.1, 1.0, 1L)
.calculateSolverTimeGradient(null));
}
}

0 comments on commit 615461f

Please sign in to comment.