【问题标题】:Shorthand for multiple choice predicates in clingocligo 中多项选择谓词的简写
【发布时间】:2021-08-02 16:25:07
【问题描述】:

现在我有一个单选谓词来定义我的搜索空间。

#const nRounds = 3.
#const nPlayers = 17.
#const nSeats = nRounds * nPlayers.
#const nRooms = 3.
#const nDecks = 6.

nSeats { seat(1..nPlayers, 1..nRooms, 1..nDecks) } nSeats.

我想限制这个搜索空间,因为我开始遇到性能问题。在我的设置中,每个玩家只能出现在 4 个“座位”谓词中,所以我想要以下内容:

#for i in 1..nPlayers
nRounds { seat(i, 1..nRooms, 1..nDecks) } nRounds.
#endfor

这在内部基本上会变成这样:

nRounds { seat(1, 1..nRooms, 1..nDecks) } nRounds.
nRounds { seat(2, 1..nRooms, 1..nDecks) } nRounds.
nRounds { seat(3, 1..nRooms, 1..nDecks) } nRounds.
...

当然,我可以“自己拼出来”,即使用另一种语言来生成这些行,但我不明白为什么这在 clgo 中不存在。

我首先寻找一种方法的原因是因为我预计(nRooms*nDecks choose nRounds) * nPlayers 会比(nRooms*nDecks*nPlayers) choose nRounds*nPlayers 小得多,而{seat(P, R, D)} 的@987654327 又比{seat(P, R, D)} 好得多@这是我开始的。

即使我的代码中的约束限制了我最终可以得到的实际可能集合,因此所有这三种表示形式都是等效的,但仍然可以从这种手动搜索空间修剪中获得性能优势,对吧?

【问题讨论】:

    标签: logic-programming answer-set-programming clingo


    【解决方案1】:

    您正在寻找的“for 循环”是一条直截了当的规则:

    nRounds { seat(S, 1..nRooms, 1..nDecks) } nRounds :- S = 1..nPlayers.
    

    使用clingo --text检查您的代码的差异

    我还建议您将 seat(P,R,D) 谓词分成几个较小的谓词(取决于您的问题。 就像每个玩家都被分配了一个房间p2r(P,R),每个房间都被分配到一个甲板r2d(R,D)。这样,您以后可以在约束中节省大量不必要的组合。当然,我的两个谓词可能对您的问题没有意义,但我相信您会找到一个组合来拆分您的座位谓词。

    【讨论】:

    • 太棒了,所以我想你可以对集合做同样的事情,例如S = (a;b;c)?有多个套牌,每个套牌都在多个房间里播放,所以我不认为这可以进一步简化。想想测验之夜 - 套牌可以是生物、化学、物理,每个套牌由不同的人在多个房间平行播放。我可以将seat(P,R,D) 拆分为seat(P, ID)round(ID, R, D),但我认为这不会产生任何影响,因为最终|ID| 将等于|R|*|D|,对吧?
    • 当然,试试--textround(1..5). player(1..5). deck(1..5). room(1..5). %Assign every player every round exactly one room 1{where(P,R,X) : room(X)} 1 :- player(P), round(R). %Assign every room every round exactly one deck 1{what(X,R,D) : deck(D)}1:- room(X), round(R).
    • {what(X,R,D): deck(D)}{what(X,R,1..5)}有区别吗?
    • 不,只是更具可重用性和可读性。当然,deck 可以是名称,而不仅仅是数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多