{"id":3612,"date":"2023-02-25T16:55:51","date_gmt":"2023-02-25T22:55:51","guid":{"rendered":"https:\/\/laurentlessard.com\/bookproofs\/?p=3612"},"modified":"2023-02-26T09:24:10","modified_gmt":"2023-02-26T15:24:10","slug":"ellipse-packing","status":"publish","type":"post","link":"https:\/\/laurentlessard.com\/bookproofs\/ellipse-packing\/","title":{"rendered":"Ellipse packing"},"content":{"rendered":"<p>You&#8217;ve heard of circle packing&#8230; Well this week&#8217;s <a href=\"https:\/\/fivethirtyeight.com\/features\/can-you-pack-the-ellipses\/\">Riddler Classic<\/a> is about ellipse packing!<\/p>\n<blockquote><p>\nThis week, try packing three ellipses with a major axis of length 2 and a minor axis of length 1 into a larger ellipse with the same aspect ratio. What is the smallest such larger ellipse you can find? Specifically, how long is its major axis?<\/p>\n<p><em>Extra credit:<\/em> Instead of three smaller ellipses, what about other numbers of ellipses?\n<\/p><\/blockquote>\n<p>My solution:<br \/>\n<a href=\"javascript:Solution('soln_ellipsepacking','toggle_ellipsepacking')\" id=\"toggle_ellipsepacking\">[Show Solution]<\/a><\/p>\n<div id=\"soln_ellipsepacking\" style=\"display: none\">\n<h3>Solution approach<\/h3>\n<p>This is a difficult problem, and I resorted to a rather brute-force computational approach. Roughly, I modeled the problem as a nonlinear (nonconvex) constrained continuous optimization problem, and then I threw a black-box optimizer at the problem with a large number of random initial guesses to help navigate the many local optima in the problem.<\/p>\n<p>I parameterized each inner ellipse by the coordinates of its center $(x_i,y_i)$ and its orientation angle $\\theta_i$. For the larger enclosing ellipse, I assumed it was centered at the origin with major axis length $r$. So for $n$ smaller ellipses, there was a total of $3n+1$ variables to solve for.<\/p>\n<p>The interior of an ellipse with major axis length $2$ and minor axis length $1$ centered at the origin is the set of points $(x,y)$ that satisfy<br \/>\n\\[<br \/>\nx^2 + 4y^2 \\leq 1<br \/>\n\\]If we rotate this ellipse by $\\theta_i$ and translate the center to $(x_i,y_i)$, we get, after some algebra, the transformed equation<br \/>\n\\[<br \/>\nE_i:\\quad\\frac{1}{2}\\begin{bmatrix}x-x_i\\\\y-y_i\\end{bmatrix}^\\top<br \/>\n\\begin{bmatrix}<br \/>\n5-3\\cos(2\\theta_i) &#038; -3\\sin(2\\theta_i) \\\\<br \/>\n-3\\sin(2\\theta_i) &#038; 5+3\\cos(2\\theta_i)<br \/>\n\\end{bmatrix}<br \/>\n\\begin{bmatrix}x-x_i\\\\y-y_i\\end{bmatrix}\\leq 1<br \/>\n\\]We also have the outer ellipse, which we assumed has major axis length $r$ and is centered at the origin with no rotation. This has the equation:<br \/>\n\\[<br \/>\nE_r:\\quad r^2x^2 + 4r^2 y^2 \\leq 1<br \/>\n\\]<\/p>\n<p>The constraints we need to worry about are as follows:<\/p>\n<ul>\n<li> The small ellipses do not intersect: $E_i \\cap E_j = \\emptyset$ for all $1 \\leq i \\lt j \\leq n$.\n<li> the small ellipses are inside the larger one: $E_i \\subseteq E_r$ for all $1 \\leq i \\leq n$.\n<\/ul>\n<p>The optimization problem we&#8217;re solving is therefore:<br \/>\n\\[<br \/>\n\\begin{aligned}<br \/>\n\\underset{x_i,y_i,\\theta_i,r}{\\text{minimize}} \\qquad&#038; r \\\\<br \/>\n\\text{such that:} \\qquad&#038;  E_i \\cap E_j = \\emptyset, \\quad 1 \\leq i \\lt j \\leq n \\\\<br \/>\n&#038; E_i \\subseteq E_r, \\quad 1 \\leq i \\leq n<br \/>\n\\end{aligned}<br \/>\n\\]<\/p>\n<p>There are various ways to specify the intersection and containment constraints, but I didn&#8217;t worry too much about it since I was going to use a black-box optimizer anyway. The method I chose was essentially to approximate the boundary of each ellipse as a polygon with a large number of vertices (I used 80). Then, containment or intersection between a pair of ellipses can be specified by enforcing that each point along the boundary of a given ellipse satisfy (or violate) the inequality defining the other ellipse. For each ellipse pair, I only used the point that yielded the worst constraint violation, thereby keeping the total number of constraints small.<\/p>\n<p>For the actual implementation, I used MATLAB&#8217;s <a href=\"https:\/\/www.mathworks.com\/help\/optim\/ug\/fmincon.html\" class=\"broken_link\">fmincon<\/a> function, which uses an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Interior-point_method\">interior-point solver<\/a> and approximates gradients using finite differencing. Roughly speaking, it repeatedly makes small adjustments to all the variables in an effort to reduce $r$ while satisfying all the constraints, and stops when it reaches a local optimum. It&#8217;s not pretty, but since the number of variables and constraints is relatively small, the algorithm is quite efficient (even in MATLAB!). As $n$ gets larger, the number of local minima also gets large, so it is increasingly likely that the solver will get &#8220;stuck&#8221; at a suboptimal solutions. To overcome this, I used 1000 random initializations for each $n$ and kept the best solutions.<\/p>\n<h3>Optimal packings<\/h3>\n<p>Here is what I found with $n=2,3,\\dots,10$. I am fairly confident that I found the optimal configurations for the smaller $n$ I tried, but for the larger ones, it may be that 1000 random trials only scratches the surface and it is possible to find better packings that the ones I&#8217;m showing here. That being said, the best packings I found for even $n$ were configurations that have 180-degree rotational symmetry. When $n$ is odd, however, the packings are much more difficult to reason about. (click to see the full-sized image)<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking1.png\" alt=\"\" width=\"2083\" height=\"1116\" class=\"aligncenter size-full wp-image-3617\" \/><\/a><\/p>\n<p>To show the effect of changing the initialization for the optimizer, here are the top 12 best solutions found by the optimizer for the case $n=10$. As we can see, there are many different configurations that yield a similar quality of solution. This is why the problem becomes increasingly difficult as we increase $n$; there are many &#8220;good&#8221; configurations, so it is difficult to find the best one.<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking2.png\" alt=\"\" width=\"2080\" height=\"1525\" class=\"aligncenter size-full wp-image-3616\" \/><\/a><\/p>\n<h3>Symmetric (suboptimal) packing<\/h3>\n<p>Another way to get a solution for the case $n=3$ is to consider the optimal packing of three circles inside another circle. There are infinitely many such arrangements, since we have rotational symmetry.<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking_anim1.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking_anim1.gif\" alt=\"\" width=\"800\" height=\"394\" class=\"aligncenter size-full wp-image-3626\" \/><\/a><\/p>\n<p>Then, if we stretch each such configuration horizontally by a factor of $2$, we obtain a valid packing of three ellipses, and again there is an infinite family with a kind of rotational symmetry. If each of the smaller ellipses has a width of $2$, then the larger ellipse has a width of $\\frac{4}{\\sqrt{3}}+2 \\approx 4.3094$. This is not as good as the packing we found above using optimization, which yielded $3.8759$. The reason for the difference is that the symmetric solution assumes the major axes of all four ellipses are aligned. It turns out that if you don&#8217;t align the ellipses, you can do better!<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking_anim2.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2023\/02\/ellipsepacking_anim2.gif\" alt=\"\" width=\"800\" height=\"394\" class=\"aligncenter size-full wp-image-3625\" \/><\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve heard of circle packing&#8230; Well this week&#8217;s Riddler Classic is about ellipse packing! This week, try packing three ellipses with a major axis of length 2 and a minor axis of length 1 into a larger ellipse with the same aspect ratio. What is the smallest such larger ellipse you can find? Specifically, how &hellip; <a href=\"https:\/\/laurentlessard.com\/bookproofs\/ellipse-packing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Ellipse packing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3623,"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":[37,10,26],"class_list":["post-3612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-riddler","tag-coding","tag-geometry","tag-optimization"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/3612","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=3612"}],"version-history":[{"count":12,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/3612\/revisions"}],"predecessor-version":[{"id":3614,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/3612\/revisions\/3614"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media\/3623"}],"wp:attachment":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media?parent=3612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/categories?post=3612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/tags?post=3612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}