Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

Has anyone tried solving these puzzles using templates?
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Solving sudoku
View previous topic :: View next topic  
Author Message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Sat Sep 17, 2005 11:53 pm    Post subject: Has anyone tried solving these puzzles using templates? Reply with quote

I've written a solver that uses the following technique:

Theory:

There are 46656 different configurations for any value in the grid.
With 1 position given, 5184 configurations are left.
(a chute is a line of 3 boxes)
With 2 positions given in 1 chute, 864 configurations are left.
With 2 positions given in 2 chutes, 576 configurations are left.
With 3 positions given in 1 chute, 288 are left.
With 3 positions given in 3 chutes, only 64 combinations are left.
Each given other value in a non-blocked position also slightly reduces the number of combinations.

In most Sudoku puzzles, there are 2 or 3 givens for each value.

Application:

My program keeps a library of all 46656 configuration templates, and maintains a list for each of the 9 values.

When a cell is filled, the list is filtered to the templates that contain that value at the cell's position.

When a cell is blocked for a value, all templates containing that value at the cell's position are removed from the list.

I have written logic in the program for detecting naked singles, hidden singles, naked subsets, hidden subsets, X-wings and coloring, but the template technique removes the same candidates (and more!) without knowing why it did that. Confused

Once the remaining templates for each value are known, they are compared to those of the other values. Overlapping combinations are filtered from the list, until only one combination remains, which then happens to be the solution to the puzzle. When the puzzle has multiple solutions, it is easily discovered with the template technique. When there is no valid solution, it is also detected.

My questions:

1. Has anyone tried this before?
2. How would one classify this method? BF? T&E? VMWOCT (very methodical waste of CPU time)?
3. I am currently using this technique in addition to known logic, but wonder if it could be used to detect new patterns and deductive rules.
Back to top
View user's profile Send private message Visit poster's website
Lummox JR

Joined: 07 Sep 2005
Posts: 202
:

Items
PostPosted: Sun Sep 18, 2005 12:11 am    Post subject: Reply with quote

I'd call this a form of brute force, but an interesting one. It may well be a good way to deduce new patterns and deductive rules, which would be the main interest in maintaining such a thing.

I know just the other day an idea occurred to me that it could be possible to define forcing chains according to a strict set of logic rules in such a way as to form linked graphs, taking it out of the realm of T&E and into applied logic. However as I've found this is only a limited form of forcing chains, or perhaps should be named differently.
Back to top
View user's profile Send private message
dukuso

Joined: 14 Jul 2005
Posts: 424
:
Location: germany

Items
PostPosted: Sun Sep 18, 2005 7:09 am    Post subject: Reply with quote

I once wrote a program to count the 9 values for each digit.
I think I wanted to use it to estimate the number of
solutions of underconstrained puzzles, don't remember exactly.

It just picks a digit d, sets all other clues to "x" and counts the
number of templates from the 46656 which meet d and avoid x.

That gives 9 numbers, whose product might be an indication for
the number of solutions, I'm not sure.

You can also pick one template at random from the most
constrained digit and then iterate for the remaining digits.


-Guenter
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ocean

Joined: 29 Aug 2005
Posts: 15
:

Items
PostPosted: Sun Sep 18, 2005 8:33 pm    Post subject: Reply with quote

Ruud wrote:

My questions:

1. Has anyone tried this before?
2. How would one classify this method? BF? T&E? VMWOCT (very methodical waste of CPU time)?
3. I am currently using this technique in addition to known logic, but wonder if it could be used to detect new patterns and deductive rules.



Good explanation!


As mentioned in another thread, I have been using a similar techique (for exploring some logic or patterns), but you probably have more experience with it. I can only say that the template method works fine, if anybody should be sceptical. The method catches all possible "2-dimensional" logic and patterns, that is logic involving only one candidate at at time. This includes locked candidates, x-wing, swordfish, turbot fish, coloring of conjugate chains of single candidates, other unknown (hidden) patterns, and forcing chains where only one candidate is involved.

I agree with Lummox that the template method in itself is on the borderline between T&E and logic. I think the challenge is to recognize the underlying logic or patterns, and present those.

One question is, is it pure logic if one uses a catalog of templates, or a catalog of patterns to find the solution? Maybe it depends on the size of such a catalog, or how effective it can be searched.
Back to top
View user's profile Send private message
angusj
Site Admin
Joined: 18 Jun 2005
Posts: 406
:

Items
PostPosted: Sun Sep 18, 2005 10:45 pm    Post subject: Re: Has anyone tried solving these puzzles using templates? Reply with quote

Ruud wrote:
There are 46656 different configurations for any value in the grid.

This prompted me to consider a much faster way to generate grids too.

46656 would be the number of rows in a dancing links matrix with 81 cells being the columns. Columns would then be chosen at random (instead of the usual picking the shortest column).

The downside of implementing this DLX algorithm would be the amount of memory consumed by the matrix - my calculation makes it about 90megs! Initializing a matrix of this size would take some time and I doubt it'd be recovered by the efficiency of grid selection.

