Skip to content

Commit

Permalink
feat: improve the reconfiguration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zepfred committed Sep 27, 2024
1 parent dcf88ed commit 77bc341
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ public LocalSearchAcceptorConfig inherit(LocalSearchAcceptorConfig inheritedConf
inheritedConfig.getStepCountingHillClimbingSize());
stepCountingHillClimbingType = ConfigUtils.inheritOverwritableProperty(stepCountingHillClimbingType,
inheritedConfig.getStepCountingHillClimbingType());
reconfigurationConfig = ConfigUtils.inheritOverwritableProperty(reconfigurationConfig,
inheritedConfig.getReconfigurationConfig());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ public void decideNextStep(LocalSearchStepScope<Solution_> stepScope) {
LocalSearchMoveScope<Solution_> moveScope = new LocalSearchMoveScope<>(stepScope, moveIndex, move);
moveIndex++;
doMove(moveScope);
if (forager.isQuitEarly()) {
break;
}
if (acceptor.needReconfiguration(stepScope)) {
logger.debug("{} Move index ({}), score ({}), reconfiguration triggered.",
logIndentation, moveScope.getMoveIndex(), moveScope.getScore());
if (forager.isQuitEarly() || acceptor.needReconfiguration(stepScope)) {
break;
}
stepScope.getPhaseScope().getSolverScope().checkYielding();
Expand Down Expand Up @@ -145,7 +140,7 @@ protected void pickMove(LocalSearchStepScope<Solution_> stepScope) {
stepScope.setScore(pickedMoveScope.getScore());
}
if (acceptor.needReconfiguration(stepScope)) {
acceptor.applyReconfiguration();
acceptor.applyReconfiguration(stepScope);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ default boolean needReconfiguration(LocalSearchStepScope<Solution_> stepScope) {
* <p>
* One typical example is the reheat operation used in the Simulated Annealing method.
*/
default void applyReconfiguration() {
default void applyReconfiguration(LocalSearchStepScope<Solution_> stepScope) {
// By default, executes nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,12 @@ public void phaseStarted(LocalSearchPhaseScope<Solution_> phaseScope) {
var initialScore = phaseScope.getBestScore();
Arrays.fill(previousScores, initialScore);
lateScoreIndex = 0;
lateAcceptanceReconfigurationRatioCount = (long) (phaseScope.getMoveSelectorSize() * moveReconfigurationRatio / 100);
lateAcceptanceReconfigurationRatioCount = (long) (lateAcceptanceSize * moveReconfigurationRatio / 100);
var lateAcceptanceReconfigurationMoveCount = (long) (phaseScope.getMoveSelectorSize() * moveCountLimitPercentage / 100);
moveCountTermination = new MoveCountTermination<>(lateAcceptanceReconfigurationMoveCount, true);
moveCountTermination.phaseStarted(phaseScope);
}

@Override
public void stepStarted(LocalSearchStepScope<Solution_> stepScope) {
super.stepStarted(stepScope);
moveCountTermination.stepStarted(stepScope);
logger.info("Late Acceptance reconfiguration move count({}), late elements count({}) ",
lateAcceptanceReconfigurationMoveCount, lateAcceptanceReconfigurationRatioCount);
}

private void validate() {
Expand All @@ -75,7 +71,7 @@ public boolean isAccepted(LocalSearchMoveScope<Solution_> moveScope) {
var moveScore = moveScope.getScore();
var lateScore = previousScores[lateScoreIndex];
if (lateScore == null) {
logger.trace("Move index ({}), score ({}), accepted after reconfiguration ({}).", moveScope.getMoveIndex(),
logger.info("Move index ({}), score ({}), accepted after reconfiguration ({}).", moveScope.getMoveIndex(),
moveScope.getScore(), moveScope.getMove());
return true;
}
Expand All @@ -89,11 +85,21 @@ public boolean isAccepted(LocalSearchMoveScope<Solution_> moveScope) {
return false;
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void stepEnded(LocalSearchStepScope<Solution_> stepScope) {
super.stepEnded(stepScope);
Score stepScore = stepScope.getScore();
var lateScoreCmp = stepScore.compareTo(previousScores[lateScoreIndex]);
var lastStepScore = stepScope.getPhaseScope().getLastCompletedStepScope().getScore();
var lastStepScoreCmp = stepScore.compareTo(lastStepScore);
previousScores[lateScoreIndex] = stepScope.getScore();
lateScoreIndex = (lateScoreIndex + 1) % lateAcceptanceSize;
if (lateScoreCmp > 0 || lastStepScoreCmp > 0) {
// The terminator is only updated when a superior solution is found.
// Otherwise, we continue incrementing the moves until the reconfiguration is triggered
moveCountTermination.stepEnded(stepScope);
}
}

@Override
Expand All @@ -110,7 +116,7 @@ public boolean needReconfiguration(LocalSearchStepScope<Solution_> stepScope) {
}

@Override
public void applyReconfiguration() {
public void applyReconfiguration(LocalSearchStepScope<Solution_> stepScope) {
var idx = lateScoreIndex;
if (previousScores[idx] == null) {
// The method still has null values from the last reconfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void applyReconfiguration() {
// Apply reconfiguration
stepScope0.setScore(SimpleScore.of(-3000));
var moveScope1 = buildMoveScope(stepScope0, -3000);
acceptor.applyReconfiguration();
acceptor.applyReconfiguration(stepScope0);
assertThat(acceptor.isAccepted(moveScope1)).isTrue();
acceptor.stepEnded(stepScope0);
assertThat(acceptor.isAccepted(moveScope1)).isTrue();
Expand Down

0 comments on commit 77bc341

Please sign in to comment.