0% found this document useful (0 votes)
16 views5 pages

Generating Cellular Puzzles With Logic P

Uploaded by

mrodelgo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Generating Cellular Puzzles With Logic P

Uploaded by

mrodelgo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Generating cellular puzzles with logic programs

Raphael Finkel Wiktor Marek Mirek Truszczyński


[email protected] [email protected] [email protected]
Department of Computer Science, University of Kentucky

Abstract 2. Generating a random complete


solution
We show how to characterize puzzles by logic programs
and how to use those characterizations to build puzzles
automatically. We can control the difficulty level of the We begin by expressing the constraints in a logic
puzzles by choosing how and when to invoke the logic formalism. For this paper, we present constraints
program. using the Aspps [ET01] answer-set formalism, al-
though many alternatives, such as smodels [NS97]
and dlv [EFLP00] exist.
1. Introduction Given n as the width of a section (for us, 3), and
m the width of the puzzle (for us, 9), the logic rules
Cellular puzzles take the form of cells whose val- of Figure 2 represent the constraints.
ues are constrained by rules involving groups of
cells. The puzzle starts with clues, which are cells Lines 1 and 2 introduce numsmall and num-
that already possess their value. It is the pleasant large as predicates true over the range 1 . . .3 and
task of the puzzle enthusiast to solve the puzzle 1 . . . 9, respectively. Lines 3, 4, and 5 give the sig-
by entering values in all the cells in a way that sat- nature of the ternary predicate place and the vari-
isfies the constraints. A well-formed puzzle has ables I, J, N, K, and M. We use place(I,J,N) to
exactly one solution. To assist the enthusiast, the represent the fact that the value N is placed in cell
puzzle may contain a hint sequence, which is a se- (I,J). Line 6 can be read “given a cell (I,J),
quence of cells that suggest the order in which the there is at least one and at most one (that is, ex-
enthusiast should be able to complete the puzzle. actly one) value N that is placed in that cell.” Line
7 represents the constraint that there is exactly one
We consider Sudoku puzzles [TFE05] as a typ-
row I for which any given value N appears in col-
ical cellular puzzle. Figure 1 shows a sample Su-
umn J. Line 8 constrains columns in a similar way.
doku puzzle. The cells are arranged in a 9 × 9 grid
Lines 9 and 10 introduce the section constraints.
subdivided into nine 3 × 3 sections. We say a set
Given a section numbered (K,M) and a value N,
of 9 cells is complete if the numbers 1 . . . 9 appear
there is exactly one cell (I,J) in that section that
exactly once in that set. The constraints that link
has that value.
the cells are that each row, column, and section is
complete. The first hint, bE, directs our attention When we run these rules as a logic program, we
to a cell that must contain 2, because in its section, get a very large number of results. We pick the first
all other locations for 2 are excluded by being filled result as a desired solution, relying on the random-
in with other numbers or because their row or col- ization performed by Aspps. We also randomize
umn already has a 2. the solution by randomly preloading values in a
This paper shows how we use logic programs few cells. If our preloading is inconsistent with the
to generate cellular puzzles of varying degrees of constraints, we choose a different random preload-
difficulty. ing and repeat.
1 numsmall(1..n).
2 numlarge(1..m).

Puzzle 3 pred place(numlarge, numlarge,


A B C D E F G H I numlarge).
a 8 2 3 4 var numlarge I,J,N.
5 var numsmall K,M.
b 4
c 6 7 5 9 6 1 { place(I,J,N)[N] } 1.
d 2 7 1 { place(I,J,N)[I] } 1.
e 9 8 3 8 1 { place(I,J,N)[J] } 1.
f 4 8 1 6
9 1 { place(I,J,N)[I,J]: I<=n*K:
J<=n*M: n*(K-1) < I:
g 3 8 5
10 n*(M-1) < J} 1.
h 7 1
i 1 5 2 6 Fig. 2. Aspps rules for the Sudoku puzzle

removableClues := solution
Solution preservedClues := Ø
A B C D E F G H I
while removableClues 6= Ø do
a 5 7 4 8 1 9 6 2 3 victimClue :=
b 8 9 1 3 2 6 5 7 4 choose(removableClues)
c 6 2 3 4 7 5 9 8 1 removableClues -:= {victimClue}
d 3 8 5 1 6 2 7 4 9 if puzzleBad(removableClues
+ preservedClues) then
e 2 1 6 9 4 7 8 3 5
preservedClues +:= {victimClue}
f 9 4 7 5 3 8 2 1 6 end if
g 7 3 8 6 5 4 1 9 2 end while
h 4 6 2 7 9 1 3 5 8 clues := preservedClues
i 1 5 9 2 8 3 4 6 7
Fig. 3. Algorithm to reduce the clue set

Hints 3. Reducing the solution to a minimal