Just some random musings from a hobbyist programmer.
Back to top
View user's profile Send private message Visit poster's website
Ocean

Joined: 29 Aug 2005
Posts: 15
:

Items
PostPosted: Sun Sep 18, 2005 11:33 pm    Post subject: Reply with quote

If size is an issue, here are some loose and maybe wild ideas:

Is there any way of exploiting the fact, that a grid may be assumed to have a fixed first row 123456789 (and afterwords doing a permutation of the numbers)? This reduces the number of remaining configurations with a factor 9, to 5184. And the number of remaining cells is reduced from 81 to 72. In total the matrix size is reduced by about 90%.

The number of cells may even be further reduced (to 52) by the fact that last cell in a row/column is determined from the first 8, but this might complicate algorithms.
Back to top
View user's profile Send private message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Mon Sep 19, 2005 12:44 am    Post subject: Reply with quote

Thanks for your interesting comments.

Ocean wrote:
Is there any way of exploiting the fact, that a grid may be assumed to have a fixed first row 123456789 (and afterwords doing a permutation of the numbers)?

That may be an interesting approach. However, the box constraints limit the number of permutations you are allowed to do.

Storage doesn't seem to be a problem in my program. The complete program, including the fully loaded templates and a GUI frontend consumes less than 24 Mb. I used a few tricks to save time and storage. The templates only activate on the first value filled or position blocked. With 1 value filled, that requires 5184 entries in the array, with 1 position blocked, it requires 41472 entries. I could delay the use of templates even further, but as long as the performance is OK, I keep it as it is.

About the solving capability:

I tested them on X-Wing, swordfish, hidden patterns, and they all were discovered by the templates.

Is it Brute Force? If you use it to completely solve a puzzle, yes indeed. However, I give the user the option to inspect certain values for hidden patterns using templates. That makes it a powerful, yet selectively applied tool. When the user suspects something "fishy" in a value's candidates, he can use this tool to save some time searching for these patterns. I am currently looking at the possibility to "reverse engineer" the logic behind candidates being removed by the templates.

I also added the option to compare the patterns of 2 or more different values for template overlaps. That introduces a 3rd dimension, finding black female cats without the messy paperwork.

Can anyone see other applications of this technique that I might be able to exploit?
Back to top
View user's profile Send private message Visit poster's website
dukuso

Joined: 14 Jul 2005
Posts: 424
:
Location: germany

Items
PostPosted: Mon Sep 19, 2005 3:07 am    Post subject: Re: Has anyone tried solving these puzzles using templates? Reply with quote

angusj wrote:
Ruud wrote:
There are 46656 different configurations for any value in the grid.

This prompted me to consider a much faster way to generate grids too.

46656 would be the number of rows in a dancing links matrix with 81 cells being the columns. Columns would then be chosen at random (instead of the usual picking the shortest column).

The downside of implementing this DLX algorithm would be the amount of memory consumed by the matrix - my calculation makes it about 90megs! Initializing a matrix of this size would take some time and I doubt it'd be recovered by the efficiency of grid selection.

Just some random musings from a hobbyist programmer.


you needn't store the whole 46656*81 matrix, just the
nonzero entries, which are 46656*9. Store them once
for the rows as 46656*9 column addresses from {1,2,..,81}
and once for the columns as 81*5184 row-addresses.
That's 419904*2 integers or 3.2MB for 9*9 sudoku.
16*16 is too large, though.


edit: with only 81 columns you'd better use a bitmap for the columns,
using 128-bit registers. That requires 746496 bytes

The whole method is probably not very fast and won't work
for 16*16 etc.

The problem with the "templates",
aren't these the bipartite matching problems as described
in briefly in Eppsteins paper or first implemented by
Regis (AFAIR) ? I'd wonder, how fast that method is here.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ocean

Joined: 29 Aug 2005
Posts: 15
:

Items
PostPosted: Mon Sep 19, 2005 8:09 am    Post subject: Re: Has anyone tried solving these puzzles using templates? Reply with quote

dukuso wrote:

The problem with the "templates",
aren't these the bipartite matching problems as described
in briefly in Eppsteins paper or first implemented by
Regis (AFAIR) ? I'd wonder, how fast that method is here.


As far as I understand it, this is probably only partly correct - if you are referring to one of Eppsteins "local" rules:
Quote:
– If the placement of digit x in cell y can not be extended to a placement of nine copies of x covering each row and column of the grid exactly once, we eliminate cell y from consideration as a placement for x.

reformulated as
Quote:
– If there exist sets S of rows and T of equally many columns, such that the only cells among rows S where digit x can be placed also belong to the columns in T, then x can not be placed in a cell that lies in a column of T but does not lie in a row of S.


As the rule is described, it will result in 9! = 362880 permissable configurations, and will only discover x-wing, swordfish and jellyfish patterns. The rule as formulated does not account for the boxes. But this is of course an obviuos and almost trivial extension, giving 46656 configurations.
Back to top
View user's profile Send private message
Ocean

