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 Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Solving sudoku
View previous topic :: View next topic  
Author Message
gaby

Joined: 02 Jul 2005
Posts: 120
:

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

Interestingly, on a sample puzzle it generates 2204098 comparisons of templates and finds three unique solutions. It only makes use of hidden chains and templating to solve it, which it manages in 40 seconds:

Code:
31--9---5
------3--
4--7---8-
--62----1
--2---7--
7----48--
-9---3--8
--4------
5---8--69

_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Fri Sep 23, 2005 4:32 pm    Post subject: Reply with quote

You might want to debug and optimize your code a little further.

My template solver found a single solution for this puzzle
Quote:
gaby wrote:
Code:
31--9---5
------3--
4--7---8-
--62----1
--2---7--
7----48--
-9---3--8
--4------
5---8--69

It took less than 1 millisecond.
Back to top
View user's profile Send private message Visit poster's website
gaby

Joined: 02 Jul 2005
Posts: 120
:

Items
PostPosted: Fri Sep 23, 2005 9:33 pm    Post subject: Reply with quote

Nice. My solver is written in PHP, so it's hardly the most efficient of solvers Smile I think you're right, I've had some other thoughts on how to optimise this analysis, so I'll give them a bash and see what the outcome is.
_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
gaby

Joined: 02 Jul 2005
Posts: 120
:

Items
PostPosted: Sat Sep 24, 2005 12:22 pm    Post subject: Reply with quote

