<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Lucky&#039;s Notes</title>
	<atom:link href="http://luckytoilet.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://luckytoilet.wordpress.com</link>
	<description>Notes on math, coding, and other stuff</description>
	<lastBuildDate>Sun, 29 Jan 2012 15:37:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='luckytoilet.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Lucky&#039;s Notes</title>
		<link>http://luckytoilet.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://luckytoilet.wordpress.com/osd.xml" title="Lucky&#039;s Notes" />
	<atom:link rel='hub' href='http://luckytoilet.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Fix for Digsby&#8217;s Facebook authentication error and broken Facebook support</title>
		<link>http://luckytoilet.wordpress.com/2012/01/26/fix-for-digsbys-broken-facebook-support/</link>
		<comments>http://luckytoilet.wordpress.com/2012/01/26/fix-for-digsbys-broken-facebook-support/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 19:01:32 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[digsby]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=2042</guid>
		<description><![CDATA[To all Digsby users (ignore this post if you don&#8217;t use Digsby): If you use Digsby with Facebook, you might have noticed that things behave strangely &#8212; the program pops up a window looking like this when it tries to connect to Facebook: Then after you give it your credentials, Digsby still thinks you&#8217;re not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2042&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To all <a href="http://www.digsby.com/">Digsby </a>users (ignore this post if you don&#8217;t use Digsby):</p>
<p>If you use Digsby with Facebook, you might have noticed that things behave strangely &#8212; the program pops up a window looking like this when it tries to connect to Facebook:</p>
<p><a href="http://imgur.com/QrmvP"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/QrmvP.png" alt="" /></a></p>
<p>Then after you give it your credentials, Digsby still thinks you&#8217;re not logged in, and so on.</p>
<p>If you found this page via a google search, there&#8217;s a simple hack / workaround you can use to patch up this problem. Basically, instead of using the Facebook protocol to connect, we let Digsby use the Jabber protocol as a &#8216;proxy&#8217; to connect to Facebook:</p>
<ol>
<li>Go to <strong>Digsby</strong> -&gt; <strong>My Accounts</strong> and in the Add Accounts section at the top, select the <strong>Jabber</strong> icon.</li>
<li>You should get a window that looks like this:<br />
<a href="http://imgur.com/OEy21"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/OEy21.png" alt="" /></a></li>
<li>In the <strong>Jabber ID</strong> box, put <strong><em>your.id</em>@chat.facebook.com</strong>, and in the password field, put your <strong>facebook password</strong>. For example, if your facebook page is at <em>facebook.com/yourname</em>, your Jabber id is <em>yourname@chat.facebook.com</em>.</li>
<li>Remove the facebook account from Digsby</li>
</ol>
<p>At this point, you&#8217;re done: Digsby should give you no more problems about Facebook. However, you can replace the Jabber icon with the Facebook one (this is for purely cosmetic purposes):</p>
<ol>
<li>Go to <strong>C:\Program Files (x86)\Digsby\res\skins\default\serviceicons</strong> (that&#8217;s the default installation path on my machine, yours may be different)</li>
<li>Delete <strong>jabber.png</strong>, duplicate <strong>facebook.png</strong>, and rename it <strong>jabber.png</strong></li>
<li>Restart Digsby</li>
</ol>
<p>There you have it &#8212; hack accomplished:</p>
<p><a href="http://imgur.com/yrhAH"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/yrhAH.png" alt="" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/2042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/2042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/2042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/2042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/2042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/2042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/2042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/2042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/2042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/2042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/2042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/2042/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/2042/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/2042/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2042&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2012/01/26/fix-for-digsbys-broken-facebook-support/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/QrmvP.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/OEy21.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/yrhAH.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Understanding Harmonic Conjugates (sort of)</title>
		<link>http://luckytoilet.wordpress.com/2012/01/07/understanding-harmonic-conjugates-sort-of/</link>
		<comments>http://luckytoilet.wordpress.com/2012/01/07/understanding-harmonic-conjugates-sort-of/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 07:06:38 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[apollonius]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[harmonic conjugates]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=2027</guid>
		<description><![CDATA[For many people (for me at least), the Harmonic Conjugate is a difficult concept to understand. I didn&#8217;t really get it the first time I saw it, at Mathcamp. Let&#8217;s take the definition of the harmonic conjugate: AB and CD are harmonic conjugates if this equation holds: If you&#8217;re like me, you&#8217;re thinking along the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2027&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For many people (for me at least), the <a href="http://en.wikipedia.org/wiki/Projective_harmonic_conjugate">Harmonic Conjugate</a> is a difficult concept to understand. I didn&#8217;t really get it the first time I saw it, at Mathcamp. Let&#8217;s take the definition of the harmonic conjugate:</p>
<p><a href="http://imgur.com/z1G8I"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/z1G8I.png" alt="" /></a></p>
<blockquote><p>AB and CD are harmonic conjugates if this equation holds:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7BAC%7D%7BBC%7D+%3D+%5Cfrac%7BAD%7D%7BBD%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=3' alt='&#92;frac{AC}{BC} = &#92;frac{AD}{BD} ' title='&#92;frac{AC}{BC} = &#92;frac{AD}{BD} ' class='latex' /></p>
</blockquote>
<p style="text-align:left;">If you&#8217;re like me, you&#8217;re thinking along the lines of<em> &#8220;But why? Why is this defined this way? Why would we spend so much time proving things about this weird concept? What&#8217;s the point, what&#8217;s the use?&#8221;</em></p>
<p style="text-align:left;">Even now, I can&#8217;t really give you an <em>intuitive</em> explanation of why this equality is so important. On the other hand, I could certainly come up with a few problems in which the concept of the harmonic conjugate turns to be <em>useful</em>.</p>
<h3 style="text-align:left;">Apollonius and Fleeing Ships</h3>
<p>Apollonius&#8217;s problem was this: you are in control of a ship (point A on diagram), and you are in pursuit of another ship (point B). The other ship is fleeing in a straight line in some direction:</p>
<p><a href="http://imgur.com/JV1ct"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/JV1ct.png" alt="" /></a></p>
<p>Your speed is (obviously) faster than the speed of the other ship: say they&#8217;re going at 30 km/h and you&#8217;re going at 50 km/h. Additionally, your ship is required to go in a straight line.</p>
<p>In which direction should you set off in order to intercept the fleeing ship?</p>
<h3>Solution with Harmonic Conjugates</h3>
<p>The first step of the solution is to construct harmonic conjugates CD so that their ratio is the ratio of your speed to the other ship&#8217;s speed (we&#8217;ll prove later that this is actually possible; assume we can do this for now):</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7BAC%7D%7BBC%7D+%3D+%5Cfrac%7BAD%7D%7BBD%7D+%3D+%5Cfrac%7B5%7D%7B3%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=2' alt='&#92;frac{AC}{BC} = &#92;frac{AD}{BD} = &#92;frac{5}{3} ' title='&#92;frac{AC}{BC} = &#92;frac{AD}{BD} = &#92;frac{5}{3} ' class='latex' /></p>
<p style="text-align:left;">Next, draw a circle with diameter CD:</p>
<p><a href="http://imgur.com/YUMee"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/YUMee.png" alt="" /></a></p>
<p>There is a point where the ray from B (their ship) intersects this circle. Now go to this point immediately, in a straight line: the ship will be there.</p>
<h3>The Proof</h3>
<p>In order to prove that this works, we&#8217;ll need to take a step back and look at how we constructed the points C and D. The solution turns out to be evident directly from the construction of the harmonic conjugates.</p>
<p>Again, let&#8217;s assume our desired ratio is 5/3. Starting with the points A and B, the first step is constructing some point P so that:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7BAP%7D%7BBP%7D+%3D+%5Cfrac%7B5%7D%7B3%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=2' alt='&#92;frac{AP}{BP} = &#92;frac{5}{3} ' title='&#92;frac{AP}{BP} = &#92;frac{5}{3} ' class='latex' /></p>
<p style="text-align:left;">This is fairly easy to do. Draw a circle of radius 5 around A, and draw a circle of radius 3 around B &#8212; the intersection P of these two circles forms the correct ratio. (if the circles don&#8217;t intersect, just scale everything up and try again)</p>
<p style="text-align:left;">Next, dropping the internal and external <em>angle bisectors</em> of the new triangle gives the harmonic conjugates C and D:</p>
<p><a href="http://imgur.com/f2CZ1"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/f2CZ1.png" alt="" /></a></p>
<p>Why angle bisectors? From the angle bisector theorems (which I won&#8217;t prove here):</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7BAP%7D%7BBP%7D+%3D+%5Cfrac%7BAC%7D%7BBC%7D+%3D+%5Cfrac%7B5%7D%7B3%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=2' alt='&#92;frac{AP}{BP} = &#92;frac{AC}{BC} = &#92;frac{5}{3} ' title='&#92;frac{AP}{BP} = &#92;frac{AC}{BC} = &#92;frac{5}{3} ' class='latex' /></p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7BAP%7D%7BBP%7D+%3D+%5Cfrac%7BAD%7D%7BBD%7D+%3D+%5Cfrac%7B5%7D%7B3%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=2' alt='&#92;frac{AP}{BP} = &#92;frac{AD}{BD} = &#92;frac{5}{3} ' title='&#92;frac{AP}{BP} = &#92;frac{AD}{BD} = &#92;frac{5}{3} ' class='latex' /></p>
<p style="text-align:left;">Combining the two proves that C and D are indeed harmonic conjugates to AB.</p>
<p style="text-align:left;">As a corollary, notice that because of angle bisecting, the angle CPD is always a right angle &#8212; hence, the <em>locus</em> of all points P forms a circle with diameter CD.</p>
<p style="text-align:left;">Returning to the ship problem, since each point P is <em>defined</em> as a point so that <img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7BAP%7D%7BBP%7D+%3D+%5Cfrac%7B5%7D%7B3%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;frac{AP}{BP} = &#92;frac{5}{3} ' title='&#92;frac{AP}{BP} = &#92;frac{5}{3} ' class='latex' />, it follows that when both ships travel to such a point P, they will meet at the same time.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/2027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/2027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/2027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/2027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/2027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/2027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/2027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/2027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/2027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/2027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/2027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/2027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/2027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/2027/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2027&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2012/01/07/understanding-harmonic-conjugates-sort-of/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/z1G8I.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/JV1ct.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/YUMee.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/f2CZ1.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
		<item>
		<title>My attempt at a Conquest AI</title>
		<link>http://luckytoilet.wordpress.com/2011/12/09/my-attempt-at-a-conquest-ai/</link>
		<comments>http://luckytoilet.wordpress.com/2011/12/09/my-attempt-at-a-conquest-ai/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 01:19:49 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ai]]></category>
		<category><![CDATA[conquest]]></category>
		<category><![CDATA[monte carlo]]></category>
		<category><![CDATA[runescape]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=2020</guid>
		<description><![CDATA[Before I move on, let me introduce Conquest &#8211; a chesslike minigame in Runescape. This isn&#8217;t a detailed description of the rules, just an overview to kind of get to know how the game works, if you haven&#8217;t seen it before. You have a 20&#215;20 board and some pieces (which you choose yourself and get [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2020&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Before I move on, let me introduce <a href="http://services.runescape.com/m=rswiki/en/Conquest">Conquest </a>&#8211; a chesslike minigame in Runescape. This isn&#8217;t a detailed description of the rules, just an overview to kind of get to know how the game works, if you haven&#8217;t seen it before.</p>
<p>You have a 20&#215;20 board and some pieces (which you choose yourself and get to set up wherever you want, with some restrictions). You&#8217;re trying to kill all of your opponent&#8217;s pieces, which you have to do in order to win.</p>
<p><a href="http://imgur.com/RsKep"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/RsKep.jpg" alt="" /></a></p>
<p>Each piece has a certain move <strong>radius</strong> &#8212; so it can move to any square within that radius (diagonals only counting as a distance of one). Then, once it has moved, it is allowed to attack an enemy within its <strong>range</strong> &#8212; again varying from piece to piece.</p>
<p>That&#8217;s the gist of the game. Oh yes, at certain times you can also use some silly <strong>special attacks</strong> <em>in addition</em> to moving and attacking &#8212; for instance, freeze an enemy piece for one move or temporarily boost the attack of one of your own pieces. And these special moves can be used on any pieces during your move, and in any combination.</p>
<h3>A Conquest AI&#8230; or not</h3>
<p>So some months ago, I was playing Conquest. I was a decent player, but after a string of losses, I thought: maybe I can write an AI to play this game for me &#8212; perhaps better than a human can? Perhaps the same way you would write a chess AI?</p>
<p>Well, I scribbled some back of the envelope calculations, but I quickly realized that the number of possible moves at each position was far too large.</p>
<p>How large? A player has maybe 5 pieces, and if a piece&#8217;s average move radius is 4, then we already have 5*9*9 (remember 4 is the radius, not diameter) which is over 400. And that&#8217;s if we never attack or use special moves. If we take special moves into account, since special moves can be stacked on top of each other, assuming 5 valid targets per special move and 3 special moves we would multiply the 400 again by 5*5*5. For one move, we would need to consider 50000 possibilities.</p>
<p>In comparison, from any position in chess there are typically 30 or 40 legal moves. So the standard minimax algorithm would probably be no good, at least not without an incredible amount of heuristics. I had to try something different for this to work.</p>
<h3>Random has a chance</h3>
<p>It turns out that there exists a very different strategy that worked quite well for Go-playing AIs. Instead of constructing and evaluating a huge search tree to find the best move, we simulate thousands <em>random</em> games, with random moves from each side. The move that gives us the best win percentage is played. All else being equal, a position that gives you a 90% chance of winning given random play from that point on is usually better than one with only a 30% chance of winning.</p>
<p>Here&#8217;s a screenshot of a <a href="http://beej.us/blog/2010/01/monte-carlo-method-for-game-ai/">connect-4 AI</a> using this technique:</p>
<p><a href="http://imgur.com/FnAVd"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/FnAVd.png" alt="" /></a></p>
<p>The <strong>Monte Carlo</strong> approach, as that&#8217;s what it&#8217;s called, seems absurd &#8212; there&#8217;s no way playing random moves can beat a good minimax! Indeed, Monte Carlo is never used to play chess, and I could sometimes beat the above AI. But where Monte Carlo really shines is when standard minimax is impractical &#8212; my scenario.</p>
<p>After finding and reading a few papers describing the method in detail, I was ready to start coding. However, there is still one more catch: to make it practical to set up the AI against other players over Runescape (and where the time limit for each move is usually 30 seconds), I would have to find a way to integrate the back end &#8212; the part that does the computations &#8212; with a front end that relayed the moves back and forth to the Runescape game client.</p>
<p>All is not lost, however: there were several freely available hacked clients for botting purposes: <a href="http://www.powerbot.org/">Powerbot </a>allowed for users to write java scripts that automated tedious ingame actions.</p>
<p>That was about as far as I got though, unfortunately. Coding the game logic proved trickier than expected. A month or so after I started my account was banned for botting; later, Jagex (the company behind the game) made an update that rendered Powerbot (as well as every other similar hacked client) unusable. Without a game account nor a practical front-end, I called it quits.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/2020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/2020/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/2020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/2020/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/2020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/2020/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/2020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/2020/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/2020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/2020/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/2020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/2020/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/2020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/2020/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2020&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/12/09/my-attempt-at-a-conquest-ai/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/RsKep.jpg" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/FnAVd.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Solving the AB Game by Brute Force</title>
		<link>http://luckytoilet.wordpress.com/2011/11/07/solving-the-ab-game-by-brute-force/</link>
		<comments>http://luckytoilet.wordpress.com/2011/11/07/solving-the-ab-game-by-brute-force/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 03:18:32 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ab]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[guess]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mastermind]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=2013</guid>
		<description><![CDATA[The AB game, a variant of Mastermind, is played with pencil and paper and with two people. One person chooses a four digit number in which no digit is repeated, say X, and the other person tries to guess it in as few moves as possible. After the guesser guesses some number, say Y, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2013&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The AB game, a variant of Mastermind, is played with pencil and paper and with two people. One person chooses a four digit number in which no digit is repeated, say X, and the other person tries to guess it in as few moves as possible. After the guesser guesses some number, say Y, the other person gives information on how closely Y matched with X:</p>
<ul>
<li>If some digit in Y coincides with a digit in X (in the same position), then the guesser scores an A.</li>
<li>If some digit in Y exists in X but is in the wrong place, then the guesser scores a B.</li>
</ul>
<p>For instance, if X is 1234 and we guess 2674, we get an A and a B, because the 4 is in the right place, and the 2 is one of the right numbers but isn&#8217;t in the right place.</p>
<p>This proceeds until the guesser gets the exact number.</p>
<p>When humans (or at least beginners) play the AB game, they usually do some form of uncoordinated trial and error, which gets the right answer after some large number of moves. This takes anywhere from about 8 to possibly 30 guesses. When I played this game with a friend, I didn&#8217;t have a very systematic strategy, but I wondered if a computer program could solve the game, always entering the optimal guess.</p>
<h3>A Bruteforce Solver</h3>
<p>My first approach happened to work fairly well. Simply, the computer keeps track of a list of all possible numbers that the answer can be. At random, the computer guesses one of the numbers, and upon receiving feedback, eliminates every number in its list that doesn&#8217;t match that feedback. Quickly it eliminates whole swaths of combinations and arrives at the answer.</p>
<p>A typical session might go like this:</p>
<p><pre class="brush: plain;">
Guess 1: 6297. Score: 5040
b
Guess 2: 8512. Score: 1440
abb
Guess 3: 2315. Score: 83
bb
Guess 4: 5842. Score: 29
bb
Guess 5: 9581. Score: 13
ab
Guess 6: 8021. Score: 1
</pre></p>
<p>It took only 5 guesses for the computer to narrow down the choices to the only possible answer (8021).</p>
<p>A variant that is usually much harder for humans to solve is to allow the number chosen to have repeats. Although significantly trickier for humans to guess by trial and error, brute force doesn&#8217;t seem to be affected too much by it:</p>
<p><pre class="brush: plain;">
Guess 1: 1796. Score: 10000
b
Guess 2: 5881. Score: 3048
a
Guess 3: 0131. Score: 531
abb
Guess 4: 3311. Score: 15
aa
Guess 5: 3201. Score: 9
abb
Guess 6: 2011. Score: 1
</pre></p>
<p>The computer&#8217;s average of five or six is much better than a human can normally do! (although I haven&#8217;t researched possible human algorithms).</p>
<h3>Optimal or Not?</h3>
<p>At this point you may begin to wonder if this strategy is the optimal one. Unfortunately, it is not &#8212; and I only need one counterexample to demonstrate that.</p>
<p>Suppose that instead of four numbers, you were allowed to choose 4 letters from A to Z. You choose the combination ABCW. Now suppose that the computer &#8216;knows&#8217; that the first three letters are ABC &#8212; that is, it has eliminated all other combinations except for ABCD, ABCE, &#8230;, ABCZ.</p>
<p>By the random guessing algorithm, the computer is forced to guess ABCJ, ABCP, etc, until it eventually hits on ABCW at random. This may take a very high number of guesses.</p>
<p>A smarter strategy would be to guess combinations of four unknown letters, say DEFG, then HIJK, etc. Instead of eliminating one letter, you eliminate four letters at a time. Although some guesses have no chance of being correct, the number of guesses required is fewer in the long run.</p>
<h3>Source Code</h3>
<p>I&#8217;ll put my somewhat unwieldy java code here for all to see:</p>
<p><pre class="brush: java; wrap-lines: false;">


import java.util.*;

public class ABSolver{
  
  // Does cc1 and cc2 together match the pattern?
  // If repeats are allowed, cc2 is matched against cc1.
  static boolean fits(char[] cc1, char[] cc2, int A, int B){

    int a = 0;
    int b = 0;
    for(int i=0; i&lt;4; i++){
      if(cc1[i] == cc2[i]) a++;
      else{
        for(int j=0; j&lt;4; j++){
          if(i != j &amp;&amp; cc1[j] == cc2[i] &amp;&amp; cc1[j]!=cc2[j]){
            b++;
            break;
          }
        }
      }
    }

    return a==A &amp;&amp; b==B;
  }

  public static void main(String[] args){
    
    Random rand = new Random();
    Scanner scan = new Scanner(System.in);

    // Combinations that haven't been eliminated yet
    List&lt;char[]&gt; ok = new ArrayList&lt;char[]&gt;(10000);

    // Generate all possible combinations
    for(int i = 0; i &lt;= 9999; i++){
      
      String i_s = Integer.toString(i);
      while(i_s.length() != 4) i_s = &quot;0&quot; + i_s;

      // Check for sameness
      char a = i_s.charAt(0);
      char b = i_s.charAt(1);
      char c = i_s.charAt(2);
      char d = i_s.charAt(3);
      boolean same = a==b || a==c || a==d || b==c || b==d || c==d;

      // Comment this line out if we're allowing repeats
      if(same) continue;

      char[] i_ia = i_s.toCharArray();
      ok.add(i_ia);

    }

    // Pick the first guess randomly
    char[] firstg = ok.get(rand.nextInt(ok.size()));
    char[] guess = null;

    int nguesses = 1;

    // Question answer cycle until we get it right
    while(true){

      // Ask for user response
      if(nguesses &gt; 1){
        String ans = scan.nextLine();
        int A = 0;
        int B = 0;
        for(char cc : ans.toCharArray()){
          if(cc == 'a') A++;
          if(cc == 'b') B++;
        }

        if(A==4) return;

        // For each one check to see if it still fits
        List&lt;char[]&gt; ok_ = new ArrayList&lt;char[]&gt;(ok.size());
        for(char[] zhe : ok){
          if(fits(zhe, guess, A, B))
            ok_.add(zhe);
        }
        ok = ok_;
      }

      char[] nextguess = null;
      if(nguesses == 1)
        nextguess = firstg;
      else nextguess = ok.get(rand.nextInt(ok.size()));

      System.out.println(&quot;Guess &quot; + nguesses + &quot;: &quot; + new String(nextguess)
                       + &quot;. Score: &quot; + ok.size());
      
      // we win!
      if(ok.size() == 1)
        return;

      guess = nextguess;
      nguesses++;
    }
    
  }
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/2013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/2013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/2013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/2013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/2013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/2013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/2013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/2013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/2013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/2013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/2013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/2013/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/2013/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/2013/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=2013&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/11/07/solving-the-ab-game-by-brute-force/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculating the Plane Angles of Platonic Solids</title>
		<link>http://luckytoilet.wordpress.com/2011/10/11/calculating-the-plane-angles-of-platonic-solids/</link>
		<comments>http://luckytoilet.wordpress.com/2011/10/11/calculating-the-plane-angles-of-platonic-solids/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 03:44:04 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[dodecahedron]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[isosahedron]]></category>
		<category><![CDATA[platonic solids]]></category>
		<category><![CDATA[tetrahedron]]></category>
		<category><![CDATA[vector geometry]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=1979</guid>
		<description><![CDATA[What is the angle between two planes of a tetrahedron? The angle between any two edges of the tetrahedron is , so it&#8217;s easy to (falsely) conclude that the angle between two faces must also be . But this isn&#8217;t the case: the plane angle is defined as the angle between two lines on the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1979&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What is the angle between two planes of a tetrahedron?<br />
<a href="http://imgur.com/d2ncO"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/d2ncO.gif" alt="" /></a></p>
<p>The angle between any two edges of the tetrahedron is <img src='http://s0.wp.com/latex.php?latex=60%5E%5Ccirc&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='60^&#92;circ' title='60^&#92;circ' class='latex' />, so it&#8217;s easy to (falsely) conclude that the angle between two faces must also be <img src='http://s0.wp.com/latex.php?latex=60%5E%5Ccirc&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='60^&#92;circ' title='60^&#92;circ' class='latex' />.</p>
<p>But this isn&#8217;t the case: the plane angle is defined as the angle between two lines on the two planes that are both perpendicular to the edge. None of the edges in a tetrahedron is perpendicular to any other, so the answer of <img src='http://s0.wp.com/latex.php?latex=60%5E%5Ccirc&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='60^&#92;circ' title='60^&#92;circ' class='latex' /> is invalid.</p>
<p>We can try to compute the angle using regular Euclidean solid geometry, but things tend to get messy. A different way to approach the problem is by using vector geometry: using vector methods we can easily calculate the plane angle of the tetrahedron (as well as the icosahedron and the dodecahedron).</p>
<p>Assume symmetry. We represent three concurrent edges of the polyhedron as three vectors beginning at the same point: <img src='http://s0.wp.com/latex.php?latex=%5Cvec+a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec a' title='&#92;vec a' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%5Cvec+b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec b' title='&#92;vec b' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%5Cvec+c&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec c' title='&#92;vec c' class='latex' />; let <img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cbeta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;beta' title='&#92;beta' class='latex' /> be the angles between the vectors (by symmetry we&#8217;re assuming that the two alpha&#8217;s are equal):<br />
<a href="http://imgur.com/jDUcg"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/jDUcg.png" alt="" /></a>(in case my poor drawing skills does not make this apparent, vectors a and b form one face of the polyhedron and c and b form another face)</p>
<p>For simplicity, let&#8217;s also say the lengths of each of the three vectors is 1.</p>
<p>We want to compute the angle between the plane formed by <img src='http://s0.wp.com/latex.php?latex=%5Cvec+a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec a' title='&#92;vec a' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cvec+c&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec c' title='&#92;vec c' class='latex' />, and the plane formed by <img src='http://s0.wp.com/latex.php?latex=%5Cvec+b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec b' title='&#92;vec b' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cvec+c&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec c' title='&#92;vec c' class='latex' />. Hence let <img src='http://s0.wp.com/latex.php?latex=%5Cvec+x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec x' title='&#92;vec x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cvec+y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec y' title='&#92;vec y' class='latex' /> be the perpendiculars to <img src='http://s0.wp.com/latex.php?latex=%5Cvec+a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec a' title='&#92;vec a' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cvec+b&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec b' title='&#92;vec b' class='latex' /> respectively each ending at the same point as their respective vectors:</p>
<p><a href="http://imgur.com/3OGkO"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/3OGkO.png" alt="" /></a>For any two vectors, the dot product is defined <img src='http://s0.wp.com/latex.php?latex=%5Cvec+a+%5Ccdot+%5Cvec+b+%3D+%7C%5Cvec+a%7C+%7C%5Cvec+b%7C+%5Ccos+%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec a &#92;cdot &#92;vec b = |&#92;vec a| |&#92;vec b| &#92;cos &#92;theta' title='&#92;vec a &#92;cdot &#92;vec b = |&#92;vec a| |&#92;vec b| &#92;cos &#92;theta' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta' title='&#92;theta' class='latex' /> being the angle between the vectors. Given that the lengths of the vectors are all 1, we have:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cvec+a+%5Ccdot+%5Cvec+c+%3D+%5Cvec+b+%5Ccdot+%5Cvec+c+%3D+%5Ccos+%5Calpha+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;vec a &#92;cdot &#92;vec c = &#92;vec b &#92;cdot &#92;vec c = &#92;cos &#92;alpha ' title='&#92;vec a &#92;cdot &#92;vec c = &#92;vec b &#92;cdot &#92;vec c = &#92;cos &#92;alpha ' class='latex' /></p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cvec+a+%5Ccdot+%5Cvec+b+%3D+%5Ccos+%5Cbeta+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;vec a &#92;cdot &#92;vec b = &#92;cos &#92;beta ' title='&#92;vec a &#92;cdot &#92;vec b = &#92;cos &#92;beta ' class='latex' /></p>
<p style="text-align:left;">Also, by vector addition:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cvec+c+%5Ccos+%5Calpha+%2B+%5Cvec+x+%3D+%5Cvec+a+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;vec c &#92;cos &#92;alpha + &#92;vec x = &#92;vec a ' title='&#92;vec c &#92;cos &#92;alpha + &#92;vec x = &#92;vec a ' class='latex' /></p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cvec+c+%5Ccos+%5Calpha+%2B+%5Cvec+y+%3D+%5Cvec+b+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;vec c &#92;cos &#92;alpha + &#92;vec y = &#92;vec b ' title='&#92;vec c &#92;cos &#92;alpha + &#92;vec y = &#92;vec b ' class='latex' /></p>
<p style="text-align:left;">Hence <img src='http://s0.wp.com/latex.php?latex=%5Cvec+x+%3D+%5Cvec+a+-+%5Cvec+c+%5Ccos+%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec x = &#92;vec a - &#92;vec c &#92;cos &#92;alpha' title='&#92;vec x = &#92;vec a - &#92;vec c &#92;cos &#92;alpha' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cvec+y+%3D+%5Cvec+b+-+%5Cvec+c+%5Ccos+%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec y = &#92;vec b - &#92;vec c &#92;cos &#92;alpha' title='&#92;vec y = &#92;vec b - &#92;vec c &#92;cos &#92;alpha' class='latex' />.</p>
<p style="text-align:left;">We want to find the angle between <img src='http://s0.wp.com/latex.php?latex=%5Cvec+x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec x' title='&#92;vec x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cvec+y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;vec y' title='&#92;vec y' class='latex' /> &#8212; call this angle <img src='http://s0.wp.com/latex.php?latex=%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta' title='&#92;theta' class='latex' />. Then</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cvec+x+%5Ccdot+%5Cvec+y+%3D+%7C%5Cvec+x%7C+%7C%5Cvec+y%7C+%5Ccos+%5Ctheta+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;vec x &#92;cdot &#92;vec y = |&#92;vec x| |&#92;vec y| &#92;cos &#92;theta ' title='&#92;vec x &#92;cdot &#92;vec y = |&#92;vec x| |&#92;vec y| &#92;cos &#92;theta ' class='latex' /></p>
<p style="text-align:left;">The dot product of vectors x and y is simply:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Bl%7D+%28%5Cvec+a+-+%5Cvec+c+%5Ccos+%5Calpha%29+%5Ccdot+%28%5Cvec+b+-+%5Cvec+c+%5Ccos+%5Calpha%29+%5C%5C+%3D+%28%5Cvec+a+-+%5Cvec+c+%28%5Cvec+a+%5Ccdot+%5Cvec+c%29%29+%5Ccdot+%28%5Cvec+b+-+%5Cvec+c+%28%5Cvec+b+%5Ccdot+%5Cvec+c%29%29+%5C%5C+%3D+%5Cvec+a+%5Ccdot+%5Cvec+b+-+%28%5Cvec+a+%5Ccdot+%5Cvec+c%29+%28%5Cvec+b+%5Ccdot+%5Cvec+c%29+%5C%5C+%3D+%5Ccos+%5Cbeta+-+%5Ccos%5E2+%5Calpha+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;begin{array}{l} (&#92;vec a - &#92;vec c &#92;cos &#92;alpha) &#92;cdot (&#92;vec b - &#92;vec c &#92;cos &#92;alpha) &#92;&#92; = (&#92;vec a - &#92;vec c (&#92;vec a &#92;cdot &#92;vec c)) &#92;cdot (&#92;vec b - &#92;vec c (&#92;vec b &#92;cdot &#92;vec c)) &#92;&#92; = &#92;vec a &#92;cdot &#92;vec b - (&#92;vec a &#92;cdot &#92;vec c) (&#92;vec b &#92;cdot &#92;vec c) &#92;&#92; = &#92;cos &#92;beta - &#92;cos^2 &#92;alpha &#92;end{array} ' title='&#92;begin{array}{l} (&#92;vec a - &#92;vec c &#92;cos &#92;alpha) &#92;cdot (&#92;vec b - &#92;vec c &#92;cos &#92;alpha) &#92;&#92; = (&#92;vec a - &#92;vec c (&#92;vec a &#92;cdot &#92;vec c)) &#92;cdot (&#92;vec b - &#92;vec c (&#92;vec b &#92;cdot &#92;vec c)) &#92;&#92; = &#92;vec a &#92;cdot &#92;vec b - (&#92;vec a &#92;cdot &#92;vec c) (&#92;vec b &#92;cdot &#92;vec c) &#92;&#92; = &#92;cos &#92;beta - &#92;cos^2 &#92;alpha &#92;end{array} ' class='latex' /></p>
<p style="text-align:left;">Additionally <img src='http://s0.wp.com/latex.php?latex=%7C%5Cvec+x%7C+%3D+%7C%5Cvec+y%7C+%3D+%5Csin+%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='|&#92;vec x| = |&#92;vec y| = &#92;sin &#92;alpha' title='|&#92;vec x| = |&#92;vec y| = &#92;sin &#92;alpha' class='latex' />. Hence the cosine of the angle is:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Ccos+%5Ctheta+%3D+%5Cfrac%7B%5Cvec+x+%5Ccdot+%5Cvec+y%7D%7B%7C%5Cvec+x%7C+%7C%5Cvec+y%7C%7D+%3D+%5Cfrac%7B%5Ccos+%5Cbeta+-+%5Ccos%5E2+%5Calpha%7D%7B%5Csin%5E2+%5Calpha%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=2' alt='&#92;cos &#92;theta = &#92;frac{&#92;vec x &#92;cdot &#92;vec y}{|&#92;vec x| |&#92;vec y|} = &#92;frac{&#92;cos &#92;beta - &#92;cos^2 &#92;alpha}{&#92;sin^2 &#92;alpha} ' title='&#92;cos &#92;theta = &#92;frac{&#92;vec x &#92;cdot &#92;vec y}{|&#92;vec x| |&#92;vec y|} = &#92;frac{&#92;cos &#92;beta - &#92;cos^2 &#92;alpha}{&#92;sin^2 &#92;alpha} ' class='latex' /></p>
<p style="text-align:left;">We can now use this newly derived formula to calculate plane angles! For example&#8230;</p>
<h3 style="text-align:left;">Tetrahedron</h3>
<p>In the tetrahedron, both <img src='http://s0.wp.com/latex.php?latex=%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha' title='&#92;alpha' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Cbeta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;beta' title='&#92;beta' class='latex' /> are 60:<br />
<a href="http://imgur.com/DzLh0"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/DzLh0.png" alt="" /></a>So <img src='http://s0.wp.com/latex.php?latex=%5Ccos+%5Ctheta+%3D+%5Cfrac%7B%5Ccos+60+-+%5Ccos%5E2+60%7D%7B%5Csin%5E2+60%7D+%3D+%5Cfrac%7B1%7D%7B2%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;cos &#92;theta = &#92;frac{&#92;cos 60 - &#92;cos^2 60}{&#92;sin^2 60} = &#92;frac{1}{2} ' title='&#92;cos &#92;theta = &#92;frac{&#92;cos 60 - &#92;cos^2 60}{&#92;sin^2 60} = &#92;frac{1}{2} ' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Ctheta+%3D+%5Carccos+%5Cfrac%7B1%7D%7B3%7D+%3D+70.8%5E%5Ccirc&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta = &#92;arccos &#92;frac{1}{3} = 70.8^&#92;circ' title='&#92;theta = &#92;arccos &#92;frac{1}{3} = 70.8^&#92;circ' class='latex' />.</p>
<p><span class="Apple-style-span" style="font-size:15px;font-weight:bold;">Icosahedron</span></p>
<p>In the icosahedron, <img src='http://s0.wp.com/latex.php?latex=%5Calpha+%3D+60&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha = 60' title='&#92;alpha = 60' class='latex' /> but <img src='http://s0.wp.com/latex.php?latex=%5Cbeta+%3D+108&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;beta = 108' title='&#92;beta = 108' class='latex' />:<br />
<a href="http://imgur.com/qD7ev"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/qD7ev.png" alt="" /></a>The top &#8216;cap&#8217; is a regular pentagon, which has a vertex angle of 108; each side of the pentagon constitutes a side of an equilateral triangle. Since <img src='http://s0.wp.com/latex.php?latex=%5Ccos+108+%3D+%5Cfrac%7B1%7D%7B4%7D+%281-%5Csqrt%7B5%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;cos 108 = &#92;frac{1}{4} (1-&#92;sqrt{5})' title='&#92;cos 108 = &#92;frac{1}{4} (1-&#92;sqrt{5})' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%5Ccos+%5Ctheta+%3D+%5Cfrac%7B%5Ccos+108+-+%5Ccos%5E2+60%7D%7B%5Csin%5E2+60%7D+%3D+-%5Cfrac%7B%5Csqrt%7B5%7D%7D%7B3%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;cos &#92;theta = &#92;frac{&#92;cos 108 - &#92;cos^2 60}{&#92;sin^2 60} = -&#92;frac{&#92;sqrt{5}}{3} ' title='&#92;cos &#92;theta = &#92;frac{&#92;cos 108 - &#92;cos^2 60}{&#92;sin^2 60} = -&#92;frac{&#92;sqrt{5}}{3} ' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%5Ctheta+%3D+%5Carccos+%28-%5Cfrac%7B%5Csqrt%7B5%7D%7D%7B3%7D%29+%3D+138.2%5E%5Ccirc&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta = &#92;arccos (-&#92;frac{&#92;sqrt{5}}{3}) = 138.2^&#92;circ' title='&#92;theta = &#92;arccos (-&#92;frac{&#92;sqrt{5}}{3}) = 138.2^&#92;circ' class='latex' />.</p>
<h3>Dodecahedron</h3>
<p>Computing angles for the dodecahedron works a bit differently from the tetrahedron and icosahedron. Instead of using existing edges as vectors, we construct an equilateral triangle by connecting three vertices:</p>
<p><a href="http://imgur.com/uFIXJ"><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/uFIXJ.png" alt="" /></a>So <img src='http://s0.wp.com/latex.php?latex=%5Calpha+%3D+36&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;alpha = 36' title='&#92;alpha = 36' class='latex' /> (since it&#8217;s part of a regular pentagon) and <img src='http://s0.wp.com/latex.php?latex=%5Cbeta+%3D+60&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;beta = 60' title='&#92;beta = 60' class='latex' />. Then <img src='http://s0.wp.com/latex.php?latex=%5Ccos+%5Ctheta+%3D+%5Cfrac%7B%5Ccos%5E2+60+-+%5Ccos%5E2+36%7D%7B%5Csin%5E2+36%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;cos &#92;theta = &#92;frac{&#92;cos^2 60 - &#92;cos^2 36}{&#92;sin^2 36} ' title='&#92;cos &#92;theta = &#92;frac{&#92;cos^2 60 - &#92;cos^2 36}{&#92;sin^2 36} ' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%5Ctheta+%3D+116.6%5E%5Ccirc&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta = 116.6^&#92;circ' title='&#92;theta = 116.6^&#92;circ' class='latex' />.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/1979/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/1979/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/1979/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/1979/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/1979/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/1979/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/1979/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/1979/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/1979/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/1979/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/1979/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/1979/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/1979/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/1979/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1979&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/10/11/calculating-the-plane-angles-of-platonic-solids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/d2ncO.gif" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/jDUcg.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/3OGkO.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/DzLh0.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/qD7ev.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/uFIXJ.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Varignon&#8217;s theorem proved in one line with vectors</title>
		<link>http://luckytoilet.wordpress.com/2011/09/06/varignons-theorem-proved-in-one-line-with-vectors/</link>
		<comments>http://luckytoilet.wordpress.com/2011/09/06/varignons-theorem-proved-in-one-line-with-vectors/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 22:39:54 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[linear algebra]]></category>
		<category><![CDATA[quadrilateral]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=1974</guid>
		<description><![CDATA[Today I was reading some math book when the author mentions Varignon&#8217;s theorem, and gives a proof. The proof was not very long, but it was somewhat confusing. On Wikipedia, several more short proofs were given, but they were all more confusing than need be. I remembered seeing the theorem proven using vector geometry before, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1974&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I was reading some math book when the author mentions Varignon&#8217;s theorem, and gives a proof. The proof was not very long, but it was somewhat confusing. On Wikipedia, several more short proofs were given, but they were all more confusing than need be.</p>
<p>I remembered seeing the theorem proven using vector geometry before, but I couldn&#8217;t find the text (nor any other page / book that proves it this way) &#8211;</p>
<p><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/E2X1M.png" alt="" /></p>
<p><em>[image shamelessly taken from Wikipedia]</em></p>
<p>Varignon&#8217;s theorem states that in any quadrilateral, if we join the midpoints of the sides, then we get a parallelogram.</p>
<p>In the diagram, it suffices to prove that vector HG is equal to vector EF &#8212; vectors must have both the same orientation and length to be equal. This works since any method that proves HG = EF can also prove HE = GF. The proof goes as follows &#8211;</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cvec+%7BHG%7D+%3D+%5Cvec%7BHD%7D+%2B+%5Cvec%7BDG%7D+%3D+%5Cfrac%7B1%7D%7B2%7D+%28%5Cvec%7BAD%7D+%2B+%5Cvec%7BDC%7D%29+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cvec%7BAC%7D+%3D+%5Cvec%7BEF%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;vec {HG} = &#92;vec{HD} + &#92;vec{DG} = &#92;frac{1}{2} (&#92;vec{AD} + &#92;vec{DC}) = &#92;frac{1}{2} &#92;vec{AC} = &#92;vec{EF} ' title='&#92;vec {HG} = &#92;vec{HD} + &#92;vec{DG} = &#92;frac{1}{2} (&#92;vec{AD} + &#92;vec{DC}) = &#92;frac{1}{2} &#92;vec{AC} = &#92;vec{EF} ' class='latex' /></p>
<p style="text-align:left;">And we&#8217;re done. (the last step is due to symmetry of HG and EF)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/1974/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/1974/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/1974/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/1974/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/1974/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/1974/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/1974/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1974&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/09/06/varignons-theorem-proved-in-one-line-with-vectors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/E2X1M.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Algorithmic Counterpoint: How I Wrote a Computer Program to Generate Music at Mathcamp 2011</title>
		<link>http://luckytoilet.wordpress.com/2011/08/10/algorithmic-counterpoint-how-i-wrote-a-computer-program-to-generate-music-at-mathcamp-2011/</link>
		<comments>http://luckytoilet.wordpress.com/2011/08/10/algorithmic-counterpoint-how-i-wrote-a-computer-program-to-generate-music-at-mathcamp-2011/#comments</comments>
		<pubDate>Wed, 10 Aug 2011 23:01:11 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[counterpoint]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mathcamp]]></category>
		<category><![CDATA[music]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=1964</guid>
		<description><![CDATA[Hello all; I have just returned from Mathcamp, a five week math program for high school students. I&#8217;m not going to write in detail about life at Mathcamp, other than that it was amazing in every way. This year at Mathcamp, I chose to do a computer programming project to &#8216;teach&#8217; a computer to write [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1964&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hello all; I have just returned from <a href="http://luckytoilet.wordpress.com/2011/06/29/mathcamp-2011/">Mathcamp</a>, a five week math program for high school students. I&#8217;m not going to write in detail about life at Mathcamp, other than that it was amazing in every way.</p>
<p>This year at Mathcamp, I chose to do a computer programming project to &#8216;teach&#8217; a computer to write &#8216;music&#8217;. The word &#8216;music&#8217; is in quotes because what it&#8217;s expected to generate is more or less random and nothing like actual music by Bach or Mozart; on the other hand, the &#8216;music&#8217; generated resembles actual music closely enough that it can still be called music, not a random string of notes. Anyways, here&#8217;s a short sample of what it can generate:</p>
<p><iframe src="http://player.vimeo.com/video/27556042" width="450" height="338" frameborder="0"></iframe></p>
<p>What can be considered &#8216;music&#8217; in some sense here is really just a set of rules backed by a random number generator. Hence what it does is really the following:</p>
<ol>
<li>Generate a main melody using a bunch of rules: ie, what notes are allowed to go with each other, what rhythms are allowed to go with what notes, etc.</li>
<li>Using the main melody as a sort of a baseline, generate one or more counterpoint melodies: a counterpoint melody is constrained by the same rules that the main melody must follow, but also has to harmonize with the existing melody, thus being constrained by another set of rules.</li>
<li>Once all the notes are generated, the program simply uses some library &#8212; in our case <a href="http://www.jfugue.org/">JFugue </a>&#8211; to play the notes through the speakers.</li>
</ol>
<p>The bulk of our time was spent implementing, tweaking, and debugging the rules and the various other substeps in the above process.</p>
<p>The source code, mostly written by myself over about two weeks, is <a href="http://code.google.com/p/contrapunctus/">available as a Google Code project</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/1964/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/1964/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/1964/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/1964/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/1964/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/1964/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/1964/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/1964/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/1964/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/1964/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/1964/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/1964/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/1964/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/1964/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1964&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/08/10/algorithmic-counterpoint-how-i-wrote-a-computer-program-to-generate-music-at-mathcamp-2011/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>
	</item>
		<item>
		<title>Mathcamp 2011</title>
		<link>http://luckytoilet.wordpress.com/2011/06/29/mathcamp-2011/</link>
		<comments>http://luckytoilet.wordpress.com/2011/06/29/mathcamp-2011/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 17:56:35 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[combinatorics]]></category>
		<category><![CDATA[mathcamp]]></category>
		<category><![CDATA[solved problem]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=1948</guid>
		<description><![CDATA[This year I got invited to Mathcamp, a five week summer camp for &#8220;mathematically talented high school students&#8221;. The camp will run from July 3 to August 7 this year, in Reed College in Portland, Oregon. This means I will be leaving three days from the time of writing! The camp is quite expensive &#8212; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1948&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This year I got invited to <a href="http://www.mathcamp.org/">Mathcamp</a>, a five week summer camp for &#8220;mathematically talented high school students&#8221;. The camp will run from July 3 to August 7 this year, in Reed College in Portland, Oregon. This means I will be leaving three days from the time of writing!</p>
<p><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/Aoyev.png" alt="" /></p>
<p>The camp is quite expensive &#8212; $4000 for normal students &#8212; but thankfully, Mathcamp has given me a pretty generous scholarship.</p>
<p>Application to Mathcamp required, among other things, a eight question <a href="http://www.mathcamp.org/2011/qquiz.pdf">qualifying quiz</a>. After working on them for one week, I managed to solve six of the problems and half of a seventh problem. One of my favorite problems in the quiz is the third:</p>
<p><em>In a park there is a merry go round with <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> seats, and <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> kids waiting to get on (the kids are evenly spaced around the merry go round). The kids climb on one by one in some order, only taking the seat in front of them and only if the seat is empty; however, each time a kid climbs on, the merry go round rotates counterclockwise by one position. For what values of <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> is it possible for every kid to get a seat?</em></p>
<p>My solution goes as follows (I&#8217;m assuming that it&#8217;s okay to discuss solutions by now as the deadline for submitting solutions has passed more than two months ago):</p>
<p>Let the children be numbered <img src='http://s0.wp.com/latex.php?latex=0%2C1%2C2%2C%5Ccdots%2Cn-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='0,1,2,&#92;cdots,n-1' title='0,1,2,&#92;cdots,n-1' class='latex' /> clockwise and the seats also numbered <img src='http://s0.wp.com/latex.php?latex=0%2C1%2C2%2C%5Ccdots%2Cn-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='0,1,2,&#92;cdots,n-1' title='0,1,2,&#92;cdots,n-1' class='latex' /> so that child <img src='http://s0.wp.com/latex.php?latex=0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='0' title='0' class='latex' /> is matched with seat <img src='http://s0.wp.com/latex.php?latex=0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='0' title='0' class='latex' />, and so on.</p>
<p>Let us denote a sequence of moves by <img src='http://s0.wp.com/latex.php?latex=c_1%2Cc_2%2C%5Ccdots%2Cc_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1,c_2,&#92;cdots,c_n' title='c_1,c_2,&#92;cdots,c_n' class='latex' /> as the sequence that the children get on the ride: for example if <img src='http://s0.wp.com/latex.php?latex=c_1%2Cc_2%2Cc_3+%3D+0%2C2%2C1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1,c_2,c_3 = 0,2,1' title='c_1,c_2,c_3 = 0,2,1' class='latex' /> then child 0 gets on, followed by child 2 then child 1. Each time a child gets on, the seats shift counterclockwise by one, so child <img src='http://s0.wp.com/latex.php?latex=c_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1' title='c_1' class='latex' /> gets onto seat <img src='http://s0.wp.com/latex.php?latex=c_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1' title='c_1' class='latex' />, but child <img src='http://s0.wp.com/latex.php?latex=c_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_2' title='c_2' class='latex' /> gets onto seat <img src='http://s0.wp.com/latex.php?latex=c_2%2B1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_2+1' title='c_2+1' class='latex' /> (modulo <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />), child <img src='http://s0.wp.com/latex.php?latex=c_3&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_3' title='c_3' class='latex' /> gets onto seat <img src='http://s0.wp.com/latex.php?latex=c_3%2B2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_3+2' title='c_3+2' class='latex' />, and so on.</p>
<p>Suppose that a particular sequence <img src='http://s0.wp.com/latex.php?latex=c_1%2C%5Ccdots%2Cc_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1,&#92;cdots,c_n' title='c_1,&#92;cdots,c_n' class='latex' /> works at getting every child on the ride. Then <img src='http://s0.wp.com/latex.php?latex=c_1%2Cc_2%2B1%2C%5Ccdots%2Cc_n%2B%28n-1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1,c_2+1,&#92;cdots,c_n+(n-1)' title='c_1,c_2+1,&#92;cdots,c_n+(n-1)' class='latex' /> must take on every value between 0 and <img src='http://s0.wp.com/latex.php?latex=n-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n-1' title='n-1' class='latex' /> modulo <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />. We claim that this is possible when <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> is odd, and impossible when <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> is even.</p>
<p>If <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> is odd, let <img src='http://s0.wp.com/latex.php?latex=c_1%2C%5Ccdots%2Cc_n%3D0%2C1%2C2%2C%5Ccdots%2Cn-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1,&#92;cdots,c_n=0,1,2,&#92;cdots,n-1' title='c_1,&#92;cdots,c_n=0,1,2,&#92;cdots,n-1' class='latex' />. We use sums to show that this sequence indeed works, and every child gets a different seat (by every sum being different modulo <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />). The sums in this case are <img src='http://s0.wp.com/latex.php?latex=0%2C2%2C4%2C%5Ccdots%2Cn-1%2Cn%2B1%2Cn%2B3%2C%5Ccdots%2C2n-2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='0,2,4,&#92;cdots,n-1,n+1,n+3,&#92;cdots,2n-2' title='0,2,4,&#92;cdots,n-1,n+1,n+3,&#92;cdots,2n-2' class='latex' /> so the sums modulo <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> are <img src='http://s0.wp.com/latex.php?latex=0%2C2%2C4%2C%5Ccdots%2Cn-1%2C1%2C3%2C5%2C%5Ccdots%2Cn-2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='0,2,4,&#92;cdots,n-1,1,3,5,&#92;cdots,n-2' title='0,2,4,&#92;cdots,n-1,1,3,5,&#92;cdots,n-2' class='latex' />. Clearly, this covers every integer modulo <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> exactly once.</p>
<p>We claim this is impossible when <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> is even. Suppose the contrary, that this is possible, or in other words, <img src='http://s0.wp.com/latex.php?latex=c_1%2Cc_2%2B1%2C%5Ccdots%2Cc_n%2B%28n-1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1,c_2+1,&#92;cdots,c_n+(n-1)' title='c_1,c_2+1,&#92;cdots,c_n+(n-1)' class='latex' /> take on every value between 0 and <img src='http://s0.wp.com/latex.php?latex=n-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n-1' title='n-1' class='latex' /> modulo <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />. Adding everything up gives</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Bl%7D+%28c_1%2Bc_2%2B%5Ccdots%2Bc_n%29%2B%281%2B2%2B%5Ccdots%2B%28n-1%29%29+%5C%5C+%5Chspace%7B50+px%7D+%5Cequiv+%281%2B2%2B%5Ccdots%2B%28n-1%29%29+%5Cmod+n+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{l} (c_1+c_2+&#92;cdots+c_n)+(1+2+&#92;cdots+(n-1)) &#92;&#92; &#92;hspace{50 px} &#92;equiv (1+2+&#92;cdots+(n-1)) &#92;mod n &#92;end{array}' title='&#92;begin{array}{l} (c_1+c_2+&#92;cdots+c_n)+(1+2+&#92;cdots+(n-1)) &#92;&#92; &#92;hspace{50 px} &#92;equiv (1+2+&#92;cdots+(n-1)) &#92;mod n &#92;end{array}' class='latex' /></p>
<p>But since <img src='http://s0.wp.com/latex.php?latex=c_1%2Cc_2%2C%5Ccdots%2Cc_n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='c_1,c_2,&#92;cdots,c_n' title='c_1,c_2,&#92;cdots,c_n' class='latex' /> is a permutation of <img src='http://s0.wp.com/latex.php?latex=0%2C%5Ccdots%2Cn-1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='0,&#92;cdots,n-1' title='0,&#92;cdots,n-1' class='latex' />, their sum is <img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%28n-1%29%7D%7B2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;frac{n(n-1)}{2}' title='&#92;frac{n(n-1)}{2}' class='latex' />. Hence</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%28n-1%29%7D%7B2%7D+%5Cequiv+0+%5Cmod+n+&amp;bg=ffffff&amp;fg=333333&amp;s=1' alt='&#92;frac{n(n-1)}{2} &#92;equiv 0 &#92;mod n ' title='&#92;frac{n(n-1)}{2} &#92;equiv 0 &#92;mod n ' class='latex' /></p>
<p>Since <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> is even, it can be written as <img src='http://s0.wp.com/latex.php?latex=2m&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='2m' title='2m' class='latex' /> for some integer <img src='http://s0.wp.com/latex.php?latex=m&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='m' title='m' class='latex' />. Then</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Bcc%7D+m%282m-1%29+%26+%5Cequiv+0+%5Cmod+2m%5C%5C+2m%5E2-m+%26+%5Cequiv+0+%5Cmod+2m%5C%5C+m+%26+%5Cequiv+0+%5Cmod+2m+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{cc} m(2m-1) &amp; &#92;equiv 0 &#92;mod 2m&#92;&#92; 2m^2-m &amp; &#92;equiv 0 &#92;mod 2m&#92;&#92; m &amp; &#92;equiv 0 &#92;mod 2m &#92;end{array} ' title='&#92;begin{array}{cc} m(2m-1) &amp; &#92;equiv 0 &#92;mod 2m&#92;&#92; 2m^2-m &amp; &#92;equiv 0 &#92;mod 2m&#92;&#92; m &amp; &#92;equiv 0 &#92;mod 2m &#92;end{array} ' class='latex' /></p>
<p>This is a contridiction. Hence the arrangement is impossible when <img src='http://s0.wp.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> is even.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/1948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/1948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/1948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/1948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/1948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/1948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/1948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/1948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/1948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/1948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/1948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/1948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/1948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/1948/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1948&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/06/29/mathcamp-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/Aoyev.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Eyes!</title>
		<link>http://luckytoilet.wordpress.com/2011/06/14/eyes/</link>
		<comments>http://luckytoilet.wordpress.com/2011/06/14/eyes/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 16:56:30 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[cute]]></category>
		<category><![CDATA[pyglet]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=1943</guid>
		<description><![CDATA[I randomly made this thing out of boredom: Cute, but it&#8217;s probably a sign that I need some better ideas for hobby programming projects xD It uses the pyglet library, and is only a page or so of python code.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1943&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I randomly made this thing out of boredom:</p>
<p><img class="aligncenter" title="Hosted by imgur.com" src="http://i.imgur.com/cpq7f.gif" alt="" /></p>
<p>Cute, but it&#8217;s probably a sign that I need some better ideas for hobby programming projects xD</p>
<p>It uses the <a href="http://www.pyglet.org/">pyglet </a>library, and is only a page or so of <a href="http://ideone.com/GLGFq">python code</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/1943/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/1943/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/1943/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/1943/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/1943/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/1943/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/1943/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/1943/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/1943/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/1943/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/1943/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/1943/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/1943/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/1943/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1943&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/06/14/eyes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/cpq7f.gif" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Coding a Tetris AI using a Genetic Algorithm</title>
		<link>http://luckytoilet.wordpress.com/2011/05/27/coding-a-tetris-ai-using-a-genetic-algorithm/</link>
		<comments>http://luckytoilet.wordpress.com/2011/05/27/coding-a-tetris-ai-using-a-genetic-algorithm/#comments</comments>
		<pubDate>Sat, 28 May 2011 04:20:24 +0000</pubDate>
		<dc:creator>luckytoilet</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[biology]]></category>
		<category><![CDATA[evolution]]></category>
		<category><![CDATA[genetic]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tetris]]></category>
		<category><![CDATA[vimeo]]></category>

		<guid isPermaLink="false">http://luckytoilet.wordpress.com/?p=1924</guid>
		<description><![CDATA[About two years ago, when I was in grade 9, I decided to make a tetris clone in Java. One or two months later, I had a fully working and playable tetris, complete with background and sound effects and scoring and graphical effects when a line is cleared. A year after I created it, I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1924&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>About two years ago, when I was in grade 9, I decided to make a <a href="http://code.google.com/p/tetris4j/">tetris clone</a> in Java. One or two months later, I had a fully working and playable tetris, complete with background and sound effects and scoring and graphical effects when a line is cleared.</p>
<p>A year after I created it, I decided to go ahead and write an AI to play my game. Although it played much better than I could (I&#8217;m not a particularly good tetris player), it would still die after a few dozen lines when the number of columns is 10. This means it was pretty outclassed by existing AI&#8217;s that could go on for <a href="http://www.ccs.neu.edu/home/punkball/tetris/">thousands of lines</a>!</p>
<p>Now, another year later, I coupled my previous AI algorithm with a genetic algorithm, with some pretty neat results:</p>
<div class='embed-vimeo' style='text-align:center;'><iframe src='http://player.vimeo.com/video/24250619' width='400' height='300' frameborder='0'></iframe></div>
<h3>Rules of the Game</h3>
<p>How does one make a computer program to play tetris? Or more generally, how does one play tetris in the first place? The rules of tetris seem to me to be better viewed than explained, but I&#8217;ll give a quick overview of tetris gameplay and tetris strategies.</p>
<p>As I&#8217;m sure most of you know, in tetris you have blocks of four (<em>tetrominoes</em>) falling from the top of the board. The player moves and rotates the blocks and stacks them up:</p>
<p style="text-align:center;"><img title="Hosted by imgur.com" src="http://i.imgur.com/AJPYF.png" alt="" /></p>
<p style="text-align:left;">Here the black outline is one of the places you can put the funny shaped block. And when a row is filled entirely with blocks (the row with the red outline below), you get a <em>clear</em>; that entire row is removed and the rest of the board is shifted down (often with a series of beeping noises and a small increase to your score):</p>
<p style="text-align:center;"><img title="Hosted by imgur.com" src="http://i.imgur.com/zvsp5.png" alt="" /></p>
<p style="text-align:left;">If the blocks don&#8217;t get cleared and they stack to the top of the board, you lose. So ideally you want to fill as many lines as possible and avoid stacking the blocks up. Very simple.</p>
<h3>Simple Strategies</h3>
<p style="text-align:left;">So what are some things that we can try to avoid losing the game? Some things immediately come to mind. We lose when the blocks get stacked up so high that we can&#8217;t put in new blocks, right? So it makes sense to avoid piling up large numbers of blocks in high positions in the first place, or to <strong>penalize </strong><strong>height:</strong></p>
<p style="text-align:center;"><img title="Hosted by imgur.com" src="http://i.imgur.com/jDwWT.png" alt="" /></p>
<p style="text-align:left;">So for each position the computer can calculate a <em>height penalty</em> &#8212; for each block the computer adds a number depending on how high it is. Then when the AI tries to decide where to put the next block, it &#8216;knows&#8217; that blocks piled up really high is a bad thing and tries to avoid it.</p>
<p style="text-align:left;">Another strategy that seems pretty obvious is to try to get clears! We assign a positive score for each line we clear, in other words we <strong>reward clears</strong>. Duh.</p>
<p style="text-align:left;">Anyone who has played a few games of tetris would probably subconsciously know a number of intuitive strategies &#8212; packing together blocks as tightly as possible for instance. How do we translate that into code? Well, to start, blocks that are packed tightly has little or no <em>holes</em> &#8212; we&#8217;ll define these as any empty spaces for which there is a block somewhere directly above it:</p>
<p style="text-align:center;"><img title="Hosted by imgur.com" src="http://i.imgur.com/O53t8.png" alt="" /></p>
<p style="text-align:left;">Why don&#8217;t we want holes? A row is only considered a clear if the entire row is filled &#8212; if there&#8217;s even a single hole in the row, it doesn&#8217;t get removed. Not good. So it makes sense to give a negative score to positions with holes in them &#8212; to <strong>penalize holes</strong>.</p>
<p style="text-align:left;">It&#8217;s best to try not to have any holes at all, but sometimes having a hole or two is inevitable. What can we do after we have holes in our formation? Good question, but we should not pile more blocks on top of our holes. If we define a <em>blockade </em>as any block that&#8217;s directly above a hole, we should <strong>penalize blockades:</strong></p>
<p style="text-align:center;"><img title="Hosted by imgur.com" src="http://i.imgur.com/xUxuK.png" alt="" /></p>
<p style="text-align:left;">Why are blockades bad again? Well, a hole only stops being a hole if there are no more blocks above it, so stacking more blocks above holes would only make it harder to remove the hole.</p>
<p style="text-align:left;">These are all the obvious strategies. I also put in less obvious scores rewarding or penalizing for hugging the wall (edge of the current block touching edge of the wall), hugging the floor (edge of current block touching the floor) and flattening (rewarding if the edge of the current block touches an existing  block). Again, these are harder to justify and mostly for fine-tuning &#8212; it&#8217;s not even clear whether they should be positive or negative.</p>
<h3>A Hedonistic AI</h3>
<p>These strategies are sufficient to make a passable tetris AI. This algorithm is very simple:</p>
<ol>
<li>Look at the current block and the next block and simulate ALL possible combinations (positions and rotations) of the two blocks.</li>
<li>Calculate a score for each of the positions.</li>
<li>Move the block to the position with the highest score and repeat.</li>
</ol>
<p>To give a score for a position, we would use an equation like this:</p>
<p style="text-align:center;"><strong>Score = A * Sum of Heights<br />
+ B * Number of Clears<br />
+ C * Number of Holes<br />
+ D * Number of Blockades</strong></p>
<p style="text-align:left;">Where A, B, C, and D are <em>weights </em>that we decide &#8212; how important is each of the factors. I initially came up with some pretty arbitrary values:</p>
<p style="text-align:left;">
<ul>
<li>-0.03 for the height multiplier</li>
<li>-7.5 per hole</li>
<li>-3.5 per blockade</li>
<li>+8.0 per clear</li>
<li>+3.0 for each edge touching another block</li>
<li>+2.5 for each edge touching the wall</li>
<li>+5.0 for each edge touching the floor</li>
</ul>
<p style="text-align:left;">The reason I gave such a low multiplier for the height is because the numbers stack up so quickly it racks up a huge penalty for each block on the field. The numbers I chose seem pretty reasonable &#8212; and puts blocks more or less where a human would put them.</p>
<h3>Playing God: Bringing in the Genetic Algorithm</h3>
<p>The biggest problem with this method is that we choosed the weights pretty much arbitrarily. They might work well or they might not, but we don&#8217;t really know whether there are better values for them.</p>
<p>What can we do about it? We could brute force it &#8212; but with solutions that range across a continuum, there is a better way &#8212; a genetic algorithm.</p>
<p>A <em>genetic</em> algorithm is just a searching heuristic; it derives its ideas from evolution, where nature creates complex and sophisticated organisms by making <em>random </em>changes to the DNA.</p>
<p>Charles Darwin specifies four criteria for the process of natural selection to occur:</p>
<ol>
<li>Variation: Organisms in a population must be slightly different from one another.</li>
<li>Inheritance: Traits of parent organisms must be passed onto their offspring.</li>
<li>Limited space: Only some of the offspring in any generation is able to survive and pass on its genes.</li>
<li>Competition: Individuals that are more <em>fit</em> are more likely to pass on their genes to the next generation.</li>
</ol>
<p>In order to turn this into an algorithm, we&#8217;ll need &#8212; let&#8217;s quote <a href="http://www.puremango.co.uk/2010/12/genetic-algorithm-for-hello-world/">this article</a>:</p>
<ol>
<blockquote>
<li>A <strong>chromosome</strong> which expresses a possible solution to the problem as a string</li>
<li>A <strong>fitness function</strong> which takes a chromosome as input and returns a higher value for better solutions</li>
<li>A <strong>population</strong> which is just a set of many chromosomes</li>
<li>A <strong>selection</strong> method which determines how parents are selected for breeding from the population</li>
<li>A <strong>crossover</strong> operation which determines how parents combine to produce offspring</li>
<li>A <strong>mutation</strong> operation which determines how random deviations manifest themselves</li>
</blockquote>
</ol>
<div>We begin by constructing a <strong>chromosome</strong> &#8212; a solution to the problem of making an AI to play tetris. This is pretty easy, since we can already run an AI with a set of seven weights. So the chromosome is simply an array of seven doubles.</div>
<p>Next, our <strong>fitness function</strong> is very easy too, since the AI already has a scoring system. Basically the program would run the tetris AI at max speed on a 8 column board until it died, after which it would use the score it earned. Why only 8 columns and not the normal 10? In later generations, AI&#8217;s are able to survive for hours in the 10 column version, but when we reduce the number of columns to 8, even the best AI&#8217;s can survive for only a few seconds to a minute (we&#8217;re still talking about hundreds or thousands of lines here).</p>
<p>I used Nintendo&#8217;s original scoring system for tetris &#8212; 40 points for one clear, 120 points for two simultaneous clears, 300 for three simultaneous clears, and 1200 for four simultaneous clears. I also added 1 point for each block placed, to differentiate between AI&#8217;s that couldn&#8217;t score any lines.</p>
<p>Three, I chose a <strong>population</strong> of sixteen chromosomes. Initially the chromosomes are filled with randomly generated numbers (floating points fitting a normal distribution). Each generation onwards, the population&#8217;s chromosomes are derived from the best candidates of the previous generation (more on this later) &#8212; but the population size stays the same.</p>
<p>Next, for the <strong>selection</strong> method I chose a simple tournament method. After we run each candidate from a generation and collect all of their scores, we randomly pair up the candidates. For each pair, we take the high scorer &#8212; the winner &#8212; and discard the low scorer. Then, we pair up the winners randomly again to generate new offspring for the next generation.</p>
<p>Lastly, I implemented the <strong>crossover</strong> as follows: for each of the seven attributes in the offspring&#8217;s chromosome, we randomly select the respective attribute from the two parents with equal probability.</p>
<p>Occasionally, we have a <strong>mutation </strong>&#8211; a trait in an offspring that does not come from either parent. Each time we give an offspring an attribute, we have a 10% chance of assigning a completely random value to that attribute instead.</p>
<h3>Results of the Genetic Algorithm</h3>
<p>In the first generation or two, most candidates performed horribly. Many candidates had completely wrong weights &#8212; rewarding height and rewarding holes! Needless to say, these programs did not survive very long. But the genetic algorithm quickly came up with some decent solutions, and pretty soon most algorithms were scoring a few hundred lines (my original values gave about 20 lines on the 8-column version by comparison)</p>
<p>After running the genetic algorithm for about ten generations, I picked a candidate that was scoring decently:</p>
<ul>
<li>-3.78 for the height multiplier</li>
<li>-2.31 per hole</li>
<li>-0.59 per blockade</li>
<li>+1.6 per clear</li>
<li>+3.97 for each edge touching another block</li>
<li>+6.52 for each edge touching the wall</li>
<li>+0.65 for each edge touching the floor</li>
</ul>
<p>Whoa &#8212; that&#8217;s a huge height multiplier. Perhaps the multiplier is so big that it just overwhelms everything else in the list &#8212; remember that the height multiplier applies to every block on the field. Also, holes and blockades might not have been as bad as I thought &#8212; and what&#8217;s with the huge bonus for touching the wall?</p>
<p>I ran the whole thing again from scratch for ten generations &#8212; using different randomly generated starting values. What it came up with made me think at first that my program had a bug somewhere:</p>
<ul>
<li>-3.71 for the height multiplier</li>
<li>-4.79 per hole</li>
<li>+1.4 per blockade</li>
<li>-1.87 per clear</li>
<li>+4.8 for each edge touching another block</li>
<li>+3.22 for each edge touching the wall</li>
<li>+3.68 for each edge touching the floor</li>
</ul>
<p>Yup, this one rewarded blockades and penalized clears. And it would outperform both my naive values and the first set of AI values &#8212; I used this set in the video. It seems to put blocks in really bad positions, creating holes and blockades when it is unnecessary to &#8212; but it still does better than anything else I have.</p>
<h3>Conclusion</h3>
<p>Was the exercise a success? I would say partially so. There is the good and the bad. The good is that it came up with a much better AI than the one I originally had. But there may have been some things that could&#8217;ve been done better:</p>
<ul>
<li>What I said about the height multiplier overwhelming everything else is a bit misleading. While it is true that the height multiplier itself applies to every block on the field, it doesn&#8217;t really work that way, and really only affects the current block. Reason being, the rest of the field &#8212; everything but the current block &#8212; stays constant no matter where the current block goes. It&#8217;s kind of like if you vote for everybody, you really vote for nobody as your votes have no effect on the outcome.</li>
<li>The lines cleared factor also turned out to be a bit misleading. While the second AI had a negative weight for clearing a line, it still cleared lines whenever it could: again tying back to the height multiplier. Clearing a line does exactly what it says: removing an entire row of blocks &#8212; and removing that many blocks does a huge blow to the height multiplier.</li>
<li>The fitness function was really kind of screwed up. By the time your AI&#8217;s can get a few thousand lines on a tiny 8-column board, the only thing that causes the AI to die is a bad sequence of hard-to-place S and Z blocks &#8212; and in any random number generator you&#8217;ll eventually get a series of unlucky blocks. So at later generations, simply simulating a 8 column tetris was fairly bad at separating the very good AI&#8217;s from the excellent AI&#8217;s.</li>
<li>Selection was also a bit screwed up. After ten generations, the entire population had more or less the same values, with only some minor variations &#8212; a bit ironic since this situation was exactly the situation the tournament selection algorithm was supposed to prevent.</li>
<li>Although a genetic algorithm can converge on a <em>local optimum</em> fairly quickly, producing a decent solution, it is very hard for it to achieve a <em>global optimum</em> &#8212; the best possible solution. You might have a situation where mutating any one value seriously harms the candidate, but mutating two or more values simultaneously in a certain way makes it better. This is a drawback for genetic algorithms in general.</li>
</ul>
<div>So that&#8217;s all I have to say. I&#8217;m fairly new to genetic algorithms, so I may have botched one or more parts of the algorithm. I&#8217;d love to know what I did wrong and how I should&#8217;ve done better.</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/luckytoilet.wordpress.com/1924/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/luckytoilet.wordpress.com/1924/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/luckytoilet.wordpress.com/1924/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/luckytoilet.wordpress.com/1924/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/luckytoilet.wordpress.com/1924/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/luckytoilet.wordpress.com/1924/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/luckytoilet.wordpress.com/1924/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/luckytoilet.wordpress.com/1924/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/luckytoilet.wordpress.com/1924/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/luckytoilet.wordpress.com/1924/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/luckytoilet.wordpress.com/1924/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/luckytoilet.wordpress.com/1924/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/luckytoilet.wordpress.com/1924/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/luckytoilet.wordpress.com/1924/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=luckytoilet.wordpress.com&amp;blog=12001659&amp;post=1924&amp;subd=luckytoilet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://luckytoilet.wordpress.com/2011/05/27/coding-a-tetris-ai-using-a-genetic-algorithm/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f915fbd3fb60671d7171db4b8e3fda1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">luckytoilet</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/AJPYF.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/zvsp5.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/jDwWT.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/O53t8.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>

		<media:content url="http://i.imgur.com/xUxuK.png" medium="image">
			<media:title type="html">Hosted by imgur.com</media:title>
		</media:content>
	</item>
	</channel>
</rss>
