[英]Using in_set/2 Constraint
I am trying to use constrain X
to not being a value in a list. 我试图使用约束
X
而不是列表中的值。
From the SICStus Prolog manual : 从SICStus Prolog手册中 :
?X in_set +FDSet
I can't figure out how to convert a list to a FDSet, though. 不过,我不知道如何将列表转换为FDSet。 I have a list of integers
[2,3,8,9]
and I want to constrain the domain of variable X
to not being in that list. 我有一个整数列表
[2,3,8,9]
,我想将变量X
的域限制为不在该列表中。 How do I do that? 我怎么做? Thanks.
谢谢。
Judging from the documentation , what about list_to_fdset/2
? 从文档来看,
list_to_fdset/2
呢? You can translate to an FDSet
, then build its complement , then post in_set/2
. 您可以转换为
FDSet
,然后构建其补集 ,然后发布in_set/2
。 If your version does not have list_to_fdset/2
, you can easily convert the list to a normal domain expression, and then post a negated in/2
constraint. 如果您的版本没有
list_to_fdset/2
,则可以轻松地将列表转换为普通的域表达式,然后发布取反的in/2
约束。 In your example, you would then post: 在您的示例中,您将发布:
#\ X in {2}\/{3}\/{8}\/{9}
and you only have to describe the relation between a list and a domain expression consisting of singletons, which is easy: 并且您只需要描述列表和由单例组成的域表达式之间的关系,这很容易:
list_domain([I|Is], Dom) :-
foldl(integer_domain_, Is, {I}, Dom).
integer_domain_(I, D0, D0 \/ {I}).
Example queries: 查询示例:
?- list_domain([1,2,3], Dom).
Dom = {1}\/{2}\/{3}.
?- list_domain([1,2,3], Dom), X in Dom.
Dom = {1}\/{2}\/{3},
X in 1..3.
I implemented one like here.. 我在这里实现了一个
/** Constraint domain to memebers of a list (of numbers only) **/
domain_list_constraint(_, []) :- !.
domain_list_constraint(DomainVar, List) :- member(E, List),
(atom(E)->atom_number(E, I),
DomainVar #= I;
DomainVar #= E).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.