This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
forked from hkociemba/sudokuNxM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnf.pas
459 lines (428 loc) · 14.2 KB
/
cnf.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// ******************************************************************************
// Code to generate the clauses for an n*m sudoku for use in a cnf-file
// ******************************************************************************
unit cnf;
interface
uses Classes;
var
n_clauses_eff: Integer;
function generate_cnf(clauses: TStringList): Integer;
procedure decode_solution(output, solution: TStringList);
function varname(r, c, v: Integer): String;
implementation
uses SysUtils, StrUtils, main;
// the variable name for a candidate in row r, column c and value v
// r>=0,x>=0, v>=1 and hence varname>=1.
function varname(r, c, v: Integer): String;
begin
result := IntToStr(r * DIM * DIM + c * DIM + v);
end;
procedure decode_solution(output, solution: TStringList);
var
a: array of array of Integer;
i, r, c, v, n: Integer;
solution_raw, s: String;
solution_split: TArray<String>;
begin
Setlength(a, DIM, DIM);
solution_raw := '';
for i := 0 to output.Count - 1 do
begin
s := output.Strings[i];
if s[1] = 's' then
begin
if ContainsText(s, 'UNSATISFIABLE') then
begin
// Form1.Memo1.Lines.Add('Puzzle is unsolvable.');
Exit;
end;
// Form1.Memo1.Lines.Add(copy(s, 3, Length(s)) + ':');
end;
if (s[1] = 'c') and ContainsText(s, 'Total wall clock time') then
begin
solution_split := custom_split(s);
Form1.Memo1.Lines.Add(copy(s, 3, Length(s)));
end;
if s[1] = 'v' then
solution_raw := solution_raw + copy(s, 3, Length(s));
end;
solution_split := custom_split(solution_raw);
for i := 0 to Length(solution_split) - 1 do
try
n := StrToInt(solution_split[i]);
// if Abs(n) > DIM * DIM * DIM then // eventually commander variables
// continue;
if n > 0 then
begin
v := (n - 1) mod DIM + 1;
n := (n - 1) div DIM;
c := n mod DIM;
r := n div DIM;
a[r, c] := v;
rc_set[DIM * r + c] := v; // update puzzle definition
end;
except
on EConvertError do
end;
for r := 0 to DIM - 1 do
begin
s := '';
for c := 0 to DIM - 1 do
if DIM < 10 then
s := s + Format('%2d', [a[r, c]])
else if DIM < 100 then
s := s + Format('%3d', [a[r, c]])
else if DIM < 1000 then
s := s + Format('%4d', [a[r, c]]);
solution.Add(s) // in the current version we do not use solution any more
end;
end;
procedure cell_clauses(clauses: TStringList);
var
row, col, num, num2: Integer;
s: String;
begin
for row := 0 to DIM - 1 do
for col := 0 to DIM - 1 do
begin
if rc_set[DIM * row + col] = 0 then // cell not occupied
begin
// each cell contains at least one number, dim^2 clauses
s := '';
for num := 1 to DIM do // iterate over numbers
if rcQ(row, col, num - 1) then
s := s + varname(row, col, num) + ' ';
if s = '' then // cnf is unsolvable, no candidates for this cell
s := varname(row, col, 1) + ' '; // add arbitrary candidate
clauses.Add(s + '0');
Inc(n_clauses_eff);
end
else
begin // clause for already set number
clauses.Add(varname(row, col, rc_set[DIM * row + col]) + ' 0');
Inc(n_clauses_eff);
end;
// never two different numbers in one cell, dim^2*Binomial(dim,2) clauses
// but if the candidate (r,c,num) or (r,c,num2) are deleted, the clause is unnecessary
for num := 1 to DIM - 1 do
if rcQ(row, col, num - 1) or (rc_set[DIM * row + col] = num) then
for num2 := num + 1 to DIM do
if rcQ(row, col, num2 - 1) or (rc_set[DIM * row + col] = num2) then
begin
clauses.Add('-' + varname(row, col, num) + ' -' + varname(row,
col, num2) + ' 0');
Inc(n_clauses_eff);
end;
end;
end;
procedure row_clauses(clauses: TStringList);
var
row, col, col2, num: Integer;
s: String;
begin
for row := 0 to DIM - 1 do
for num := 1 to DIM do
begin
// each row contains each number at least once, dim^2 clauses
s := '';
for col := 0 to DIM - 1 do // iterate over columns of row
if rcQ(row, col, num - 1) then
s := s + varname(row, col, num) + ' ';
if s <> '' then
begin
clauses.Add(s + '0');
Inc(n_clauses_eff);
end;
// never two same numbers in one row, dim^2*Binomial(dim,2) clauses
for col := 0 to DIM - 2 do
if rcQ(row, col, num - 1) or (rc_set[DIM * row + col] = num) then
for col2 := col + 1 to DIM - 1 do
if rcQ(row, col2, num - 1) or (rc_set[DIM * row + col2] = num) then
begin
clauses.Add('-' + varname(row, col, num) + ' -' + varname(row,
col2, num) + ' 0');
Inc(n_clauses_eff);
end;
end;
end;
procedure column_clauses(clauses: TStringList);
var
row, row2, col, num: Integer;
s: String;
begin
for col := 0 to DIM - 1 do
for num := 1 to DIM do
begin
// each column contains each number at least once, dim^2 clauses
s := '';
for row := 0 to DIM - 1 do // iterate over rows of column
if rcQ(row, col, num - 1) then
s := s + varname(row, col, num) + ' ';
if s <> '' then
begin
clauses.Add(s + '0');
Inc(n_clauses_eff);
end;
// never two same numbers in one column, dim^2*Binomial(dim,2) clauses
for row := 0 to DIM - 2 do
if rcQ(row, col, num - 1) or (rc_set[DIM * row + col] = num) then
for row2 := row + 1 to DIM - 1 do
if rcQ(row2, col, num - 1) or (rc_set[DIM * row2 + col] = num) then
begin
clauses.Add('-' + varname(row, col, num) + ' -' + varname(row2,
col, num) + ' 0');
Inc(n_clauses_eff);
end;
end;
end;
procedure block_clauses(clauses: TStringList);
var
blk, k, k2, num: Integer;
s: String;
begin
for blk := 0 to DIM - 1 do // block
begin
for num := 1 to DIM do
begin
// each block contains each number at least once, dim^2 clauses
s := '';
for k := 0 to DIM - 1 do // iterate over block elements
if rcQ(bk_to_r(blk, k), bk_to_c(blk, k), num - 1) then
s := s + varname(bk_to_r(blk, k), bk_to_c(blk, k), num) + ' ';
if s <> '' then
begin
clauses.Add(s + '0');
Inc(n_clauses_eff);
end;
// never two same numbers in same block, dim^2*Binomial(dim,2) clauses
for k := 0 to DIM - 2 do
if rcQ(bk_to_r(blk, k), bk_to_c(blk, k), num - 1) or
(rc_set[DIM * bk_to_r(blk, k) + bk_to_c(blk, k)] = num) then
for k2 := k + 1 to DIM - 1 do
if rcQ(bk_to_r(blk, k2), bk_to_c(blk, k2), num - 1) or
(rc_set[DIM * bk_to_r(blk, k2) + bk_to_c(blk, k2)] = num) then
begin
clauses.Add('-' + varname(bk_to_r(blk, k), bk_to_c(blk, k), num) +
' -' + varname(bk_to_r(blk, k2), bk_to_c(blk, k2), num) + ' 0');
Inc(n_clauses_eff);
end;
end;
end;
end;
// Positionen
procedure pos_clauses(clauses: TStringList);
var
pos, blk, blk2, num: Integer;
s: String;
begin
for pos := 0 to DIM - 1 do // positions
begin
for num := 1 to DIM do
begin
// each pos contains each number at least once, dim^2 clauses
s := '';
for blk := 0 to DIM - 1 do // iterate over blocks
if rcQ(bk_to_r(blk, pos), bk_to_c(blk, pos), num - 1) then
s := s + varname(bk_to_r(blk, pos), bk_to_c(blk, pos), num) + ' ';
if s <> '' then
begin
clauses.Add(s + '0');
Inc(n_clauses_eff);
end;
// never two same numbers in same position, dim^2*Binomial(dim,2) clauses
for blk := 0 to DIM - 2 do
if rcQ(bk_to_r(blk, pos), bk_to_c(blk, pos), num - 1) or
(rc_set[DIM * bk_to_r(blk, pos) + bk_to_c(blk, pos)] = num) then
for blk2 := blk + 1 to DIM - 1 do
if rcQ(bk_to_r(blk2, pos), bk_to_c(blk2, pos), num - 1) or
(rc_set[DIM * bk_to_r(blk2, pos) + bk_to_c(blk2, pos)] = num) then
begin
clauses.Add('-' + varname(bk_to_r(blk, pos), bk_to_c(blk, pos),
num) + ' -' + varname(bk_to_r(blk2, pos), bk_to_c(blk2, pos),
num) + ' 0');
Inc(n_clauses_eff);
end;
end;
end;
end;
// clauses for Windoku
procedure window_clauses(clauses: TStringList);
var
win, k, k2, num: Integer;
s: String;
begin
for win := 0 to DIMWIN - 1 do // Window
begin
for num := 1 to DIM do
begin
// each window contains each number at least once, dim*dimwin clauses
s := '';
for k := 0 to DIM - 1 do // iterate over window elements
if rcQ(wk_to_r(win, k), wk_to_c(win, k), num - 1) then
s := s + varname(wk_to_r(win, k), wk_to_c(win, k), num) + ' ';
if s <> '' then
begin
clauses.Add(s + '0');
Inc(n_clauses_eff);
end;
// never two same numbers in same window, dim*dimwin*Binomial(dim,2) clauses
for k := 0 to DIM - 2 do
if rcQ(wk_to_r(win, k), wk_to_c(win, k), num - 1) or
(rc_set[DIM * wk_to_r(win, k) + wk_to_c(win, k)] = num) then
for k2 := k + 1 to DIM - 1 do
if rcQ(wk_to_r(win, k2), wk_to_c(win, k2), num - 1) or
(rc_set[DIM * wk_to_r(win, k2) + wk_to_c(win, k2)] = num) then
begin
clauses.Add('-' + varname(wk_to_r(win, k), wk_to_c(win, k), num) +
' -' + varname(wk_to_r(win, k2), wk_to_c(win, k2), num) + ' 0');
Inc(n_clauses_eff);
end;
end;
end;
end;
procedure XSudoku_clauses(clauses: TStringList);
var
k, k2, num: Integer;
s: String;
begin
for num := 1 to DIM do
begin
s := '';
for k := 0 to DIM - 1 do // iterate over diagonal
if rcQ(k, k, num - 1) then
s := s + varname(k, k, num) + ' ';
if s <> '' then
begin
clauses.Add(s + '0');
Inc(n_clauses_eff);
end;
s := '';
for k := 0 to DIM - 1 do // iterate over antidiagonal
if rcQ(DIM - 1 - k, k, num - 1) then
s := s + varname(DIM - 1 - k, k, num) + ' ';
if s <> '' then
begin
clauses.Add(s + '0');
Inc(n_clauses_eff);
end;
for k := 0 to DIM - 2 do
if rcQ(k, k, num - 1) or (rc_set[DIM * k + k] = num) then
for k2 := k + 1 to DIM - 1 do
if rcQ(k2, k2, num - 1) or (rc_set[DIM * k2 + k2] = num) then
begin
clauses.Add('-' + varname(k, k, num) + ' -' + varname(k2, k2,
num) + ' 0');
Inc(n_clauses_eff);
end;
for k := 0 to DIM - 2 do
if rcQ(DIM - 1 - k, k, num - 1) or (rc_set[DIM * (DIM - 1 - k) + k] = num)
then
for k2 := k + 1 to DIM - 1 do
if rcQ(DIM - 1 - k2, k2, num - 1) or
(rc_set[DIM * (DIM - 1 - k2) + k2] = num) then
begin
clauses.Add('-' + varname(DIM - 1 - k, k, num) + ' -' +
varname(DIM - 1 - k2, k2, num) + ' 0');
Inc(n_clauses_eff);
end;
end;
end;
procedure NCPlus_clauses(clauses: TStringList);
var
i, row, row2, col, col2, num, num2: Integer;
//H2164 Variable 's' is declared but never used in 'NCPlus_clauses'
//s: String;
const
DP = 1; // Up to this value cyclical differences are not allowed
begin
for i := 1 to DP do
for num := 1 to DIM do
begin
num2 := (num - 1 + i) mod DIM + 1;
for row := 0 to DIM - 1 - 1 do
for col := 0 to DIM - 1 do
begin
row2 := row + 1;
col2 := col;
clauses.Add('-' + varname(row, col, num) + ' -' + varname(row2, col2,
num2) + ' 0');
Inc(n_clauses_eff);
clauses.Add('-' + varname(row, col, num2) + ' -' + varname(row2, col2,
num) + ' 0');
Inc(n_clauses_eff);
end;
for col := 0 to DIM - 1 - 1 do
for row := 0 to DIM - 1 do
begin
col2 := col + 1;
row2 := row;
clauses.Add('-' + varname(row, col, num) + ' -' + varname(row2, col2,
num2) + ' 0');
Inc(n_clauses_eff);
clauses.Add('-' + varname(row, col, num2) + ' -' + varname(row2, col2,
num) + ' 0');
Inc(n_clauses_eff);
end;
//add this for NC+ of the two diagonals
// for row := 0 to DIM - 1 - 1 do
// begin
// col := row;
// row2 := row + 1;
// col2 := col + 1;
// clauses.Add('-' + varname(row, col, num) + ' -' + varname(row2, col2,
// num2) + ' 0');
// Inc(n_clauses_eff);
// clauses.Add('-' + varname(row, col, num2) + ' -' + varname(row2, col2,
// num) + ' 0');
// Inc(n_clauses_eff);
// end;
//
// for row := 0 to DIM - 1 - 1 do
// begin
// col := DIM - row - 1;
// row2 := row + 1;
// col2 := DIM - row2 - 1;
// clauses.Add('-' + varname(row, col, num) + ' -' + varname(row2, col2,
// num2) + ' 0');
// Inc(n_clauses_eff);
// clauses.Add('-' + varname(row, col, num2) + ' -' + varname(row2, col2,
// num) + ' 0');
// Inc(n_clauses_eff);
// end;
end;
end;
// generate cnf file in DIMACS format
function generate_cnf(clauses: TStringList): Integer;
var
DIM, n_variables: Integer;
n_clauses: UInt64;
begin
n_clauses_eff := 0;
DIM := B_ROW * B_COL;
//W1073 Combining signed type and unsigned 64-bit type - treated as an unsigned type
{$WARN COMBINING_SIGNED_UNSIGNED64 OFF}
n_clauses := UInt64(4) * DIM * DIM * (DIM * (DIM - 1) div 2 + 1);
{$WARN COMBINING_SIGNED_UNSIGNED64 DEFAULT}
n_variables := DIM * DIM * DIM;
clauses.Clear;
clauses.Add('c CNF file in DIMACS format');
clauses.Add('c Unreduced the problem could have up to ' + IntToStr(n_clauses)
+ ' clauses');
clauses.Add('dummy'); // clauses.Strings[2]
cell_clauses(clauses);
row_clauses(clauses);
column_clauses(clauses);
block_clauses(clauses);
if Form1.CheckSudokuX.Checked then
XSudoku_clauses(clauses);
if Form1.CheckSudokuP.Checked then
pos_clauses(clauses);
if Form1.CheckSudokuW.Checked then
window_clauses(clauses);
if Form1.CheckNC.Checked then
NCPlus_clauses(clauses);
clauses.Strings[2] := 'p cnf ' + IntToStr(n_variables) + ' ' +
IntToStr(n_clauses_eff);
result := n_clauses_eff;
end;
end.