Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


2 Pages12>
Options
Go to last post Go to first unread
Offline overlord  
#1 Posted : 30 August 2019 14:52:43(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 1,128
Turkey

Was thanked: 509 time(s) in 339 post(s)
I have been trying to make this code work for two days.
Actually it is a C code which I am trying to implicate to SMath. C code is also included and runs perfectly.
In SMath, code works to a point actually. When I debug it I can see it can assign numbers, backtrack if there is no option.
So SMath code also seems legit but eventually the algorithm just stop working. No error no significant reason.
With different puzzles, code terminated at different places at middle of calculation.
I couldn't figure out what is wrong. Is it a bug or my coding is insufficient?
Any help should be appreciated.

Regards.

sudoku.sm (27kb) downloaded 39 time(s).
sudoku.7z (39kb) downloaded 19 time(s).


Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline alyles  
#2 Posted : 30 August 2019 19:06:01(UTC)
alyles


Rank: Advanced Member

Groups: Registered
Joined: 23/03/2016(UTC)
Posts: 277
United States

Was thanked: 91 time(s) in 57 post(s)
I posted something very inaccurate before and have since deleted it. However, in my testing, it seems that it's not dependent on the number of iterations. The function seems to stop when rw,cl get to around 2,4 or 2,5. This is independent of the number of function calls to fillSudoku.
Feel free to join the SMath Studio Users Discord Channel: https://discord.gg/PayZpJW
Offline alyles  
#3 Posted : 30 August 2019 19:18:25(UTC)
alyles


Rank: Advanced Member

Groups: Registered
Joined: 23/03/2016(UTC)
Posts: 277
United States

Was thanked: 91 time(s) in 57 post(s)
Originally Posted by: alyles Go to Quoted Post
I posted something very inaccurate before and have since deleted it. However, in my testing, it seems that it's not dependent on the number of iterations. The function seems to stop when rw,cl get to around 2,4 or 2,5. This is independent of the number of function calls to fillSudoku.


This looks to correlate to a depth of ~21 or 22 levels of recursion no matter the starting puzzle.
Feel free to join the SMath Studio Users Discord Channel: https://discord.gg/PayZpJW
Offline Jean Giraud  
#4 Posted : 30 August 2019 19:44:22(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: overlord Go to Quoted Post
With different puzzles, code terminated at different places at middle of calculation.
I couldn't figure out what is wrong. Is it a bug or my coding is insufficient?

I never heard of Sudoku. As it looks, you first scan for coincidence.
Can it relate to some truth table ? Then else game ... !

Sudoku.PNG
Offline Jean Giraud  
#5 Posted : 30 August 2019 19:47:06(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
... your original creates "critical error", that can be repaired in the first module.
Offline Jean Giraud  
#6 Posted : 30 August 2019 19:51:21(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: Jean Giraud Go to Quoted Post
... your original creates "critical error", that can be repaired in the first module.


Sudoku First Module.sm (18kb) downloaded 12 time(s).

Offline Jean Giraud  
#7 Posted : 30 August 2019 20:09:53(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
... make sure you completely understand the Bolean
from the code you imitate. As corrected, red.
Red may mean not winning ?

sudoku Freak.sm (24kb) downloaded 15 time(s).

Offline alyles  
#8 Posted : 30 August 2019 21:25:01(UTC)
alyles


Rank: Advanced Member

Groups: Registered
Joined: 23/03/2016(UTC)
Posts: 277
United States

Was thanked: 91 time(s) in 57 post(s)
Originally Posted by: alyles Go to Quoted Post
Originally Posted by: alyles Go to Quoted Post
I posted something very inaccurate before and have since deleted it. However, in my testing, it seems that it's not dependent on the number of iterations. The function seems to stop when rw,cl get to around 2,4 or 2,5. This is independent of the number of function calls to fillSudoku.


This looks to correlate to a depth of ~21 or 22 levels of recursion no matter the starting puzzle.


To better illustrate what I think is happening I've created the following worksheet. The first function definition is just a simple function that calls itself recursively until its 200th call at which point it exits. However, when run it experiences the same error as we see in the sudoku.sm. I rewrote the function using a try/on error and we see we only get to 50 iterations before it fails.

