avianey
avianey

Reputation: 5823

At least one Var equals to X within a Var array using Choco Solver

I'm using Choco Solver and given an array of int vars, I want a constraint that check that at least one var in the array is equal to a static value...

Something similar to IntConstraintFactory#count but with the following doc :

/**
 * Let N be the number of variables of the VARIABLES collection assigned to value VALUE;
 * Enforce condition N >= LIMIT to hold.
 * <p>
 *
 * @param VALUE an int
 * @param VARS  a vector of variables
 * @param LIMIT a variable
 */
public static Constraint at_least(int VALUE, IntVar[] VARS, IntVar LIMIT) {
    return new Constraint("At least", /* help here ? */);
}

Does someone knows if it exists or how I can implement it efficiently ?

Upvotes: 1

Views: 657

Answers (1)

Jean-Guillaume Fages
Jean-Guillaume Fages

Reputation: 249

If you want to post constraint atLeast(VALUE,VARS, LIMIT) in Choco Solver, you can simply post count(VALUE,VARS,X), with X an IntVar of initial domain [0,VARS.length], and post arithm(X,">=",LIMIT). This will do the job. There is no need of implementing a specific constraint for that.

If you want to check that at least one variable in VARS is equal to VALUE, it is even simpler, simply post count(VALUE, VARS, X) with [1,VARS.length] as the initial domain for X. So the minimal number of occurrence of VALUE will be at least 1. No need to create a second variable and the arithmetic constraint.

Upvotes: 1

Related Questions