与Clingo(答案集编程)中的选择规则作斗争



我目前正与以下问题作斗争:让我们假设这个伪代码:

% Animal:  ID, Type, Cost 
animal    (id1, dog, 300).
animal    (id2, dog, 400).
animal    (id3, dog, 600).
animal    (id1, cat, 200).
animal    (id2, cat, 400).
animal    (id3, cat, 400).
animal    (id1, fish, 20).
animal    (id2, fish, 20).
animal    (id3, fish, 20).
% Search the most expensive animals of each animal-type
most_expensive_animals(ID, Type, Cost) :- animal(ID, Type, Cost),
#max {X:animal(_, Type, X)} =  Cost.
#show most_expensive_animals/3.

答案集是:

most_expensive_animals(id3,dog,600) 
most_expensive_animals(id2,cat,400) 
most_expensive_animals(id3,cat,400) 
most_expensive_animals(id1,fish,20) 
most_expensive_animals(id2,fish,20) 
most_expensive_animals(id3,fish,20)

我想要实现的是一条规则,它只选择每种动物类型中的一种,而与本我无关(也可以随机发生(。目标是,答案集如下:

most_expensive_animals(id3,dog,600)  
most_expensive_animals(id2,cat,400) 
most_expensive_animals(id1,fish,20) 

此外,被踢出的解决方案应该保存在一个替代答案集中,如下所示:

alternative_most_expensive_animals(id3,cat,400) 
alternative_most_expensive_animals(id2,fish,20)  
alternative_most_expensive_animals(id3,fish,20) 

我遵循了一些选择规则的方法,但没有成功。有人能进一步帮我吗?

提前感谢!

您的程序是确定的,无法做出任何选择:

most_expensive_animals(ID, Type, Cost) :- ...

你可以通过声明来改变这一点

{ most_expensive_animals(ID, Type, Cost) } :- ...

因此,每一种最昂贵的动物都可以被列为最昂贵或不昂贵。你也要涵盖非案件。我个人会这样写:

1 { most_expensive_animals(ID, Type, Cost) ; alt_most_expensive_animals(ID, Type, Cost) } 1 :- ...

这意味着每一种最昂贵的动物都可以被列为同类或替代品——如果身体着火,其中一个谓词必须正确。但这里仍然存在一个问题:多只或根本没有动物可能被列入名单。我个人会写两个约束来防止这两种情况。

:- most_expensive_animals(ID, Type, Cost), most_expensive_animals(ID2, Type, Cost), ID != ID2.
% it is not possible that 2 or more IDs for the same Type and Cost appear
:- animal(_, Type, _), not most_expensive_animals(_, Type, _).
% it is not possible for every type of animal to not have a most expensive listed animal.

只能使用一个约束:

:- animal(ID, Type, Cost), #max {X:animal(_, Type, X)} =  Cost, {most_expensive_animals(IDtmp, Type, Cost)} != 1.
% for every Type/Cost pair where Cost is maxuimum for this Type, it can not be the the number of most expensive animals is different to 1.

以下是输出(使用网络版的cliano进行测试(:

clingo version 5.5.0
Reading from stdin
Solving...
Answer: 1
most_expensive_animals(id3,dog,600) most_expensive_animals(id3,cat,400) most_expensive_animals(id1,fish,20)
Answer: 2
most_expensive_animals(id3,dog,600) most_expensive_animals(id3,cat,400) most_expensive_animals(id3,fish,20)
Answer: 3
most_expensive_animals(id3,dog,600) most_expensive_animals(id3,cat,400) most_expensive_animals(id2,fish,20)
Answer: 4
most_expensive_animals(id3,dog,600) most_expensive_animals(id2,cat,400) most_expensive_animals(id1,fish,20)
Answer: 5
most_expensive_animals(id3,dog,600) most_expensive_animals(id2,cat,400) most_expensive_animals(id3,fish,20)
Answer: 6
most_expensive_animals(id3,dog,600) most_expensive_animals(id2,cat,400) most_expensive_animals(id2,fish,20)
SATISFIABLE

最新更新