File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
main/java/ai/timefold/solver/core/impl/solver/termination
test/java/ai/timefold/solver/core/impl/solver/termination Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -37,8 +37,14 @@ public SolverBridgePhaseTermination(SolverTermination<Solution_> solverTerminati
37
37
}
38
38
39
39
@ Override
40
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
40
41
public boolean isPhaseTerminated (AbstractPhaseScope <Solution_ > phaseScope ) {
41
- return solverTermination .isSolverTerminated (phaseScope .getSolverScope ());
42
+ var terminated = solverTermination .isSolverTerminated (phaseScope .getSolverScope ());
43
+ // If the solver is not finished yet, we need to check the phase termination
44
+ if (!terminated && solverTermination instanceof PhaseTermination phaseTermination ) {
45
+ return phaseTermination .isPhaseTerminated (phaseScope );
46
+ }
47
+ return terminated ;
42
48
}
43
49
44
50
@ Override
Original file line number Diff line number Diff line change @@ -329,6 +329,24 @@ void terminateEarlyLocalSearchInterrupted(TerminationConfig terminationConfig) {
329
329
assertThat (resultingSolution ).isNotNull ();
330
330
}
331
331
332
+ @ Test
333
+ void mixedSolverPhaseTerminations () {
334
+ var solverConfig = new SolverConfig ()
335
+ .withSolutionClass (TestdataSolution .class )
336
+ .withEntityClasses (TestdataEntity .class )
337
+ .withEasyScoreCalculatorClass (TestdataEasyScoreCalculator .class )
338
+ .withTerminationConfig (new TerminationConfig ().withSpentLimit (Duration .ofHours (1 )))
339
+ .withPhases (new LocalSearchPhaseConfig ()
340
+ .withTerminationConfig (new TerminationConfig ().withStepCountLimit (4 )));
341
+ var solution = TestdataSolution .generateSolution (2 , 2 );
342
+ var solver = SolverFactory .<TestdataSolution > create (solverConfig )
343
+ .buildSolver ();
344
+ var bestSolution = solver .solve (solution );
345
+ // The global spent limit is one hour, but the phase limit is four steps.
346
+ // The solver process is finished after 4 steps
347
+ assertThat (bestSolution ).isNotNull ();
348
+ }
349
+
332
350
static class TerminationArgumentSource implements ArgumentsProvider {
333
351
334
352
@ Override
You can’t perform that action at this time.
0 commit comments