Skip to content

Commit

Permalink
PLANNER-491 Ident logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Nov 17, 2016
1 parent d1d2c8b commit 78921cb
Show file tree
Hide file tree
Showing 25 changed files with 190 additions and 108 deletions.
Expand Up @@ -114,8 +114,9 @@ public ConstructionHeuristicPhase buildPhase(int phaseIndex, HeuristicConfigPoli
HeuristicConfigPolicy phaseConfigPolicy = solverConfigPolicy.createPhaseConfigPolicy();
phaseConfigPolicy.setReinitializeVariableFilterEnabled(true);
phaseConfigPolicy.setInitializedChainedValueFilterEnabled(true);
DefaultConstructionHeuristicPhase phase = new DefaultConstructionHeuristicPhase();
configurePhase(phase, phaseIndex, phaseConfigPolicy, bestSolutionRecaller, solverTermination);
DefaultConstructionHeuristicPhase phase = new DefaultConstructionHeuristicPhase(
phaseIndex, solverConfigPolicy.getLogIndentation(), bestSolutionRecaller,
buildPhaseTermination(phaseConfigPolicy, solverTermination));
phase.setDecider(buildDecider(phaseConfigPolicy, phase.getTermination()));
ConstructionHeuristicType constructionHeuristicType_ = defaultIfNull(
constructionHeuristicType, ConstructionHeuristicType.ALLOCATE_ENTITY_FROM_QUEUE);
Expand Down Expand Up @@ -157,7 +158,8 @@ private ConstructionHeuristicDecider buildDecider(HeuristicConfigPolicy configPo
ConstructionHeuristicForagerConfig foragerConfig_ = foragerConfig == null
? new ConstructionHeuristicForagerConfig() : foragerConfig;
ConstructionHeuristicForager forager = foragerConfig_.buildForager(configPolicy);
ConstructionHeuristicDecider decider = new ConstructionHeuristicDecider(termination, forager);
ConstructionHeuristicDecider decider = new ConstructionHeuristicDecider(
configPolicy.getLogIndentation(), termination, forager);
EnvironmentMode environmentMode = configPolicy.getEnvironmentMode();
if (environmentMode.isNonIntrusiveFullAsserted()) {
decider.setAssertMoveScoreFromScratch(true);
Expand Down
Expand Up @@ -130,8 +130,9 @@ public ExhaustiveSearchPhase buildPhase(int phaseIndex, HeuristicConfigPolicy so
: exhaustiveSearchType_.getDefaultEntitySorterManner());
phaseConfigPolicy.setValueSorterManner(valueSorterManner != null ? valueSorterManner
: exhaustiveSearchType_.getDefaultValueSorterManner());
DefaultExhaustiveSearchPhase phase = new DefaultExhaustiveSearchPhase();
configurePhase(phase, phaseIndex, phaseConfigPolicy, bestSolutionRecaller, solverTermination);
DefaultExhaustiveSearchPhase phase = new DefaultExhaustiveSearchPhase(
phaseIndex, solverConfigPolicy.getLogIndentation(), bestSolutionRecaller,
buildPhaseTermination(phaseConfigPolicy, solverTermination));
boolean scoreBounderEnabled = exhaustiveSearchType_.isScoreBounderEnabled();
NodeExplorationType nodeExplorationType_;
if (exhaustiveSearchType_ == ExhaustiveSearchType.BRUTE_FORCE) {
Expand Down Expand Up @@ -213,7 +214,8 @@ private ExhaustiveSearchDecider buildDecider(HeuristicConfigPolicy configPolicy,
SelectionCacheType.JUST_IN_TIME, SelectionOrder.ORIGINAL);
ScoreBounder scoreBounder = scoreBounderEnabled
? new TrendBasedScoreBounder(configPolicy.getScoreDirectorFactory()) : null;
ExhaustiveSearchDecider decider = new ExhaustiveSearchDecider(bestSolutionRecaller, termination,
ExhaustiveSearchDecider decider = new ExhaustiveSearchDecider(configPolicy.getLogIndentation(),
bestSolutionRecaller, termination,
manualEntityMimicRecorder, moveSelector, scoreBounderEnabled, scoreBounder);
EnvironmentMode environmentMode = configPolicy.getEnvironmentMode();
if (environmentMode.isNonIntrusiveFullAsserted()) {
Expand Down
Expand Up @@ -29,10 +29,14 @@
import org.optaplanner.core.impl.heuristic.selector.value.mimic.ValueMimicRecorder;
import org.optaplanner.core.impl.score.definition.ScoreDefinition;
import org.optaplanner.core.impl.score.director.InnerScoreDirectorFactory;
import org.optaplanner.core.impl.solver.ChildThreadType;
import org.optaplanner.core.impl.solver.scope.DefaultSolverScope;
import org.optaplanner.core.impl.solver.termination.Termination;

public class HeuristicConfigPolicy {

private final EnvironmentMode environmentMode;
private final String logIndentation;
private final InnerScoreDirectorFactory scoreDirectorFactory;

private EntitySorterManner entitySorterManner = EntitySorterManner.NONE;
Expand All @@ -46,14 +50,23 @@ public class HeuristicConfigPolicy {
= new HashMap<>();

public HeuristicConfigPolicy(EnvironmentMode environmentMode, InnerScoreDirectorFactory scoreDirectorFactory) {
this(environmentMode, "", scoreDirectorFactory);
}

public HeuristicConfigPolicy(EnvironmentMode environmentMode, String logIndentation, InnerScoreDirectorFactory scoreDirectorFactory) {
this.environmentMode = environmentMode;
this.logIndentation = logIndentation;
this.scoreDirectorFactory = scoreDirectorFactory;
}

public EnvironmentMode getEnvironmentMode() {
return environmentMode;
}

public String getLogIndentation() {
return logIndentation;
}

public SolutionDescriptor getSolutionDescriptor() {
return scoreDirectorFactory.getSolutionDescriptor();
}
Expand Down Expand Up @@ -122,6 +135,10 @@ public HeuristicConfigPolicy createPhaseConfigPolicy() {
return new HeuristicConfigPolicy(environmentMode, scoreDirectorFactory);
}

public HeuristicConfigPolicy createChildThreadConfigPolicy(ChildThreadType childThreadType) {
return new HeuristicConfigPolicy(environmentMode, logIndentation + " ", scoreDirectorFactory);
}

// ************************************************************************
// Worker methods
// ************************************************************************
Expand Down
Expand Up @@ -107,8 +107,9 @@ public void setForagerConfig(LocalSearchForagerConfig foragerConfig) {
public LocalSearchPhase buildPhase(int phaseIndex, HeuristicConfigPolicy solverConfigPolicy,
BestSolutionRecaller bestSolutionRecaller, Termination solverTermination) {
HeuristicConfigPolicy phaseConfigPolicy = solverConfigPolicy.createPhaseConfigPolicy();
DefaultLocalSearchPhase phase = new DefaultLocalSearchPhase();
configurePhase(phase, phaseIndex, phaseConfigPolicy, bestSolutionRecaller, solverTermination);
DefaultLocalSearchPhase phase = new DefaultLocalSearchPhase(
phaseIndex, solverConfigPolicy.getLogIndentation(), bestSolutionRecaller,
buildPhaseTermination(phaseConfigPolicy, solverTermination));
phase.setDecider(buildDecider(phaseConfigPolicy,
phase.getTermination()));
EnvironmentMode environmentMode = phaseConfigPolicy.getEnvironmentMode();
Expand All @@ -123,20 +124,17 @@ public LocalSearchPhase buildPhase(int phaseIndex, HeuristicConfigPolicy solverC
}

private LocalSearchDecider buildDecider(HeuristicConfigPolicy configPolicy, Termination termination) {
LocalSearchDecider decider = new LocalSearchDecider();
decider.setTermination(termination);
MoveSelector moveSelector = buildMoveSelector(configPolicy);
decider.setMoveSelector(moveSelector);
Acceptor acceptor = buildAcceptor(configPolicy);
decider.setAcceptor(acceptor);
Forager forager = buildForager(configPolicy);
decider.setForager(forager);
LocalSearchDecider decider = new LocalSearchDecider(configPolicy.getLogIndentation(),
termination, moveSelector, acceptor, forager);
if (moveSelector.isNeverEnding() && !forager.supportsNeverEndingMoveSelector()) {
throw new IllegalStateException("The moveSelector (" + moveSelector
+ ") has neverEnding (" + moveSelector.isNeverEnding()
+ "), but the forager (" + forager
+ ") does not support it."
+ " Configure the <forager> with <acceptedCountLimit>.");
+ ") does not support it.\n"
+ " Maybe configure the <forager> with an <acceptedCountLimit>.");
}
EnvironmentMode environmentMode = configPolicy.getEnvironmentMode();
if (environmentMode.isNonIntrusiveFullAsserted()) {
Expand Down
Expand Up @@ -253,7 +253,7 @@ public Acceptor buildAcceptor(HeuristicConfigPolicy configPolicy) {
if ((acceptorTypeList != null && acceptorTypeList.contains(AcceptorType.ENTITY_TABU))
|| entityTabuSize != null || entityTabuRatio != null
|| fadingEntityTabuSize != null || fadingEntityTabuRatio != null) {
EntityTabuAcceptor acceptor = new EntityTabuAcceptor();
EntityTabuAcceptor acceptor = new EntityTabuAcceptor(configPolicy.getLogIndentation());
if (entityTabuSize != null) {
if (entityTabuRatio != null) {
throw new IllegalArgumentException("The acceptor cannot have both entityTabuSize ("
Expand Down Expand Up @@ -283,7 +283,7 @@ public Acceptor buildAcceptor(HeuristicConfigPolicy configPolicy) {
if ((acceptorTypeList != null && acceptorTypeList.contains(AcceptorType.VALUE_TABU))
|| valueTabuSize != null || valueTabuRatio != null
|| fadingValueTabuSize != null || fadingValueTabuRatio != null) {
ValueTabuAcceptor acceptor = new ValueTabuAcceptor();
ValueTabuAcceptor acceptor = new ValueTabuAcceptor(configPolicy.getLogIndentation());
if (valueTabuSize != null) {
if (valueTabuRatio != null) {
throw new IllegalArgumentException("The acceptor cannot have both valueTabuSize ("
Expand Down Expand Up @@ -317,7 +317,7 @@ public Acceptor buildAcceptor(HeuristicConfigPolicy configPolicy) {
}
if ((acceptorTypeList != null && acceptorTypeList.contains(AcceptorType.MOVE_TABU))
|| moveTabuSize != null || fadingMoveTabuSize != null) {
MoveTabuAcceptor acceptor = new MoveTabuAcceptor();
MoveTabuAcceptor acceptor = new MoveTabuAcceptor(configPolicy.getLogIndentation());
acceptor.setUseUndoMoveAsTabuMove(false);
if (moveTabuSize != null) {
acceptor.setTabuSizeStrategy(new FixedTabuSizeStrategy(moveTabuSize));
Expand All @@ -332,7 +332,7 @@ public Acceptor buildAcceptor(HeuristicConfigPolicy configPolicy) {
}
if ((acceptorTypeList != null && acceptorTypeList.contains(AcceptorType.UNDO_MOVE_TABU))
|| undoMoveTabuSize != null || fadingUndoMoveTabuSize != null) {
MoveTabuAcceptor acceptor = new MoveTabuAcceptor();
MoveTabuAcceptor acceptor = new MoveTabuAcceptor(configPolicy.getLogIndentation());
acceptor.setUseUndoMoveAsTabuMove(true);
if (undoMoveTabuSize != null) {
acceptor.setTabuSizeStrategy(new FixedTabuSizeStrategy(undoMoveTabuSize));
Expand All @@ -347,7 +347,7 @@ public Acceptor buildAcceptor(HeuristicConfigPolicy configPolicy) {
}
if ((acceptorTypeList != null && acceptorTypeList.contains(AcceptorType.SOLUTION_TABU))
|| solutionTabuSize != null || fadingSolutionTabuSize != null) {
SolutionTabuAcceptor acceptor = new SolutionTabuAcceptor();
SolutionTabuAcceptor acceptor = new SolutionTabuAcceptor(configPolicy.getLogIndentation());
if (solutionTabuSize != null) {
acceptor.setTabuSizeStrategy(new FixedTabuSizeStrategy(solutionTabuSize));
}
Expand Down
Expand Up @@ -37,6 +37,7 @@
import org.optaplanner.core.impl.partitionedsearch.DefaultPartitionedSearchPhase;
import org.optaplanner.core.impl.partitionedsearch.PartitionedSearchPhase;
import org.optaplanner.core.impl.partitionedsearch.partitioner.SolutionPartitioner;
import org.optaplanner.core.impl.solver.ChildThreadType;
import org.optaplanner.core.impl.solver.recaller.BestSolutionRecaller;
import org.optaplanner.core.impl.solver.termination.Termination;
import org.optaplanner.core.impl.solver.thread.DefaultSolverThreadFactory;
Expand Down Expand Up @@ -112,8 +113,9 @@ public void setPhaseConfigList(List<PhaseConfig> phaseConfigList) {
public PartitionedSearchPhase buildPhase(int phaseIndex, HeuristicConfigPolicy solverConfigPolicy,
BestSolutionRecaller bestSolutionRecaller, Termination solverTermination) {
HeuristicConfigPolicy phaseConfigPolicy = solverConfigPolicy.createPhaseConfigPolicy();
DefaultPartitionedSearchPhase phase = new DefaultPartitionedSearchPhase();
configurePhase(phase, phaseIndex, phaseConfigPolicy, bestSolutionRecaller, solverTermination);
DefaultPartitionedSearchPhase phase = new DefaultPartitionedSearchPhase(
phaseIndex, solverConfigPolicy.getLogIndentation(), bestSolutionRecaller,
buildPhaseTermination(phaseConfigPolicy, solverTermination));
phase.setThreadPoolExecutor(buildThreadPoolExecutor());
phase.setActiveThreadCount(resolvedActiveThreadCount());
phase.setSolutionPartitioner(buildSolutionPartitioner());
Expand All @@ -124,7 +126,7 @@ public PartitionedSearchPhase buildPhase(int phaseIndex, HeuristicConfigPolicy s
new LocalSearchPhaseConfig());
}
phase.setPhaseConfigList(phaseConfigList_);
phase.setConfigPolicy(phaseConfigPolicy);
phase.setConfigPolicy(phaseConfigPolicy.createChildThreadConfigPolicy(ChildThreadType.PART_THREAD));
EnvironmentMode environmentMode = phaseConfigPolicy.getEnvironmentMode();
if (environmentMode.isNonIntrusiveFullAsserted()) {
phase.setAssertStepScoreFromScratch(true);
Expand Down
Expand Up @@ -70,17 +70,13 @@ public void setTerminationConfig(TerminationConfig terminationConfig) {
public abstract Phase buildPhase(int phaseIndex,
HeuristicConfigPolicy solverConfigPolicy, BestSolutionRecaller bestSolutionRecaller, Termination solverTermination);

protected void configurePhase(AbstractPhase phase, int phaseIndex,
HeuristicConfigPolicy configPolicy, BestSolutionRecaller bestSolutionRecaller, Termination solverTermination) {
phase.setPhaseIndex(phaseIndex);
phase.setBestSolutionRecaller(bestSolutionRecaller);
protected Termination buildPhaseTermination(HeuristicConfigPolicy configPolicy, Termination solverTermination) {
TerminationConfig terminationConfig_ = terminationConfig == null ? new TerminationConfig()
: terminationConfig;
// In case of childThread PART_THREAD, the solverTermination is actually the parent phase's phaseTermination
// with the bridge removed, so it's ok to add it again
Termination phaseTermination = new PhaseToSolverTerminationBridge(solverTermination);
phase.setTermination(terminationConfig_.buildTermination(configPolicy,
phaseTermination));
return terminationConfig_.buildTermination(configPolicy, phaseTermination);
}

@Override
Expand Down
Expand Up @@ -84,8 +84,9 @@ public void setForceUpdateBestSolution(Boolean forceUpdateBestSolution) {
public CustomPhase buildPhase(int phaseIndex, HeuristicConfigPolicy solverConfigPolicy,
BestSolutionRecaller bestSolutionRecaller, Termination solverTermination) {
HeuristicConfigPolicy phaseConfigPolicy = solverConfigPolicy.createPhaseConfigPolicy();
DefaultCustomPhase customPhase = new DefaultCustomPhase();
configurePhase(customPhase, phaseIndex, phaseConfigPolicy, bestSolutionRecaller, solverTermination);
DefaultCustomPhase phase = new DefaultCustomPhase(
phaseIndex, solverConfigPolicy.getLogIndentation(), bestSolutionRecaller,
buildPhaseTermination(phaseConfigPolicy, solverTermination));
if (ConfigUtils.isEmptyCollection(customPhaseCommandClassList)) {
throw new IllegalArgumentException(
"Configure at least 1 <customPhaseCommandClass> in the <customPhase> configuration.");
Expand All @@ -99,13 +100,13 @@ public CustomPhase buildPhase(int phaseIndex, HeuristicConfigPolicy solverConfig
customPhaseCommand.applyCustomProperties(customProperties_);
customPhaseCommandList.add(customPhaseCommand);
}
customPhase.setCustomPhaseCommandList(customPhaseCommandList);
customPhase.setForceUpdateBestSolution(forceUpdateBestSolution == null ? false : forceUpdateBestSolution);
phase.setCustomPhaseCommandList(customPhaseCommandList);
phase.setForceUpdateBestSolution(forceUpdateBestSolution == null ? false : forceUpdateBestSolution);
EnvironmentMode environmentMode = phaseConfigPolicy.getEnvironmentMode();
if (environmentMode.isNonIntrusiveFullAsserted()) {
customPhase.setAssertStepScoreFromScratch(true);
phase.setAssertStepScoreFromScratch(true);
}
return customPhase;
return phase;
}

@Override
Expand Down

0 comments on commit 78921cb

Please sign in to comment.