{"id":102,"date":"2014-05-24T10:18:38","date_gmt":"2014-05-24T14:18:38","guid":{"rendered":"https:\/\/dorcgames.com\/blog\/?p=102"},"modified":"2014-05-24T12:49:25","modified_gmt":"2014-05-24T16:49:25","slug":"in-search-of-the-fabled-flagorithm","status":"publish","type":"post","link":"https:\/\/dorcgames.com\/blog\/?p=102","title":{"rendered":"In Search of the Fabled Flagorithm"},"content":{"rendered":"<p><a href=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/FactoryLogo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-106\" src=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/FactoryLogo.png\" alt=\"My Pretentions\" width=\"880\" height=\"880\" srcset=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/FactoryLogo.png 880w, https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/FactoryLogo-150x150.png 150w, https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/FactoryLogo-300x300.png 300w, https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/FactoryLogo-800x800.png 800w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/a><\/p>\n<p>Wherein a frustrated businessman is thrust into the harsh light of international industry, and comes to grips with a challenge far greater than himself:\u00a0 The Flag Unions.<\/p>\n<p>What&#8217;s this got to do with HUSSAR?\u00a0 Palettes.\u00a0 It&#8217;s always palettes.<\/p>\n<p><!--more--><\/p>\n<p>I&#8217;ll paraphrase the problem I posted to Facebook in late 2012:<\/p>\n<p><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">I own a <span class=\"il\">flag<\/span>-making business and have an opportunity to make a big haul on a large order, but first I need to prove out my logistics with a smaller run. I&#8217;m trying to minimize my costs.<\/span><br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/> <br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">I have two factories (Factory A is in Algeria, and Factory B is in Brazil). Each factory has its own <span class=\"il\">flag<\/span>-making machine, and each machine can only hold 5 colors of ink. Ink is horrendously expensive, so I&#8217;m trying to optimize which ink barrels go to each Factory. Changing ink is also time-consuming and expensive (damn those Brazilian and Algerian unions!), so I want to figure out which factories get which inks in advance. Also due to union rules, a <span class=\"il\">flag<\/span> must be made at one and only one factory (so no printing half a <span class=\"il\">flag<\/span>&#8216;s colors in one factory and shipping it to the other to finish).<\/span><br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/> <br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">Ink comes in the following colors: <\/span><\/p>\n<p><a href=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/AvailableColors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-109\" src=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/AvailableColors.png\" alt=\"AvailableColors\" width=\"640\" height=\"128\" srcset=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/AvailableColors.png 640w, https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/AvailableColors-300x60.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">I&#8217;m not allowed to mix inks, again because of those pesky union rules.<\/span><br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/> <br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">A <span class=\"il\">flag<\/span> consists of between 1 and 5 colors, which is convenient because as stated before, the machines can only hold 5 inks.<\/span><br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/> <br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">Here&#8217;s my trial order of flags, and the Factories illustrated:<\/span><\/p>\n<div id=\"attachment_110\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapOverview.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-110\" class=\"size-full wp-image-110\" src=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapOverview.png\" alt=\"A total of 6 unique colors, more than any one Factory can hold.\" width=\"600\" height=\"440\" srcset=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapOverview.png 600w, https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapOverview-300x220.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-110\" class=\"wp-caption-text\">A total of 6 unique colors, more than any one Factory can hold, so we definitely have to split this up!<\/p><\/div>\n<p><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">Here is a possible solution of which inks to send to each factory:<\/span><\/p>\n<div style=\"font-family: arial,sans-serif; font-size: 13px;\">\n<div id=\"attachment_111\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-111\" class=\"size-full wp-image-111\" src=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution1.png\" alt=\"A total of 9 colors.\" width=\"600\" height=\"248\" srcset=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution1.png 600w, https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution1-300x124.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-111\" class=\"wp-caption-text\">An initial solution.<\/p><\/div>\n<p><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">That&#8217;s 9 barrels of ink.<\/span><br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/> <br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">Here&#8217;s another:<\/span><\/p>\n<\/div>\n<div style=\"font-family: arial,sans-serif; font-size: 13px;\">\n<div id=\"attachment_112\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-112\" class=\"wp-image-112 size-full\" src=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution2.png\" alt=\"MapSolution2\" width=\"600\" height=\"248\" srcset=\"https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution2.png 600w, https:\/\/dorcgames.com\/blog\/wp-content\/uploads\/2014\/05\/MapSolution2-300x124.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-112\" class=\"wp-caption-text\">Better&#8230;but is it the best?<\/p><\/div>\n<p><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">That&#8217;s only 8 barrels of ink! Is this the best I can do, though? I&#8217;d like an algorithm that could prove that, while running in a reasonable amount of time (preferably in under a couple of seconds).<\/span><br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/> <br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">As mentioned before, this is a trial run for moving up to the big leagues. In the big leagues, I&#8217;ll have up to 64 ink choices, my Factory machines will be upgraded to hold 16 inks, and my orders will be up to 512 unique flags with up to 16 colors in each <span class=\"il\">flag<\/span>. The big-big leagues are 4096 ink choices, and 16 factories!<\/span><br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/> <br style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\" \/><span style=\"text-align: left; font-size: 14px; white-space: pre-wrap; font-family: calibri,arial,sans,sans-serif;\">There are certain heuristics that can be employed, like building a hierarchy (e.g., I know that if I can fit USA, I can certainly fit Canada or Finland as they are proper subsets), or tossing out possible solutions if the number of colors would overflow.<\/span><\/p>\n<\/div>\n<div style=\"font-family: arial,sans-serif; font-size: 13px;\"><\/div>\n<div style=\"font-family: arial,sans-serif; font-size: 13px;\">To sum up, we need to solve the following problem:<\/div>\n<p><div style=\"font-family: arial,sans-serif; font-size: 13px; text-align: center;\"><em>Given a set of Factories (each with a fixed capacity for ink barrels) and an arbitrary set of Flags (each made up of a combination of inks), and where each Flag must be made completely at one Factory, find a method that allocates Flags to Factories such that the total number of inks is minimized.<\/em><\/div>\n<p><div style=\"font-family: arial,sans-serif; font-size: 13px;\">Let&#8217;s call it the <strong>Flagorithm\u2122<\/strong>, a term coined, copyrighted, patented, fiercely protected by, and used under license (with much gratitude!) from Kat over at <a title=\"Klobit\" href=\"http:\/\/klobit.com\/\" target=\"_blank\">Klobit<\/a>.<\/div>\n<p><h2>What&#8217;s This Really About?<\/h2>\n<p>The analogy, as alluded to in the teaser, regards hardware palettes and how to distribute colors of our images across those palettes.\u00a0 As you&#8217;ll remember from <a title=\"Developing a Palate for Palettes\" href=\"https:\/\/dorcgames.com\/blog\/?p=69\">the previous post<\/a>, palettes are at a premium in old school hardware, and yet capable of some fantastic effects that can really bring our games alive.\u00a0 We want to maximize our palette usage to allow as many colors onscreen as possible, and to leave room for all those cool tricks.<\/p>\n<p>However, it turns out this is a non-trivial problem to solve:\u00a0 determining how we allocate our images (flags) across the hardware palettes (factories) quickly becomes infeasible.\u00a0 Our worst-case scenarios can easily enter into the billions&#8211;or more!&#8211;possible combinations.\u00a0 Time to break out the ol&#8217; computer science textbooks.<\/p>\n<h2>Brute Force<\/h2>\n<p>One very valid approach might be to brute force our way through all of the various combinations.\u00a0 The issue here, of course, is how difficult that winds up being.\u00a0 Let&#8217;s consider the facts:<\/p>\n<ul>\n<li>Each flag must be dedicated to one factory<\/li>\n<li>No flag can have more colors than a single factory can hold inks<\/li>\n<li>There are no limits to how many flags will be made at a given factory<\/li>\n<\/ul>\n<p>With that in mind, each flag could be made at any factory.\u00a0 If we have 8 flags and 2 factories, as in our example, each flag could be made at either factory, or 2<span class=\"nowrap\"><span style=\"margin-left: 0.25em; margin-right: 0.15em;\">\u00d7<\/span><\/span>2<span class=\"nowrap\"><span style=\"margin-left: 0.25em; margin-right: 0.15em;\">\u00d7<\/span><\/span>2<span class=\"nowrap\"><span style=\"margin-left: 0.25em; margin-right: 0.15em;\">\u00d7<\/span><\/span>2<span class=\"nowrap\"><span style=\"margin-left: 0.25em; margin-right: 0.15em;\">\u00d7<\/span><\/span>2<span class=\"nowrap\"><span style=\"margin-left: 0.25em; margin-right: 0.15em;\">\u00d7<\/span><\/span>2<span class=\"nowrap\"><span style=\"margin-left: 0.25em; margin-right: 0.15em;\">\u00d7<\/span><\/span>2<span class=\"nowrap\"><span style=\"margin-left: 0.25em; margin-right: 0.15em;\">\u00d7<\/span><\/span>2 = 256 combinations.\u00a0 Extrapolating, we wind up with a simple equation of:<\/p>\n<p>numCombinations = numFactories to the numFlags power.<\/p>\n<p>This is a very reasonable number for our test case.\u00a0 However, on even the Sega Master System (2 palettes, maximum of 448 tiles), this is a whopping <span id=\"cwos\" class=\"cwcot\">7.268387e+134<\/span> combinations.\u00a0 On a system with 4 palettes&#8230;well, I&#8217;ll let you think about that.\u00a0 This matches the definition of an exponential worst-case scenario:\u00a0 O( c^n ), where c &gt; 1.<\/p>\n<h2>Other Factors<\/h2>\n<p>As we explore solutions to this problem, let us consider a few additional wrinkles:<\/p>\n<ul>\n<li>This isn&#8217;t a straight-up <a title=\"Bin-Packing Problem\" href=\"http:\/\/en.wikipedia.org\/wiki\/Bin_packing_problem\" target=\"_blank\">bin-packing problem<\/a>, as choosing a flag for a factory doesn&#8217;t necessarily add the total colors in the flag to the factory&#8217;s roster&#8211;there&#8217;s often some overlap.\u00a0 Most bin-packing algorithms reduce the space available with each &#8220;move.&#8221;<\/li>\n<li>Similarly, this isn&#8217;t exactly a <a title=\"Knapsack Problem\" href=\"http:\/\/en.wikipedia.org\/wiki\/Knapsack_problem\" target=\"_blank\">knapsack problem<\/a>, either, although it seems to share many of the same constraints.<\/li>\n<li>How we calculate the effects of a &#8220;move&#8221; (i.e., see how many inks a given flag would add) can become computationally expensive.\u00a0 We need efficient algorithms, such as fast <a title=\"Union-Find data structures\" href=\"http:\/\/en.wikipedia.org\/wiki\/Union-find\" target=\"_blank\">union-find structures<\/a>, to gauge impact.<\/li>\n<li>At the end of the day, we only need to make exactly numFlags moves, but the effects of <em>each one of those moves<\/em> will influence future choices.<\/li>\n<li>We need to look at flags relationships to each other, and not necessarily in the obvious ways.\u00a0 Which flags share most in common with others?\u00a0 Which ones get us the most bang-for-the-buck?<\/li>\n<li>Is there a way to prove that we can solve this?\u00a0 Are we in the realm of <a href=\"http:\/\/en.wikipedia.org\/wiki\/NP-hard\" target=\"_blank\">NP-Hard<\/a> or <a href=\"http:\/\/en.wikipedia.org\/wiki\/NP-complete\" target=\"_blank\">NP-Complete<\/a>?<\/li>\n<\/ul>\n<h2>Coming Soon<\/h2>\n<p>In the future we&#8217;ll be looking into several approaches to the Flag Problem.\u00a0 One solution, suggested by Jason Hughes of <a href=\"http:\/\/steelpennygames.com\/newsite\/\" target=\"_blank\">Steel Penny Games<\/a>, set me on a long and winding road through such hallowed names as Skiena and Sedgewick.\u00a0 While I prepare a post about that solution, I&#8217;d welcome other thoughts about how to solve this deceptively complex problem!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wherein a frustrated businessman is thrust into the harsh light of international industry, and comes to grips with a challenge far greater than himself:\u00a0 The Flag Unions. What&#8217;s this got to do with HUSSAR?\u00a0 Palettes.\u00a0 It&#8217;s always palettes.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-hussar"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=102"}],"version-history":[{"count":10,"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":121,"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions\/121"}],"wp:attachment":[{"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dorcgames.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}