I need to model this (simple) constraint in Eclipse CLP:

Given three domain variables, lets say D1, D2, and D3 and I want to ensure that these three variables will not end up with the same value. Two of them can have equal value.

Version 1

My first idea was something like:

D1 #\= D2 or D1 #\= D3

But I do not like disjunctions in the model.

Version 2

Then I changed the model to the form of implications:

D1 #= D2 => D1 #\= D3

Is there some more efficient way how to model this constraint?

I was thinking about alldifferent([D1,D2,D3],2) or neg nvalue([D1,D2,D3],1) but I am not sure it is not overcomplicated for such a simple usage.

1 Answers

3
hakank On

Using nvalue(N, X) and then constrain N to be larger than 1 (N #> 1) will require that there should be 2 or 3 distinct values.

Example:

:-lib(ic).
:-lib(ic_search).
:-lib(ic_global).

go :-
    Len = 3,
    dim(X,[Len]),
    X :: 1..Len,
    N :: 1..Len,        

    nvalue(N,X),
    N #> 1,

    term_variables([X],Vars),
    search(Vars,0,first_fail,indomain,complete,[]),

    writeln([n:N, x:X]),
    fail.

The model give the following solutions:

[n : 2, x : [](1, 1, 2)]
[n : 2, x : [](1, 1, 3)]
[n : 2, x : [](1, 2, 1)]
[n : 2, x : [](1, 2, 2)]
[n : 3, x : [](1, 2, 3)]
[n : 2, x : [](1, 3, 1)]
[n : 3, x : [](1, 3, 2)]
[n : 2, x : [](1, 3, 3)]
[n : 2, x : [](2, 1, 1)]
[n : 2, x : [](2, 1, 2)]
[n : 3, x : [](2, 1, 3)]
[n : 2, x : [](2, 2, 1)]
[n : 2, x : [](2, 2, 3)]
[n : 3, x : [](2, 3, 1)]
[n : 2, x : [](2, 3, 2)]
[n : 2, x : [](2, 3, 3)]
[n : 2, x : [](3, 1, 1)]
[n : 3, x : [](3, 1, 2)]
[n : 2, x : [](3, 1, 3)]
[n : 3, x : [](3, 2, 1)]
[n : 2, x : [](3, 2, 2)]
[n : 2, x : [](3, 2, 3)]
[n : 2, x : [](3, 3, 1)]
[n : 2, x : [](3, 3, 2)]