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

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.


Usage

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

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

 

Advertisements