Sudoku via simulated annealing

February 22, 2010

(This article was first published on Xi'an's Og » R, and kindly contributed to R-bloggers)

The Sudoku puzzle in this Sunday edition of Le Monde was horrendously difficult, so after spending one hour with only 4 entries filled, I decided to feed it to the simulated annealing R program I wrote while visiting SAMSI last year. The R program reached the exact (and only) solution in about 6000 iterations, as shown (?) on the graph above. The Sudoku grid is defined in the R program by a 9×9 matrix s and the simulated annealing target function counts the number of duplicates

for (r in 1:9){

After pruning out the deterministic entries (3 in my case!), the R program uses the temperature sequence


to weight the target function. and it runs over the 10,000 iterations random moves on some of the unallocated sites. On the graph above, the green dots correspond to accepted moves. The yellow dots correspond to accepted proposals to move a single site. These choices lead to a correct solution most of the time, the other cases most often producing a penalty of two. (Please note there is nothing optimised about my code. It takes ten to twenty minutes to produce the graph above. a far cry from the fastest Sudoku solvers!)

Filed under: R, Statistics Tagged: simulated annealing, sudoku

To leave a comment for the author, please follow the link and comment on their blog: Xi'an's Og » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

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

Tags: , , ,

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)