【问题标题】:Struggles with the choice rule in Clingo (Answer-Set-Programming)与 Clingo 中的选择规则作斗争(Answer-Set-Programming)
【发布时间】:2022-01-01 18:49:54
【问题描述】:

我目前正在努力解决以下问题: 让我们假设这个伪代码:

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

我想要实现的是一个规则,它只选择一种独立于 id 的动物类型(它也可以随机发生)。目标是,答案集如下:

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) 

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

提前致谢!

【问题讨论】:

    标签: answer-set-programming clingo


    【解决方案1】:

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

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

    你可以通过声明来改变它

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

    因此,每一种最昂贵的动物都可以被列为最昂贵的或不列为最昂贵的。您还想涵盖 not 情况。我个人会这样写:

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

    意思是每一种最昂贵的动物都可以被列为这样或作为替代品——如果身体着火,这些谓词中的一个必须是真的。但是这里仍然存在一个问题:可以列出多个或根本没有动物。我个人会写 2 个约束来防止这两种情况。

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

    这是输出(用 cligo 的web version 测试):

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-27
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2022-06-10
      • 2022-01-08
      相关资源
      最近更新 更多