Ranjay Krishna

Can an Artificial Intelligence win 2048?

12/8/2014

19 Comments

 

Motivation

Sometime ago, just like everyone else, I managed to get addicted to the 2048 game. Whenever, I wanted to take a break from work, I would just open up a new tab and begin the repetitious arrow smashing. Watching the tiles fly by was just as much fun as actually trying to mine for that prized 2048 tile. 

Unfortunately, the game wouldn't just complete in minutes and I saw myself consistently spending more time on this game than I should. From an academic perspective, I thought it would be interesting to build an artificial intelligence (AI) to play the game for me. I reasoned that if I could get something to play for me, then I invariably have solved the game and would no longer be inclined to play it anymore and waste my time.
 
I was definitely wrong. Now that I have an AI playing the game for me, I tend to waste time not playing the game but by simply sitting there and watching the tiles move about, waiting for notable errors in my heuristic.

Artificial Intelligence Explanation

On every turn, the artificial intelligence is essentially making all 4 moves: UP, DOWN, LEFT, RIGHT and evaluating which one would be the best decision based on which one brings it closer to winning.

Since the game is randomized and at every step a random block is generated, the AI algorithm also generated all possible tiles in the available empty tiles with values of 2 and 4. It then continues to make moves and attempts to account for all possible random tile generations.

Because the AI is able to consider all possible future moves, shouldn't it always be able to win?Unfortunately, because considering all those cases grows exponentially and our browsers have limited CPU bandwidth, the algorithm above only looks 6 moves into the future. Check out the Time Calculations section for an analysis of the algorithm's speed.

So after looking 6 moves into the future, it uses a heuristic to estimate the value of the moves that has made. 

After evaluating all possible moves, it chooses the one that achieves the highest estimated value according to the heuristic.

Artificial Intelligence Algorithm

My two main requirements for the AI was to always win the game (achieve the 2048 tile) and to be fast enough to be rendered seamlessly using client side javascript.

The algorithm I used is expectimax. It follows the following recurrence relationship:
Picture
where state encodes the current game state with the location and values of all the tiles. Utility(state) is described in the Heuristics section below. PLAYER is an identifier indicating that it is the player's turn to move. After every turn, the BOARD generates a random tile.

Prob(state, tile) is the probability of a given tile with a specific location and value showing up on given state described by state. For a given location the probability of a tile with a value of 2 occurring is 0.9 while a value of 4 occurring is 0.1.

Successor(state, a) returns the state of the game after applying action a on the state. Finally, GenerateTIle() adds the tile to the state and returns the new state of the game.

Heuristics

I used three separate heuristics to evaluate the utility of a given game state.

Empty Tiles
The first heuristic was rather simple. It main main was to maximize the number of empty tiles on the board. However, every turn, the board generates a new tile. The intuition behind this heuristic was that the only way to move towards a board with more empty tiles is by merging blocks together. Merging blocks aligned perfectly with the main objective of the game and the heuristic performed rather well.

Monotonicity
Picture
The next heuristic attempted to maximize monotonicity on the grid. In other words, in the 4 directions, up, down, left and right, the heuristic attempted to arrange the numbers in decreasing order along one of those 4 directions. 

Gradients
Picture
The final heuristic assigned a varying weight to each of the different tiles in the board. Tiles in the corner were assigned a high weight to optimize for either one of the four corners. The heuristic pushed the highest valued tiles to one of the corners. This also increased the surface area of smaller valued tiles to occur next to the newly generated tiles, allowing easy merging without much reordering.

Results
The results for the three heuristics are shown in the table here. The gradient outperforms the other heuristics and is the one that is used in the AI above.

Heuristic

Empty Tiles

Monotonicity

Gradients

Average Score

18,383.02

2,497.088

60,631.4

Comparisons to Minimax

I also implemented a Minimax algorithm and the source for that can also be found in the repository. Unlike the expectimax algorithm, minimax optimizes for the worse case generation of tiles. It assumes that the board is not randomly generating tiles but is doing so to prevent the user from winning. The recurrence relationship that my minimax follows is below:
Picture
where the methods are the same as the ones described above for expectimax.

Unfortunately, the assumption minimax makes performs a lot worse than expectimax. In an attempt to prevent a losing scenario, it fails to take risks and merge the medium sized tiles of value 8 or 16, essentially causing the algorithm to lose more often than it should.

However, if this algorithm wasn't being optimized for rendering the animation, and was implemented on a server, it would be able to search to a higher depth, allowing the algorithm to perform a lot better.

Time Calculations

One of the biggest bottle necks of this algorithm is not being able to render the animation for the game and still have an efficient AI play the game using client side javascript. To that end, I conducted the following benchmarks for run time of the AI algorithms I implemented.
The first experiment I conducted was applying alpha-beta pruning to the minimax algorithm. Essentially, I wanted to reduce the exponential increase in state space as I increased depth. I got great expected gains. The runtime decreased from around 1000ms to less than 200ms for a board with 15 empty tiles.

