Live data from Hacker News

Solving the “Miracle Sudoku” in Prolog

benjamincongdon.me

21–30 of 36 posts

Re: Solving the “Miracle Sudoku” in Prolog

#21
post #8
post #5

https://benjamincongdon.me/blog/2020/05/23/Solving-the-Mirac... has the list of all the boards that are 'miracle' sudoku. Only 72.

Is that accounting for symmetry?

What I can see, of these 72 solutions, there are at most 9 distinct solutions when symmetries are removed (rotation, transpose, flipping). I might have missed some solution.

Though, of these 9 solutions there are more similarities, i.e. some of them are the same rows except that the positions are switched.

Some other thing. if the lines are sorted, there are only 4 distinct variants.

Also, the solution of the original Miracle Sudoku instance has some invariants, for example the rows are rotations of these numbers 1,5,9,4,8,3,7,2,6 and the columns are (when rows are sorted) rotations of the numbers 1..9.

I've added comments about this in my Picat model: http://hakank.org/picat/miracle_sudoku.pi

Re: Solving the “Miracle Sudoku” in Prolog

#23

The OP says there are 6x10^21 Sudoku puzzles. If we remove rotations and reflections, there may be (far) fewer unique Sudoku puzzles than we think. In fact, there are many sub-reflections that are essentially the 'same' puzzle: Take the first three rows as a block, 'rotate' to the bottom of the puzzle - its still 'solved'. In fact permute the blocks of three rows in any order, still the 'same' puzzle in a sense. Same…

With the extra constraints in this Sudoku variant, there can't be many unique solutions -- otherwise two numbers wouldn't be sufficient to uniquely specify a solution. In fact, there's only a single pattern. Note how along one axis every digit is 4 higher (mod 9) than the previous digit. One starting digit specifies the starting point (alignment of the pattern), the other starting digit the rotation + mirroring of the pattern. 9 possible alignments times 4 rotations times 2 (mirrored or not) = 9 * 4 * 2 = the 72 different solutions mentioned in the article.

But this is caused by the Knight's+King's move constraints (with the "consecutive numbers" constraint prohibiting relabeling of the numbers). There's way more solutions without those extra constraints.

Re: Solving the “Miracle Sudoku” in Prolog

#24
post #21
post #8

Earlier quoted context omitted.

Is that accounting for symmetry?

What I can see, of these 72 solutions, there are at most 9 distinct solutions when symmetries are removed (rotation, transpose, flipping). I might have missed some solution. Though, of these 9 solutions there are more similarities, i.e. some of them are the same rows except that the positions are switched. Some other thing. if the lines are sorted, there are only 4 distinct variants. Also, the solution of the origina…

It's only a single pattern. Every solution has the 1,5,9,4,8,3,7,2,6 pattern (every step is +4 mod 9) in either the rows or the columns.

9 ways to transpose; 4 rotations, 2 for flipped or not. Multiply those, you get the full 72.

Re: Solving the “Miracle Sudoku” in Prolog

#25

z3 solver for comparison, from the original thread: https://gist.github.com/sielicki/fd86d68733133f654128519b3c4... I find z3 python interface much simpler, more intuitive and more powerful than prolog for this kind of things

Have you gotten a chance to look at miniKanren/cKanren? Any opinions?

Re: Solving the “Miracle Sudoku” in Prolog

#26

I said just the other day, "Adding constraints that encode the additional rules is also a little puzzle, eh?" I think I see a kind of hierarchy that reminds me of the Futamura Projections: 1. Solving Sudoku puzzles. 2. Designing new Sudoku puzzles (with additional constraints even.) 3. Designing (e.g. CLP(FD/ℤ)) programs to solve all sudoku puzzles everywhere. - - - - How do you pronounce "ℤ" in this context? "see el…

As a mathematician, I'd say "Cee Ell Pee Eff Dee over Zee". American mathematicians never say Zahlen and rarely say zed.

Re: Solving the “Miracle Sudoku” in Prolog

#27
post #19

Absolutely wonderful puzzle. Back when I taught Prolog in college, I created a number of Sudokus for my students to solve, that and a few other prolog puzzles are here for those enjoying a lazy afternoon of logic programming: https://github.com/maebert/prolog_puzzles#sudoku

Thank you for sharing those puzzles; I rarely get a chance to use Prolog, and the 99 Problems are pretty boring, so it's nice to have a fresh challenge! Just a note: it's 'exercise', not 'excercise'.

