Skip to content

Commit

Permalink
PLANNER-491 Fix exception if CH terminates early and LS should not run
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Nov 17, 2016
1 parent 1e89f94 commit b83c9a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Expand Up @@ -108,9 +108,7 @@ public Solution_ solve(Solution_ part) {
try {
solverScope.setBestSolution(part);
solvingStarted(solverScope);
for (Phase<Solution_> phase : phaseList) {
phase.solve(solverScope);
}
runPhases(solverScope);
solvingEnded(solverScope);
return solverScope.getBestSolution();
} finally {
Expand Down
Expand Up @@ -16,11 +16,13 @@

package org.optaplanner.core.impl.solver;

import java.util.Iterator;
import java.util.List;

import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.event.SolverEventListener;
import org.optaplanner.core.impl.partitionedsearch.PartitionSolver;
import org.optaplanner.core.impl.phase.Phase;
import org.optaplanner.core.impl.phase.event.PhaseLifecycleListener;
import org.optaplanner.core.impl.phase.event.PhaseLifecycleSupport;
Expand All @@ -32,6 +34,10 @@
import org.optaplanner.core.impl.solver.termination.Termination;

/**
* Common code between {@link DefaultSolver} and child solvers (such as {@link PartitionSolver}.
* <p>
* Do not create a new child {@link Solver} to implement a new heuristic or metaheuristic,
* just use a new {@link Phase} for that.
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
* @see Solver
* @see DefaultSolver
Expand Down Expand Up @@ -74,6 +80,18 @@ public void solvingStarted(DefaultSolverScope<Solution_> solverScope) {
}
}

protected void runPhases(DefaultSolverScope<Solution_> solverScope) {
Iterator<Phase<Solution_>> it = phaseList.iterator();
while (!termination.isSolverTerminated(solverScope) && it.hasNext()) {
Phase<Solution_> phase = it.next();
phase.solve(solverScope);
if (it.hasNext()) {
solverScope.setWorkingSolutionFromBestSolution();
}
}
// TODO support doing round-robin of phases (only non-construction heuristics)
}

public void solvingEnded(DefaultSolverScope<Solution_> solverScope) {
for (Phase<Solution_> phase : phaseList) {
phase.solvingEnded(solverScope);
Expand Down
Expand Up @@ -39,6 +39,7 @@
* Default implementation for {@link Solver}.
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
* @see Solver
* @see AbstractSolver
*/
public class DefaultSolver<Solution_> extends AbstractSolver<Solution_> {

Expand Down Expand Up @@ -156,7 +157,7 @@ public final Solution_ solve(Solution_ planningProblem) {
boolean restartSolver = true;
while (restartSolver) {
solvingStarted(solverScope);
runPhases();
runPhases(solverScope);
solvingEnded(solverScope);
restartSolver = checkProblemFactChanges();
}
Expand Down Expand Up @@ -185,18 +186,6 @@ public void solvingStarted(DefaultSolverScope<Solution_> solverScope) {
(randomFactory != null ? randomFactory : "not fixed"));
}

protected void runPhases() {
Iterator<Phase<Solution_>> it = phaseList.iterator();
while (!termination.isSolverTerminated(solverScope) && it.hasNext()) {
Phase<Solution_> phase = it.next();
phase.solve(solverScope);
if (it.hasNext()) {
solverScope.setWorkingSolutionFromBestSolution();
}
}
// TODO support doing round-robin of phases (only non-construction heuristics)
}

public void solvingEnded(DefaultSolverScope<Solution_> solverScope) {
super.solvingEnded(solverScope);
solverScope.endingNow();
Expand Down

0 comments on commit b83c9a4

Please sign in to comment.