Link Search Menu Expand Document
PyCSP3
Models & Data GitHub XCSP3 About

Constraint AllEqual

The constraint AllEqual enforces that the elements (typically, variables) from a specified list take the same value.

It is mainly introduced for its ease of use.

Below, we give several illustrations corresponding to representative use cases of the constraint AllEqual.

Case 1: AllEqual on Variables

The first case is when all variables from a specified list must take the same value.

To see how this constraint works, we need first to import the library PyCSP$^3$:

from pycsp3 import *

For our illustration, we introduce an array $x$ of 4 variables, each one with {0,1,2} as domain.

x = VarArray(size=4, dom=range(3))

We can display (the structure of) the array as well as the domain of the first variable.

print("Array of variable x: ", x)
print("Domain of any variable: ", x[0].dom)
Array of variable x:  [x[0], x[1], x[2], x[3]]
Domain of any variable:  0..2

We simply post a single constraint AllEqual on $x$.

satisfy(
    AllEqual(x)
);

By calling the function solve(), we can check that the problem (actually, the single constraint) is satisfiable (SAT). We can also print the values assigned to the variables in the found solution; we can call the function values() that collects the values assigned (in the last found solution) to a specified list of variables.

if solve() is SAT:
    print("Solution: ", values(x))
Solution:  [0, 0, 0, 0]

We can enumerate all solutions (supports) for this contraint by calling solve() with the named parameter sols. Note how the named parameter sol of values() is used to indicate the index of a solution.

if solve(sols=ALL) is SAT:
    for i in range(n_solutions()):
        print(f"Solution {i+1}: {values(x, sol=i)}")
Solution 1: [0, 0, 0, 0]
Solution 2: [1, 1, 1, 1]
Solution 3: [2, 2, 2, 2]

Case 2: AllEqual on Expressions

To start, we remove the last posted constraint by calling the function unpost().

unpost()

We control that there are no more constraints.

print(posted())
[]

We post a single constraint AllEqual on expressions involving the variables of $x$.

satisfy(
    AllEqual(abs(x[0]-x[1]), abs(x[1]-x[2]), abs(x[2]-x[3]))
);

Interestingly, we can display the internal representation of the posted constraint; although a little bit technical, we can see that the information is correctly recorded. Note that dist(a,b) is equivalent to abs(a-b).

print(posted())
allEqual(list:[dist(x[0],x[1]), dist(x[1],x[2]), dist(x[2],x[3])])

We can run the solver

if solve() is SAT:
    print("Solution: ", values(x))
Solution:  [0, 0, 0, 0]
By enumerating the solutions, one can observe that the three expressions are always all equal.
if solve(sols=ALL) is SAT:
    for i in range(n_solutions()):
        print(f"Solution {i+1}: {values(x, sol=i)}")
Solution 1: [0, 0, 0, 0]
Solution 2: [0, 1, 0, 1]
Solution 3: [0, 1, 2, 1]
Solution 4: [0, 2, 0, 2]
Solution 5: [2, 0, 2, 0]
Solution 6: [2, 2, 2, 2]
Solution 7: [1, 1, 1, 1]
Solution 8: [2, 1, 0, 1]
Solution 9: [2, 1, 2, 1]
Solution 10: [1, 0, 1, 0]
Solution 11: [1, 2, 1, 0]
Solution 12: [1, 2, 1, 2]
Solution 13: [1, 0, 1, 2]