<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LAMBDAPHONE &#187; heuristics</title>
	<atom:link href="http://coder.bsimmons.name/blog/tag/heuristics/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.bsimmons.name/blog</link>
	<description>fragmentary ideas  ䷿  intellectual what-nots  ䷷  and haskell programming  ䷴</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>17&#215;17: some Simulated Annealing updates</title>
		<link>http://coder.bsimmons.name/blog/2010/07/17x17-some-simulated-annealing-updates/</link>
		<comments>http://coder.bsimmons.name/blog/2010/07/17x17-some-simulated-annealing-updates/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 02:46:06 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[17x17]]></category>
		<category><![CDATA[Graph Theory]]></category>
		<category><![CDATA[heuristics]]></category>
		<category><![CDATA[short]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=459</guid>
		<description><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no</em></p></blockquote><p>&#8230; <a href="http://coder.bsimmons.name/blog/2010/07/17x17-some-simulated-annealing-updates/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no rectangle is formed from four cells of the same color.</em></p></blockquote>
<p>Just a few follow-ups to my previous <a href="http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/">post in which I use a simulated annealing-type algorithm</a> to find a good (hopefully complete) cover of a grid by swapping rows and columns from four identical sub-grids of 74 colors.</p>
<h2>Easing into new thresholds</h2>
<p>I modified my algorithm so that the likelihood of jumping into the highest permitted energy level is decreased over time; thus instead of having an abrupt transition to a new threshold, resulting in a steep dive, we instead <em>ease into</em> the new energy level. </p>
<p>Here is a graph of a run with the adjusted meta-heuristic (in blue) alongside a previous abrupt threshold transition run (in black). You can check compare it to the <a href="http://coder.bsimmons.name/blog/wp-content/uploads/5-2.combined-annotated.png">graph from the previous post</a>:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/output.10000.5.smooth_vs_jagged.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/output.10000.5.smooth_vs_jagged-300x81.png" alt="a more smooth graph vs. a graph with abrupt changes at each threshold change" title="output.10000.5.smooth_vs_jagged" width="300" height="81" class="aligncenter size-medium wp-image-460" /></a></p>
<p><span id="more-459"></span></p>
<p>Unfortunately this change didn&#8217;t cause any improvement in the solutions generated. They still seem to flatten at a grid with 19 &#8211; 22 uncolored cells.</p>
<h2>Testing a combination of sub-grids with known good solution</h2>
<p>I wondered if the lower bound I was smacking into was caused by the fact that I was trying to find a combination of four permutations <em>of the same subset</em>, and perhaps this particular subset didn&#8217;t mesh all that well with itself.</p>
<p>In the original blog posting laying out this challenge there is four-coloring of 17&#215;17 with only one cell left un-colorable in a truly painful refactoring process, I modified my script to take four separate and distinct sub-grids and perform the same shuffling procedure.</p>
<p>Rather than find the original arrangement of the colored subsets or one nearly as good, the program settles on a solution with around 30 uncolored cells. That is worse than my original version, corresponding to the fact that there are fewer cells to work with in this latter set of colored subsets: 288 vs. 296.</p>
<h2>Conclusion</h2>
<p>This doesn&#8217;t say anything about whether a full-cover can likely be obtained by permuting four overlapping copies of a single subset of colors. It does tell us that either my code is flawed, this isn&#8217;t a particularly effective method for this kind of search, or it needs to be tuned better.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/07/17x17-some-simulated-annealing-updates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>17&#215;17: A Simulated Annealing approach using thresholds</title>
		<link>http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/</link>
		<comments>http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 17:05:07 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[17x17]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[heuristics]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=443</guid>
		<description><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no</em></p></blockquote><p>&#8230; <a href="http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no rectangle is formed from four cells of the same color.</em></p></blockquote>
<p>This is something I&#8217;ve been wanting to play with for months now, but haven&#8217;t made the time to implement: I wondered if <a href="http://en.wikipedia.org/wiki/Simulated_annealing">simulated annealing</a> techniques could be effective in finding a complete grid covering. </p>
<p>We would simply start with four copies of the 74-color grid and swap their rows and columns around, within each color set, trying to cover all of the cells by the union of the four subsets. So all of the sets of cells of a single color are fundamentally simply different permutations of the same 74-cell rectangle-free Graph. </p>
<p>Here is an illustration of the concept. Our algorithm starts with all four colored sets of cell overlapping (the red cells are on top, covering the other three colors):</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-74-coloring.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-74-coloring-285x300.png" alt="Illustration showing four sets of colored cells, all overlapping in every cell" title="Screenshot - 74-coloring" width="285" height="300" class="aligncenter size-medium wp-image-446" /></a></p>
<p>&#8230;then we swap two rows or two columns from a colored subset of cells, until the four colored subsets spread out covering as much of the grid as possible. Here we cover all but 21 cells:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-20-free.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-20-free-300x291.png" alt="Four layers of cells of colors RGBY, spread out to cover most of the grid" title="Screenshot - 20 free" width="300" height="291" class="aligncenter size-medium wp-image-447" /></a></p>
<p><span id="more-443"></span></p>
<h2>A Brief Intro to Simulated Annealing</h2>
<p>Simulated Annealing is a bit of an intimidating term (has its origins in <a href="http://en.wikipedia.org/wiki/Annealing_%28metallurgy%29">metallurgy</a> of all things), but the idea is simple: you start off with an initial solution (in my case, four sets of cells, all of which overlap) and then you mutate it randomly by choosing a random &#8220;neighbor&#8221; solution (in my case, a grid in which two rows or two columns of the same color are swapped).</p>
<p>You score these solutions so that you can compare a current solution with a proposed mutated solution, and here&#8217;s the key: </p>
<blockquote><p>
You begin by accepting nearly every mutation, whether it improves your solution or not. But as the procedure progresses you become more picky about &#8220;how much worse&#8221; a new solution can be from the previous one.
</p></blockquote>
<p>So in the classic <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem">Traveling Salesman Problem</a> a neighbor solution would be a new route that goes from city C to city E, where in the last solution we went from city E to city C.</p>
<h3>Threshold Acceptance</h3>
<p>There are various ways of accomplishing the task of &#8220;ratcheting down&#8221; the computation over time. The traditional method (the method most matching the metallurgical metaphor, if you will) is to have a function that computes the probability of a proposed change being accepted. This probability function changes over time, such that as we progress it becomes more and more unlikely that a solution worse than the previous will be accepted.</p>
<p>Another method, and the one I chose to implement, seems to be referred to as <a href="http://comisef.wikidot.com/concept:thresholdaccepting">Threshold Accepting</a>; instead of having a probability function, you accept all mutations that are below a certain threshold of change from the previous solution. The threshold becomes more strict over time.</p>
<p>So in our case we might start off with a threshold of five, meaning that we will accept all changes that give us a <em>worse</em> solution of no more than 5 new un-colored cells. So for example: if swapping blue rows 5 and 12 create a new solution with 6 fewer colored (covered) cells from the previous solution (i.e. a worse solution by six), then it won&#8217;t be accepted by our meta-algorithm at this stage.</p>
<h2>Notes on the implementation</h2>
<p>The approach I tried to describe above was really attractive to me because it short-circuits the whole complex &#8220;rectangle-free&#8221; constraint entirely. We simply start with a known rectangle-free subset and see if we can make four copies fit together. </p>
<p>Also, because we swap two rows or columns at a time, we need only look at the local change of score produced by the swap and then add it to the global score to obtain the new global score.</p>
<p>The astronomical number (<code>17!^2</code>) of possible permutations of a single rectangle-free subset give me hope that this approach could work, but that is an open question as far as I know. </p>
<p>I wrote this first draft of the script in haskell (as usual), and was happy with how fast it is, thanks in large part to the <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/vector">vector library</a>, which was a pleasure to work with. The code uses 4 pairs of <code>Vector</code>s (arrays of Ints), 8 in total, each of which represent a row or column ordering of one of the four colors. </p>
<p>We store the original 74-cell rectangle-free subset as a 2D <code>Array (Int,Int) Bool</code>. The pairs of Vectors act as references to row or column slices of this 2D array.</p>
<p>When we want to score a swap, we have to &#8220;render&#8221; all four colors of the pair of rows/columns in question using our 2D reference array. Then a swap consists of swapping two Ints in a single Vector. This is much more friendly on more poor laptop than if we were to try storing all four color subsets as 2D arrays and swapping them around!</p>
<p>You can <a href="http://coder.bsimmons.name/code/simulatedAnnealing.lhs">download the code here</a>, but mind it isn&#8217;t incredibly pretty.</p>
<h2>Initial findings</h2>
<p>The best I&#8217;ve done with this initial version is a grid with 19 empty cells:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-19-free.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-19-free-283x300.png" alt="A 17 by 17 grid showing all but 19 cells colored" title="Screenshot - 19 free" width="283" height="300" class="aligncenter size-medium wp-image-448" /></a></p>
<p>It is obvious though that the heuristic needs a lot of tuning, and that in my case the &#8220;Threshold Accepting&#8221; approach isn&#8217;t working well. That is because it is far too granular: we get from complete disorder to a minimum in just a handful of threshold rounds.</p>
<p>Here is a quick graph overlaying two runs, one of which starts with a threshold (each threshold round bracketed in black) of 5, the other (in blue) starts with a threshold of 2. The Y-axis is the number of uncolored cells in the grid at the current solution:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/5-2.combined-annotated.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/5-2.combined-annotated-300x136.png" alt="" title="5-2.combined-annotated" width="300" height="136" class="aligncenter size-medium wp-image-449" /></a></p>
<p>You can see that both basically look like <a href="http://en.wikipedia.org/wiki/Brownian_motion">random Brownian walks</a> during the course of a threshold round, and quickly drop to a lower energy level (better solution) as soon as the threshold is tightened. Most dramatic is the jump when we go from 2 to 1.</p>
<p>This looks like we&#8217;re being thrust into a local minima, which is the opposite of what we want here. But whether a more finely-tuned annealing schedule can do better is anyone&#8217;s guess.</p>
<h2>What&#8217;s next?</h2>
<p>It should be very simple to implement a more traditional annealing procedure that would ease us into lower &#8220;energy levels&#8221; without the harsh dives we get from using only a handful of threshold levels. So that is what is next on the agenda for this problem.</p>
<p>If this annealing business looks promising after I master the black art of &#8220;tuning the meta-heuristic&#8221;, it would be cool to come up with a single-coloring algorithm, capable of generating rectangle free colorings of 72,73, or 74 cells. We would then feed those solutions into our annealing framework, finding the best combinations of colorings via a genetic algorithm or the like.</p>
<p>But that leads to the question I&#8217;ve posed earlier: it is sometimes trivial to determine if two colorings are &#8220;unique&#8221; (i.e. by counting colored cells in rows and columns); but when it isn&#8217;t trivial, determining whether two colorings are <em>&#8220;equivalent&#8221;</em> seems to be a very difficult problem. </p>
<p>What a hole I&#8217;ve fallen into&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk
Database Caching 8/15 queries in 0.041 seconds using disk

Served from: coder.bsimmons.name @ 2012-02-05 12:17:37 -->
