Skip to content

Commit a3619ca

Browse files
committed
chore: address comments
1 parent 2255f55 commit a3619ca

File tree

5 files changed

+9
-9
lines changed

5 files changed

+9
-9
lines changed

core/src/main/java/ai/timefold/solver/core/impl/heuristic/selector/move/decorator/FilteringMoveSelector.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ private long determineBailOutSize() {
100100

101101
private class JustInTimeFilteringMoveIterator extends UpcomingSelectionIterator<Move<Solution_>> {
102102

103+
private final long TERMINATION_BAIL_OUT_SIZE = 1000L;
103104
private final Iterator<Move<Solution_>> childMoveIterator;
104105
private final long bailOutSize;
105106
private final AbstractPhaseScope<Solution_> phaseScope;
@@ -118,9 +119,8 @@ protected Move<Solution_> createUpcomingSelection() {
118119
Move<Solution_> next;
119120
long attemptsBeforeBailOut = bailOutSize;
120121
// To reduce the impact of checking for termination on each move,
121-
// we only check for termination
122-
// after filtering out a total number of moves equal to size(childMoveSelector).
123-
long attemptsBeforeCheckTermination = bailOutSize / BAIL_OUT_MULTIPLIER;
122+
// we only check for termination after filtering out 1000 moves.
123+
long attemptsBeforeCheckTermination = TERMINATION_BAIL_OUT_SIZE;
124124
do {
125125
if (!childMoveIterator.hasNext()) {
126126
return noUpcomingSelection();
@@ -133,7 +133,7 @@ protected Move<Solution_> createUpcomingSelection() {
133133
return noUpcomingSelection();
134134
} else if (termination != null && attemptsBeforeCheckTermination <= 0L) {
135135
// Reset the counter
136-
attemptsBeforeCheckTermination = bailOutSize / BAIL_OUT_MULTIPLIER;
136+
attemptsBeforeCheckTermination = TERMINATION_BAIL_OUT_SIZE;
137137
if (termination.isPhaseTerminated(phaseScope)) {
138138
logger.trace(
139139
"Bailing out of neverEnding selector ({}) because the termination setting has been triggered.",

core/src/main/java/ai/timefold/solver/core/impl/localsearch/DefaultLocalSearchPhase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public void phaseStarted(LocalSearchPhaseScope<Solution_> phaseScope) {
125125
super.phaseStarted(phaseScope);
126126
decider.phaseStarted(phaseScope);
127127
assertWorkingSolutionInitialized(phaseScope);
128-
phaseScope.setTermination(phaseTermination);
129128
}
130129

131130
@Override

core/src/main/java/ai/timefold/solver/core/impl/phase/AbstractPhase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public void phaseStarted(AbstractPhaseScope<Solution_> phaseScope) {
9595
solver.phaseStarted(phaseScope);
9696
}
9797
phaseTermination.phaseStarted(phaseScope);
98+
phaseScope.setTermination(phaseTermination);
9899
phaseLifecycleSupport.firePhaseStarted(phaseScope);
99100
}
100101

core/src/main/java/ai/timefold/solver/core/impl/phase/scope/AbstractPhaseScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class AbstractPhaseScope<Solution_> {
3838
protected int bestSolutionStepIndex;
3939

4040
/**
41-
* The solver termination configuration
41+
* The phase termination configuration
4242
*/
4343
private PhaseTermination<Solution_> termination;
4444

core/src/test/java/ai/timefold/solver/core/impl/heuristic/selector/move/decorator/FilteringMoveSelectorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void bailOutByTermination() {
5252
var moveSelector = mock(MoveSelector.class);
5353
var termination = mock(BasicPlumbingTermination.class);
5454
var iterator = mock(Iterator.class);
55-
when(moveSelector.getSize()).thenReturn(100L);
55+
when(moveSelector.getSize()).thenReturn(1000L);
5656
when(moveSelector.isNeverEnding()).thenReturn(true);
5757
when(moveSelector.iterator()).thenReturn(iterator);
5858
when(iterator.hasNext()).thenReturn(true);
@@ -61,8 +61,8 @@ void bailOutByTermination() {
6161
var filteredMoveSelector = FilteringMoveSelector.of(moveSelector, (scoreDirector, selection) -> false);
6262
filteredMoveSelector.phaseStarted(phaseScope);
6363
assertThat(filteredMoveSelector.iterator().hasNext()).isFalse();
64-
// The termination returns true at the second call, and only (100 * 10) * 30% calls are executed per check
65-
verify(iterator, times(200)).next();
64+
// The termination returns true at the second call, and 2000 calls are executed in total
65+
verify(iterator, times(2000)).next();
6666
}
6767

6868
public void filter(SelectionCacheType cacheType, int timesCalled) {

0 commit comments

Comments
 (0)