A pure R poker hand evaluator

December 5, 2011
By

(This article was first published on Modern Toolmaking, and kindly contributed to R-bloggers)

There's already a lot of great posts out there about poker hand evaluators, so I'll keep this short.  Kenneth J. Shackleton recently released a very slick 5-card and 7-card poker hand evaluator called SpecialK.  This evaluator is licensed under GPL 3, and is described in detail in 2 blog posts: part 1 and part 2.  Since the provided code is open source, I felt free to hack around with it a bit, and ported the python source to R.

You can download my code from github, and save it to ~/SpecialK/R.  Run the following script to initialize the evaluator and test it out.  Higher numbers=better hands.

My R code is almost identical to the python source, as all I did was change python classes to R lists and make the lists 1-indexed (R) rather than 0-indexed (python).  I also used the wonderful bitops package for bitwise operations. Obviously this code should be vectorized, but I don't have time to do that right now.  I also used the "compiler" package to speed things up somewhat, but the "SevenEval" file still takes a long time to load.

Right now, I'm getting about 18,000 hands per second on my core i5 laptop, which is pretty crappy compared to the 250 million hands per second Shackleton reports for the C++ version.  Vectorization should further increase performance, but I don't think pure R is ever going to approach C++ in terms of raw speed.  While this was a fun port to make, I think this code is an obvious candidate for a re-write using the Rcpp package.

I'm interested to see how far the pure R code can be optimized.


To leave a comment for the author, please follow the link and comment on his blog: Modern Toolmaking.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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...

Comments are closed.