R based game “2048” with a simple API for ML benchmarking

March 16, 2016

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

For a long time I have been on the lookout for a ready-to-use R-based API for testing ML algorithms. My ideal “tool” would be an R-based API for the game “go” (“weiqi” in Chinese and “baduk” in  Korean), but I found none so far.

Since writing a rules engine is quite time-consuming, I would not venture developing one until recently when I stumbled upon the game “2048.” Many people use this simple game to benchmark their machine learning algorithms; and YouTube  has so many machine learning demos using “2048” that I won’t even list them here. To my surprise, all the videos I watched seem to use a java script based front-end for testing. Using a java script connection seemed like an over-complication for me including an additional bottleneck.

So after spending an hour building my own R-based version of “2048,” I decided to see what other options were available; and I found this nice implementation in C: https://github.com/mevdschee/2048.c by Maurits van der Schee. Porting from C saved me a lot of time. Many thanks to the original author.

I added a simple API for attaching the game code to a ML algorithm. I will just make a note that each game may be run in its own R environment, which allows for easy set up of parallel computing using the standard “foreach.” Other than that, the whole code is a little over 400 lines, so everything about its usage should be self-explanatory.

The program could be run in an interactive text mode as well (using `main_interactive()`). However, I found no way to port text coloring to the console of RStudio. Still, as this code is meant to be played by machines rather than human users, I suppose, I achieved my goal.

You can download the R version of the code from my github repository: github.com/cloudcell/2048_4ML. Comments / suggestions are always welcome.


# interactive mode
p.env <- new.env()

# using for benchmarking
p.env <- new.env()
main_ML_run(p.env, m =”L” )
# use p.env$board to retrieve board state
main_ML_run(p.env, m =”D”, show_board = FALSE)
# …


To leave a comment for the author, please follow the link and comment on their blog: gtdir.

R-bloggers.com 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...

Comments are closed.


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)