|
| 1 | +package ai.timefold.solver.core.impl.localsearch.decider.acceptor.stuckcriterion; |
| 2 | + |
| 3 | +import ai.timefold.solver.core.api.score.Score; |
| 4 | +import ai.timefold.solver.core.impl.localsearch.scope.LocalSearchMoveScope; |
| 5 | +import ai.timefold.solver.core.impl.localsearch.scope.LocalSearchPhaseScope; |
| 6 | +import ai.timefold.solver.core.impl.localsearch.scope.LocalSearchStepScope; |
| 7 | +import ai.timefold.solver.core.impl.solver.scope.SolverScope; |
| 8 | +import ai.timefold.solver.core.impl.solver.termination.UnimprovedTimeMillisSpentTermination; |
| 9 | + |
| 10 | +public class UnimprovedTimeStuckCriterion<Solution_, Score_ extends Score<Score_>> |
| 11 | + extends AbstractGeometricStuckCriterion<Solution_> { |
| 12 | + protected static final long START_TIME_WINDOW_MILLIS = 30_000; |
| 13 | + protected static final long REGULAR_TIME_WINDOW_MILLIS = 300_000; |
| 14 | + |
| 15 | + private UnimprovedTimeMillisSpentTermination<Solution_> unimprovedTimeStuckCriterion; |
| 16 | + |
| 17 | + private boolean triggered; |
| 18 | + private Score_ currentBestScore; |
| 19 | + |
| 20 | + public UnimprovedTimeStuckCriterion() { |
| 21 | + this(new UnimprovedTimeMillisSpentTermination<>(START_TIME_WINDOW_MILLIS)); |
| 22 | + } |
| 23 | + |
| 24 | + protected UnimprovedTimeStuckCriterion(UnimprovedTimeMillisSpentTermination<Solution_> unimprovedTimeStuckCriterion) { |
| 25 | + super(START_TIME_WINDOW_MILLIS); |
| 26 | + this.unimprovedTimeStuckCriterion = unimprovedTimeStuckCriterion; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + boolean evaluateCriterion(LocalSearchMoveScope<Solution_> moveScope) { |
| 31 | + triggered = unimprovedTimeStuckCriterion.isPhaseTerminated(moveScope.getStepScope().getPhaseScope()); |
| 32 | + return triggered; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void stepStarted(LocalSearchStepScope<Solution_> stepScope) { |
| 37 | + currentBestScore = stepScope.getPhaseScope().getBestScore(); |
| 38 | + if (triggered) { |
| 39 | + // We need to recreate the termination criterion as the time window has changed |
| 40 | + // After the first restart we use a higher time window |
| 41 | + setScalingFactor(REGULAR_TIME_WINDOW_MILLIS); |
| 42 | + unimprovedTimeStuckCriterion = new UnimprovedTimeMillisSpentTermination<>(nextRestart); |
| 43 | + // The criterion must be initialized; otherwise, no restart will occur |
| 44 | + unimprovedTimeStuckCriterion.phaseStarted(stepScope.getPhaseScope()); |
| 45 | + triggered = false; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void stepEnded(LocalSearchStepScope<Solution_> stepScope) { |
| 51 | + unimprovedTimeStuckCriterion.stepEnded(stepScope); |
| 52 | + if (currentBestScore.compareTo(stepScope.getPhaseScope().getBestScore()) < 0 |
| 53 | + && nextRestart > START_TIME_WINDOW_MILLIS) { |
| 54 | + // If the solution has been improved after a restart, |
| 55 | + // we reset the criterion and restart the evaluation of the metric |
| 56 | + setScalingFactor(START_TIME_WINDOW_MILLIS); |
| 57 | + super.solvingStarted(stepScope.getPhaseScope().getSolverScope()); |
| 58 | + unimprovedTimeStuckCriterion = new UnimprovedTimeMillisSpentTermination<>(nextRestart); |
| 59 | + // The criterion must be initialized; otherwise, no restart will occur |
| 60 | + unimprovedTimeStuckCriterion.phaseStarted(stepScope.getPhaseScope()); |
| 61 | + logger.info("Stuck criterion reset, next restart ({}), previous best score({}), new best score ({})", nextRestart, |
| 62 | + currentBestScore, stepScope.getPhaseScope().getBestScore()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void phaseStarted(LocalSearchPhaseScope<Solution_> phaseScope) { |
| 68 | + super.phaseStarted(phaseScope); |
| 69 | + unimprovedTimeStuckCriterion.phaseStarted(phaseScope); |
| 70 | + triggered = false; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void phaseEnded(LocalSearchPhaseScope<Solution_> phaseScope) { |
| 75 | + super.phaseEnded(phaseScope); |
| 76 | + unimprovedTimeStuckCriterion.phaseEnded(phaseScope); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void solvingStarted(SolverScope<Solution_> solverScope) { |
| 81 | + super.solvingStarted(solverScope); |
| 82 | + unimprovedTimeStuckCriterion.solvingStarted(solverScope); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void solvingEnded(SolverScope<Solution_> solverScope) { |
| 87 | + super.solvingEnded(solverScope); |
| 88 | + unimprovedTimeStuckCriterion.solvingEnded(solverScope); |
| 89 | + } |
| 90 | +} |
0 commit comments