Untitled.png
recursion.sm (5kb) downloaded 13 time(s).
Feel free to join the SMath Studio Users Discord Channel: https://discord.gg/PayZpJW
Offline overlord  
#9 Posted : 30 August 2019 22:14:24(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 1,128
Turkey

Was thanked: 509 time(s) in 339 post(s)
Originally Posted by: alyles Go to Quoted Post
This looks to correlate to a depth of ~21 or 22 levels of recursion no matter the starting puzzle.

Originally Posted by: alyles Go to Quoted Post

To better illustrate what I think is happening I've created the following worksheet. The first function definition is just a simple function that calls itself recursively until its 200th call at which point it exits. However, when run it experiences the same error as we see in the sudoku.sm. I rewrote the function using a try/on error and we see we only get to 50 iterations before it fails.

Untitled.png
recursion.sm (5kb) downloaded 13 time(s).


What I understood considering your last messages, you think there is nothing significant wrong with the code itself. Problem is SMath handling of recursions.
So, it comes forward that is why I wouldn't be able to make an SMath Sudoku Solver with recursion.

Thank you Alyles.

Edited by user 09 July 2021 01:22:14(UTC)  | Reason: Not specified

Offline Jean Giraud  
#10 Posted : 31 August 2019 01:41:02(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
I think it's possible to fill the '0' positions of the 9x9 Sudoku
with random not common to the existing filled position.
Filling one col at time, collect ...

Sudoku in(x,y).PNG
Offline Davide Carpi  
#11 Posted : 31 August 2019 16:50:00(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,650
Man
Italy
Location: Italy

Was thanked: 1338 time(s) in 878 post(s)
Originally Posted by: overlord Go to Quoted Post
Originally Posted by: alyles Go to Quoted Post
This looks to correlate to a depth of ~21 or 22 levels of recursion no matter the starting puzzle.

Originally Posted by: alyles Go to Quoted Post

To better illustrate what I think is happening I've created the following worksheet. The first function definition is just a simple function that calls itself recursively until its 200th call at which point it exits. However, when run it experiences the same error as we see in the sudoku.sm. I rewrote the function using a try/on error and we see we only get to 50 iterations before it fails.

Untitled.png
recursion.sm (5kb) downloaded 13 time(s).


What I understood considering your last messages, you think there is nothing significant wrong with the code itself. Problem is SMath handling of recursions.
So, it comes forward that I wouldn't be able to make an SMath Sudoku Solver.

Thank you Alyles.


I remember this limitation was introduced some time ago to avoid unintentional hard crashes on wrong formed algorithms; considering that any recursive algorithm can be made iterative, you probably have to design it in another way or find a different code Good
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects
thanks 1 user thanked Davide Carpi for this useful post.
on 31/08/2019(UTC)
Offline alyles  
#12 Posted : 31 August 2019 17:33:57(UTC)
alyles


Rank: Advanced Member

Groups: Registered
Joined: 23/03/2016(UTC)
Posts: 277
United States

Was thanked: 91 time(s) in 57 post(s)
As Davide said, you don't have to do it recursively. Biggrin

It may be slow, but it works. Might be able to optimize for better performance.

Untitled.png
sudoku_lyles2.sm (29kb) downloaded 21 time(s).
Feel free to join the SMath Studio Users Discord Channel: https://discord.gg/PayZpJW
thanks 1 user thanked alyles for this useful post.
on 31/08/2019(UTC)
Offline alyles  
#13 Posted : 31 August 2019 17:48:48(UTC)
alyles


Rank: Advanced Member

Groups: Registered
Joined: 23/03/2016(UTC)
Posts: 277
United States

Was thanked: 91 time(s) in 57 post(s)
Originally Posted by: alyles Go to Quoted Post
As Davide said, you don't have to do it recursively. Biggrin

It may be slow, but it works. Might be able to optimize for better performance.

Untitled.png
sudoku_lyles2.sm (29kb) downloaded 21 time(s).


Well it works for the first two puzzles at least. Having issues with the third. Haven't tried the fourth....

It's a start
Feel free to join the SMath Studio Users Discord Channel: https://discord.gg/PayZpJW
Offline ndtma  
#14 Posted : 31 August 2019 18:42:16(UTC)
ndtma


Rank: Advanced Member

Groups: Registered
Joined: 05/06/2014(UTC)
Posts: 348
Man
Sri Lanka
Location: Colombo

