问题
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