Skip to content

Commit

Permalink
rename selectedSizeLimit to selectedCountLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Feb 7, 2014
1 parent 3eb7e3b commit 6a9d72b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
Expand Up @@ -48,7 +48,7 @@
import org.optaplanner.core.impl.heuristic.selector.move.decorator.CachingMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.FilteringMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.ProbabilityMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.SelectedSizeLimitMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.SelectedCountLimitMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.ShufflingMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.SortingMoveSelector;

Expand Down Expand Up @@ -76,7 +76,7 @@ public abstract class MoveSelectorConfig extends SelectorConfig {

protected Class<? extends SelectionProbabilityWeightFactory> probabilityWeightFactoryClass = null;

protected Long selectedSizeLimit = null;
protected Long selectedCountLimit = null;

private Double fixedProbabilityWeight = null;

Expand Down Expand Up @@ -144,12 +144,12 @@ public void setProbabilityWeightFactoryClass(Class<? extends SelectionProbabilit
this.probabilityWeightFactoryClass = probabilityWeightFactoryClass;
}

public Long getSelectedSizeLimit() {
return selectedSizeLimit;
public Long getSelectedCountLimit() {
return selectedCountLimit;
}

public void setSelectedSizeLimit(Long selectedSizeLimit) {
this.selectedSizeLimit = selectedSizeLimit;
public void setSelectedCountLimit(Long selectedCountLimit) {
this.selectedCountLimit = selectedCountLimit;
}

public Double getFixedProbabilityWeight() {
Expand Down Expand Up @@ -181,7 +181,7 @@ public MoveSelector buildMoveSelector(HeuristicConfigPolicy configPolicy,
validateCacheTypeVersusSelectionOrder(resolvedCacheType, resolvedSelectionOrder);
validateSorting(resolvedSelectionOrder);
validateProbability(resolvedSelectionOrder);
validateSelectedSizeLimit(minimumCacheType);
validateSelectedLimit(minimumCacheType);

MoveSelector moveSelector = buildBaseMoveSelector(configPolicy,
SelectionCacheType.max(minimumCacheType, resolvedCacheType),
Expand All @@ -192,7 +192,7 @@ public MoveSelector buildMoveSelector(HeuristicConfigPolicy configPolicy,
moveSelector = applyProbability(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applyShuffling(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applyCaching(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applySelectedSizeLimit(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applySelectedLimit(resolvedCacheType, resolvedSelectionOrder, moveSelector);
return moveSelector;
}

Expand Down Expand Up @@ -338,21 +338,21 @@ private MoveSelector applyProbability(SelectionCacheType resolvedCacheType, Sele
return moveSelector;
}

private void validateSelectedSizeLimit(SelectionCacheType minimumCacheType) {
if (selectedSizeLimit != null
private void validateSelectedLimit(SelectionCacheType minimumCacheType) {
if (selectedCountLimit != null
&& minimumCacheType.compareTo(SelectionCacheType.JUST_IN_TIME) > 0) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") with selectedSizeLimit (" + selectedSizeLimit
+ ") with selectedCountLimit (" + selectedCountLimit
+ ") has a minimumCacheType (" + minimumCacheType
+ ") that is higher than " + SelectionCacheType.JUST_IN_TIME + ".");
}
}

private MoveSelector applySelectedSizeLimit(
private MoveSelector applySelectedLimit(
SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder,
MoveSelector moveSelector) {
if (selectedSizeLimit != null) {
moveSelector = new SelectedSizeLimitMoveSelector(moveSelector, selectedSizeLimit);
if (selectedCountLimit != null) {
moveSelector = new SelectedCountLimitMoveSelector(moveSelector, selectedCountLimit);
}
return moveSelector;
}
Expand Down Expand Up @@ -390,8 +390,8 @@ protected void inherit(MoveSelectorConfig inheritedConfig) {
sorterClass, inheritedConfig.getSorterClass());
probabilityWeightFactoryClass = ConfigUtils.inheritOverwritableProperty(
probabilityWeightFactoryClass, inheritedConfig.getProbabilityWeightFactoryClass());
selectedSizeLimit = ConfigUtils.inheritOverwritableProperty(
selectedSizeLimit, inheritedConfig.getSelectedSizeLimit());
selectedCountLimit = ConfigUtils.inheritOverwritableProperty(
selectedCountLimit, inheritedConfig.getSelectedCountLimit());

fixedProbabilityWeight = ConfigUtils.inheritOverwritableProperty(
fixedProbabilityWeight, inheritedConfig.getFixedProbabilityWeight());
Expand Down
Expand Up @@ -19,25 +19,22 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionFilter;
import org.optaplanner.core.impl.heuristic.selector.common.iterator.SelectionIterator;
import org.optaplanner.core.impl.heuristic.selector.common.iterator.UpcomingSelectionIterator;
import org.optaplanner.core.impl.heuristic.selector.move.AbstractMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.MoveSelector;
import org.optaplanner.core.impl.move.Move;
import org.optaplanner.core.impl.score.director.ScoreDirector;

public class SelectedSizeLimitMoveSelector extends AbstractMoveSelector {
public class SelectedCountLimitMoveSelector extends AbstractMoveSelector {

protected final MoveSelector childMoveSelector;
protected final long selectedSizeLimit;
protected final long selectedCountLimit;

public SelectedSizeLimitMoveSelector(MoveSelector childMoveSelector, long selectedSizeLimit) {
public SelectedCountLimitMoveSelector(MoveSelector childMoveSelector, long selectedCountLimit) {
this.childMoveSelector = childMoveSelector;
this.selectedSizeLimit = selectedSizeLimit;
if (selectedSizeLimit < 0L) {
this.selectedCountLimit = selectedCountLimit;
if (selectedCountLimit < 0L) {
throw new IllegalArgumentException("The selector (" + this
+ ") has a negative selectedSizeLimit (" + selectedSizeLimit + ").");
+ ") has a negative selectedCountLimit (" + selectedCountLimit + ").");
}
solverPhaseLifecycleSupport.addEventListener(childMoveSelector);
}
Expand All @@ -56,31 +53,31 @@ public boolean isNeverEnding() {

public long getSize() {
long childSize = childMoveSelector.getSize();
return Math.min(selectedSizeLimit, childSize);
return Math.min(selectedCountLimit, childSize);
}

public Iterator<Move> iterator() {
return new SelectedSizeLimitMoveIterator(childMoveSelector.iterator());
return new SelectedCountLimitMoveIterator(childMoveSelector.iterator());
}

private class SelectedSizeLimitMoveIterator extends SelectionIterator<Move> {
private class SelectedCountLimitMoveIterator extends SelectionIterator<Move> {

private final Iterator<Move> childMoveIterator;
private long selectedSize;

public SelectedSizeLimitMoveIterator(Iterator<Move> childMoveIterator) {
public SelectedCountLimitMoveIterator(Iterator<Move> childMoveIterator) {
this.childMoveIterator = childMoveIterator;
selectedSize = 0L;
}

@Override
public boolean hasNext() {
return selectedSize < selectedSizeLimit && childMoveIterator.hasNext();
return selectedSize < selectedCountLimit && childMoveIterator.hasNext();
}

@Override
public Move next() {
if (selectedSize >= selectedSizeLimit) {
if (selectedSize >= selectedCountLimit) {
throw new NoSuchElementException();
}
selectedSize++;
Expand All @@ -91,7 +88,7 @@ public Move next() {

@Override
public String toString() {
return "SelectedSizeLimit(" + childMoveSelector + ")";
return "SelectedCountLimit(" + childMoveSelector + ")";
}

}
Expand Up @@ -16,30 +16,24 @@

package org.optaplanner.core.impl.heuristic.selector.move.decorator;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.optaplanner.core.impl.heuristic.selector.SelectorTestUtils;
import org.optaplanner.core.impl.heuristic.selector.common.SelectionCacheType;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionFilter;
import org.optaplanner.core.impl.heuristic.selector.move.MoveSelector;
import org.optaplanner.core.impl.move.DummyMove;
import org.optaplanner.core.impl.phase.AbstractSolverPhaseScope;
import org.optaplanner.core.impl.phase.step.AbstractStepScope;
import org.optaplanner.core.impl.score.director.ScoreDirector;
import org.optaplanner.core.impl.solver.scope.DefaultSolverScope;

import static org.mockito.Mockito.*;
import static org.optaplanner.core.impl.testdata.util.PlannerAssert.*;

public class SelectedSizeLimitMoveSelectorTest {
public class SelectedCountLimitMoveSelectorTest {

@Test
public void selectSizeLimitLowerThanSelectorSize() {
MoveSelector childMoveSelector = SelectorTestUtils.mockMoveSelector(DummyMove.class,
new DummyMove("a1"), new DummyMove("a2"), new DummyMove("a3"), new DummyMove("a4"), new DummyMove("a5"));
MoveSelector moveSelector = new SelectedSizeLimitMoveSelector(childMoveSelector, 3L);
MoveSelector moveSelector = new SelectedCountLimitMoveSelector(childMoveSelector, 3L);

DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
moveSelector.solvingStarted(solverScope);
Expand Down Expand Up @@ -98,7 +92,7 @@ public void selectSizeLimitLowerThanSelectorSize() {
public void selectSizeLimitHigherThanSelectorSize() {
MoveSelector childMoveSelector = SelectorTestUtils.mockMoveSelector(DummyMove.class,
new DummyMove("a1"), new DummyMove("a2"), new DummyMove("a3"));
MoveSelector moveSelector = new SelectedSizeLimitMoveSelector(childMoveSelector, 5L);
MoveSelector moveSelector = new SelectedCountLimitMoveSelector(childMoveSelector, 5L);

DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
moveSelector.solvingStarted(solverScope);
Expand Down
Expand Up @@ -1044,16 +1044,16 @@
<para>Selecting all possible moves sometimes does not scale well enough, especially for construction heuristics
(which don't support <link linkend="acceptedCountLimit">acceptedCountLimit</link>).</para>

<para>To limit the number of selected moves per step, apply a <literal>selectedSizeLimit</literal> on the
<para>To limit the number of selected moves per step, apply a <literal>selectedCountLimit</literal> on the
selector:</para>

<programlisting language="xml"> &lt;changeMoveSelector&gt;
&lt;selectedSizeLimit&gt;100&lt;/selectedSizeLimit&gt;
&lt;selectedCountLimit&gt;100&lt;/selectedCountLimit&gt;
&lt;/changeMoveSelector&gt;</programlisting>

<note>
<para>To scale Local Search, setting <link linkend="acceptedCountLimit">acceptedCountLimit</link> is usually
better than using <literal>selectedSizeLimit</literal>.</para>
better than using <literal>selectedCountLimit</literal>.</para>
</note>
</section>
</section>
Expand Down
Expand Up @@ -23,10 +23,25 @@
<maximumSecondsSpend>120</maximumSecondsSpend>
</termination>
<constructionHeuristic>
<constructionHeuristicType>FIRST_FIT_DECREASING</constructionHeuristicType>
<forager>
<pickEarlyType>FIRST_NON_DETERIORATING_SCORE</pickEarlyType>
</forager>
<queuedEntityPlacer>
<entitySelector id="placerEntitySelector">
<cacheType>PHASE</cacheType>
<selectionOrder>SORTED</selectionOrder>
<sorterManner>DECREASING_DIFFICULTY</sorterManner>
</entitySelector>
<changeMoveSelector>
<entitySelector mimicSelectorRef="placerEntitySelector"/>
<valueSelector>
<cacheType>JUST_IN_TIME</cacheType>
<selectionOrder>RANDOM</selectionOrder>
<!--<sorterManner>INCREASING_STRENGTH</sorterManner>-->
</valueSelector>
<selectedCountLimit>100</selectedCountLimit>
</changeMoveSelector>
</queuedEntityPlacer>
<!--<forager>-->
<!--<pickEarlyType>FIRST_NON_DETERIORATING_SCORE</pickEarlyType>-->
<!--</forager>-->
</constructionHeuristic>
<localSearch>
<acceptor>
Expand Down

0 comments on commit 6a9d72b

Please sign in to comment.