Was thanked: 125 time(s) in 82 post(s)
Originally Posted by: alyles Go to Quoted Post

Well it works for the first two puzzles at least. Having issues with the third. Haven't tried the fourth....


It works for the fourth, but fails for the third puzzle.
Look within!... The secret is inside you.
Best Regards
Eng. NDTM Amarasekera - Sri Lanka
Offline overlord  
#15 Posted : 31 August 2019 19:04:33(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 1,128
Turkey

Was thanked: 509 time(s) in 339 post(s)
Originally Posted by: Davide Carpi Go to Quoted Post
I remember this limitation was introduced some time ago to avoid unintentional hard crashes on wrong formed algorithms; considering that any recursive algorithm can be made iterative, you probably have to design it in another way or find a different code Good

Wow, I didn't know there is a purposed limitation for recursion. For those who knows what they are doing, I think that limit can be removed by adding an extra code at the beginning like (recurselimit:=0), or re-adding the limit (recurselimit:=1).

Yeah it can be made iterative, alyles already did one as far as I can see. I just wanted you guys to check if I did something wrong about my algorithm. I just love recursing, I always feel like it is magical to me. Good

Thank you all guys, regards


Offline overlord  
#16 Posted : 31 August 2019 19:31:14(UTC)
overlord


Rank: Advanced Member

Groups: Registered
Joined: 23/07/2013(UTC)
Posts: 1,128
Turkey

Was thanked: 509 time(s) in 339 post(s)
Originally Posted by: ndtma Go to Quoted Post
Originally Posted by: alyles Go to Quoted Post

Well it works for the first two puzzles at least. Having issues with the third. Haven't tried the fourth....


It works for the fourth, but fails for the third puzzle.


Don't bother about it. I found these sudoku's on internet without checking them.
I checked that puzzle with C code right now and it has no solution.

Regards
Offline Jean Giraud  
#17 Posted : 01 September 2019 00:15:53(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Confirmed: puzzle 3 fails "Requested matrix does not exist" [q=> 1..99]
Offline Razonar  
#18 Posted : 01 September 2019 07:50:14(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,357
Uruguay

Was thanked: 817 time(s) in 517 post(s)
Originally Posted by: overlord Go to Quoted Post

Wow, I didn't know there is a purposed limitation for recursion. For those who knows what they are doing, I think that limit can be removed by adding an extra code at the beginning like (recurselimit:=0), or re-adding the limit (recurselimit:=1).



Hi. It exists in other languages. For instance, can see it at https://reference.wolfram.com/language/ref/$RecursionLimit.html

Best regards.
Alvaro.
thanks 1 user thanked Razonar for this useful post.
on 01/09/2019(UTC)
Offline Jean Giraud  
#19 Posted : 01 September 2019 16:15:02(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: overlord Go to Quoted Post
Don't bother about it. I found these sudoku's on internet without checking them.
I checked that puzzle with C code right now and it has no solution.

You mean puzzle 3 has no solution ?
Solution is => give to champions.
Attached utilities may be useful otherwise.
Mathemtica 4.0 "recursion limit" is pure jargon.
In some program, if/otherwise does not work [if/else = YES].
Is there a catch wrt puzzle 3 ... Watson ?

Utilities Matrix Count in Soduku.sm (23kb) downloaded 10 time(s).
Offline Razonar  
#20 Posted : 03 September 2019 05:14:14(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,357
Uruguay

Was thanked: 817 time(s) in 517 post(s)
Hi. Here, the mathcad 11 version for the recursive method. Notice that mathcad don't pass values by reference, only by value, so the matrix can't be modified inside the fucntion, only 'outside'. Also, mathcad have the return function as keyword but isn't used.

SudokuRec.zip (25kb) downloaded 12 time(s).
SudokuRec.pdf (286kb) downloaded 15 time(s).

This other is te recurrent version in SMath: don't works.

SudokuRec.sm (46kb) downloaded 13 time(s).

This other is a rewrite of the alyles version.

SudokuBruteForce_lyles.sm (44kb) downloaded 26 time(s).

Best regards.
Alvaro.

Edited by user 03 September 2019 05:18:41(UTC)  | Reason: Not specified

thanks 1 user thanked Razonar for this useful post.
on 03/09/2019(UTC)
Users browsing this topic
Guest
2 Pages12>
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.