Skip to content

Commit

Permalink
fix: address comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
zepfred committed Nov 14, 2024
1 parent b64318c commit 7c5ec1c
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 80 deletions.
2 changes: 1 addition & 1 deletion core/src/build/revapi-differences.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"annotationType": "jakarta.xml.bind.annotation.XmlType",
"attribute": "propOrder",
"oldValue": "{\"terminationClass\", \"terminationCompositionStyle\", \"spentLimit\", \"millisecondsSpentLimit\", \"secondsSpentLimit\", \"minutesSpentLimit\", \"hoursSpentLimit\", \"daysSpentLimit\", \"unimprovedSpentLimit\", \"unimprovedMillisecondsSpentLimit\", \"unimprovedSecondsSpentLimit\", \"unimprovedMinutesSpentLimit\", \"unimprovedHoursSpentLimit\", \"unimprovedDaysSpentLimit\", \"unimprovedScoreDifferenceThreshold\", \"bestScoreLimit\", \"bestScoreFeasible\", \"stepCountLimit\", \"unimprovedStepCountLimit\", \"scoreCalculationCountLimit\", \"terminationConfigList\"}",
"newValue": "{\"terminationClass\", \"terminationCompositionStyle\", \"spentLimit\", \"millisecondsSpentLimit\", \"secondsSpentLimit\", \"minutesSpentLimit\", \"hoursSpentLimit\", \"daysSpentLimit\", \"unimprovedSpentLimit\", \"unimprovedMillisecondsSpentLimit\", \"unimprovedSecondsSpentLimit\", \"unimprovedMinutesSpentLimit\", \"unimprovedHoursSpentLimit\", \"unimprovedDaysSpentLimit\", \"unimprovedScoreDifferenceThreshold\", \"bestScoreLimit\", \"bestScoreFeasible\", \"stepCountLimit\", \"unimprovedStepCountLimit\", \"scoreCalculationCountLimit\", \"moveCountLimit\", \"flatLineDetectionRatio\", \"newCurveDetectionRatio\", \"minimalExecutionTimeSeconds\", \"terminationConfigList\"}",
"newValue": "{\"terminationClass\", \"terminationCompositionStyle\", \"spentLimit\", \"millisecondsSpentLimit\", \"secondsSpentLimit\", \"minutesSpentLimit\", \"hoursSpentLimit\", \"daysSpentLimit\", \"unimprovedSpentLimit\", \"unimprovedMillisecondsSpentLimit\", \"unimprovedSecondsSpentLimit\", \"unimprovedMinutesSpentLimit\", \"unimprovedHoursSpentLimit\", \"unimprovedDaysSpentLimit\", \"unimprovedScoreDifferenceThreshold\", \"bestScoreLimit\", \"bestScoreFeasible\", \"stepCountLimit\", \"unimprovedStepCountLimit\", \"scoreCalculationCountLimit\", \"moveCountLimit\", \"stopFlatLineDetectionRatio\", \"noStopFlatLineDetectionRatio\", \"minimalExecutionTimeSeconds\", \"terminationConfigList\"}",
"justification": "Add new termination config"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"unimprovedStepCountLimit",
"scoreCalculationCountLimit",
"moveCountLimit",
"flatLineDetectionRatio",
"newCurveDetectionRatio",
"stopFlatLineDetectionRatio",
"noStopFlatLineDetectionRatio",
"minimalExecutionTimeSeconds",
"terminationConfigList"
})
Expand Down Expand Up @@ -81,8 +81,8 @@ public class TerminationConfig extends AbstractConfig<TerminationConfig> {

private Long moveCountLimit = null;

private Double flatLineDetectionRatio = null;
private Double newCurveDetectionRatio = null;
private Double stopFlatLineDetectionRatio = null;
private Double noStopFlatLineDetectionRatio = null;
private Long minimalExecutionTimeSeconds = null;

@XmlElement(name = "termination")
Expand Down Expand Up @@ -264,20 +264,20 @@ public void setMoveCountLimit(@Nullable Long moveCountLimit) {
this.moveCountLimit = moveCountLimit;
}

public @Nullable Double getFlatLineDetectionRatio() {
return flatLineDetectionRatio;
public @Nullable Double getStopFlatLineDetectionRatio() {
return stopFlatLineDetectionRatio;
}

public void setFlatLineDetectionRatio(@Nullable Double flatLineDetectionRatio) {
this.flatLineDetectionRatio = flatLineDetectionRatio;
public void setStopFlatLineDetectionRatio(@Nullable Double stopFlatLineDetectionRatio) {
this.stopFlatLineDetectionRatio = stopFlatLineDetectionRatio;
}

public @Nullable Double getNewCurveDetectionRatio() {
return newCurveDetectionRatio;
public @Nullable Double getNoStopFlatLineDetectionRatio() {
return noStopFlatLineDetectionRatio;
}

public void setNewCurveDetectionRatio(@Nullable Double newCurveDetectionRatio) {
this.newCurveDetectionRatio = newCurveDetectionRatio;
public void setNoStopFlatLineDetectionRatio(@Nullable Double noStopFlatLineDetectionRatio) {
this.noStopFlatLineDetectionRatio = noStopFlatLineDetectionRatio;
}

public @Nullable Long getMinimalExecutionTimeSeconds() {
Expand Down Expand Up @@ -411,13 +411,13 @@ public TerminationConfig withTerminationClass(Class<? extends Termination> termi
return this;
}

public @NonNull TerminationConfig withFlatLineDetectionRatio(@NonNull Double flatLineDetectionRatio) {
this.flatLineDetectionRatio = flatLineDetectionRatio;
public @NonNull TerminationConfig withStopFlatLineDetectionRatio(@NonNull Double stopFlatLineDetectionRatio) {
this.stopFlatLineDetectionRatio = stopFlatLineDetectionRatio;
return this;
}

public @NonNull TerminationConfig withNewCurveDetectionRatio(@NonNull Double newCurveDetectionRatio) {
this.newCurveDetectionRatio = newCurveDetectionRatio;
public @NonNull TerminationConfig withNoStopFlatLineDetectionRatio(@NonNull Double noStopFlatLineDetectionRatio) {
this.noStopFlatLineDetectionRatio = noStopFlatLineDetectionRatio;
return this;
}

Expand Down Expand Up @@ -535,8 +535,8 @@ public boolean isConfigured() {
unimprovedStepCountLimit != null ||
scoreCalculationCountLimit != null ||
moveCountLimit != null ||
flatLineDetectionRatio != null ||
newCurveDetectionRatio != null ||
stopFlatLineDetectionRatio != null ||
noStopFlatLineDetectionRatio != null ||
minimalExecutionTimeSeconds != null ||
isTerminationListConfigured();
}
Expand Down Expand Up @@ -578,10 +578,10 @@ private boolean isTerminationListConfigured() {
inheritedConfig.getScoreCalculationCountLimit());
moveCountLimit = ConfigUtils.inheritOverwritableProperty(moveCountLimit,
inheritedConfig.getMoveCountLimit());
flatLineDetectionRatio = ConfigUtils.inheritOverwritableProperty(flatLineDetectionRatio,
inheritedConfig.getFlatLineDetectionRatio());
newCurveDetectionRatio = ConfigUtils.inheritOverwritableProperty(newCurveDetectionRatio,
inheritedConfig.getNewCurveDetectionRatio());
stopFlatLineDetectionRatio = ConfigUtils.inheritOverwritableProperty(stopFlatLineDetectionRatio,
inheritedConfig.getStopFlatLineDetectionRatio());
noStopFlatLineDetectionRatio = ConfigUtils.inheritOverwritableProperty(noStopFlatLineDetectionRatio,
inheritedConfig.getNoStopFlatLineDetectionRatio());
minimalExecutionTimeSeconds = ConfigUtils.inheritOverwritableProperty(minimalExecutionTimeSeconds,
inheritedConfig.getMinimalExecutionTimeSeconds());
terminationConfigList = ConfigUtils.inheritMergeableListConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ public <Score_ extends Score<Score_>> Termination<Solution_> buildTermination(
if (terminationConfig.getMoveCountLimit() != null) {
terminationList.add(new MoveCountTermination<>(terminationConfig.getMoveCountLimit()));
}
if (terminationConfig.getFlatLineDetectionRatio() != null) {
terminationList.add(new UnimprovedBestSolutionTermination<>(terminationConfig.getFlatLineDetectionRatio(),
terminationConfig.getNewCurveDetectionRatio(), terminationConfig.getMinimalExecutionTimeSeconds()));
if (terminationConfig.getStopFlatLineDetectionRatio() != null
|| terminationConfig.getNoStopFlatLineDetectionRatio() != null
|| terminationConfig.getMinimalExecutionTimeSeconds() != null) {
terminationList.add(new UnimprovedBestSolutionTermination<>(terminationConfig.getStopFlatLineDetectionRatio(),
terminationConfig.getNoStopFlatLineDetectionRatio(), terminationConfig.getMinimalExecutionTimeSeconds()));
}
terminationList.addAll(buildInnerTermination(configPolicy));
return buildTerminationFromList(terminationList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public final class UnimprovedBestSolutionTermination<Solution_> extends Abstract
// that is allowed without any improvements since the last best solution was identified.
// For example, if the last solution was found at 10 seconds and the setting is configured to 0.5,
// the solver will stop if no improvement is made within 5 seconds.
private final double flatLineDetectionRatio;
// This criterion functions similarly to the flatLineDetectionRatio,
private final double stopFlatLineDetectionRatio;
// This criterion functions similarly to the stopFlatLineDetectionRatio,
// as it is also used to identify periods without improvement.
// However, the key difference is that it focuses on detecting "flat lines" between solution improvements.
// When a flat line is detected after the solution has improved,
// it indicates that the previous duration was not enough to terminate the process.
// However, it also indicates that the solver will begin
// it indicates that the previous duration was not enough to terminate the process,
// but it indicates that the solver will begin
// re-evaluating the termination criterion from the last improvement before the recent improvement.
private final double newCurveDetectionRatio;
private final double noStopFlatLineDetectionRatio;
private final Clock clock;
// The field stores the time of the first best solution of the current curve.
// If a solving process involves multiple curves,
Expand All @@ -37,33 +37,33 @@ public final class UnimprovedBestSolutionTermination<Solution_> extends Abstract
protected boolean waitForFirstBestScore;
protected Boolean terminate;

public UnimprovedBestSolutionTermination(Double flatLineDetectionRatio,
Double newCurveDetectionRatio, Long minimalExecutionTimeSeconds) {
this(flatLineDetectionRatio, newCurveDetectionRatio, minimalExecutionTimeSeconds, Clock.systemUTC());
public UnimprovedBestSolutionTermination(Double stopFlatLineDetectionRatio,
Double noStopFlatLineDetectionRatio, Long minimalExecutionTimeSeconds) {
this(stopFlatLineDetectionRatio, noStopFlatLineDetectionRatio, minimalExecutionTimeSeconds, Clock.systemUTC());
}

public UnimprovedBestSolutionTermination(Double flatLineDetectionRatio, Double newCurveDetectionRatio,
Long minimalExecutionTimeSeconds, Clock clock) {
this.flatLineDetectionRatio = Objects.requireNonNull(flatLineDetectionRatio,
"The field flatLineDetectionRatio is required for the termination UnimprovedBestSolutionTermination");
this.newCurveDetectionRatio = Objects.requireNonNull(newCurveDetectionRatio,
"The field newCurveDetectionRatio is required for the termination UnimprovedBestSolutionTermination");
public UnimprovedBestSolutionTermination(Double stopFlatLineDetectionRatio, Double noStopFlatLineDetectionRatio,
Long minimalExecutionTimeSeconds, Clock clock) {
this.stopFlatLineDetectionRatio = Objects.requireNonNull(stopFlatLineDetectionRatio,
"The field stopFlatLineDetectionRatio is required for the termination UnimprovedBestSolutionTermination");
this.noStopFlatLineDetectionRatio = Objects.requireNonNull(noStopFlatLineDetectionRatio,
"The field noStopFlatLineDetectionRatio is required for the termination UnimprovedBestSolutionTermination");
this.minimalExecutionTimeMillis = Objects.requireNonNull(minimalExecutionTimeSeconds,
"The field minimalExecutionTimeSeconds is required for the termination UnimprovedBestSolutionTermination")
* 1000L;
this.clock = Objects.requireNonNull(clock);
if (flatLineDetectionRatio < 0) {
if (stopFlatLineDetectionRatio < 0) {
throw new IllegalArgumentException(
"The flatLineDetectionRatio (%.2f) cannot be negative.".formatted(flatLineDetectionRatio));
"The stopFlatLineDetectionRatio (%.2f) cannot be negative.".formatted(stopFlatLineDetectionRatio));
}
if (newCurveDetectionRatio < 0) {
if (noStopFlatLineDetectionRatio < 0) {
throw new IllegalArgumentException(
"The newCurveDetectionRatio (%.2f) cannot be negative.".formatted(newCurveDetectionRatio));
"The noStopFlatLineDetectionRatio (%.2f) cannot be negative.".formatted(noStopFlatLineDetectionRatio));
}
if (newCurveDetectionRatio > flatLineDetectionRatio) {
if (noStopFlatLineDetectionRatio > stopFlatLineDetectionRatio) {
throw new IllegalArgumentException(
"The newCurveDetectionRatio (%.2f) cannot be greater than flatLineDetectionRatio (%.2f)."
.formatted(newCurveDetectionRatio, flatLineDetectionRatio));
"The noStopFlatLineDetectionRatio (%.2f) cannot be greater than stopFlatLineDetectionRatio (%.2f)."
.formatted(noStopFlatLineDetectionRatio, stopFlatLineDetectionRatio));
}
if (minimalExecutionTimeSeconds <= 0) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -130,8 +130,8 @@ public boolean isPhaseTerminated(AbstractPhaseScope<Solution_> phaseScope) {
// If there is a flat line between the last and new best solutions,
// the initial value becomes the most recent best score,
// as it would be the starting point for the new curve.
var minInterval = Math.floor(lastImprovementInterval * newCurveDetectionRatio);
var maxInterval = Math.floor(lastImprovementInterval * flatLineDetectionRatio);
var minInterval = Math.floor(lastImprovementInterval * noStopFlatLineDetectionRatio);
var maxInterval = Math.floor(lastImprovementInterval * stopFlatLineDetectionRatio);
if (lastImprovementMillis > 0 && completeInterval >= minimalExecutionTimeMillis && newInterval > minInterval
&& newInterval < maxInterval) {
initialCurvePointMillis = lastImprovementMillis;
Expand All @@ -150,7 +150,7 @@ public boolean isPhaseTerminated(AbstractPhaseScope<Solution_> phaseScope) {
if (completeInterval < minimalExecutionTimeMillis) {
return false;
}
var maxInterval = Math.floor(lastImprovementInterval * flatLineDetectionRatio);
var maxInterval = Math.floor(lastImprovementInterval * stopFlatLineDetectionRatio);
if (newInterval > maxInterval) {
terminate = true;
return true;
Expand Down Expand Up @@ -185,12 +185,12 @@ public double calculatePhaseTimeGradient(AbstractPhaseScope<Solution_> phaseScop
@Override
public UnimprovedBestSolutionTermination<Solution_> createChildThreadTermination(SolverScope<Solution_> solverScope,
ChildThreadType childThreadType) {
return new UnimprovedBestSolutionTermination<>(flatLineDetectionRatio, newCurveDetectionRatio,
return new UnimprovedBestSolutionTermination<>(stopFlatLineDetectionRatio, noStopFlatLineDetectionRatio,
minimalExecutionTimeMillis, clock);
}

@Override
public String toString() {
return "UnimprovedBestSolutionTermination(%.2f, %.2f)".formatted(flatLineDetectionRatio, newCurveDetectionRatio);
return "UnimprovedBestSolutionTermination(%.2f, %.2f)".formatted(stopFlatLineDetectionRatio, noStopFlatLineDetectionRatio);
}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7c5ec1c

Please sign in to comment.