我有什么似乎可以正常工作的ASP代码,但没有返回答案集?

时间:2019-06-27 15:11:06

标签: answer-set-programming clingo

我正在尝试使用答案集编程,Clingo和MKAtoms解决二进制数独难题。我有一套有效的代码,但是没有返回任何答案。

我已经检查过代码,但是不确定问题所在。

% Binary Sudoku solver: Binary Sudoku is a similar puzzle to sudoku. 
% The rules are as follows
% 1. Each row and column must have the same number of 1's and 0's
% 2. Each row and column must be unique
% 3. Each number may not appear in consecutive triples
% 
% The program works in conjuction with python 3.6, Clingo, and     Mkatoms to create the required files and solve the user input puzzle.
%
% Required Files:
%   BinaryTemp.sm
%   BinaryOut.sm
%   BinaryPy.sm - This File
%
% Using the defined rules Clingo will attempt to find an answer set that will statisfy all conditions
num(0..1).
range(0..6).

% There may only be one number in each X,Y position

1{pos(N,X,Y):num(N)}1:-num(X), num(Y).

% Each row and column must have the same number of 1’s and 0’s

% Row check - check each rows sum to see if they hold the same number of 1's 
total_row(S) :- S = #sum{ N : pos(N,X,Y1), pos(N,X+1,Y1),           pos(N,X+2,Y1), pos(N,X+3,Y1), pos(N,X+4,Y1), pos(N,X+5,Y1)}.
total_row1(S) :- S = #sum{ N : pos(N,X,Y2), pos(N,X+1,Y2),  pos(N,X+2,Y2), pos(N,X+3,Y2), pos(N,X+4,Y2), pos(N,X+5,Y2)}.
:- total_row(S) != total_row1(S), range(S).

% Column Check
total_column(S) :- S = #sum{ N : pos(N,X1,Y), pos(N,X,Y+1),    pos(N,X1,Y+2), pos(N,X1,Y+3), pos(N,X1,Y+4), pos(N,X1,Y+5)}.
total_column1(S) :- S = #sum{ N : pos(N,X2,Y), pos(N,X2,Y+1), pos(N,X2,Y+2), pos(N,X2,Y+3), pos(N,X2,Y+4), pos(N,X2,Y+5)}.
:- total_column(S) != total_column1(S), range(S).

% Each row and column must be unique

% Row check - check if 2 values in seperate rows are different
different(X1,X2) :- pos(N1,X1,Y), pos(N2,X2,Y), N1!=N2.
:-pos(N,X1,Y), pos(N,X2,Y), X1!=X2, not different(X1,X2).

% Column check
different(Y1,Y2) :- pos(N1,X,Y1), pos(N2,X,Y2), N1!=N2.
:-pos(N,X,Y1), pos(N,X,Y2), Y1!=Y2, not different(Y1,Y2).

% Each number may not appear in consecutive triples

:-pos(N,X,Y), pos(N,X+1,Y), pos(N,X+2,Y), num(N).
:-pos(N,X,Y), pos(N,X,Y+2), pos(N,X,Y+2), num(N).

#show pos/3.

输入文件:

  • pos(0,1,1)。

  • pos(1,1,4)。

  • pos(1,2,2)。

  • pos(0,2,5)。

  • pos(1,3,6)。

  • pos(1,4,3)。

  • pos(0,4,4)。

  • pos(1,5,1)。

  • pos(0,5,4)。

  • pos(0,6,2)。

  • pos(1,6,5)。

没有错误,但应返回与上述规则匹配的答案的位置。

0 个答案:

没有答案