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

<channel>
	<title>Master Baboon &#187; Python</title>
	<atom:link href="http://www.masterbaboon.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.masterbaboon.com</link>
	<description>The sea of the simulation</description>
	<lastBuildDate>Mon, 14 Nov 2011 08:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Join us at the Advanced Scientific Programming in Python summer school in Scotland</title>
		<link>http://www.masterbaboon.com/2011/03/join-us-at-the-advanced-scientific-programming-in-python-summer-school-in-scotland/</link>
		<comments>http://www.masterbaboon.com/2011/03/join-us-at-the-advanced-scientific-programming-in-python-summer-school-in-scotland/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 17:31:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[PacMan capture-the-flag]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=489</guid>
		<description><![CDATA[If you&#8217;re interested in learning more about scientific programming in Python, and want to compete in a Pacman capture-the-flag tournament, join us for the next Advanced Scientific Programming in Python summer school in St. Andrew, UK! The faculty line-up includes developers of well-known scientific libraries for Python (e.g., Francesc Alted of PyTables fame). The program [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re interested in learning more about scientific programming in Python, and want to compete in a <a href="http://www.masterbaboon.com/2009/09/pacman-capture-the-flag-a-fun-game-for-artificial-intelligence-development-and-education/">Pacman</a> <a href="http://www.masterbaboon.com/2010/09/tracking-down-the-enemy/">capture-the-flag</a> <a href="http://www.masterbaboon.com/2011/01/tracking-down-the-enemy-2/">tournament</a>, join us for the next <a href="https://python.g-node.org/wiki/">Advanced Scientific Programming in Python</a> summer school in St. Andrew, UK!</p>
<p>The faculty line-up includes developers of well-known scientific libraries for Python (e.g., <a href="http://www.pytables.org/moin/FrancescAlted">Francesc Alted</a> of <a href="http://www.pytables.org/moin">PyTables</a> fame). The program covers advanced topics in numerical programming (advanced NumPy, Cython, parallel applications, data serialization, &#8230;) and modern techniques for writing robust, efficient code (agile programming, test driven development, version control, optimization techniques, &#8230;). Most of all, the school is a great opportunity to meet like-minded people and have fun writing Python code together! Participation is free of charge, and you can <a href="https://python.g-node.org/wiki/">apply online</a>.</p>
<p>You can see a few picture from previous summer schools on our<a href="http://www.facebook.com/group.php?gid=145875921776" target="_blank"> Facebook group</a>.</p>
<p style="text-align: center;">
<div class="wp-caption aligncenter" style="width: 372px"><img class=" " title="Summer school in Berlin" src="http://a5.sphotos.ak.fbcdn.net/hphotos-ak-snc1/6830_1201852400426_1052055109_608170_5509474_n.jpg" alt="Summer school participants in Berlin, working hard on their PacMan agents." width="362" height="243" /><p class="wp-caption-text">Summer school participants in Berlin, working hard on their PacMan agents.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2011/03/join-us-at-the-advanced-scientific-programming-in-python-summer-school-in-scotland/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tracking down the enemy (2)</title>
		<link>http://www.masterbaboon.com/2011/01/tracking-down-the-enemy-2/</link>
		<comments>http://www.masterbaboon.com/2011/01/tracking-down-the-enemy-2/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 23:39:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[PacMan capture-the-flag]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[PacMan]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=456</guid>
		<description><![CDATA[I never got the chance to show a working agent based on the Bayesian estimator for the enemy position in the PacMan capture-the-flag game. In the previous PacMan post, I wrote about merging a model of agent movements with the noisy measurements returned by the game to track the enemy agents across the maze. Clearly, [...]]]></description>
			<content:encoded><![CDATA[<p>I never got the chance to show a working agent based on the Bayesian estimator for the enemy position in the PacMan capture-the-flag game. In the <a href="http://www.masterbaboon.com/2010/09/tracking-down-the-enemy/" target="_self">previous PacMan post</a>, I wrote about merging a model of agent movements with the noisy measurements returned by the game to track the enemy agents across the maze. Clearly, this information can give you an edge when planning an attack (to avoid ghosts) or when defending (to intercept the PacMen).</p>
<p>For  the traditional faculty-vs-students tournament at the <a href="https://portal.g-node.org/python-autumnschool/" target="_blank">G-Node scientific programming summer school</a> this year, I wrote a PacMan team made by a simple attacker, and a more sophisticated defender that tries to intercept and devour enemy agents.</p>
<p>Both agents plan their movements using a shortest-path algorithm on a weighted graph: before the start of the game, the maze is transformed in a graph, where nodes are the maze tiles, and edges connect adjacent tiles. Weights along the edges are adjusted according to the estimated position of the agents:</p>
<ul>
<li>Weights on edges close to an enemy ghost are increased (starting value is proportional to the probability of the enemy being there, and falls off exponentially with distance)</li>
<li>Weights on edges close to an enemy PacMan are decreased</li>
<li>Weights on edges close to a friendly agent are increased</li>
</ul>
<p>An agent navigating on such a maze will tend to avoid ghosts, chase PacMen, and cover parts of the maze far from other friendly agents. My attacker does little else than updating the weights of the graph at every turn, and move toward the closest food dot.</p>
<p>On the other hand, defending is quite difficult in this game, so I needed a more sophisticated strategy. While the enemy is still a ghost in its own part of the maze, the defender moves toward the closest enemy agent (its estimated position, that is). When the enemy is a PacMan in the friendly half, the chase is on! Since ghosts and PacMen move at the same speed, it would be pointless to just follow it around, one needs to catch them from the front&#8230; Once more, the solution was to modify the weights of the maze graph, making weights behind the enemy (i.e., opposite to its direction of motion) very high, and lowering the edges in front of it.</p>
<p>The combination of estimator and the weighted graph strategy can be quite entertaining:</p>
<p><iframe title="YouTube video player" type="text/html" width="544" height="327" src="http://www.youtube.com/embed/YM-rQJPL16o" frameborder="0" allowFullScreen></iframe></p>
<p>Sometimes the defender only needs to guard the border to scare the opponent shitless:</p>
<p><iframe title="YouTube video player" class="youtube-player" type="text/html" width="544" height="327" src="http://www.youtube.com/embed/1ciCtwW14RY" frameborder="0" allowFullScreen></iframe></p>
<p>Another useful thing to keep in mind for the future: it is better to base strategies on soft constraints (weighted graphs, probabilities). Setting hard, deterministic rules tends to get you stuck in loops. Soft constraints and some randomness give you more flexibility when you need it but are otherwise just as good.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2011/01/tracking-down-the-enemy-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solving the game Set®</title>
		<link>http://www.masterbaboon.com/2010/09/solving-the-game-set/</link>
		<comments>http://www.masterbaboon.com/2010/09/solving-the-game-set/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 14:58:05 +0000</pubDate>
		<dc:creator>pietro</dc:creator>
				<category><![CDATA[games]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=292</guid>
		<description><![CDATA[Recently a friend of mine introduced me to a game called Set. Set is a logic cards game for several players; 3-5 players is probably best, but in there is no limit in principle, and it is also fun to play as a solitary. The game contains a deck of cards with symbols varying across [...]]]></description>
			<content:encoded><![CDATA[<p>Recently a friend of mine introduced me to a game called <em><a href="http://www.setgame.com/set/index.html">Set</a></em>. <em>Set </em>is a logic cards game for several players; 3-5 players is probably best, but in there is no limit in principle, and it is also fun to play as a solitary.</p>
<p>The game contains a deck of cards with symbols varying across 4 dimensions: color, number, shape, and texture. For each dimension, there are 3 possible features (e.g., there are 3 possible textures: full, empty, striped). A valid <em>set</em> is formed by three cards that have on each dimension either the <em>same</em> feature, or <em>three different</em> features. So for example in the image below, the first three cards are a valid set, as they are different on all features across all dimensions; the second three cards also form a valid set, because they share the same features for color and number, and are different in shape and texture; the cards on the bottom are not a set, because two cards have the &#8220;full&#8221; texture, while one is striped.</p>
<p style="text-align: center;">
<div id="attachment_445" class="wp-caption aligncenter" style="width: 410px"><img class="size-large wp-image-445 " title="sets_examples" src="http://www.masterbaboon.com/wp-content/uploads/2010/09/sets_examples1-1024x586.png" alt="Example of valid (top) and invalid (bottom) sets." width="400" height="229" /><p class="wp-caption-text">Example of valid (top) and invalid (bottom) sets.</p></div>
<p>In a Set game, 12 cards are put on the table, and the players have to find a valid set. The quickest among them collects the set and replaces the removed cards with new ones. If the players agree that there is no set on the table, 3 more cards are added. Once the deck is empty, the player who collected the most sets wins! Simple but fun&#8230;</p>
<p>I decided to write a solver for the game that finds all possible sets given a set of cards, with the goal of collecting statistics over thousands of games. The solver should thus be as efficient as I can manage to write it.</p>
<p>I&#8217;m going to represent each cards as a four-dimensional vector (color, shape, texture, number), each element containing either 0, 1, or 2 (representing the 3 possible values for each dimension). A function that checks if three cards form a valid set would thus look like this:</p>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> numpy<br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">itertools</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> same<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;Returns True if all elements are the same.&quot;&quot;&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> numpy.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span>x <span style="color: #66cc66;">==</span> x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> different<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;Returns True if all elements are different.&quot;&quot;&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numpy.<span style="color: black;">unique</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> is_set<span style="color: black;">&#40;</span>cards<span style="color: #66cc66;">,</span> indices<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;Checks that the cards indexed by 'indices' form a valid set.&quot;&quot;&quot;</span><br />
&nbsp; &nbsp; ndims <span style="color: #66cc66;">=</span> cards.<span style="color: black;">shape</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; subset <span style="color: #66cc66;">=</span> cards<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> indices<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> dim <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>ndims<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># cards must be all the same or all different for all dimensions</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> same<span style="color: black;">&#40;</span>subset<span style="color: black;">&#91;</span>dim<span style="color: #66cc66;">,</span> :<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> different<span style="color: black;">&#40;</span>subset<span style="color: black;">&#91;</span>dim<span style="color: #66cc66;">,</span> :<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span></div></td></tr></tbody></table></div>
</pre>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># solution checker</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># &#8212;&#8212;&#8212;&#8212;&#8212;-</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def same(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are the same.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return numpy.all(x == x[0])</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def different(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are different.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return len(numpy.unique(x)) == len(x)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def is_set(cards, indices):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Checks that the cards indexed by &#8216;indices&#8217; form a valid set.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">ndims = cards.shape[0]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">subset = cards[:, indices]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">for dim in range(ndims):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># cards must be all the same or all different for all dimensions</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">if not same(subset[dim, :]) and not different(subset[dim, :]):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return False</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return True</div>
<div>A brute force solver could then try all possible combinations of three cards and check whether they form a valid set:</div>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> find_sets<span style="color: black;">&#40;</span>cards<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;Brute-force Sets solver.&quot;&quot;&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>indices<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> indices <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">itertools</span>.<span style="color: black;">combinations</span><span style="color: black;">&#40;</span><span style="color: #008000;">range</span><span style="color: black;">&#40;</span>cards.<span style="color: black;">shape</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> is_set<span style="color: black;">&#40;</span>cards<span style="color: #66cc66;">,</span> indices<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></div></td></tr></tbody></table></div>
</pre>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># solution checker</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># &#8212;&#8212;&#8212;&#8212;&#8212;-</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def same(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are the same.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return numpy.all(x == x[0])</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def different(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are different.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return len(numpy.unique(x)) == len(x)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def is_set(cards, indices):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Checks that the cards indexed by &#8216;indices&#8217; form a valid set.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">ndims = cards.shape[0]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">subset = cards[:, indices]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">for dim in range(ndims):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># cards must be all the same or all different for all dimensions</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">if not same(subset[dim, :]) and not different(subset[dim, :]):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return False</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return True</div>
<p>The brute force approach is very inefficient, but it is also very useful to test more efficient solutions (just check that they give the same response as the brute force one).</p>
<p>The second solver improves on the first one with a simple observation. Once you have two cards, there is only one possible card that completes the set: for each dimension, if the two cards have the same feature, the missing one will also have to have the same feature; if the features are different, the missing card will have to have the missing feature.</p>
<p style="text-align: center;">
<div id="attachment_446" class="wp-caption aligncenter" style="width: 410px"><img class="size-full wp-image-446  " title="complete_set" src="http://www.masterbaboon.com/wp-content/uploads/2010/09/complete_set1.png" alt="Given two cards, there is only one possible card that forms a valid set." width="400" height="234" /><p class="wp-caption-text">Given two cards, there is only one possible card that forms a valid set.</p></div>
<p>A possible strategy is thus to consider all possible pairs of cards, derive the one completing the set, and check if it is present on the table. This solution is much more efficient, as there are 220 possible triplets out of 12 cards, but only 66 pairs. It runs about 6 times faster:</p>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> find_sets2<span style="color: black;">&#40;</span>cards<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; ndims<span style="color: #66cc66;">,</span> ncards <span style="color: #66cc66;">=</span> cards.<span style="color: black;">shape</span><br />
&nbsp; &nbsp; all_features <span style="color: #66cc66;">=</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># solutions contain the indices of the cards forming sets</span><br />
&nbsp; &nbsp; solutions <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># iterate over all pairs</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> idx1<span style="color: #66cc66;">,</span> idx2 <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">itertools</span>.<span style="color: black;">combinations</span><span style="color: black;">&#40;</span><span style="color: #008000;">range</span><span style="color: black;">&#40;</span>ncards - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; c1<span style="color: #66cc66;">,</span> c2 <span style="color: #66cc66;">=</span> cards<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> idx1<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> cards<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> idx2<span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># compute card that would complete the set</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; missing <span style="color: #66cc66;">=</span> numpy.<span style="color: black;">empty</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>ndims<span style="color: #66cc66;">,</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> dtype<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'i'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>ndims<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> c1<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span> <span style="color: #66cc66;">==</span> c2<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># same feature on this dimension -&gt;; missing card also has same</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; missing<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> c1<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># different features -&gt; find third missing feature</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; missing<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>all_features - <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>c1<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> c2<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># look for missing card in the cards array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; where_idx <span style="color: #66cc66;">=</span> numpy.<span style="color: black;">flatnonzero</span><span style="color: black;">&#40;</span>numpy.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span>cards<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> idx2 + <span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>.<span style="color: black;">T</span> <span style="color: #66cc66;">==</span> missing<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; axis<span style="color: #66cc66;">=</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># append to solutions if found</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>where_idx<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solutions.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>idx1<span style="color: #66cc66;">,</span> idx2<span style="color: #66cc66;">,</span> where_idx<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + idx2 + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> solutions</div></td></tr></tbody></table></div>
</pre>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># solution checker</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># &#8212;&#8212;&#8212;&#8212;&#8212;-</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def same(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are the same.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return numpy.all(x == x[0])</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def different(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are different.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return len(numpy.unique(x)) == len(x)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def is_set(cards, indices):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Checks that the cards indexed by &#8216;indices&#8217; form a valid set.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">ndims = cards.shape[0]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">subset = cards[:, indices]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">for dim in range(ndims):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># cards must be all the same or all different for all dimensions</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">if not same(subset[dim, :]) and not different(subset[dim, :]):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return False</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return True</div>
<p>The trained eye will see at this point that the problem can be re-written as a dynamic programming one. We can start looking at the first dimension, and form groups of cards with the same feature. We know by the reasoning above that valid sets will either contain cards in the same group, or contain one card from each group. First we consider the former case, and recursively apply the same procedure to all the remaining dimensions for cards within each group. Second, we consider all triplets of cards that have one card per group and verify if it&#8217;s a set:</p>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> find_sets3<span style="color: black;">&#40;</span>cards<span style="color: #66cc66;">,</span> indices<span style="color: #66cc66;">=</span><span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; nd<span style="color: #66cc66;">,</span> n <span style="color: #66cc66;">=</span> cards.<span style="color: black;">shape</span><br />
&nbsp; &nbsp; c0 <span style="color: #66cc66;">=</span> cards<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> :<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> indices <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; indices <span style="color: #66cc66;">=</span> numpy.<span style="color: black;">arange</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; groups <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>c0 <span style="color: #66cc66;">==</span> f<span style="color: black;">&#41;</span>.<span style="color: black;">nonzero</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>nfeatures<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># equals</span><br />
&nbsp; &nbsp; solequal <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> g <span style="color: #ff7700;font-weight:bold;">in</span> groups:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>g<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">3</span>: <span style="color: #ff7700;font-weight:bold;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; solequal +<span style="color: #66cc66;">=</span> find_sets3<span style="color: black;">&#40;</span>cards<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:nd<span style="color: #66cc66;">,</span> g<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> indices<span style="color: black;">&#91;</span>g<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># different</span><br />
&nbsp; &nbsp; soldiff <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>indices<span style="color: black;">&#91;</span>i0<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> indices<span style="color: black;">&#91;</span>i1<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> indices<span style="color: black;">&#91;</span>i2<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff7700;font-weight:bold;">for</span> i0 <span style="color: #ff7700;font-weight:bold;">in</span> groups<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i1 <span style="color: #ff7700;font-weight:bold;">in</span> groups<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i2 <span style="color: #ff7700;font-weight:bold;">in</span> groups<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff7700;font-weight:bold;">if</span> is_set<span style="color: black;">&#40;</span>cards<span style="color: #66cc66;">,</span> <span style="color: black;">&#40;</span>i0<span style="color: #66cc66;">,</span> i1<span style="color: #66cc66;">,</span> i2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> solequal + soldiff</div></td></tr></tbody></table></div>
</pre>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># solution checker</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># &#8212;&#8212;&#8212;&#8212;&#8212;-</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def same(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are the same.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return numpy.all(x == x[0])</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def different(x):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Returns True if all elements are different.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return len(numpy.unique(x)) == len(x)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">def is_set(cards, indices):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">&#8220;&#8221;"Checks that the cards indexed by &#8216;indices&#8217; form a valid set.&#8221;"&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">ndims = cards.shape[0]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">subset = cards[:, indices]</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">for dim in range(ndims):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;"># cards must be all the same or all different for all dimensions</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">if not same(subset[dim, :]) and not different(subset[dim, :]):</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return False</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">return True</div>
<p>It turns out that, although it is a very efficient strategy to use  while playing with cards, find_sets3 is slower than  find_set2, probably because the overhead of calling the function  recursively outweighs the efficiency for such a small number of cards.</p>
<p>Let&#8217;s have a look at some statistics, then. While playing it can be quite frustrating to stare at the cards without being able to find any set. The instructions that ship with the official game say that such a situation should occur only once every 33 turns, but it certainly doesn&#8217;t feel that way. Is that really so?</p>
<p>First, I drew at random 12 cards from a complete deck of cards, and used the solver to compute the number of valid sets present on the table. I repeated this 10000 times, and ended up with this distribution for the number of set in a random draw:</p>
<div id="attachment_388" class="wp-caption aligncenter" style="width: 410px"><img class="size-full wp-image-388 " title="histogram_random_draws" src="http://www.masterbaboon.com/wp-content/uploads/2010/09/histogram_random_draws.png" alt="histogram_random_draws" width="400" height="248" /><p class="wp-caption-text">Probability of finding a given number of sets in 12 cards drawn at random.</p></div>
<p>As the instructions say, about 3% of the time (1 in 33) there is no set in the cards. However, this is misleading, as during a game the cards are not independently drawn each turn: the players remove one set from the cards on the table, and replace it with new cards. I thus simulated complete games, where at every turn I removed one of the set present on the table at random. The code to simulate a game look like this:</p>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> random_deck<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># initialize cards deck</span><br />
&nbsp; &nbsp; cards <span style="color: #66cc66;">=</span> numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>card <span style="color: #ff7700;font-weight:bold;">for</span> card <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">itertools</span>.<span style="color: black;">product</span><span style="color: black;">&#40;</span><span style="color: #008000;">range</span><span style="color: black;">&#40;</span>nfeatures<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat<span style="color: #66cc66;">=</span>ndims<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">T</span><br />
&nbsp; &nbsp; n <span style="color: #66cc66;">=</span> cards.<span style="color: black;">shape</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># shuffle</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> cards<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> numpy.<span style="color: #dc143c;">random</span>.<span style="color: black;">permutation</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
<br />
ncards <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">12</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> onegame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; nsolutions <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; deck <span style="color: #66cc66;">=</span> random_deck<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pos <span style="color: #66cc66;">=</span> ncards<br />
&nbsp; &nbsp; cards <span style="color: #66cc66;">=</span> deck<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> :pos<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># find all sets</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">sets</span> <span style="color: #66cc66;">=</span> find_sets2<span style="color: black;">&#40;</span>cards<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; nsets <span style="color: #66cc66;">=</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sets</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> nsets <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># choose a random set</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chosen <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">sets</span><span style="color: black;">&#91;</span>numpy.<span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sets</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># remove cards from chosen set</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idx <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span>i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>cards.<span style="color: black;">shape</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">if</span> i <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> chosen<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards <span style="color: #66cc66;">=</span> cards<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> idx<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># add new cards</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> cards.<span style="color: black;">shape</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">12</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nadd <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">12</span> - cards.<span style="color: black;">shape</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards <span style="color: #66cc66;">=</span> numpy.<span style="color: black;">concatenate</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>cards<span style="color: #66cc66;">,</span> deck<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> pos:pos + nadd<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> axis<span style="color: #66cc66;">=</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos +<span style="color: #66cc66;">=</span> nadd<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> pos <span style="color: #66cc66;">&gt;=</span> deck.<span style="color: black;">shape</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">break</span> <span style="color: #808080; font-style: italic;"># game is over</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># add additional cards</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards <span style="color: #66cc66;">=</span> numpy.<span style="color: black;">concatenate</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>cards<span style="color: #66cc66;">,</span> deck<span style="color: black;">&#91;</span>:<span style="color: #66cc66;">,</span> pos:pos + <span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> axis<span style="color: #66cc66;">=</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos +<span style="color: #66cc66;">=</span> <span style="color: #ff4500;">3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; nsolutions.<span style="color: black;">append</span><span style="color: black;">&#40;</span>nsets<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> nsolutions</div></td></tr></tbody></table></div>
</pre>
<p>The distribution of the number of sets on the table at any point in time looks quite different now (after simulating <del datetime="2010-09-21T14:40:52+00:00">1000</del> 5000 random games):</p>
<div id="attachment_389" class="wp-caption aligncenter" style="width: 410px"><img src="http://www.masterbaboon.com/wp-content/uploads/2010/09/histogram_game_draws1.png" alt="histogram_game_draws" title="histogram_game_draws" width="400" height="248" class="aligncenter size-full wp-image-453" /><p class="wp-caption-text">Probability of there being a given number of sets on the table at any point in a Set game.</p></div>
<p>As you see, the probability of there being no set on the table tripled and became about 1 in 10!  Now that&#8217;s a relief, it is not that weird not to be able to find a set&#8230; or is it? The distribution also tells us that about half of the times there are <em>3 sets or more</em> on the table! Now that one, I didn&#8217;t expect&#8230; (43 percent of the times, to be precise.)</p>
<p>The code and other material is available on the git repository at <a href="http://github.com/pberkes/masterbaboon/tree/master/projects/setgame/">http://github.com/pberkes/masterbaboon/tree/master/projects/setgame/</a> .</p>
<p><strong>Update 09/21/10:</strong> I updated the histogram of number of sets during a game with more games, so that the result is more accurate. The probability of not having a set at any point is even higher than reported before.</p>
<p><em>Cards and game © 1988, 1991 Cannei, LLC.  All rights reserved.  SET® is  registered trademark of Cannei, LLC.  Used with permission from Set  Enterprises, Inc.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2010/09/solving-the-game-set/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Planet Wars &#8211; Google AI Challenge</title>
		<link>http://www.masterbaboon.com/2010/09/planet-wars-google-ai-challenge/</link>
		<comments>http://www.masterbaboon.com/2010/09/planet-wars-google-ai-challenge/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 21:09:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[Planet Wars]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=340</guid>
		<description><![CDATA[The Computer Science of the University of Waterloo is organizing its second Google AI Challenge. The challenge is a competition between computer programs that control the artificial intelligence of the players in a video game. This time, the game is set in space, and features a symmetric configuration of planets, each containing a fleet of [...]]]></description>
			<content:encoded><![CDATA[<p>The Computer Science of the University of Waterloo is organizing its second <a href="http://ai-contest.com/index.php">Google AI Challenge</a>. The challenge is a competition between computer programs that control the artificial intelligence of the players in a video game.</p>
<p>This time, the game is set in space, and features a symmetric configuration of planets, each containing a fleet of defending spaceships. Each turn, new ships are created, with larger planets creating more ships. At the beginning of the game, each player controls one of the planets and a hundred ships; the AIs issue orders to the ships to send them to conquer planets by outnumbering the local defense. The goal of the game is, of course, to eliminate all of the enemy forces.</p>
<p>This video shows an example game between my first AI (green) playing against DualBot (red), one of the bots included in the starter package; the number floating around represent the fleets commanded by the AIs:</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/A0ATZoeWV48?fs=1&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/A0ATZoeWV48?fs=1&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>The competition is open to everybody, and it is possible to write the AI in basically any programming language. If you plan to use Python, I recommend not using the official starter package, which is un-pythonic and not very sophisticate, but rather <a href="http://github.com/ulope/planetwars-python-kit">this un-official client</a>. The alternative client offers in particular the possibility to log debug information to a local file.</p>
<p>Also, it can be quite frustrating to wait for the official server to match you with some other program, which can take up to an hour. There is an <a href="http://www.benzedrine.cx/planetwars/">alternative server</a> that lets you play with another opponent straight away.</p>
<p>The submission period ends November 27, good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2010/09/planet-wars-google-ai-challenge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tracking down the enemy</title>
		<link>http://www.masterbaboon.com/2010/09/tracking-down-the-enemy/</link>
		<comments>http://www.masterbaboon.com/2010/09/tracking-down-the-enemy/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 03:13:17 +0000</pubDate>
		<dc:creator>pietro</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[PacMan capture-the-flag]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[PacMan]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=300</guid>
		<description><![CDATA[As another scientific Python course is approaching, I&#8217;ve been brushing up my PacMan skills. I decided to give a try to a strategy I had been thinking on, which relies upon having a good estimate of the enemy&#8217;s position. I should remind the reader that in the PacMan capture-the-flag game, one team does not know [...]]]></description>
			<content:encoded><![CDATA[<p>As another <a href="https://portal.g-node.org/python-autumnschool/">scientific Python course</a> is approaching, I&#8217;ve been brushing up my<a href="http://www.masterbaboon.com/2009/09/pacman-capture-the-flag-a-fun-game-for-artificial-intelligence-development-and-education/"> PacMan skills</a>. I decided to give a try to a strategy I had been thinking on, which relies upon having a good estimate of the enemy&#8217;s position. I should remind the reader that in the <a href="http://www.masterbaboon.com/2009/09/pacman-capture-the-flag-a-fun-game-for-artificial-intelligence-development-and-education/">PacMan capture-the-flag game</a>, one team does not know the exact position of the other agents unless they are within 5 squares of one&#8217;s own agents. The game does, however, return a rough estimate of the opponent&#8217;s distance. Our agents-tracker will thus have to blindly make its best guess, and keep a probability distribution over possible positions.</p>
<p>To estimate the position of the opponent agents we need to apply some probability theory:</p>
<p>P(x(t)) = sum_{x(t-1)}  P( x(t-1) ) P( x(t) | x(t-1) )</p>
<p>or, in other words, the probability that the agent is at position x(t) at time t is equal to the sum of the probability of it being at x(t-1) at time t-1, times the probability of transitioning from x(t-1) to x(t). The first term is given simply by the previous estimate, while the second term is our model of the behavior of our opponent (*).</p>
<p>For example, a very conservative model could assume that the opponent could take any legal move at random:</p>
<p>P( x(t) | x(t-1) ) = 1/N</p>
<p>if x(t-1)-&gt;x(t) is a legal move, where N is the total number of legal moves from x(t-1), and</p>
<p>P( x(t) | x(t-1) ) = 0</p>
<p>otherwise. This video shows how such a model performs when the opponent behaves exactly as assumed; the red agent, in the bottom left corner, is estimating the position of the blue agent in the opposite corner; the area of the red squares is proportional to the probability P(x(t)):</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="544" height="327" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/393puqZt6OM?fs=1&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="544" height="327" src="http://www.youtube.com/v/393puqZt6OM?fs=1&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>The tracker is doing a good job in this case, but fails miserably for a more realistic opponent:</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="544" height="327" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/1EyIRp7xe-8?fs=1&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="544" height="327" src="http://www.youtube.com/v/1EyIRp7xe-8?fs=1&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>We clearly need to improve the opponent&#8217;s model&#8230; luckily another simple model results in a large improvement: we can safely assume that the opponent tends to explore new parts of the maze in search of food. We can formalize this as</p>
<p>P( x(t) | x(t-1) ) = 1/Z exp(-beta * v(x(t))</p>
<p>if x(t-1)-&gt;x(t) is a legal move, and 0 otherwise. v(x) is the number of times the agent visited x in the past, and beta is a constant that controls the how exploratory the opponent is. When beta=0, the model is equivalent to the previous random model. Z is a normalizing constant such that P( x(t) | x(t-1) ) sums to 1.</p>
<p>Let&#8217;s see how this model does in practice (in the video, beta = 10):<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="544" height="327" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/ni827laFLw8?fs=1&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="544" height="327" src="http://www.youtube.com/v/ni827laFLw8?fs=1&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>Much better, isn&#8217;t it? We can do even better by using two other sources of information: first, the game gives us a noisy estimation of the distance of the opponent (actual distance +/- 6); second, we know that if the opponent is not visible, it must be at least 5 squares away. We can take this information into account by setting P(x(t)) to 0 for squares that lie outside the noisy distance range, and for those inside the visibility range.</p>
<p>The last video shows the complete tracker at work. The blue lines show the area in which the agent might be according to the noisy distance, and the green line shows the visibility range:</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="544" height="327" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/EsfQ_54MTxk?fs=1&amp;hl=en_US" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="544" height="327" src="http://www.youtube.com/v/EsfQ_54MTxk?fs=1&amp;hl=en_US" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>The biggest area for improvement here is the agent&#8217;s model P(x(t)|x(t-1)). One possibility could be to simulate several common strategies, and to use the transition statistics for the simulated agents to estimate that probability&#8230;</p>
<p>Now, can we use the estimated position to program better AI agents? I&#8217;ll give it a try, and report back soon!</p>
<p><span style="color: #888888;">(*) Strictly speaking, we are doing &#8220;filtering&#8221; here, i.e., we&#8217;re estimating the current position assuming the past inferences are fixed. The alternative is to do &#8220;smoothing&#8221;, where the full joint probability P(x(t), &#8230;, x(1)) is estimated at each step. The information coming from the new observation is propagated back and forth at each to improve the past inferences. For example, knowing that the agent is at a given position at time t might exclude another position at time t-2 because of too large a distance, which in turn could improve the estimate at time t.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2010/09/tracking-down-the-enemy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PacMan capture-the-flag: a fun game for artificial intelligence development and education</title>
		<link>http://www.masterbaboon.com/2009/09/pacman-capture-the-flag-a-fun-game-for-artificial-intelligence-development-and-education/</link>
		<comments>http://www.masterbaboon.com/2009/09/pacman-capture-the-flag-a-fun-game-for-artificial-intelligence-development-and-education/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 17:55:03 +0000</pubDate>
		<dc:creator>pietro</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[PacMan capture-the-flag]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[PacMan]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=260</guid>
		<description><![CDATA[At the beginning of September I&#8217;ve been invited to teach at a summer school about scientific programming. The whole experience has been really rewarding, but it was the student&#8217;s project that got me going: we had the students write artificial intelligence algorithms for the agents of a PacMan-like game, and organized a tournament for them [...]]]></description>
			<content:encoded><![CDATA[<p>At the beginning of September I&#8217;ve been invited to teach at a<a href="http://www.g-node.org/python-summerschool-2009" target="_blank"> summer school about scientific programming</a>. The whole experience has been really rewarding, but it was the student&#8217;s project that got me going: we had the students write artificial intelligence algorithms for the agents of a PacMan-like game, and organized a tournament for them to compete against each other.</p>
<p>The <strong>PacMan capture-the-flag game</strong> has been written originally by John DeNero, and has been used to teach an artificial intelligence course <a href="http://inst.eecs.berkeley.edu/~cs188/sp09/information.html" target="_blank">by him at Berkley</a> and by Hal Daume III at <a href="http://www.cs.utah.edu/~hal/courses/2009S_AI/" target="_blank">University of Utah</a>. Very often, this kind of games have a single strategy that dominates all others, and once you find it the interest fizzles out. In this case, I was impressed by how rich this game is. The game offers a lot of opportunities to develop and test complex learning and planning algorithms, including cooperation strategies for games with multiple agents.</p>
<p><img class="aligncenter size-large wp-image-264" title="capture_the_flag" src="http://www.masterbaboon.com/wp-content/uploads/2009/09/capture_the_flag-1024x397.png" alt="capture_the_flag" width="516" height="200" /></p>
<p>The rules of the game are quite simple: the board is a PacMan maze, divided in a red and a blue half. The two halves belong to two teams of agents, who are controlled by computer programs to eat the opponent&#8217;s food and protect their own. When in the opponent&#8217;s half, the agents are <strong>PacMan</strong> (PacMen?), while in their own half, the agents are <strong>ghosts</strong> and can kill the opponent&#8217;s PacMan agents, in which case these are returned to their initial position. The players get one point for each food dot they eat; no points are assigned for eating the other team&#8217;s agents. The game ends when one of the two teams eats all of the opponent&#8217;s food, or after 3000 moves; the team with the highest score wins.</p>
<p>To make the game more interesting, one can only observe the position of the other team&#8217;s agents when they are very close to one&#8217;w own agents (5 squares away); otherwise, one can only obtain a noisy estimate of their distance.</p>
<p>The game is written in Python, my programming language of choice, which allows to write rapidly even sophisticated algorithms. I recommend the game to anyone wanting to organize an artificial intelligence course, or simply have fun writing AI agents. I plan to dedicate a couple of posts to the basic strategies to write successful agents in this game.</p>
<p>Here&#8217;s a video of the best students&#8217; agents (red team) playing against the best tutors&#8217; agents (blue team). The tutors won, saving our reputation!</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/E82IrVhVaVc&amp;hl=en&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/E82IrVhVaVc&amp;hl=en&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Update:</strong> The authors of the PacMan capture-the-flag game decided to keep the game close-source, and in particular would prefer not to publish the code of agents playing their game, fearing that it might interfere with their course. It&#8217;s a shame because I was planning to write some Genetic Programming agents for the game, but of course I respect their decision. I guess there will be no series of posts re:PacMan&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2009/09/pacman-capture-the-flag-a-fun-game-for-artificial-intelligence-development-and-education/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

