1

I am attempting to solve a Binary sudoku puzzle with Answer Set Programming, Clingo, and MKAtoms. I have a working set of code, but it is not returning any answer sets.

I have reviewed the code but am unsure of the problem.

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

Input File:

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

There are no errors but it should return positions of answers matching the rules stated above.

DuDa
  • 3,718
  • 4
  • 16
  • 36
Cody West
  • 11
  • 1
  • 1
    If the solver gives you UNSAT when you think there should be answer sets, you can debug it by commenting out some of your filters. Here there seems to be at least problems in `total_row` and `total_column` (I'm not sure if the `total_column(S) != total_column1(S)` syntax will work at all) and consecutive triples filters. The consecutive triples filter has a simple typo. Also your generator should probably be `1{pos(N,X,Y):num(N)}1:-range(X), range(Y).` and not `...:-num(X), num(Y)`. I'd recommend going through your code slowly and with thought. – vukk Jun 27 '19 at 20:53

0 Answers0