Solving Sudoku with Simulated Annealing

February 25, 2010

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

How long would it take you to solve this devlishly hard Sudoku puzzle (from Le Monde)?


You could do it the old-fashioned way — with a pencil — but Xi’an decided to solve it by programming a simulated annealing solver in R. The algorithm works by first guessing a solution at random — filling in the empty cells above with random digits between 1 and 9. Then it "scores" this solution by counting the number of digits duplicated in all the rows, columns and blocks. Next it evaluates a number of candidate new solutions by tweaking one of the free digits, and scores those. The algorithm then selects one of the candidate solutions at random for the next step, weighted by the change in the score.

The code to do all this , which you can use to solve your own Soduku puzzles if, say, you’ve lost your pencil, is at Xi’an’s blog.

Xi’an’s og: Sudoku via simulated annealing

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)