Skip to content

Commit 935c769

Browse files
committed
fix: adjust failing tests
1 parent 01399e0 commit 935c769

File tree

4 files changed

+38
-57
lines changed

4 files changed

+38
-57
lines changed

core/src/main/java/ai/timefold/solver/core/impl/localsearch/decider/acceptor/RestartableAcceptor.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ai.timefold.solver.core.impl.localsearch.decider.acceptor;
22

33
import ai.timefold.solver.core.impl.localsearch.decider.acceptor.stuckcriterion.StuckCriterion;
4-
import ai.timefold.solver.core.impl.localsearch.scope.LocalSearchMoveScope;
54
import ai.timefold.solver.core.impl.localsearch.scope.LocalSearchPhaseScope;
65
import ai.timefold.solver.core.impl.localsearch.scope.LocalSearchStepScope;
76
import ai.timefold.solver.core.impl.solver.scope.SolverScope;
@@ -60,14 +59,6 @@ public void stepEnded(LocalSearchStepScope<Solution_> stepScope) {
6059
stuckCriterion.stepEnded(stepScope);
6160
}
6261

63-
@Override
64-
public boolean isAccepted(LocalSearchMoveScope<Solution_> moveScope) {
65-
66-
return accept(moveScope);
67-
}
68-
69-
protected abstract boolean accept(LocalSearchMoveScope<Solution_> moveScope);
70-
7162
/**
7263
* The stuck criterion may trigger a restart event, but the acceptor might choose to delay it.
7364
* This method rechecks the restart event condition on the acceptor side.

core/src/main/java/ai/timefold/solver/core/impl/localsearch/decider/acceptor/lateacceptance/LateAcceptanceAcceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private void validate() {
8282

8383
@Override
8484
@SuppressWarnings("unchecked")
85-
public boolean accept(LocalSearchMoveScope<Solution_> moveScope) {
85+
public boolean isAccepted(LocalSearchMoveScope<Solution_> moveScope) {
8686
var moveScore = moveScope.getScore();
8787
if (moveScore.compareTo(moveScope.getStepScope().getPhaseScope().getBestScore()) != 0
8888
&& (bestStepScore == null || moveScore.compareTo(bestStepScore) > 0)) {

core/src/test/java/ai/timefold/solver/core/impl/localsearch/decider/acceptor/lateacceptance/LateAcceptanceAcceptorTest.java

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,10 @@ void negativeLateAcceptanceSize() {
268268
assertThatIllegalArgumentException().isThrownBy(() -> acceptor.phaseStarted(null));
269269
}
270270

271-
@Test
272-
void triggerRestart() {
273-
var stuckCriterion = mock(StuckCriterion.class);
274-
when(stuckCriterion.isSolverStuck(any())).thenReturn(true);
275-
var acceptor = new LateAcceptanceAcceptor<>(stuckCriterion);
276-
acceptor.setLateAcceptanceSize(3);
277-
var solverScope = new SolverScope<>();
278-
var phaseScope = new LocalSearchPhaseScope<>(solverScope, 0);
279-
var stepScope0 = new LocalSearchStepScope<>(phaseScope);
280-
var moveScope0 = buildMoveScope(stepScope0, -2000);
281-
assertThat(acceptor.isAccepted(moveScope0)).isTrue();
282-
assertThat(phaseScope.isSolverStuck()).isTrue();
283-
}
284-
285271
@Test
286272
void delayRestart() {
287273
var stuckCriterion = mock(StuckCriterion.class);
288274
var acceptor = new LateAcceptanceAcceptor<>(stuckCriterion);
289-
var restartStrategy = new AcceptorRestartStrategy(acceptor);
290275
acceptor.setLateAcceptanceSize(5);
291276
var solverScope = new SolverScope<>();
292277
var phaseScope = new LocalSearchPhaseScope<>(solverScope, 0);
@@ -295,65 +280,69 @@ void delayRestart() {
295280
var moveScope0 = buildMoveScope(stepScope0, -3000);
296281
phaseScope.setLastCompletedStepScope(stepScope0);
297282
solverScope.setBestScore(SimpleScore.of(-1000));
283+
when(stuckCriterion.isSolverStuck(any())).thenReturn(true);
284+
285+
// Init
298286
acceptor.solvingStarted(solverScope);
299287
acceptor.phaseStarted(phaseScope);
300288
acceptor.stepStarted(stepScope0);
301289
assertThat(acceptor.isAccepted(moveScope0)).isFalse();
302290

303-
// Delay because there aren't enough top scores in the queue
304-
assertThat(acceptor.bestScoreQueue.size()).isOne();
305-
restartStrategy.applyRestart(stepScope0);
306-
assertThat(acceptor.coefficient).isZero();
307-
308-
// Delay because the diversity is still high on the first event
291+
// Delay because the diversity is still high
309292
acceptor.bestScoreQueue.addLast(SimpleScore.of(-999));
310293
acceptor.bestScoreQueue.addLast(SimpleScore.of(-998));
311294
acceptor.previousScores[0] = SimpleScore.of(-1002);
312295
acceptor.previousScores[1] = SimpleScore.of(-1001);
313296
acceptor.previousScores[2] = SimpleScore.of(-1000);
314297
acceptor.previousScores[3] = SimpleScore.of(-999);
315298
acceptor.previousScores[4] = SimpleScore.of(-998);
316-
restartStrategy.applyRestart(stepScope0);
317-
assertThat(acceptor.coefficient).isZero();
299+
// Check the restart event
300+
acceptor.stepEnded(stepScope0);
301+
assertThat(phaseScope.isSolverStuck()).isFalse();
302+
303+
// Delay because the best score has no improvement
304+
acceptor.previousScores[0] = SimpleScore.of(-1001);
305+
acceptor.previousScores[1] = SimpleScore.of(-1001);
306+
acceptor.previousScores[2] = SimpleScore.of(-1001);
307+
acceptor.previousScores[3] = SimpleScore.of(-1001);
308+
acceptor.previousScores[4] = SimpleScore.of(-1001);
309+
acceptor.bestScoreQueue.clear();
310+
acceptor.bestScoreQueue.addLast(SimpleScore.of(-999));
311+
acceptor.stepEnded(stepScope0);
312+
assertThat(phaseScope.isSolverStuck()).isFalse();
318313
}
319314

320315
@Test
321-
void delayOnlyFirstRestart() {
316+
void restart() {
322317
var stuckCriterion = mock(StuckCriterion.class);
323318
var acceptor = new LateAcceptanceAcceptor<>(stuckCriterion);
324-
var restartStrategy = new AcceptorRestartStrategy(acceptor);
325319
acceptor.setLateAcceptanceSize(5);
326320
var solverScope = new SolverScope<>();
327321
var phaseScope = new LocalSearchPhaseScope<>(solverScope, 0);
328322
var stepScope0 = new LocalSearchStepScope<>(phaseScope);
329323
stepScope0.setScore(SimpleScore.of(-1000));
330-
var moveScope0 = buildMoveScope(stepScope0, -3000);
331324
phaseScope.setLastCompletedStepScope(stepScope0);
332325
solverScope.setBestScore(SimpleScore.of(-1000));
326+
when(stuckCriterion.isSolverStuck(any())).thenReturn(true);
327+
328+
// Init
333329
acceptor.solvingStarted(solverScope);
334330
acceptor.phaseStarted(phaseScope);
335331
acceptor.stepStarted(stepScope0);
336-
assertThat(acceptor.isAccepted(moveScope0)).isFalse();
337332

338-
// Delay because the diversity is still high on the first event
339-
acceptor.bestScoreQueue.addLast(SimpleScore.of(-999));
340-
acceptor.bestScoreQueue.addLast(SimpleScore.of(-998));
341-
acceptor.previousScores[0] = SimpleScore.of(-1002);
333+
// Trigger the restart event as the diversity dropped
334+
// no diversity
335+
acceptor.previousScores[0] = SimpleScore.of(-1001);
342336
acceptor.previousScores[1] = SimpleScore.of(-1001);
343-
acceptor.previousScores[2] = SimpleScore.of(-1000);
344-
acceptor.previousScores[3] = SimpleScore.of(-999);
345-
acceptor.previousScores[4] = SimpleScore.of(-998);
346-
restartStrategy.applyRestart(stepScope0);
347-
assertThat(acceptor.coefficient).isZero();
337+
acceptor.previousScores[2] = SimpleScore.of(-1001);
338+
acceptor.previousScores[3] = SimpleScore.of(-1001);
339+
acceptor.previousScores[4] = SimpleScore.of(-1001);
340+
// more than one best score
341+
acceptor.bestScoreQueue.addLast(SimpleScore.of(-1001));
342+
acceptor.bestScoreQueue.addLast(SimpleScore.of(-1002));
348343

349-
// Trigger the restart event as the diversity dropped
350-
acceptor.previousScores[0] = SimpleScore.of(-1002);
351-
acceptor.previousScores[1] = SimpleScore.of(-1002);
352-
acceptor.previousScores[2] = SimpleScore.of(-1002);
353-
acceptor.previousScores[3] = SimpleScore.of(-1002);
354-
acceptor.previousScores[4] = SimpleScore.of(-1002);
355-
restartStrategy.applyRestart(stepScope0);
356-
assertThat(acceptor.coefficient).isOne();
344+
acceptor.stepEnded(stepScope0);
345+
assertThat(phaseScope.isSolverStuck()).isTrue();
357346
}
358347

359348
@Test

core/src/test/java/ai/timefold/solver/core/impl/localsearch/decider/acceptor/stuckcriterion/DiminishedReturnsStuckCriterionTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.timefold.solver.core.impl.localsearch.decider.acceptor.stuckcriterion;
22

3+
import static ai.timefold.solver.core.impl.localsearch.decider.acceptor.stuckcriterion.DiminishedReturnsStuckCriterion.REGULAR_TIME_WINDOW_MILLIS;
34
import static ai.timefold.solver.core.impl.localsearch.decider.acceptor.stuckcriterion.DiminishedReturnsStuckCriterion.START_TIME_WINDOW_MILLIS;
45
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
56
import static org.mockito.ArgumentMatchers.any;
@@ -29,7 +30,7 @@ void isSolverStuck() {
2930
when(moveScope.getStepScope()).thenReturn(stepScope);
3031
when(stepScope.getPhaseScope()).thenReturn(phaseScope);
3132
when(phaseScope.getSolverScope()).thenReturn(solverScope);
32-
when(moveScope.getScore()).thenReturn(SimpleScore.of(1));
33+
when(stepScope.getScore()).thenReturn(SimpleScore.of(1));
3334
when(phaseScope.getBestScore()).thenReturn(SimpleScore.of(1));
3435
when(termination.isTerminated(anyLong(), any())).thenReturn(false, true);
3536

@@ -59,7 +60,7 @@ void reset() {
5960
when(moveScope.getStepScope()).thenReturn(stepScope);
6061
when(stepScope.getPhaseScope()).thenReturn(phaseScope);
6162
when(phaseScope.getSolverScope()).thenReturn(solverScope);
62-
when(moveScope.getScore()).thenReturn(SimpleScore.of(1));
63+
when(stepScope.getScore()).thenReturn(SimpleScore.of(1));
6364
when(phaseScope.getBestScore()).thenReturn(SimpleScore.of(1));
6465
when(termination.isTerminated(anyLong(), any())).thenReturn(true);
6566

@@ -74,6 +75,6 @@ void reset() {
7475
strategy.stepStarted(stepScope);
7576
when(phaseScope.getBestScore()).thenReturn(SimpleScore.of(2));
7677
strategy.stepEnded(stepScope);
77-
assertThat(strategy.nextRestart).isEqualTo(START_TIME_WINDOW_MILLIS);
78+
assertThat(strategy.nextRestart).isEqualTo(REGULAR_TIME_WINDOW_MILLIS);
7879
}
7980
}

0 commit comments

Comments
 (0)