-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
151 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 13 additions & 42 deletions
55
...ver/core/impl/heuristic/selector/move/generic/RuinRecreateConstructionHeuristicPhase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,31 @@ | ||
package ai.timefold.solver.core.impl.heuristic.selector.move.generic; | ||
|
||
import java.util.List; | ||
|
||
import ai.timefold.solver.core.impl.constructionheuristic.ConstructionHeuristicPhase; | ||
import ai.timefold.solver.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase; | ||
import ai.timefold.solver.core.impl.constructionheuristic.decider.ConstructionHeuristicDecider; | ||
import ai.timefold.solver.core.impl.constructionheuristic.placer.EntityPlacer; | ||
import ai.timefold.solver.core.impl.solver.termination.Termination; | ||
import ai.timefold.solver.core.impl.constructionheuristic.scope.ConstructionHeuristicPhaseScope; | ||
import ai.timefold.solver.core.impl.constructionheuristic.scope.ConstructionHeuristicStepScope; | ||
|
||
public final class RuinRecreateConstructionHeuristicPhase<Solution_> | ||
final class RuinRecreateConstructionHeuristicPhase<Solution_> | ||
extends DefaultConstructionHeuristicPhase<Solution_> | ||
implements ConstructionHeuristicPhase<Solution_> { | ||
|
||
public RuinRecreateConstructionHeuristicPhase(RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> builder) { | ||
RuinRecreateConstructionHeuristicPhase(RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> builder) { | ||
super(builder); | ||
} | ||
|
||
@Override | ||
protected boolean isNested() { | ||
return true; | ||
protected void processWorkingSolutionDuringStep(ConstructionHeuristicStepScope<Solution_> stepScope) { | ||
// Ruin and Recreate CH doesn't process the working solution, it is a nested phase. | ||
} | ||
|
||
public static final class RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> | ||
extends DefaultConstructionHeuristicPhaseBuilder<Solution_> { | ||
|
||
private List<Object> elementsToRecreate; | ||
|
||
public RuinRecreateConstructionHeuristicPhaseBuilder(Termination<Solution_> phaseTermination, | ||
EntityPlacer<Solution_> entityPlacer, ConstructionHeuristicDecider<Solution_> decider) { | ||
super(0, false, "", phaseTermination, entityPlacer, decider); | ||
} | ||
|
||
public RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> withElementsToRecreate(List<Object> elements) { | ||
this.elementsToRecreate = elements; | ||
return this; | ||
} | ||
|
||
@Override | ||
public EntityPlacer<Solution_> getEntityPlacer() { | ||
if (elementsToRecreate == null || elementsToRecreate.isEmpty()) { | ||
return super.getEntityPlacer(); | ||
} | ||
return super.getEntityPlacer().rebuildWithFilter((scoreDirector, selection) -> { | ||
for (var element : elementsToRecreate) { | ||
if (selection == element) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
} | ||
@Override | ||
protected void updateBestSolutionAndFire(ConstructionHeuristicPhaseScope<Solution_> phaseScope) { | ||
// Ruin and Recreate CH doesn't update the best solution, it is a nested phase. | ||
} | ||
|
||
@Override | ||
public RuinRecreateConstructionHeuristicPhase<Solution_> build() { | ||
return new RuinRecreateConstructionHeuristicPhase<>(this); | ||
} | ||
@Override | ||
public String getPhaseTypeString() { | ||
return "Ruin & Recreate Construction Heuristics"; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...e/impl/heuristic/selector/move/generic/RuinRecreateConstructionHeuristicPhaseBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ai.timefold.solver.core.impl.heuristic.selector.move.generic; | ||
|
||
import java.util.List; | ||
|
||
import ai.timefold.solver.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig; | ||
import ai.timefold.solver.core.config.solver.termination.TerminationConfig; | ||
import ai.timefold.solver.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase; | ||
import ai.timefold.solver.core.impl.constructionheuristic.decider.ConstructionHeuristicDecider; | ||
import ai.timefold.solver.core.impl.constructionheuristic.placer.EntityPlacer; | ||
import ai.timefold.solver.core.impl.heuristic.HeuristicConfigPolicy; | ||
import ai.timefold.solver.core.impl.solver.termination.Termination; | ||
import ai.timefold.solver.core.impl.solver.termination.TerminationFactory; | ||
|
||
public final class RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> | ||
extends DefaultConstructionHeuristicPhase.DefaultConstructionHeuristicPhaseBuilder<Solution_> { | ||
|
||
public static <Solution_> RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> | ||
create(HeuristicConfigPolicy<Solution_> solverConfigPolicy) { | ||
var constructionHeuristicConfig = new ConstructionHeuristicPhaseConfig(); | ||
return create(solverConfigPolicy, constructionHeuristicConfig); | ||
} | ||
|
||
public static <Solution_> RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> create( | ||
HeuristicConfigPolicy<Solution_> solverConfigPolicy, ConstructionHeuristicPhaseConfig constructionHeuristicConfig) { | ||
var constructionHeuristicPhaseFactory = | ||
new RuinRecreateConstructionHeuristicPhaseFactory<Solution_>(constructionHeuristicConfig); | ||
return (RuinRecreateConstructionHeuristicPhaseBuilder<Solution_>) constructionHeuristicPhaseFactory.getBuilder(0, false, | ||
solverConfigPolicy, TerminationFactory.<Solution_> create(new TerminationConfig()) | ||
.buildTermination(solverConfigPolicy)); | ||
} | ||
|
||
private List<Object> elementsToRecreate; | ||
|
||
RuinRecreateConstructionHeuristicPhaseBuilder(Termination<Solution_> phaseTermination, | ||
EntityPlacer<Solution_> entityPlacer, ConstructionHeuristicDecider<Solution_> decider) { | ||
super(0, false, "", phaseTermination, entityPlacer, decider); | ||
} | ||
|
||
public RuinRecreateConstructionHeuristicPhaseBuilder<Solution_> withElementsToRecreate(List<Object> elements) { | ||
this.elementsToRecreate = elements; | ||
return this; | ||
} | ||
|
||
@Override | ||
public EntityPlacer<Solution_> getEntityPlacer() { | ||
if (elementsToRecreate == null || elementsToRecreate.isEmpty()) { | ||
return super.getEntityPlacer(); | ||
} | ||
return super.getEntityPlacer().rebuildWithFilter((scoreDirector, selection) -> { | ||
for (var element : elementsToRecreate) { | ||
if (selection == element) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
} | ||
|
||
@Override | ||
public DefaultConstructionHeuristicPhase<Solution_> build() { | ||
return new RuinRecreateConstructionHeuristicPhase<>(this); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...e/impl/heuristic/selector/move/generic/RuinRecreateConstructionHeuristicPhaseFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ai.timefold.solver.core.impl.heuristic.selector.move.generic; | ||
|
||
import ai.timefold.solver.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig; | ||
import ai.timefold.solver.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase.DefaultConstructionHeuristicPhaseBuilder; | ||
import ai.timefold.solver.core.impl.constructionheuristic.DefaultConstructionHeuristicPhaseFactory; | ||
import ai.timefold.solver.core.impl.constructionheuristic.decider.ConstructionHeuristicDecider; | ||
import ai.timefold.solver.core.impl.constructionheuristic.placer.EntityPlacer; | ||
import ai.timefold.solver.core.impl.heuristic.HeuristicConfigPolicy; | ||
import ai.timefold.solver.core.impl.solver.termination.BasicPlumbingTermination; | ||
import ai.timefold.solver.core.impl.solver.termination.PhaseToSolverTerminationBridge; | ||
import ai.timefold.solver.core.impl.solver.termination.Termination; | ||
|
||
final class RuinRecreateConstructionHeuristicPhaseFactory<Solution_> | ||
extends DefaultConstructionHeuristicPhaseFactory<Solution_> { | ||
|
||
RuinRecreateConstructionHeuristicPhaseFactory(ConstructionHeuristicPhaseConfig phaseConfig) { | ||
super(phaseConfig); | ||
} | ||
|
||
@Override | ||
protected DefaultConstructionHeuristicPhaseBuilder<Solution_> createBuilder( | ||
HeuristicConfigPolicy<Solution_> phaseConfigPolicy, | ||
Termination<Solution_> solverTermination, int phaseIndex, boolean triggerFirstInitializedSolutionEvent, | ||
EntityPlacer<Solution_> entityPlacer) { | ||
var phaseTermination = new PhaseToSolverTerminationBridge<>(new BasicPlumbingTermination<Solution_>(false)); | ||
return new RuinRecreateConstructionHeuristicPhaseBuilder<>(phaseTermination, entityPlacer, | ||
buildDecider(phaseConfigPolicy, phaseTermination)); | ||
|
||
} | ||
|
||
@Override | ||
protected ConstructionHeuristicDecider<Solution_> buildDecider(HeuristicConfigPolicy<Solution_> configPolicy, | ||
Termination<Solution_> termination) { | ||
return new RuinRecreateConstructionHeuristicDecider<>(termination, buildForager(configPolicy)); | ||
} | ||
|
||
} |
Oops, something went wrong.