{"id":5288,"date":"2014-05-02T12:16:20","date_gmt":"2014-05-02T16:16:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5288"},"modified":"2020-10-09T00:17:11","modified_gmt":"2020-10-09T04:17:11","slug":"2048-algorithms","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/05\/02\/2048-algorithms\/","title":{"rendered":"2048 Algorithms"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s picks this week are 2048 algorithms submitted by Oliver Woodford, Athi Narayanan, and Viktor.\r\n   <\/p>\r\n   <p>4 weeks ago, I posted this <a href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/\">blog post<\/a> asking people to submit algorithms for solving my <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46124-2048-matlab-edition\">2048 App<\/a>. I just want to say \"WOW and Thanks!!\" to everyone who participated. It was great to have so many entries. I received 15\r\n      different entries, most of which were posted as <a href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/#view_comments\">comments<\/a> for the blog post. The entries ranged from simple algorithms (including my random-direction strategy, \"myAI\") to very sophisticated\r\n      algorithms that looked ahead multiple moves to find the best move.\r\n   <\/p>\r\n   <p>For this blog post, I'm simply going to reveal the results from the 1000-run simulations for each algorithm. Let's take a\r\n      look at the summary of the simulations.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">summaryTable<\/span>\r\nsummaryTable(:,{<span style=\"color: #A020F0\">'Algorithm'<\/span>,<span style=\"color: #A020F0\">'Author'<\/span>,<span style=\"color: #A020F0\">'HighScore'<\/span>,<span style=\"color: #A020F0\">'HighBlock'<\/span>,<span style=\"color: #A020F0\">'Time'<\/span>})<\/pre><pre style=\"font-style:oblique\">ans = \r\n          Algorithm             Author      HighScore    HighBlock     Time  \r\n    ______________________    __________    _________    _________    _______\r\n    oliverAI_v2               Oliver W      60540        4096          504.96\r\n    myAI_athi                 Athi N        49132        4096             NaN\r\n    viktorAI                  Viktor        36600        2048          4.5363\r\n    oliverAI_v1               Oliver W      36276        2048          13.848\r\n    myAI_JRM                  Jeremy M      35448        2048            3570\r\n    myAI_kasztel              kasztel       31052        2048           2.567\r\n    myAIFinal                 Boon          19752        1024          8.7533\r\n    greedyAI                  Guanfeng L    16764        1024          42.379\r\n    recurAI                   Guanfeng L    16356        1024          177.27\r\n    downAI                    Keith          8388         512          1.1215\r\n    myAI_Pooya                Pooya R        7400         512          1.2279\r\n    myAI_sequential           Seth D         7052         512         0.24592\r\n    myAISmart                 Jiro D         7016         512          0.9444\r\n    myAI_left                 John T         6616         512         0.31477\r\n    myAI_move2pair_v2         John T         5688         512          15.995\r\n    myAI_sequential_double    John T         5288         512         0.20424\r\n    myAI                      Jiro D         3052         256         0.43289\r\n<\/pre><p>It looks like we have a clear winner! Oliver's second version was able to get a high score of over 60000, with a high block\r\n      of 4096! His algorithm is one of the algorithms that heavily make use of the look-ahead technique. But you can also see that\r\n      his highest score took over 500 seconds to complete. Athi's algorithm had the 2nd highest score of 49132. <i>I had some problems with my analysis code for reproducing runs, so I don't have the time for Athi's best run.<\/i> What was impressive was that Viktor's algorithm reached 36600 points (high block of 2048) in only 4.5 seconds. In fact, you\r\n      can see quite a variation in execution times across different algorithms.\r\n   <\/p>\r\n   <p>But this table is only looking at the best score out of 1000 simulations. Let's now look at the distribution of the highest\r\n      blocks that were achieved. We'll take a look at it as a heat map. Note that the order is the same as the table above (in descending\r\n      order of HighScore).\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/2048Heatmap.png\"> <\/p>\r\n   <p>In general, there seems to be a correlation between the distribution of the high blocks and the highest score achieved. It's\r\n      impressive to see that Oliver's algorithm was able to get 2048 or higher over 70% of the time. Very nice!\r\n   <\/p>\r\n   <p><b>Other analyses<\/b><\/p>\r\n   <p>There are other metrics, which I haven't been able to look at for this blog post. A similar heat map for the scores would\r\n      be a good one. However, in general the scores and the high blocks were correlated, so I would expect a similar-looking heat\r\n      map.\r\n   <\/p>\r\n   <p><b>Animation of the algorithms<\/b><\/p>\r\n   <p>Let's take a look at a couple of the high scoring algorithms. Note that this animation is a \"replay\", so it doesn't reflect\r\n      the true computation time of the algorithms. The left one is Oliver's algorithm and the right one is Viktor's algorithm. You\r\n      can clearly see the differences in the strategies. Viktor's algorithm tries to stack the blocks to the bottom left, while\r\n      Oliver's has a more spread out approach trying to bring together similar blocks, even if they're far apart.\r\n   <\/p>\r\n   <p>\r\n\r\n<div style=\"width: 330px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-5288-1\" width=\"330\" height=\"410\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/Oliver.mp4?_=1\" \/><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/Oliver.mp4\">https:\/\/blogs.mathworks.com\/pick\/files\/Oliver.mp4<\/a><\/video><\/div> <div style=\"width: 330px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-5288-2\" width=\"330\" height=\"410\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/Viktor.mp4?_=2\" \/><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/Viktor.mp4\">https:\/\/blogs.mathworks.com\/pick\/files\/Viktor.mp4<\/a><\/video><\/div>\r\n      \r\n   <\/p>\r\n   <p><b>Winner<\/b><\/p>\r\n   <p>So who is the winner? Well, everyone's a winner! :) It was a lot of fun seeing the various algorithms. Okay, okay. I'll narrow\r\n      down the winners to a few people. Even though I had defined a clear metric in my previous <a href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/\">post<\/a>, I'm going to slightly modify it and give prizes to three people - Oliver Woodford, Athi Narayanan, and Viktor. Both Oliver\r\n      and Athi were able to very successfully achieve 2048, while Viktor was able to get the 3rd highest score with a fast algorithm.\r\n      Congratulations!! We'll contact you so that we can send you some MathWorks prizes.\r\n   <\/p>\r\n   <p>I hope you all enjoyed this mini-challenge. If you have any comments on this topic, please post <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5288#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_bcbb5aae9d2541ffa7ffa4587e7db972() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='bcbb5aae9d2541ffa7ffa4587e7db972 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' bcbb5aae9d2541ffa7ffa4587e7db972';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_bcbb5aae9d2541ffa7ffa4587e7db972()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p>\r\n<\/div>\r\n<!--\r\nbcbb5aae9d2541ffa7ffa4587e7db972 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s picks this week are 2048 algorithms submitted by Oliver Woodford,\r\n% Athi Narayanan, and Viktor.\r\n%\r\n% 4 weeks ago, I posted this\r\n% <https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/\r\n% blog post> asking people to submit algorithms for solving my\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46124-2048-matlab-edition 2048 App>. I\r\n% just want to say \"WOW and Thanks!!\" to everyone who participated. It was\r\n% great to have so many entries. I received 15 different entries, most of\r\n% which were posted as\r\n% <https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/#view_comments\r\n% comments> for the blog post. The entries ranged from simple algorithms\r\n% (including my random-direction strategy, \"myAI\") to very sophisticated\r\n% algorithms that looked ahead multiple moves to find the best move.\r\n%\r\n% For this blog post, I'm simply going to reveal the results from the\r\n% 1000-run simulations for each algorithm. Let's take a look at the summary\r\n% of the simulations.\r\n\r\nload summaryTable\r\nsummaryTable(:,{'Algorithm','Author','HighScore','HighBlock','Time'})\r\n\r\n%%\r\n% It looks like we have a clear winner! Oliver's second version was able to\r\n% get a high score of over 60000, with a high block of 4096! His algorithm\r\n% is one of the algorithms that heavily make use of the look-ahead\r\n% technique. But you can also see that his highest score took over 500\r\n% seconds to complete. Athi's algorithm had the 2nd highest score of 49132.\r\n% _I had some problems with my analysis code for reproducing runs, so I\r\n% don't have the time for Athi's best run._ What was impressive was that\r\n% Viktor's algorithm reached 36600 points (high block of 2048) in only 4.5\r\n% seconds. In fact, you can see quite a variation in execution times across\r\n% different algorithms.\r\n%\r\n% But this table is only looking at the best score out of 1000 simulations.\r\n% Let's now look at the distribution of the highest blocks that were\r\n% achieved. We'll take a look at it as a heat map. Note that the order is\r\n% the same as the table above (in descending order of HighScore).\r\n%\r\n% <<2048Heatmap.png>>\r\n%\r\n% In general, there seems to be a correlation between the distribution of\r\n% the high blocks and the highest score achieved. It's impressive to see\r\n% that Oliver's algorithm was able to get 2048 or higher over 70% of the\r\n% time. Very nice!\r\n%\r\n% *Other analyses*\r\n%\r\n% There are other metrics, which I haven't been able to look at for this\r\n% blog post. A similar heat map for the scores would be a good one. However,\r\n% in general the scores and the high blocks were correlated, so I would\r\n% expect a similar-looking heat map.\r\n%\r\n% *Animation of the algorithms*\r\n%\r\n% Let's take a look at a couple of the high scoring algorithms. Note that\r\n% this animation is a \"replay\", so it doesn't reflect the true computation\r\n% time of the algorithms. The left one is Oliver's algorithm and the\r\n% right one is Viktor's algorithm. You can clearly see the differences in\r\n% the strategies. Viktor's algorithm tries to stack the blocks to the\r\n% bottom left, while Oliver's has a more spread out approach trying to\r\n% bring together similar blocks, even if they're far apart.\r\n%\r\n% <html>\r\n%         <div id=\"media\">\r\n%             <object id=\"csSWF\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"330\" height=\"428\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,115,0\">\r\n%                 <param name=\"src\" value=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Oliver_controller.swf\"\/>\r\n%                 <param name=\"bgcolor\" value=\"#1a1a1a\"\/>\r\n%                 <param name=\"quality\" value=\"best\"\/>\r\n%                 <param name=\"allowScriptAccess\" value=\"always\"\/>\r\n%                 <param name=\"allowFullScreen\" value=\"true\"\/>\r\n%                 <param name=\"scale\" value=\"showall\"\/>\r\n%                 <param name=\"flashVars\" value=\"autostart=false#&thumb=https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Oliver_FirstFrame.png&thumbscale=45&color=0x1A1A1A,0x1A1A1A\"\/>\r\n%                 <embed name=\"csSWF\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Oliver_controller.swf\" width=\"330\" height=\"428\" bgcolor=\"#1a1a1a\" quality=\"best\" allowScriptAccess=\"always\" allowFullScreen=\"true\" scale=\"showall\" flashVars=\"autostart=false&thumb=https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Oliver_FirstFrame.png&thumbscale=45&color=0x1A1A1A,0x1A1A1A\" pluginspage=\"http:\/\/www.macromedia.com\/shockwave\/download\/index.cgi?P1_Prod_Version=ShockwaveFlash\"><\/embed>\r\n%             <\/object>\r\n%             <object id=\"csSWF\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"330\" height=\"428\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,115,0\">\r\n%                 <param name=\"src\" value=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Viktor_controller.swf\"\/>\r\n%                 <param name=\"bgcolor\" value=\"#1a1a1a\"\/>\r\n%                 <param name=\"quality\" value=\"best\"\/>\r\n%                 <param name=\"allowScriptAccess\" value=\"always\"\/>\r\n%                 <param name=\"allowFullScreen\" value=\"true\"\/>\r\n%                 <param name=\"scale\" value=\"showall\"\/>\r\n%                 <param name=\"flashVars\" value=\"autostart=false#&thumb=https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Viktor_FirstFrame.png&thumbscale=45&color=0x1A1A1A,0x1A1A1A\"\/>\r\n%                 <embed name=\"csSWF\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Viktor_controller.swf\" width=\"330\" height=\"428\" bgcolor=\"#1a1a1a\" quality=\"best\" allowScriptAccess=\"always\" allowFullScreen=\"true\" scale=\"showall\" flashVars=\"autostart=false&thumb=https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/Viktor_FirstFrame.png&thumbscale=45&color=0x1A1A1A,0x1A1A1A\" pluginspage=\"http:\/\/www.macromedia.com\/shockwave\/download\/index.cgi?P1_Prod_Version=ShockwaveFlash\"><\/embed>\r\n%             <\/object>\r\n%         <\/div>\r\n% <\/html>\r\n%\r\n% *Winner*\r\n%\r\n% So who is the winner? Well, everyone's a winner! :) It was a lot of fun\r\n% seeing the various algorithms. Okay, okay. I'll narrow down the winners\r\n% to a few people. Even though I had defined a clear metric in my previous\r\n% <https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/\r\n% post>, I'm going to slightly modify it and give prizes to three people -\r\n% Oliver Woodford, Athi Narayanan, and Viktor. Both Oliver and Athi were\r\n% able to very successfully achieve 2048, while Viktor was able to get the\r\n% 3rd highest score with a fast algorithm. Congratulations!! We'll contact\r\n% you so that we can send you some MathWorks prizes.\r\n%\r\n% I hope you all enjoyed this mini-challenge. If you have any comments on\r\n% this topic, please post <https:\/\/blogs.mathworks.com\/pick\/?p=5288#respond\r\n% here>.\r\n\r\n##### SOURCE END ##### bcbb5aae9d2541ffa7ffa4587e7db972\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048contestresults\/2048Heatmap.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   Jiro's picks this week are 2048 algorithms submitted by Oliver Woodford, Athi Narayanan, and Viktor.\r\n   \r\n   4 weeks ago, I posted this blog post asking people to submit algorithms for solving... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/05\/02\/2048-algorithms\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5288"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5288"}],"version-history":[{"count":17,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5288\/revisions"}],"predecessor-version":[{"id":11799,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5288\/revisions\/11799"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}