Re: Solving the “Miracle Sudoku” in Prolog

#28
post #3

Very nicely done, thank you for sharing this! A CLP(FD/ℤ) solution such as this one has two parts: First, the relevant constraints are posted. Second, a search tries to find concrete solutions. In general, a search is necessary because the constraints by themselves are not sufficient to deduce the unique solution as that would be computationally prohibitive. You can therefore influence the speed of the logic program…

Thank you for your explanation. However, as someone who has merely dabbled in Prolog (I've written less than 2000 lines of Prolog in total), I still don't understand the distinction between all_different/1 and all_distinct/1.

Even the documentation for all_different/1 https://www.swi-prolog.org/pldoc/man?predicate=all_different... asks the user to consider using all_distinct instead. I'm afraid I just don't have a mental model of how CLP(FD) works to understand what is meant to "stronger" or "weaker" propagation.

Re: Solving the “Miracle Sudoku” in Prolog

#29
post #24
post #21

Earlier quoted context omitted.

What I can see, of these 72 solutions, there are at most 9 distinct solutions when symmetries are removed (rotation, transpose, flipping). I might have missed some solution. Though, of these 9 solutions there are more similarities, i.e. some of them are the same rows except that the positions are switched. Some other thing. if the lines are sorted, there are only 4 distinct variants. Also, the solution of the origina…

It's only a single pattern. Every solution has the 1,5,9,4,8,3,7,2,6 pattern (every step is +4 mod 9) in either the rows or the columns. 9 ways to transpose; 4 rotations, 2 for flipped or not. Multiply those, you get the full 72.

Which are the "9 ways to transpose"? In general there are 8 symmetries of a matrix, but you might also count reordering of rows/columns or blocks?

I agree that 1,5,9,4,8,3,7,2,6 pattern rotations is there somewhere, either in the columns or the rows of a solution, but that don't make all solutions the same.

Re: Solving the “Miracle Sudoku” in Prolog

#30
post #28
post #3

Very nicely done, thank you for sharing this! A CLP(FD/ℤ) solution such as this one has two parts: First, the relevant constraints are posted. Second, a search tries to find concrete solutions. In general, a search is necessary because the constraints by themselves are not sufficient to deduce the unique solution as that would be computationally prohibitive. You can therefore influence the speed of the logic program…

Thank you for your explanation. However, as someone who has merely dabbled in Prolog (I've written less than 2000 lines of Prolog in total), I still don't understand the distinction between all_different/1 and all_distinct/1. Even the documentation for all_different/1 https://www.swi-prolog.org/pldoc/man?predicate=all_different... asks the user to consider using all_distinct instead. I'm afraid I just don't have a me…

Some terminology: a propagator is an implementation of a constraint, and is used to remove values from variable domains, by deducing that they are in no solution. In general, propagation strength is about how much a propagator can deduce, that is, how many values it can remove while still not removing a value that is in some solution to the constraint.

As an example, consider the variables

    x=1, y in {2, 3}, z in {2, 3}, w in {1, 2, 3, 4}
with the constraint all_different(x, y, z, w) added (note here that I will use that names for the constraint, and refer to the propagation strength of a propagator).

With value-propagation (sometimes called forward-checking), a propagator would deduce the new domains

    x=1, y in {2, 3}, z in {2, 3}, w in {2, 3, 4}
by simply removing assigned values from other variables domains. However, there are stronger propagators for the constraint. In particular, there is well-known and reasonably efficient (O(n^2.5)) propagator that is domain-consistent (also known as GAC or generalized arc consistent). That means that it can always remove _all_ values from variables that are in no solution. The above example with that propagator would deduce

    x=1, y in {2, 3}, z in {2, 3}, w=4
The reasoning is based on something called Hall-sets. In the above example, y and z form a Hall set of size 2, since they must take the values 2 and 3 in any solution. there are also other propagators with different propagation strengths. For all_different, there are also bounds-consistent propagators, that can do more than value propagation, but only on does advanced reasoning based on the bounds of variables.

I think that SWI Prolog uses the all_different name for value propagation, and all_distinct for domain propagation. In the CP system Gecode that I'm most familiar with, we use an argument to the distinct-constraint (same constraint, different name) to indicate the desired level of propagation (https://www.gecode.org/doc-latest/reference/group__TaskModel...).

Post reply on HN