关于 ASP/Clingo 中完整性约束和选择规则之间区别的新手问题?

Newbie question about the difference between integrity constraints and choice rules in ASP/Clingo?

#const w = 2.
#const h = 1.

% Create a grid wxh
row(1..h).
col(1..w).

% Generate
% {} below indicates all rooms need not exist in the solution.
% There can be 1x1 rooms, that can be placed anywhere on the grid.
{room11(X, Y) : col(X), row(Y) }.
% 2x1 (width 2) rooms can be placed anywhere except the last column,
% because that will take it out of the grid
{room21(X, Y) : col(X), col(X+1), row(Y) }.

% Calculate what areas the generated rooms cover.
% Multiple rooms covering the same X,Y , or no room covering it,
% would indicate a invalid model.

% A 1x1 room covers its own location. 
covered(room11(X, Y), X, Y) :- room11(X, Y) .

% Put all generated rooms in a list
_r(room11(X, Y)) :- room11(X, Y) .

% A 2x1 room covers X, Y and X+1, Y
covered(room21( X, Y), X, Y) :- room21(X, Y) .
covered(room21( X-1, Y), X, Y) :- room21(X-1, Y) .

% Put all generated rooms in a list
_r(room21(X, Y)) :- room21(X, Y) .

% Each grid point can be covered by one and only one room.
% There can be only one covered(_,X,Y) in the model.
1 {covered(R, X, Y) : _r(R) } 1 :- col(X), row(Y) .

% Count the number of cover(_, X, Y) atoms for debugging
cc (X, Y, N) :- N = {covered(R, X, Y) : _r(R) } , col(X), row(Y) .

在上面的评论中解释了我的想法。

我期待 2 个答案

room11(1,1) room11(2,1)

room21(1,1)

但是当我列举所有答案时,我也得到了很多其他不一致的答案。

我做错了什么?

Answer: 1
row(1) col(1) col(2) room11(1,1) _r(room11(1,1)) 
covered(room11(1,1),1,1) cc(1,1,1) covered(room11(1,1),2,1) cc(2,1,1)

原子 covered(room11(1,1),2,1) 是如何形成的?是 1 {covered(R, X, Y) : _r(R) } 1 :- col(X), row(Y) . 造成的吗?正确的写法是什么?

Answer: 10
row(1) col(1) col(2)
room11(1,1) room11(2,1) _r(room11(1,1)) _r(room11(2,1)) room21(1,1) _r(room21(1,1))
covered(room21(1,1),1,1) covered(room21(1,1),2,1)
cc(1,1,1) cc(2,1,1)

room11(1,1) 怎么可能是真的,而 covered(room11(1,1),1,1) 却没有 true/not 出现在模型中?

规则头部的一切都可以推导出来。您不想无缘无故地导出 covered/2。因此,您必须用以下规则替换 covered/2 的生成规则:

% Each grid point can be covered by one and only one room.
% There can be only one covered(_,X,Y) in the model.
:- 1 != {covered(R, X, Y)}, col(X), row(Y).

这为您提供了 2 个预期的解决方案。

PS:如果你想概括你的编码,你应该把房间的尺寸作为房间谓词的参数,例如room(Width,Height,PosX,PosY).