The results can be seen in graph (a).
Picture
(a)
Picture
(b)
The graph on the left, (b), shows how increasing the depth of the minimax algorithm results in a huge difference in run time between a depth of 6 and 8. This is after applying alpha-beta pruning.
The final time related experiment I conducted was comparing expectimax and minimax. Those results are on graph (c). 

Even though expectimax is a lot slower, it still performs a lot better than minimax in terms of average score. 
Picture
(c)
The current algorithm used above uses expectimax with a depth of 6 that varies if the number of empty tile is higher than a given threshold. It assumes that each move takes approximately 450ms to complete along with the animations in order to generate a smooth viewing of the AI in action.

Main Takeaway 

I had a lot of fun implementing this. There are tons of improvements and benchmarks to run but I don't have the time to spend on this. Hope you enjoy this game as much as I have and like watching the AI "do it's thing".
19 Comments
luke
12/9/2014 03:17:02 am

great job this is amazing

Reply
www.scorespro.com link
2/12/2015 11:58:24 am

Choosing good tennis game instructors together with tennis motorcoaches is problematic. The greater part are old college or graduation players what person look relatively good punishing a shot, but own little authentic teaching working experience.

Reply
Peter Harkins link
12/9/2014 03:25:00 am

I'd be curious to see how much better (or worse...) it does with two rule changes.

1. Drop monotinicity in a direction and strive for monotinicity in a connected graph (eg a snake pattern from lower-left corner to upper-left, then the square right, down to the bottom, etc. - but tolerant of different bends if a bad tile gets in the way).

2. Weighting the value of monotinicity with the point value of the tile, so it is more cautious about disrupting the higher-value tiles.

This is pretty much how I play and I run up to 16,384 (despite knowing there's a regular trap I fall into that I can't yet see far enough ahead to predict - but I'd expect 6 moves would be plenty!).

I think with these you could probably drop the corner gradient rule, which I can see pushing the algorithm to take unnecessary risks by waste empty tiles. That's mostly offset now by how far it looks ahead.

Reply
Simon Zhu link
12/9/2014 02:28:02 pm

Hey Ranjay! Great Article! You are quite amazing!

Reply
Ankit Gadi
12/9/2014 04:09:30 pm

Great Article Bro.... Really Interesting stuff.

Reply
Bulk SMS in Jaipur link
12/22/2014 02:43:41 am

Hey that was terrific to study. Thanks for the wonderful post .Loved every single portion of it.

Reply
sell loose diamonds link
1/2/2015 04:19:20 am

Hey that was terrific to study. Thanks for the excellent post .Loved every single portion of it.

Reply
Free online astrology reports link
1/9/2015 07:49:32 pm

Great blog share I like your post.

Reply
Vashikaran specialist link
1/12/2015 06:37:07 pm

Great Article !! I like it...

Reply
Husband wife relationship problems solution link
1/16/2015 02:41:40 pm

Nice information share with us...

Reply
Husband-wife problems solutions link
1/16/2015 07:25:41 pm

Nice Blog !! your writing skills is very unique and informative...

Reply
Online relationship solutions link
1/16/2015 08:59:56 pm

Nice Post ! your blog is always unique and informative..

Reply
Solve my love problem link
1/18/2015 03:19:41 pm

Nice Article !! i like your post

Reply
Love Problem Solution link
1/21/2015 01:41:54 pm

Great website. Such an responsible and worth data and that i like that sort of anticipation when you produced it for us.

Reply
Lost love back in India link
1/21/2015 08:32:16 pm

Great Article !! Lot of information in your Article and many people like your information...

Reply
Love Problem Solution Baba Ji link
1/30/2015 02:48:11 pm

Supplied correct here definitely important aspects that may be really handy for all of us in any way. Quite a few many thanks for sharing stated right here.

Reply
girlsdoporn link
2/4/2015 02:32:10 am

I reasoned that if I could get something to play for me, then I invariably have solved the game and would no longer be inclined to play it anymore and waste my time.

Reply
Modular office Workstations Furniture Partitions in jaipur | Pronto Panel link
2/5/2015 06:46:03 pm

We are so much happy to your services. Your blog providing nice content writing services..

Reply
Brave Frontier Hack Free Unlimited Resources link
2/11/2015 10:36:35 pm

get from here the link to free brave frontier game.this is the homepage to the link for brave gems and coins online .grab them now.

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    Archives

    March 2015
    December 2014
    April 2014
    December 2013
    July 2013

    Categories

    All
    Artificial Intelligence
    Big Data
    Career
    Crisis
    Data Visualization
    Life
    Technology
    Writer

    RSS Feed

Web Hosting by iPage