Skip to content

Commit

Permalink
Add frequency + fallback value selector
Browse files Browse the repository at this point in the history
  • Loading branch information
cprudhom committed Aug 21, 2023
1 parent 6cbf048 commit 5ab2ab7
Showing 1 changed file with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public final class IntDomainBest implements IntValueSelector {
*/
private final BiPredicate<IntVar, Integer> condition;

private final IntValueSelector fallbackValueSelector;

private final int frequency;

/**
* Create a value selector that returns the best value wrt to the objective to optimize.
* When an enumerated variable domain exceeds {@link #maxdom}, only bounds are considered.
Expand All @@ -54,14 +58,40 @@ public final class IntDomainBest implements IntValueSelector {
* is kept.
* </p>
*
* @param maxdom a maximum domain size to satisfy to use this value selector.
* @param dop the decision operator used to make the decision
* @param condition predicate to break ties
* @param maxdom a maximum domain size to satisfy to use this value selector
* @param intValueSelector fallback value selector
* @param frequency the frequency of application of the best value selector.
* If f is the value indicated, the selector will be applied when restarts mod f = 0.
* In the other case, the fallback value selector is applied.
* @param dop the decision operator used to make the decision
* @param condition predicate to break ties
*/
public IntDomainBest(int maxdom, DecisionOperator<IntVar> dop, BiPredicate<IntVar, Integer> condition) {
public IntDomainBest(int maxdom, IntValueSelector intValueSelector, int frequency, DecisionOperator<IntVar> dop, BiPredicate<IntVar, Integer> condition) {
this.maxdom = maxdom;
this.dop = dop;
this.condition = condition;
this.fallbackValueSelector = intValueSelector;
this.frequency = frequency;
}

/**
* Create a value selector that returns the best value wrt to the objective to optimize.
* When an enumerated variable domain exceeds {@link #maxdom}, only bounds are considered.
*
* <p>
* {@code condition} is called when the evaluated {@code value} returns a score
* equals to the current best one. In that case, if {@code condition} returns {@code true}
* then {@code value} is retained as the new best candidate, otherwise the previous one
* is kept.
* </p>
*
* @param intValueSelector fallback value selector
* @param frequency the frequency of applying best value selector.
* If f is the value indicated, the selector will be applied when restarts mod f = 0.
* In the other case, the fallback value selector is applied.
*/
public IntDomainBest(IntValueSelector intValueSelector, int frequency) {
this(100, intValueSelector, frequency, DecisionOperatorFactory.makeIntEq(), (k, v) -> false);
}

/**
Expand All @@ -78,7 +108,7 @@ public IntDomainBest(int maxdom, DecisionOperator<IntVar> dop, BiPredicate<IntVa
* @param condition predicate to break ties
*/
public IntDomainBest(BiPredicate<IntVar, Integer> condition) {
this(100, DecisionOperatorFactory.makeIntEq(), condition);
this(100, new IntDomainMin(), 16, DecisionOperatorFactory.makeIntEq(), condition);
}


Expand All @@ -88,14 +118,17 @@ public IntDomainBest(BiPredicate<IntVar, Integer> condition) {
* Always-false condition is set by default.
*/
public IntDomainBest() {
this(100, DecisionOperatorFactory.makeIntEq(), (k, v) -> false);
this(100, new IntDomainMin(), 16, DecisionOperatorFactory.makeIntEq(), (k, v) -> false);
}

/**
* {@inheritDoc}
*/
@Override
public int selectValue(IntVar var) {
if (var.getModel().getSolver().getRestartCount() % frequency > 0) {
return fallbackValueSelector.selectValue(var);
}
assert var.getModel().getObjective() != null;
if (var.hasEnumeratedDomain() && var.getDomainSize() < maxdom) {
int bestCost = Integer.MAX_VALUE;
Expand Down

0 comments on commit 5ab2ab7

Please sign in to comment.