如何将域变量添加到 global_cardinality? [英] How to add domain variable to global_cardinality?
问题描述
我正在尝试将约束 global_cardinality
添加到我的程序和 SICStus Prolog 手册 是这样写的:
I'm trying to add a constraint global_cardinality
to my program and in the manual of SICStus Prolog is written:
global_cardinality(+Xs,+Vals)
global_cardinality(+Xs,+Vals)
global_cardinality(+Xs,+Vals,+Options)
global_cardinality(+Xs,+Vals,+Options)
其中 Xs = [X1,...,Xd] 是整数或域变量的列表,并且Vals = [K1-V1,...,Kn-Vn] 是一个对的列表,其中每个键 Ki 是一个唯一整数,Vi 是域变量或整数.真如果Xs 的每个元素都等于某个键,并且对于每对 Ki-Vi,Xs 的 Vi 元素正好等于 Ki.
where Xs = [X1,...,Xd] is a list of integers or domain variables, and Vals = [K1-V1,...,Kn-Vn] is a list of pairs where each key Ki is a unique integer and Vi is a domain variable or an integer. True if every element of Xs is equal to some key and for each pair Ki-Vi, exactly Vi elements of Xs are equal to Ki.
现在我可以写了:
global_cardinality([A,B,C], [1-2, 2-1]).
表示数字 1
将被使用两次.数字 2
将只使用一次.
to say that the number 1
will be used twice. The number 2
will be used just once.
但是我想说1
这个数字会被用到:一次、两次或三次
But I would like to say that the number 1
will be used: once, twice or three times
根据手册,我需要一个域变量,但正确的语法是什么?
According to the manual I need a domain variable but what is the proper syntax for that?
推荐答案
对此不确定,但来自 SWI-Prolog 页面 我想你可以试试
not sure about this, but from SWI-Prolog page I think you could try
...global_cardinality([A,B,C], [1-X, 2-1]), (X #= 1 #\/ X #= 2 #\/ X #= 2)...
或
?- global_cardinality([A,B,C], [1-X, 2-1]), X in 1..3, label([A,B,C]).
A = B, B = 1,
C = X, X = 2 ;
A = C, C = 1,
B = X, X = 2 ;
A = X, X = 2,
B = C, C = 1.
这篇关于如何将域变量添加到 global_cardinality?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!