{"id":970,"date":"2016-08-14T18:43:22","date_gmt":"2016-08-14T23:43:22","guid":{"rendered":"http:\/\/www.laurentlessard.com\/bookproofs\/?p=970"},"modified":"2016-10-08T14:31:22","modified_gmt":"2016-10-08T19:31:22","slug":"pokemon-go-efficiency","status":"publish","type":"post","link":"https:\/\/laurentlessard.com\/bookproofs\/pokemon-go-efficiency\/","title":{"rendered":"Pok\u00e9mon Go Efficiency"},"content":{"rendered":"<p>This <a href=\"http:\/\/fivethirtyeight.com\/features\/can-you-solve-the-puzzle-of-pokemon-go-efficiency\/\">Riddler puzzle<\/a> is about a topic near and dear to many hearts: Pok\u00e9mon!<\/p>\n<blockquote><p>\nYour neighborhood park is full of Pok\u00e9stops \u2014 places where you can restock on Pok\u00e9balls to, yes, catch more Pok\u00e9mon! You are at one of them right now and want to visit them all. The Pok\u00e9stops are located at points whose (x, y) coordinates are integers on a fixed coordinate system in the park.<\/p>\n<p>For any given pair of Pok\u00e9stops in your park, it is possible to walk from one to the other along a path that always goes from one Pok\u00e9stop to another Pok\u00e9stop adjacent to it. (Two Pok\u00e9stops are considered adjacent if they are at points that are exactly 1 unit apart. For example, Pok\u00e9stops at (3, 4) and (4, 4) would be considered adjacent.)<\/p>\n<p>You\u2019re an ambitious and efficient Pok\u00e9mon trainer, who is also a bit of a homebody: You wish to visit each Pok\u00e9stop and return to where you started, while traveling the shortest possible total distance. In this open park, it is possible to walk in a straight line from any point to any other point \u2014 you\u2019re not confined to the coordinate system\u2019s grid. It turns out that this is a really hard problem, so you seek an approximate solution.<\/p>\n<p>If there are N Pok\u00e9stops in total, find the upper and lower bounds on the total length of the optimal walk. (Your objective is to find bounds whose ratio is as close to 1 as possible.)<\/p>\n<p>Advanced extra credit: For solvers who prefer a numerical question with this theme, suppose that the Pok\u00e9stops are located at every point with coordinates (x, y), where x and y are relatively prime positive integers less than or equal to 1,000. Find upper and lower bounds for the length of the optimal walk, again seeking bounds whose ratio is as close to 1 as possible.\n<\/p><\/blockquote>\n<p>The problem of visiting a set of locations while minimizing total distance traveled is known as a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Travelling_salesman_problem\">Traveling Salesman Problem<\/a> (TSP), and it is indeed a famous and notoriously difficult problem in computer science. That being said, <em>bounding<\/em> the solution to a particular TSP instance can be easy if we take advantage of its structure.<\/p>\n<p>Here is my solution to the first part:<br \/>\n<a href=\"javascript:Solution('soln_pokemon1','toggle_pokemon1')\" id=\"toggle_pokemon1\">[Show Solution]<\/a><\/p>\n<div id=\"soln_pokemon1\" style=\"display: none\">\n<p>To clarify, we know there are $N$ Pok\u00e9stops but not how they are arranged. The question asks us to find the smallest and largest possible value for the minimum tour that visits each Pok\u00e9stop and returns to the starting point.<\/p>\n<h3>Upper bound<\/h3>\n<p>The worst-case arrangement of Pok\u00e9stops is to have them lie in a line, which leads to a path length of $2(N-1)$. Proving that this is indeed the worst possible arrangements of stops is a bit more involved, so read on if you&#8217;re interested.<\/p>\n<p>Let $T$ be a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Minimum_spanning_tree\">minimum spanning tree<\/a> for the set of Pok\u00e9stops. Any tree with $N$ nodes has $N-1$ edges, so $T$ has $N-1$ edges. It also follows from the adjacency property of Pok\u00e9stops that each edge of $T$ must have length $1$. One way to visit all the nodes is by using a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tree_traversal#Depth-first_search\">depth-first tree traversal<\/a>:<\/p>\n<ul>\n<li> Imagine all edges of $T$ are initially colored black.<\/li>\n<li> Pick a node at random to be the current node.<\/li>\n<li> Find a black edge emanating from the current node. Paint that edge blue and follow it to the next node, which becomes the new current node. If there are no more black edges, follow a blue edge instead and paint it red.<\/li>\n<li> Repeat until all edges are painted red.<\/li>\n<\/ul>\n<p>This process clearly visits all the nodes and visits each edge exactly twice, for a total tour length of $2(N-1)$. This construction works for <em>any<\/em> arrangement of Pok\u00e9stops. Of course this is merely an upper bound; the optimal path may be shorter. By arranging the $N$ stops in a line, we attain our upper bound of $2(N-1)$, so it must be a tight bound.<\/p>\n<h3>Lower bound<\/h3>\n<p>Since the Pok\u00e9stops lie at integer coordinates, the shortest possible distance from one to the next is $1$. So a path visiting $N$ Pok\u00e9stops and then returning to the start has length at least $N$. Does there exist an arrangement of Pok\u00e9stops that achieves this minimum? If $N$ is even, the answer is yes; arrange the stops in a $2\\times N$ grid:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_evengraph.png\" alt=\"pokemon_evengraph\" width=\"800\" height=\"600\" class=\"aligncenter size-full wp-image-976\" \/>If the number of stops is odd, however, it&#8217;s impossible to arrange them in a way that creates a tour of length $N$. To see why, imagine that the stops are on a checkerboard. Each minimum-length move takes you from black square to a white square or vice versa. Therefore, we can&#8217;t return to where we started after an odd number of moves. The second-shortest move has length $\\sqrt{2}$, so the best we can do is:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_oddgraph.png\" alt=\"pokemon_oddgraph\" width=\"800\" height=\"600\" class=\"aligncenter size-full wp-image-977\" \/>Therefore, a lower bound for the length of the optimal walk is given by:<\/p>\n<p>\\[<br \/>\n\\text{Lower Bound} = \\begin{cases}<br \/>\n0 &#038; \\text{if }N = 1 \\\\<br \/>\nN &#038; \\text{if }N\\text{ is even} \\\\<br \/>\nN-1+\\sqrt{2} &#038; \\text{if }N\\text{ is odd and }N \\ge 3<br \/>\n\\end{cases}<br \/>\n\\]<\/p>\n<h3>Putting it all together<\/h3>\n<p>So in the end, the smallest possible ratio of bounds assuming there are at least two Pok\u00e9stops is given by:<\/p>\n<p style=\"text-align: center;\"><span style=\"background-color: #AFC8E6; padding: 40px 20px 40px 20px;\">$\\displaystyle<br \/>\n\\text{Bound Ratio} = \\begin{cases}<br \/>\n2-\\tfrac{2}{N} &#038; \\text{if }N\\text{ is even} \\\\<br \/>\n2-\\tfrac{2\\sqrt{2}}{N-1+\\sqrt{2}} &#038; \\text{if }N\\text{ is odd}<br \/>\n\\end{cases}<br \/>\n$<\/span><\/p>\n<p>The smallest bound ratio that works for all $N$ is $2$, so this is the ratio we should use if we don&#8217;t know how many Pok\u00e9stops there are.\n<\/p><\/div>\n<p>Here is my solution to the second part:<br \/>\n<a href=\"javascript:Solution('soln_pokemon2','toggle_pokemon2')\" id=\"toggle_pokemon2\">[Show Solution]<\/a><\/p>\n<div id=\"soln_pokemon2\" style=\"display: none\">\n<p>For the second part of the Riddle, we are given a very specific arrangement of Pok\u00e9stops: the set of points $1 \\le i,j \\le M$ such that $\\mathrm{gcd}(i,j) = 1$ (greatest common divisor is $1$). The problem asks for $M=1000$, but we&#8217;ll keep $M$ as a parameter for now.<\/p>\n<h3>Upper bounds<\/h3>\n<p>Any tour that visits all the Pok\u00e9stop and returns to the starting point gives us an upper bound. With this many nodes, it&#8217;s impossible to check every tour, so we&#8217;ll compare different heuristics for choosing admissible paths and see how they compare.<\/p>\n<p><b>Trivial upper bound:<\/b> An obvious way to hit all the stops is to simply visit every point with integer coordinates in the $M\\times M$ square. We know how to do this optimally from the first part of the problem; the optimal tour has length $M^2$ if $M$ is even and $M^2+\\sqrt{2}-1$ if $M$ is odd. See the illustration below for example constructions:<br \/>\n<a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_ub_easy.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_ub_easy.png\" alt=\"pokemon_ub_easy\" width=\"784\" height=\"424\" class=\"aligncenter size-full wp-image-1030\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_ub_easy.png 784w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_ub_easy-300x162.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_ub_easy-768x415.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a>So the trivial bound for the case of interest is 1,000,000. We can definitely do better!<\/p>\n<p><b>Sierpi\u0144ski bound:<\/b> One way to find a reasonable tour is to use a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Space-filling_curve\">space-filling curve<\/a>, which maps every point in the square to a point on a curve. This gives us a way to order the stops based on where they lie on the curve. We then visit the stops in that order. This heuristic was pioneered by <a href=\"http:\/\/www2.isye.gatech.edu\/~jjb\/research\/mow\/mow.html\">Prof. John Bartholdi III<\/a> and others, and does very well in many cases.<br \/>\nThe <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sierpi%C5%84ski_curve\">Sierpi\u0144ski curve<\/a> is often used here, and it&#8217;s what I used to find an upper bound. Here are some examples of solutions for different $M$ values.<br \/>\n<a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_sierpinski_paths.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_sierpinski_paths.png\" alt=\"pokemon_sierpinski_paths\" width=\"1200\" height=\"600\" class=\"aligncenter size-full wp-image-1019\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_sierpinski_paths.png 1200w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_sierpinski_paths-300x150.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_sierpinski_paths-768x384.png 768w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_sierpinski_paths-1024x512.png 1024w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a>These paths have lengths 313.9 and 1828.8, respectively. Using this method, our upper bound improves to 815,234 for the $M=1000$ case, and this was computed in 0.35 seconds.<\/p>\n<p><b>Columnwise bound:<\/b> The Sierpi\u0144ski approach takes advantage of the pseudouniform distribution of points, but it doesn&#8217;t leverage the fact that points often appear in lines. For example, the rows and columns corresponding to prime coordinates are always full. Another heuristic, suggested by <a href=\"https:\/\/theexcelements.com\/\">Diarmuid Early<\/a> in a comment, is to proceed in a lawnmower-like pattern, but two columns at a time. So as we move up or down a pair of columns, we pick the next nearest point from the pair of available points at each step. Here are examples for the same grid sizes.<br \/>\n<a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_column_paths.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_column_paths.png\" alt=\"pokemon_column_paths\" width=\"1200\" height=\"600\" class=\"aligncenter size-full wp-image-1041\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_column_paths.png 1200w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_column_paths-300x150.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_column_paths-768x384.png 768w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_column_paths-1024x512.png 1024w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a>These paths are slightly shorter than the Sierpi\u0144ski ones, with lengths of 308.3 and 1721.8, respectively. Using this method, our upper bound improves to 735,394 for the $M=1000$ case, and was computed in 2 seconds.<\/p>\n<p><b>More on upper bounds:<\/b> Other notable heuristics for upper-bounding TSP problems include the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Nearest_neighbour_algorithm\">nearest neighbor algorithm<\/a> and the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Christofides_algorithm\">Christofides algorithm<\/a>. These don&#8217;t work well for our instance because they require computing <em>all<\/em> pairwise distances, a prohibitively expensive task when we have roughly 600,000 Pok\u00e9stops. The Sierpi\u0144ski and columnwise algorithms have <a href=\"https:\/\/en.wikipedia.org\/wiki\/Big_O_notation\">complexity<\/a> $\\mathcal{O}(n)$, where $n$ is the number of stops, whereas computing pairwise distances is $\\mathcal{O}(n^2)$. This is a significant difference because $n$ is so large &#8212; if an $\\mathcal{O}(n)$ algorithm takes 1 second then $\\mathcal{O}(n^2)$ will take roughly 7 days!<\/p>\n<h3>Lower bounds<\/h3>\n<p><b>Trivial lower bound:<\/b> One possibility is to count how many total stops there are, and assume we can find a path consisting entirely of adjacent stops. This isn&#8217;t possible of course, but it yields a lower bound. It&#8217;s relatively easy to write computer code to count this quantity, or we can use the fact that the density of stops in the $M \\times M$ square approaches $6\/\\pi^2 \\approx 0.6079$ as $M\\to\\infty$ (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Coprime_integers#Probabilities\">source<\/a>). For the case $M=1000$, the lower bound turns out to be 608,383. Lots of room for improvement!<\/p>\n<p><b>Averaging bound:<\/b> Another observation, again courtesy of <a href=\"https:\/\/theexcelements.com\/\">Diarmuid Early<\/a>, is that each Pok\u00e9stop has two edges emanating from it. So if we assume each stop is connected to its two nearest stops, we will get another lower bound on the shortest possible path. Computing this quantity for the case $M=1000$, we obtain a much-improved 643,811.<\/p>\n<p><b>Held-Karp relaxation:<\/b> This final bounding method is significantly more advanced. Finding a set of edges (i.e. assigning each edge a label 0 or 1) that yields a valid tour requires two things:<\/p>\n<ol>\n<li>each node has exactly two incident edges labeled 1<\/li>\n<li>there are no disjoint cycles (it&#8217;s a connected path)<\/li>\n<\/ol>\n<p>What makes this problem so hard is the second constraint. There are exponentially many cycles, so it&#8217;s not feasible to rule each one out. If we <em>relax<\/em> the problem by removing the disjoint cycles constraint, and allow the label on each edge to be any number between 0 and 1, the problem turns into a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Linear_programming\">linear program<\/a> (LP), which can be solved very efficiently. Successive refinements can then be made to improve the solution by ruling out particular cycles. This is known as the <a href=\"http:\/\/people.mpi-inf.mpg.de\/~rbeier\/opt2001\/held_karp.pdf\">Held-Karp LP relaxation<\/a>. Unfortunately, this still requires computing all pairwise distances, which we already decided was too costly (see above). The LP also has $\\mathcal{O}(n^2)$ variables, since we need a variable for each edge. So I made two simplifying assumptions:<\/p>\n<ol>\n<li>Each node may only be connected to one of its $k$ nearest neighbors. This is a relaxation to the GTSP, (traveling salesmsan problem on a graph). This means the number of variables, constraints, and distances we need to compute are now $\\mathcal{O}(kn)$.<\/li>\n<li>The final path will be symmetric ($x=y$). This allows us to use half as many variables and constraints and greatly speeds up computation.\n<\/ol>\n<p>To find an appropriate $k$, I solved the relaxed LP with $k$ values of 4, 8, 12, 16, 20. The solutions were 695251, 694956, 694954, 694952, 694952. So it seems there is no use in adding any more edges. For those that are interested, I implemented the solution in <a href=\"http:\/\/julialang.org\/\">Julia<\/a> using <a href=\"https:\/\/github.com\/JuliaOpt\/JuMP.jl\">JuMP<\/a> with <a href=\"http:\/\/www.gurobi.com\/\">Gurobi<\/a>. On my laptop, the LPs took 30&#8211;90 seconds to solve.<\/p>\n<h3>Putting everything together<\/h3>\n<p>Here is a plot comparing all the bounds above.<br \/>\n<a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_all_bounds.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_all_bounds.png\" alt=\"pokemon_all_bounds\" width=\"800\" height=\"500\" class=\"aligncenter size-full wp-image-1026\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_all_bounds.png 800w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_all_bounds-300x188.png 300w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2016\/08\/pokemon_all_bounds-768x480.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a>So ultimately, our best bound for the case $M=1000$ is<\/p>\n<p>\\[<br \/>\n694952 \\le L^\\star \\le 735394<br \/>\n\\]<\/p>\n<p>for an approximation ratio of about $1.06$.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This Riddler puzzle is about a topic near and dear to many hearts: Pok\u00e9mon! Your neighborhood park is full of Pok\u00e9stops \u2014 places where you can restock on Pok\u00e9balls to, yes, catch more Pok\u00e9mon! You are at one of them right now and want to visit them all. The Pok\u00e9stops are located at points whose &hellip; <a href=\"https:\/\/laurentlessard.com\/bookproofs\/pokemon-go-efficiency\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Pok\u00e9mon Go Efficiency&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1019,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[7],"tags":[25,30,27,26,2,32],"class_list":["post-970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-riddler","tag-graph-theory","tag-integer-programming","tag-linear-programming","tag-optimization","tag-riddler","tag-traveling-salesman"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/comments?post=970"}],"version-history":[{"count":63,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/970\/revisions"}],"predecessor-version":[{"id":1050,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/970\/revisions\/1050"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media\/1019"}],"wp:attachment":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media?parent=970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/categories?post=970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/tags?post=970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}