Joined: 29 Aug 2005
Posts: 15
:

Items
PostPosted: Mon Sep 19, 2005 2:52 pm    Post subject: Reply with quote

Ruud wrote:
I also added the option to compare the patterns of 2 or more different values for template overlaps. That introduces a 3rd dimension, finding black female cats without the messy paperwork.


Interesting! What mechanism do you use for the compare?
Back to top
View user's profile Send private message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Mon Sep 19, 2005 5:42 pm    Post subject: Reply with quote

Ocean wrote:
Ruud wrote:
I also added the option to compare the patterns of 2 or more different values for template overlaps.


Interesting! What mechanism do you use for the compare?

Since the template data is stored in bit arrays, I simply AND the bit arrays of the 2 templates, where a non-zero result indicates an overlap. It is basically the same mechanism I use for testing the templates against candidates, since the candidates for a value can also be represented in a bit array. Data for 3 rows (27 cells) is stored in a 32-bit integer, requiring 3 integers to a piece of info about the entire grid.
Back to top
View user's profile Send private message Visit poster's website
Myth Jellies

Joined: 20 Sep 2005
Posts: 14
:

Items
PostPosted: Tue Sep 20, 2005 9:28 am    Post subject: Reply with quote

This is similar to the Pattern Overlay Method that I use to solve SudoKu by hand. I describe the method in this post.

Personally, I consider my method logical because
1. There is no reason why you can't switch modes from creating a list of possible numbers to creating a list of possible solution patterns for each number. Simple rules of SudoKu suffice to compute either of these.
2. I use a simple algebraic logic equation derived from the content of one of the cells to eliminate possible patterns (I just don't have room for a master conflict list of 46,656 entries in my head Smile)
3. Nowhere do I assume that 1 number or pattern is valid or not valid and then see what happens.

It would be interesting to see what the computer could do if it was restricted to the Pattern Overlay Method rules. The method is difficult when there are a large number of unique patterns associated with a digit.

Creating a list of unique patterns associated with a cell would be a useful tool/library to have as well.
_________________
Myth Jellies
Back to top
View user's profile Send private message
Myth Jellies

Joined: 20 Sep 2005
Posts: 14
:

Items
PostPosted: Tue Sep 20, 2005 9:41 am    Post subject: Reply with quote

46656 -- interesting number, that. 36 cubed. Each unique pattern could have its own unique 3 character alpha-numeric label....just thinking out loud here....
_________________
Myth Jellies
Back to top
View user's profile Send private message
Ocean

Joined: 29 Aug 2005
Posts: 15
:

Items
PostPosted: Tue Sep 20, 2005 7:43 pm    Post subject: Reply with quote

Ruud wrote:
Since the template data is stored in bit arrays, I simply AND the bit arrays of the 2 templates, where a non-zero result indicates an overlap. It is basically the same mechanism I use for testing the templates against candidates, since the candidates for a value can also be represented in a bit array. Data for 3 rows (27 cells) is stored in a 32-bit integer, requiring 3 integers to a piece of info about the entire grid.


Thank you! I frankly belived it was not possible to do this without a time explosion!


Myth Jellies wrote:
46656 -- interesting number, that. 36 cubed. Each unique pattern could have its own unique 3 character alpha-numeric label....just thinking out loud here....


The 46656 configurations or templates are all actually based on one single pattern (pick any of the patterns). Here is one starter (represented by the digits 147258369):
Code:

  147258369
 *-----------------*
 |1 . .|. . .|. . .|
 |. . .|1 . .|. . .|
 |. . .|. . .|1 . .|
 |-----+-----+-----|
 |. 1 .|. . .|. . .|
 |. . .|. 1 .|. . .|
 |. . .|. . .|. 1 .|
 |-----+-----+-----|
 |. . 1|. . .|. . .|
 |. . .|. . 1|. . .|
 |. . .|. . .|. . 1|
 *-----------------*


All permissible configurations can be generated by a selective permutation of this basic pattern. One way of doing it is to combine all these: Permute row 1,2,3. Permute row 4,5,6. Permute row 7,8,9. Permute column 1,2,3. Permute column 4,5,6. Permute column 7,8,9.

This gives 46656 patterns. Further permutations will either give duplicates or illegal configurations.

So initialising the templates is fairly straightforward. Whether algorithms can exploit this property directly, is another interesting question (which I have not sorted out yet).
Back to top
View user's profile Send private message
gaby

Joined: 02 Jul 2005
Posts: 120
:

Items
PostPosted: Fri Sep 23, 2005 12:19 pm    Post subject: Reply with quote

I've added this to my PHP solver, and it seems that it is an effective brute force approach, but I can see it as a very straightforward way of generating puzzles. Interestingly, it finds multiple solutions to some puzzles that I thought only had single solutions. I'm going to run it against a few thousand puzzles and see what the outcome is, but I like this technique. It is quite an elegant approach.
_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Solving sudoku All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron