How to add domain variable to global_cardinality?

青春壹個敷衍的年華 提交于 2019-12-10 18:37:21

问题


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,+Options)

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.

Now I can write:

global_cardinality([A,B,C], [1-2, 2-1]).

to say that the number 1 will be used twice. The number 2 will be used just once.

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?


回答1:


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)...

or

?- 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.



回答2:


?- X in 1..3, global_cardinality([A,B,C], [1-X, 2-1]).


来源:https://stackoverflow.com/questions/16399792/how-to-add-domain-variable-to-global-cardinality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!