[英]CLP: Efficient model of 'not three same values'
I need to model this (simple) constraint in Eclipse CLP: 我需要在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. 给定三个域变量,让我们说D1
, D2
和D3
,我想确保这三个变量不会以相同的值结束。 Two of them can have equal value. 其中两个可以有相同的价值。
Version 1 版本1
My first idea was something like: 我的第一个想法是:
D1 #\\= D2 or D1 #\\= D3
But I do not like disjunctions in the model. 但我不喜欢模型中的脱节。
Version 2 版本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. 我正在考虑alldifferent([D1,D2,D3],2)
或neg nvalue([D1,D2,D3],1)
但我不确定它是不是因为这么简单的用法而过于复杂。
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. 使用nvalue(N, X)
然后将N
约束为大于1( N #> 1
)将要求应该有2或3个不同的值。
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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.