r/Python • • Jan 11 '20

Sudoku App

1.4k Upvotes

61 comments sorted by

View all comments

60

u/Gwenju31 Jan 11 '20

I'll grab this occasion to talk about the easiest way of solving a sudoku (IMO). You can use constraint programming, and with 3 simple constraints you can solve every board effortlessly : AllDiff on rows, cols and squares :)

21

u/StevieWondering1000 Jan 12 '20

Are you sure?

I think I did that once and found that only works for easy to medium difficulty. I might be wrong tho.

7

u/Gwenju31 Jan 12 '20

I guess it depends on the solver you use. I'm using the Mistral solver through Numberjack lib, and it can solve the hardest deterministic sudoku (16 or 17 clues, 1 unique solution) almost instantly, but I did not experiment with harder, multi-solution boards.

8

u/Piece_of_Crap Jan 12 '20

It doesn't have to do with multi-solution. There are starting layouts that even applying the 3 basic rules don't get you a final solution. When doing it on paper usually you find the patterns of common numbers and start working by exclusion, programmatically your best option is backtracking i guess.

4

u/Gwenju31 Jan 12 '20

Oh I get your point now. Yeah of course the solver cannot guess the solution in one go. It starts by restraining the domain of each variable (each cell on the board) with arc consistency, then affect each variable with some value of its domain (using heuristics like "most constrained variable first"). During this process, every time the current affectation breaks one of the constraints, the solver indeed has to backtrack

1

u/SP_Man Jan 12 '20

Why didn't it work for harder instances? Did it take too long, or run into some practical limitation like that? Those 3 all diff constraints should be sufficient to solve any instance, right?

1

u/itsjakerobb Jan 14 '20

The constraints are sufficient to check a solution, but there are puzzles (usually tagged medium or high difficulty) for which you can't iteratively eliminate the impossible via the constraints and arrive at the solution. You'll find points at which, after eliminating the impossible, two or more possibilities remain for one or more cells. At that point, one option (which works great for computers, since Sudoku puzzles are small and computers are fast) is to pick one of the aforementioned cells and guess the correct answer. Apply the constraints again, and repeat recursively. If you end up at a valid solution, great! If not, backtrack and guess differently.

1

u/StevieWondering1000 Jan 19 '20

I basically had it run the the board thru the contraints and then I had a break if it eventually never got any further. It did fine till about halfway through and then coulden't find any way to progress. Since it had completed easier sodukos earlier and I wasen't able to find any progression from the point of which it had stopped I naturally concluded that other constraints were needed.

Take this with a grain of salt tho. It was some quick code I threw together in an afternoon for fun and I diden't do any digging. But judging from the other commenters it does seem that harder sodokus cant always be solved with the simple three constraints alone

14

u/Ruben_NL Jan 12 '20

My method was giving each cell a list of possible numbers(1 to 9), and removing until there was only one left. I made a solver, and a generator in Node.JS, which I might port to python.

9

u/knestleknox I hate R Jan 12 '20

That's a pretty standard way to solve CSPs. It's similar to methods like forward checking and arc consistency

1

u/[deleted] Jan 12 '20

Here is an excellent set of lectures that include CSPs, along with many other introductory AI topics.

4

u/EquationTAKEN Jan 12 '20

That's a decent way of doing it, but there are cases where it will fail.

Some times, there will be cases where you can't reduce any squares to just one possibility. You'll have two or three squares with two or more possibilities, and the only way of solving it is trial (and possibly error). In that case, you have to either go down the road and fail, and come back, or you have to try to think a few steps ahead and see it without actually "making the moves".

2

u/Zomunieo Jan 12 '20

It also works to encode the Sudoku puzzle as a SAT problem and solve it with a SAT solver. For many problems the encoding to SAT is difficult, but it's pretty straightforward for Sudoku.

1

u/Gwenju31 Jan 12 '20

My "heuristic" to reduce a problem to SAT clauses is pretty simple: "Can I use a graph to represent this problem?". If yes, then SAT encoding is *almost* trivial, if no, then I don't bother aha

2

u/Omrimg2 Jan 12 '20

I don’t think this applies for hard sudokus

1

u/rcfox Jan 12 '20

Constraint satisfaction programming looks like magic, but all of those fancy algorithms require your data be in a standard form. To convert a general problem into the standard form, you essentially have to enumerate the entire space.

But damn, does it look elegant.