Found out what the problem was. It was allowing templates that could place numbers in places where there was no candidate for that number (eg, it would select templates that said a 2 could go in r3,c4 when the candidates on the board said that it couldn't put a 2 in that cell).

It's still not particularly quick, but at least it works now Smile
_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
gaby

Joined: 02 Jul 2005
Posts: 120
:

Items
PostPosted: Sat Sep 24, 2005 12:40 pm    Post subject: Reply with quote

In fact, I'm still getting three solutions for that puzzle. Which one of these is wrong? Here are the three sets of templates that my solver returns:

Code:

Set 1:
1:010000000000000010000001000000000001000010000001000000100000000000000100000100000
2:000001000100000000000000001000100000001000000000000010000000100000010000010000000
3:100000000000000100000010000010000000000100000000000001000001000000000010001000000
4:000100000000000001100000000000000010010000000000001000000010000001000000000000100
5:000000001000010000001000000000000100010000000000001000000000010000100000100000000
6:000000100000001000010000000001000000000000001000010000000100000100000000000000010
7:000000010010000000000100000000010000000000100100000000001000000000000001000001000
8:001000000000100000000000010000001000100000000000000100000000001010000000000010000
9:000010000001000000000000100100000000000000010000100000010000000000001000000000001

Set 2:
1:010000000000000010000001000000000001000010000001000000100000000000000100000100000
2:000001000100000000000000001000100000001000000000000010000000100000010000010000000
3:100000000000000100000010000010000000000100000000000001000001000000000010001000000
4:000100000000000001100000000000000010010000000000001000000010000001000000000000100
5:000000001000010000001000000000000100000001000010000000000000010000100000100000000
6:000000100000001000010000000001000000000000001000010000000100000100000000000000010
7:000000010010000000000100000000010000000000100100000000001000000000000001000001000
8:001000000000100000000000010000001000100000000000000100000000001010000000000010000
9:000010000001000000000000100100000000000000010000100000010000000000001000000000001

Set 3:
1:010000000000000010000001000000000001100000000000010000001000000000000100000100000
2:000001000010000000000000001000100000001000000000000010100000000000010000000000100
3:100000000000000100000010000000000010000100000010000000000001000000000001001000000
4:000000100000010000100000000010000000000000001000001000000100000001000000000000010
5:000000001000001000001000000000000100010000000000100000000010000000000010100000000
6:000000100000010000010000000001000000000000001000001000000100000100000000000000010
7:001000000000000001000100000000010000000000100100000000000000010000001000010000000
8:000100000001000000000000010100000000000001000000000100000000001010000000000010000
9:000010000100000000000000100000001000000000010001000000010000000000100000000000001


And if we group them by number, we have:

Code:
1.1:010000000000000010000001000000000001000010000001000000100000000000000100000100000
1.2:010000000000000010000001000000000001000010000001000000100000000000000100000100000
1.3:010000000000000010000001000000000001100000000000010000001000000000000100000100000

2.1:000001000100000000000000001000100000001000000000000010000000100000010000010000000
2.2:000001000100000000000000001000100000001000000000000010000000100000010000010000000
2.3:000001000010000000000000001000100000001000000000000010100000000000010000000000100

3.1:100000000000000100000010000010000000000100000000000001000001000000000010001000000
3.2:100000000000000100000010000010000000000100000000000001000001000000000010001000000
3.3:100000000000000100000010000000000010000100000010000000000001000000000001001000000

4.1:000100000000000001100000000000000010010000000000001000000010000001000000000000100
4.2:000100000000000001100000000000000010010000000000001000000010000001000000000000100
4.3:000000100000010000100000000010000000000000001000001000000100000001000000000000010

5.1:000000001000010000001000000000000100010000000000001000000000010000100000100000000
5.2:000000001000010000001000000000000100000001000010000000000000010000100000100000000
5.3:000000001000001000001000000000000100010000000000100000000010000000000010100000000

6.1:000000100000001000010000000001000000000000001000010000000100000100000000000000010
6.2:000000100000001000010000000001000000000000001000010000000100000100000000000000010
6.3:000000100000010000010000000001000000000000001000001000000100000100000000000000010

7.1:000000010010000000000100000000010000000000100100000000001000000000000001000001000
7.2:000000010010000000000100000000010000000000100100000000001000000000000001000001000
7.3:001000000000000001000100000000010000000000100100000000000000010000001000010000000

8.1:001000000000100000000000010000001000100000000000000100000000001010000000000010000
8.2:001000000000100000000000010000001000100000000000000100000000001010000000000010000
8.3:000100000001000000000000010100000000000001000000000100000000001010000000000010000

9.1:000010000001000000000000100100000000000000010000100000010000000000001000000000001
9.2:000010000001000000000000100100000000000000010000100000010000000000001000000000001
9.3:000010000100000000000000100000001000000000010001000000010000000000100000000000001


Apparently there are differences in all the numbers. I wonder if it has just found that some of the rows/columns can be transposed, and this is reflected in the templates? The function that chooses the permutations of templates is recursive and exhaustive, so it will find every possible combination. I still need to check that all three solutions are valid...
_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
gaby

Joined: 02 Jul 2005
Posts: 120
:

Items
PostPosted: Sat Sep 24, 2005 12:46 pm    Post subject: Reply with quote

Solutions 1 and 2 are valid, solution 3 is broken. Yes, more debuggering to do methinks...

Code:
Solution 1:
318492675
279856314
465731982
936278541
842315796
751964823
197643258
684529137
523187469

Solution 2:
318492675
279856314
465731982
936278541
842315796
751964823
197643258
684529137
523187469

Solution 3:
3178926-5
928-45317
465731982
846279531
1523-8794
73951482-
291453-78
684927153
57318-269


Clearly something is amiss.
_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Sat Sep 24, 2005 2:38 pm    Post subject: Reply with quote

Set 1 overlaps on values 4 and 5.
Set 2 seems to be fine.
Set 3 overlaps on values 4 and 6.

Check your code and you will probably see that you do not correctly compare the templates of these values.
Back to top
View user's profile Send private message Visit poster's website
gaby

Joined: 02 Jul 2005
Posts: 120
:

Items
PostPosted: Sat Sep 24, 2005 3:05 pm    Post subject: Reply with quote

How very curious. I have two types of comparison that the system does, template_overlap and template_contained. template_overlap( $a, $b ) checks if template $a has any common elements with template $b, and template_contained( $a, $b ) checks that template $a is a subset of template $b, eg is completely contained within it. Templates are either the 46656 initial templates, or templates of the current state of a number, or an exclusion map, where numbers cannot go.

All the templates are in a binary form, as shown above. Firstly the initial templates are calculated that each number can contain. For each of the 46656 templates {t}, we do two comparisons:

1. Look at the template for each number{tN}. If the binary AND of {t} & {tN} == {tN}, then {tN} is completely contained within {t}, so {t} is a possible template for the number N.

2. Look at the template that represents where the number N cannot go in the board, eg a 1 for cells that don't have N as a candidate, and a 0 for ones that do. This is called the exclusion template, or {tNX}. If the binary AND of {t} & {tNX} > 0, then there are some elements in the template {t} that appear in {tNX}. However, {tNX} contains all the places we know that we can't put the number N, so we can remove that template.

We now have a list of templates for number N. Repeat for all the numbers until we have a list for each number.

Start with the number with fewest templates. For each template for this number, remove other templates from the other numbers that have any places in common. That is, if by doing a binary AND of this template and each of the other number's templates, the result is > 0, there is some overlap, so we can remove that template.

We recursively try this for all the templates. If we end up with a number that has 0 templates left for it, it's not a valid combination. If we end up at a point where each number has 1 template, we've found a valid combination (like the three above). The recursive function will try all combinations and find all possibiliites.

So, the glitch is somewhere in the recursive comparison function, as it's not removing some templates that should not be there. Am I missing a vital step here? Should I be removing more templates to begin with?
_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
gaby

Joined: 02 Jul 2005
Posts: 120
:

Items
PostPosted: Sat Sep 24, 2005 8:55 pm    Post subject: Reply with quote

Interesting. The recursive function sorts the list of templates for each number by the number of templates that each number has left for it, so the output will be a sequence, but not necessarily in order 1 to 9. Sets 1, 2 and 3 come out in this order:

Numbers for Set 1: 2, 3, 8, 9, 7, 6, 1, 4, 5
Numbers for Set 2: 2, 3, 8, 9, 7, 6, 1, 4, 5
Numbers for Set 3: 2, 8, 3, 9, 7, 1, 5, 6, 4

Set 1 has an overlap in numbers 4 and 5, and set 3 with numbers 4 and 6. In both cases these are the last numbers that are searched, so the problem lies within the closing conditions of the recursive function...

Success! One template set matches, and it is a valid set. The section of the recursive function that detected if there was only one number left to find a template for, wasn't removing overlapping templates. Now it removes the overlaps, the results come out clean.

Question now is how to speed it up. I'm guessing that your program is written in a slighty faster language that PHP? C++ or something similar? When you say it took less than a millisecond, was that just the templating analysis? How do you scan the tree of possibilities, recursively or using a 9 level nested set of for loops? I kept mine recursive because I'd like my solver to be able to solve NxN sudoku as well as the usual 9x9.
_________________
Free daily sudoku - Online puzzle database
http://vanhegan.net/sudoku/
Back to top
View user's profile Send private message Visit poster's website
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Mon Sep 26, 2005 2:48 pm    Post subject: Reply with quote

gaby wrote:
Question now is how to speed it up.
To optimize a process like this, you need to look at the 2 factors that influence the performance:
1. The number of iterations being executed.
2. The number of tests and actions executed in each iteration.

To optimize the iterations:
- Filter as many candidates from the list as you can
- Do not filter by flagging, but create a subset of the list

To optimize the tests and actions:
- Merge the templates at each recursion level, so you need only to perform a single test.
- Strip any line of code that does not contribute to the solving itself

If that doesn't give you the performance you want, try testing the templates in pairs first. Each paired test will filter some templates for each of the 2 digits involved.

Here's an example:
compare templates for digits 1 and 2 and build a list of valid combinations
compare templates for digits 3 and 4 and build a list of valid combinations
compare templates for digits 5 and 6 and build a list of valid combinations
compare templates for digits 7 and 8 and build a list of valid combinations
compare merged templates for 1/2 and 3/4 and build a list of valid combinations
compare merged templates for 5/6 and 7/8 and build a list of valid combinations
compare merged templates for 1/2/3/4 and 5/6/7/8 and build a list of valid combinations
compare the final merged result with the templates for 9 and you have your solution(s).
Back to top
View user's profile Send private message Visit poster's website
rkral

Joined: 21 Oct 2005
Posts: 233
:

Items
PostPosted: Mon Nov 28, 2005 8:52 pm    Post subject: Reply with quote

Ocean wrote:
The 46656 configurations or templates are all actually based on one single pattern (pick any of the patterns).

Do you know the number of "orthogonal" template sets?

By orthogonal sets, I mean nine templates such that when all templates are overlapped, each grid cell is filled exactly once.
Back to top
View user's profile Send private message
Auguste

Joined: 17 Feb 2006
Posts: 2
:

Items
PostPosted: Mon Feb 20, 2006 5:33 am    Post subject: Reply with quote

Interesting idea.

All the exclusions or assertions by logic will be proofed by template. This method seems very quick and require no knowledge. As a matter of fact, the templates themselves are the knowledge!

I am interested in the speed comparison with DLX.

Razz
Back to top
View user's profile Send private message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Mon Feb 20, 2006 1:24 pm    Post subject: Reply with quote

Hi Auguste,

I have both templates and DLX implemented in SudoCue, and DLX is definitely faster.

Templates have more flexibility. You can choose to test the templates for a single digit only, or for 2 digits.

With templates, it is also easy to implement POM (Pattern Overlay Mapping)

Ruud.
_________________
Meet me at sudocue.net
Back to top
View user's profile Send private message Visit poster's website
Auguste

Joined: 17 Feb 2006
Posts: 2
:

Items
PostPosted: Tue Feb 21, 2006 1:01 pm    Post subject: Reply with quote

A seeming crazy idea is using the templates as the nodes in DLX, considering the fact that sudoku puzzles have given clues to remarkably reduce the size of DLX search.
Back to top
View user's profile Send private message
daj95376

Joined: 05 Feb 2006
Posts: 349
:

Items
PostPosted: Thu Jun 29, 2006 10:06 pm    Post subject: Re: Has anyone tried solving these puzzles using templates? Reply with quote

Ruud wrote:
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.


Would you please expand on the particulars of the comparisons performed and how overlapping combinations are filtered. Thanks!!!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Solving sudoku All times are GMT
Goto page Previous  1, 2, 3  Next
Page 2 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