1:bE 2:eF 3:eE 4:fE 5:hG 6:iF set of clues
7:fD 8:cH 9:cI 10:gG 11:fG 12:cB
13:eI 14:hH 15:bH 16:eA 17:hC 18:gI We take the solution as the initial set of clues and
19:gA 20:fC 21:fA 22:hA 23:aA 24:dC reduce the clue set until it is minimal by the algo-
25:bG 26:aG 27:aB 28:dA 29:dB 30:bA rithm of Figure 3. Each iteration randomly picks
31:iG 32:iC 33:bB 34:bF 35:gD 36:gF a single clue and tries to solve the puzzle with-
37:cD 38:bD 39:cC 40:bC 41:eB 42:eC out it. If the puzzle is now bad (we’ll explain that
43:hB 44:aE 45:dD 46:dE 47:aF 48:hE shortly), the clue is added to the set of fixed clues,
49:iE 50:hI 51:iI 52:dG 53:dI 54:gH which must be preserved. Eventually, all the origi-
55:dH 56:aC nal clues are either removed or preserved; the pre-
served clues become the final set of clues.
Fig. 1. A 9 × 9 Sudoku puzzle, its solution, and The only complex part of this algorithm is the
hints puzzleBad function, which determines whether
a given set of clues makes a good puzzle. We try
solving the puzzle with Aspps. The Aspps soft-
ware has two stages, the grounder and the solver.
The grounder expands the rules of Figure 2 for all size initial time iteration time mem
possible values of the variables, and then it uses 16 × 16 0.2 0.10 4
unit propagation to determine as many facts as it 25 × 25 0.6 – 2.7 0.14 6
can. It is able to look ahead one step, that is, try 36 × 36 4.8 – 54 0.24 14
setting a fact to true and to false hoping to find a 49 × 49 22 – 300 0.49 30
solution in only one of those cases. The solver em-
ploys full backtrack, using a heuristic to pick facts
Fig. 4. Time (seconds) and memory (MB) require-
to assert and following the consequences. The fol-
ments for generating Sudoku puzzles
lowing results can arise from applying Aspps.

• The puzzle has no solutions. This situation


should never happen, because each iteration 4. Generating a hint sequence
tests a less-constrained puzzle than the pre-
vious one.
In a cellular puzzle, most atoms turn out to be false.
For example, in the puzzle of Figure 1 (page 2), it
• The grounder finds a solution without looka-
turns out that place(1,1,5) is true. Therefore
head. The puzzle is “good” and the clue may
the grounder at some point also derives that
be safely removed.
place(1,1,1) is false, as is place(1,1,2) and
all the other related values. The positive (true) in-
• The grounder finds the solution, but it needs stances of place are the interesting ones. By ex-
lookahead. Depending on how difficult we amining the grounder’s log, we can determine the
wish to make the puzzle, we might call the order in which it discovers positive atoms by unit
puzzle either “good” or “bad”. In practice, propagation. That list begins with the clues them-
unless we are trying to generate a hard puz- selves. The rest of the positive instances of place
zle, we tell the grounder not to use looka- in the log form the hint sequence.
head. So when this situation arises, we call
the puzzle “good”.

• Aspps requires the full solver and backtrack 5. Performance


to find a sole solution. Although the puzzle
is well-formed, it is too hard for general en- The logic-program approach to generating cellu-
thusiasts. We call the puzzle “bad”. lar puzzles is remarkably efficient in programmer
time. Each puzzle type requires only a few lines of
• The puzzle has multiple solutions. The miss- Aspps code. The algorithm of Figure 3 is encoded
ing clue is required to keep the puzzle well- in about 500 lines of Perl [WS90], much of which is
formed. The puzzle is “bad”. devoted to generating formatted puzzle and hint
output.
These last two situations require the solver to dis- Given the Aspps rules, generating a puzzle has
tinguish. But we consider both to be “bad”. There- two phases: Finding the solution and reducing the
fore, we don’t ever call the solver. Either the grounder clues. The time needed for the first phase depends,
can solve the puzzle (“good”) or it can’t (“bad”). of course, on the complexity of the constraints and
If we are trying to generate a hard puzzle, we the size of the puzzle. On a 3GHz Pentium 4 run-
let the grounder use one-step lookahead. We also ning Linux, we accomplish the first phase for Su-
apply one more test after reaching the final set of doku puzzles of various sizes in time and memory
clues: Can the grounder solve the final set of clues shown in Figure 4. The time for the first phase has
without using lookahead? If so, then the puzzle a large variance, especially for larger puzzles. Fig-
isn’t hard enough, but removing any clues would ure 4 also shows the time for each iteration in the
make it ill-formed or too hard. In this case, we re- second phase. This value has much smaller vari-
ject the puzzle completely and start afresh. ance.
6. Puzzle types

We have investigated several cellular puzzle types.


• Sudoku. These are n2 × n2 squares with n2
sections containing n × n cells, where each
row, column, and section are complete.
• Diagonal Sudoku. These puzzles are the same
as Sudoku, but the two diagonals are also
complete. Cells (i, i) constitute the first diag-
Puzzle
onal, and (i, n2 + 1 − i) constitute the other. A B C D E F G H I
We show such a puzzle in Figure 5.
a 1 4
• MultiFour. This puzzle is our own invention. b 2 3
It has 4n × 4n cells. Again, each row and c 9 5 2
column is complete, but now a set of cells is
d 1
complete if every value 1 . . . 4 occurs exactly
n times. We show such a puzzle in Figure 6. e
For n = 2, we can add the extra constraint that f 9 8
every 2 × 2 section has each of 1 . . . 4 exactly g 1 2 9 3
once. This constraint is apparently not satis- h 8 6 7
fiable for larger n.
i 7 4
• MultiSpot. These puzzles are our own inven-
tion. They have n×n cells, for n = 5, 6, 7, 8, 9.
Each row and column is complete, as is each Solution
irregularly-shaped region. We show such a A B C D E F G H I
puzzle for n = 7 in Figure 7. a 2 8 5 6 3 7 1 9 4
b 9 1 6 4 8 2 7 5 3
All these puzzles use only completeness constraints.
We could also impose numerical constraints, such c 3 7 4 9 5 1 2 6 8
as requiring that particular cells have values adding d 6 4 8 3 7 9 5 2 1
to some value. One form of numeric cellular puz- e 5 2 1 8 6 4 9 3 7
zle is arranged as a planar graph of a fixed shape.
f 7 9 3 1 2 5 4 8 6
Each region and each edge is a cell that must have
g 1 6 7 2 9 3 8 4 5
an integer value. The value on each edge must be
the sum of the values of the regions it borders. It h 4 3 9 5 1 8 6 7 2
remains to be seen if such puzzles are attractive to i 8 5 2 7 4 6 3 1 9
enthusiasts. It is certainly easy to generate them.

7. References
Fig. 5. A 9 × 9 diagonal Sudoku puzzle
[EFLP00] Thomas Eiter, Wolfgang Faber, Nicola
Leone, and Gerald Pfeifer. Declarative
problem-solving in DLV. In Jack Minker,
editor, Logic-Based Artificial Intelligence,
pages 79–103. Kluwer Academic Pub-
lishers, Dordrecht, 2000.
[ET01] D. East and M. Truszczyński. aspps —
an implementation of answer-set pro-
gramming with propositional schemata.
Puzzle In Proceedings of Logic Programming and
A B C D E F G H I J K L Nonmonotonic Reasoning Conference, LP-
a 3 1 3 1 4 4 NMR 2001, volume 2173, pages 402–405.
b 1 2 4 3 4 2 3
c 1 1 4 4 3 1
Lecture Notes in Artificial Intelligence,
d 1 1 2 2 2 Springer Verlag, 2001.
e 2 1 1 1 2 4 4 4
f 2 2 1 4 1 4 [NS97] I. Niemelä and P. Simons. Smodels —
g 3 4 3 2 an implementation of the stable model
h 4 4 2 2 1 2 1 and well-foundd semantics for normal
i 4 3 2 3 1 logic programs. In Logic Programming
j 3 3 2 4 2 2 3
and Nonmonotonic Reasoning (the 4th
k 2 1 3 2 2
l 4 3 3 2 2 4 1
International Conference, Dagstuhl, Ger-
many, 1997), volume 1265 of Lecture
Notes in Computer Science, pages 420–
Solution 429. Springer-Verlag, 1997.
A B C D E F G H I J K L
a 3 2 2 3 2 1 3 1 1 4 4 4 [TFE05] Wikipedia: The Free Encyclopedia. Su-
b 2 1 3 2 1 1 4 3 4 2 3 4 doku, 2005. http://en.wikipedia.
c 1 1 4 4 4 2 2 3 3 1 2 3 org/wiki/Sudoku.
d 3 1 1 2 1 3 4 2 4 3 4 2
e 2 2 1 1 3 1 3 2 4 4 3 4 [WS90] L. Wall and R. L. Schwartz. Programming
f 2 2 2 1 3 4 4 1 1 4 3 3
Perl. O’Reilly and Associates, 1990.
g 1 3 2 1 4 4 3 4 2 3 1 2
h 4 4 4 3 2 3 1 2 3 1 2 1
i 1 4 4 4 3 2 1 3 2 3 2 1
j 3 3 1 2 4 4 2 4 1 2 1 3
k 4 4 3 4 1 3 2 1 3 2 1 2
l 4 3 3 3 2 2 1 4 2 1 4 1

Fig. 6. An n = 3 MultiFour puzzle

Puzzle
A B C D E F G
a 4 3 ♣ ♣
b ♣7 ♣
c 3 ♣6 ♣1 ♣4 ♣
d ♣5 ♣ ♣ ♣3
e ♣ ♣
f 7 ♣ ♣ 1
g

Solution
A B C D E F G
a 2 5 4 3 ♣7 ♣6 1
b 1 ♣7 6 ♣5 2 3 4
c 3 ♣6 ♣1 7 ♣4 ♣2 5
d 4 2 ♣5 ♣1 6 ♣7 ♣3
e 6 3 7 4 ♣1 5 ♣2
f 7 4 ♣3 ♣2 5 1 6
g 5 1 2 6 3 4 7

Fig. 7. An n = 7 MultiSpot puzzle

You might also like