<?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>benjismith.net &#187; aicoder</title>
	<atom:link href="http://benjismith.net/index.php/category/aicoder/feed/" rel="self" type="application/rss+xml" />
	<link>http://benjismith.net</link>
	<description>Benji Smith, Software Research</description>
	<lastBuildDate>Wed, 01 Jun 2011 17:08:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AI-Coder Due Diligence: Why I&#8217;m Changing Plans</title>
		<link>http://benjismith.net/index.php/2006/08/27/ai-coder-due-diligence-why-im-changing-plans/</link>
		<comments>http://benjismith.net/index.php/2006/08/27/ai-coder-due-diligence-why-im-changing-plans/#comments</comments>
		<pubDate>Mon, 28 Aug 2006 03:53:01 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[aicoder]]></category>
		<category><![CDATA[biz]]></category>

		<guid isPermaLink="false">http://benjismith.net/index.php/2006/08/27/ai-coder-due-diligence-why-im-changing-plans/</guid>
		<description><![CDATA[As much as I&#8217;m loathe to admit it, I&#8217;ve bitten off a bit more than I think I can chew with the AI-Coder project.
Why the sudden change of heart?
I&#8217;ve been doing my homework.
Over the last three weeks, I&#8217;ve given a lot of consideration to the four high-level design goals that I outlined a few days [...]]]></description>
			<content:encoded><![CDATA[<p>As much as I&#8217;m loathe to admit it, I&#8217;ve bitten off a bit more than I think I can chew with the AI-Coder project.</p>
<p>Why the sudden change of heart?</p>
<p>I&#8217;ve been doing my homework.</p>
<p><span id="more-72"></span>Over the last three weeks, I&#8217;ve given a lot of consideration to the four high-level design goals that I outlined <a href="http://benjismith.net/index.php/2006/08/22/ai-coder-high-level-design/">a few days ago</a> (fun, security, speed, and legality). I&#8217;ve written some sample code, I&#8217;ve buried my head in a small stack of technical books, and I&#8217;ve read through pages and pages of federal and state laws.</p>
<p>As a result, I&#8217;ve identified some major obstacles. On their own, each of the obstacles presents a barrier I could probably overcome. But combined, the accumulated weight of these obstacles is more than I&#8217;m willing to tackle for this project.</p>
<p>Needless to say, I&#8217;m pretty disappointed.</p>
<p>Anyhow, to give you an idea of the challenges inherent to this project, I&#8217;ll walk you through my thought process as I considered the technical and business implications of making the AI-Coder project fun, secure, fast, and legal.</p>
<p><strong>Sounds Like Fun!!!</strong></p>
<p>There are plenty of programmers who write code in their spare time, for fun or self-betterment (as evidenced by more than 120,000 projects and more than 1.3 million registered users at <a href="http://sf.net">SourceForge</a>), so I&#8217;ve never been too concerned when people have asked me how I&#8217;d find people willing to write code in their spare time. Hobby programming is already remarkably common. And hobby programming with the potential to win cash prizes is almost a no-brainer.</p>
<p>But the one detail that always nagged at me was this: How many programmers are really passionate about chess strategy?</p>
<p>Sure, coders are more likely to play board games than your average citizen, but focusing on super-geek games like chess, backgammon, go, or othello would be too limiting. To develop a large, broad, and enthusiastic community, I&#8217;d need to include a game with more broad-based appeal. Something cool. The kind of game that could even convince non-programmers to start slinging some code.</p>
<p>So I decided to include Texas Hold&#8217;Em (or some other poker variant) in the AI-Coder platform. (As a bonus, I&#8217;ve already written thousands of lines of poker AI code myself over the last year.)</p>
<p>In my opinion, the availability of cash-money pokerbot tables would be a key ingredient to the long-term success of the AI-Coder project. Although I&#8217;d offer cash tournaments for plenty of other turn-based strategy games, I think poker would be the real money-maker.</p>
<p><strong>Dealing from the Bottom of the Deck</strong></p>
<p>Even without offering poker-style games, I&#8217;d have to think about cheaters. What are all the possible ways people could exploit the system to gain an unfair advantage?</p>
<p>What if one player could sneak a peek at another player&#8217;s cards?</p>
<p>What if a player could modify the state of the table, stacking his own hand with better cards, or sabotaging the hands of his opponents?</p>
<p>What if, in a game with more than two players, two or more players could secretly communicate with one other, conspiring against the solo players at the table? What kind of software architecture can prevent two bots from colluding?</p>
<p>What if, at the moment preceding a catastrophic loss, the losing player can crash the system by blowing the stack, eating through the heap, or overflowing a buffer? How can the software prevent (or at least detect and punish) any bot who sabotages the game to avoid losing?</p>
<p>This is where I thought the .NET platform would provide a straightforward solution. With my experience writing code for a managed environment (the JVM), I assumed that I could get up to speed on .NET security without too much trouble.</p>
<p>My technical experimentation over the last few weeks has proven that assumption wrong.</p>
<p>Microsoft has actually given a lot of thought to the security architecture in the .NET framework with a concept they call <em>AppDomains</em>. All managed code executed by the CLR is first loaded into a privilege-managed <em>AppDomain</em> sandbox. Code executing in one <em>AppDomain</em> is prevented from accessing the objects allocated in another <em>AppDomain</em> unless the objects are deliberately marshalled (using dynamic proxies or serialization) across the sandbox boundaries. In addition, each <em>AppDomain</em> can have its resource-access privileges completely locked down, preventing certain assemblies from accessing the file-system or connecting to a remote host on the network.</p>
<p>By loading each bot into its own <em>AppDomain</em>, I could use those isolation mechanisms to prevent quite a few exploits. A rogue bot would be unable to sabotage the game-state or peek at the hidden cards in his opponents&#8217; hands or collude with another conspiring player.</p>
<p>But, unfortunately, all <em>AppDomains</em> in the parent process still allocate their objects on the same global heap, so it would be impossible to enforce memory restrictions on a player-by-player basis, leaving the door wide open for a player to deliberately over-allocate objects, throwing an <em>OutOfMemoryException</em> and corrupting the state of the current game.</p>
<p>How could I monitor allocations to fairly distribute resources among all the active players?</p>
<p>This is where it gets really tricky.</p>
<p>By implementing the game controller as an unmanaged C++ application, I could create an instance of the CLR as an <em>ICLRRuntimeHost</em> COM object. By instantiating that COM object using a custom implementation of the <em>ICLRPolicyManager</em> interface, I could set my own escalation policy for trapping (and recovering from) <em>OutOfMemoryExceptions</em>, using a custom <em>IHostMemoryManager</em> to keep careful records of allocations, deallocations, and GC compactions. I could catch <em>OutOfMemoryExceptions</em> as they occur, causing the offending player to automatically forfeit his hand.</p>
<p>That&#8217;s pretty impressive stuff, but it <strong><em>still</em></strong> wouldn&#8217;t really solve the problem. What if, at a ten-player table, Player#1 filled 90% of the available memory with his objects? By the time Player#10 got his turn, he might throw an <em>OutOfMemoryException</em> by allocating only a fraction of the memory unfairly claimed by the first player. Rather than punishing the player who actually uses the last byte of memory, I should punish the player who allocates more than his fair share.</p>
<p>As far as I can tell, the only way to accomplish that granularity of memory management is to run each player&#8217;s code in its own separate process, using COM interop for inter-process communication. At that point, the conveniences of the .NET framework fall mostly by the wayside, and I&#8217;m left implementing a whole lot of unmanaged C++ code.</p>
<p>I was prepared to write a thin C++ management layer as the controller for this architecture, but this is more than I bargained for. With only a little C++ experience, and no exposure to COM at all, I&#8217;m reluctant to pursue this route.</p>
<p><strong>The Need for Speed (or, at least, Non-Slowness)</strong></p>
<p>The idea of having third-party code executing on my server is not appealing, even if that code was confined to execute within a secure sandbox. AI code, often written by novices, would probably chew through a server&#8217;s CPU cycles pretty quickly.</p>
<p>I&#8217;ve considered building speed-incentives into the game rules. Maybe the winner of a particular game would forfeit a percentage of his winnings if his code was too slow. Maybe the winner of a Hold&#8217;Em hand would be determined by some combination of hand strength and execution speed.</p>
<p>But what if a GC collection was triggered in the middle of someone&#8217;s algorithm, to clean up for the allocations of the previous player? Without process (and heap) isolation, it would be very hard to come up with a fair mechanism for measuring execution time of any given player&#8217;s code.</p>
<p>A few people have suggested to me that I should just host the game server, allowing people to connect their algorithmic code via a SOAP interface, or some other remote API. But then it&#8217;s impossible to guarantee the integrity of the game. What if someone&#8217;s &#8220;AI&#8221; code just consists of a GUI where a human can compete against all the other machines (in many cases, humans are <strong><em>much</em></strong> better than machines at playing these games). And what if a few players started teaming with one another, colluding against the other players at the table. The only way to prevent those kinds of exploits is to execute the code in a known secure sandbox.</p>
<p>But what would it cost to fully host an ongoing competition like I&#8217;ve envisioned with the AI-Coder project?</p>
<p>In my experience writing poker AI code, a straightforward decision (folding a hand with a deuce and a three, off-suite) can be made in a few milliseconds. But more difficult decisions (like determining whether to fold a straight when an opponent might be bluffing a flush) can take two or three seconds to run through all of the probabilistic calculations, stochastically modelling the opponent&#8217;s likely hand, based on his observed play-style. (The exact sequence of checks, calls, and raises tells you a lot about what cards the player is actually holding.)</p>
<p>Realistically, with novices writing code, I&#8217;d expect an average ten-seat table to run at real-life poker-table speeds: about one full hand per minute. Maybe two. So a single server would be able to handle between ten and twenty simultaneous users.</p>
<p>In order to be profitable, I&#8217;d need to host a large cluster of powerful servers. Although I could start off small with a closed beta, increasing the hardware along with market demand and using an invitation system to control the flow of new members, it would definitely be a challenge to grow a scalable architecture.</p>
<p><strong>I Don&#8217;t Want to Go to Prison</strong></p>
<p>And of course, I&#8217;ve spent many many hours conducting legal research, trying to identify the potential regulatory hurdles I&#8217;d encounter with a business like this.</p>
<p>It&#8217;s not pretty.</p>
<p>Most of the laws (at the federal and state level) would consider the AI-Coder business model to be &#8220;gambling&#8221;, as long as the games involved an &#8220;element of chance&#8221;. Even if the game outcome is predominantly affected by the skill level of the participants (as would surely be the case with an AI programming contest), the influence of that &#8220;element of chance&#8221; would make the contest subject to gambling laws.</p>
<p>In <a href="http://caselaw.lp.findlaw.com/casecode/uscodes/18/parts/i/chapters/50/sections/section_1081.html">Title 18, Chapter 50, Section 1081</a> of the US Code, a &#8220;gambling establishment&#8221; is defined as:</p>
<div style="margin-left:20px;font-style:italic;">any common gaming or gambling establishment operated for the purpose of gaming or gambling, including accepting, recording, or registering bets, or carrying on a policy game or any other lottery, or playing any game of chance, for money or other thing of value.</div>
<p>The penalty for operating a gambling establishment is detailed in <a href="http://caselaw.lp.findlaw.com/casecode/uscodes/18/parts/i/chapters/95/sections/section_1955.html">Title 18, Chapter 95, Section 1955</a>, to whit:</p>
<div style="margin-left:20px;font-style:italic;">Whoever conducts, finances, manages, supervises, directs, or owns all or part of an illegal gambling business shall be fined under this title or imprisoned not more than five years, or both.</div>
<p>Ouch.</p>
<p>And, as if that wasn&#8217;t bad enough, I&#8217;d like to refer you to <a href="http://caselaw.lp.findlaw.com/casecode/uscodes/18/parts/i/chapters/96/sections/section_1961.html">Title 18, Chapter 96, Section 1961</a> and <a href="http://caselaw.lp.findlaw.com/casecode/uscodes/18/parts/i/chapters/96/sections/section_1962.html">Section 1962</a>:</p>
<div style="margin-left:20px;font-style:italic;">&#8220;Racketeering activity&#8221; means (A) any act or threat involving murder, kidnapping, gambling, arson, robbery, bribery, extortion, dealing in obscene matter, or dealing in a controlled substance &#8230; which is chargeable under State law and punishable by imprisonment for more than one year;</p>
<p>It shall be unlawful for any person who has received any income derived &#8230; from a pattern of racketeering activity &#8230; to use or invest &#8230; any part of such income &#8230; in interstate or foreign commerce.</p></div>
<p>Racketeering, huh?</p>
<p>And you thought writing AI code was difficult? I bet you&#8217;ve never tried writing a thousand lines of code with a pair of brass knuckles on your fingers. Now that&#8217;s what I call hard-core programming!! And how would you like to try on a pair of cement shoes, my friend?</p>
<p>Of course, the Feds will be very interested in all the internet traffic going to and from my servers. According to <a href="http://caselaw.lp.findlaw.com/casecode/uscodes/18/parts/i/chapters/119/sections/section_2516.html">Title 18, Chapter 119, Section 2516</a>:</p>
<div style="margin-left:20px;font-style:italic;">The Attorney General &#8230; may grant &#8230; an order authorizing or approving the interception of wire or oral communications by the Federal Bureau of Investigation, &#8230; when such interception may provide or has provided evidence of &#8230; any offense which is punishable under the [gambling and racketeering] sections of this title.</div>
<p>Of course, all of this relies on the notion that the AI-Coder platform could be classified as &#8220;gambling&#8221;, a classification that rests largely on the &#8220;element of chance&#8221; inherent in some of the games.</p>
<p>But what if I could eliminate the element of chance from the games? Obviously, games like chess and backgammon are already purely games of skill. But would it be possible to slightly re-write the rules of Texas Hold&#8217;Em to eliminate the element of chance?</p>
<p>I&#8217;ve developed an ingenious idea that I call &#8220;collaborative shuffling&#8221;, wherein one player gets to arrange the deck in whatever order he likes. He can stack the deck to his own advantage. But then he passes the deck to a second player, who re-arranges the cards again. The only restriction is that the second player is unable to see the values of the cards, so he can&#8217;t know how the first player arranged them.</p>
<p>The collaborative shuffling process would eliminate randomness from the game, while still providing a mechanism for scattering cards throughout the deck. As a classic example of a <a href="http://en.wikipedia.org/wiki/Prisoners_Dilemma">Prisoner&#8217;s Dilemma</a>, the shuffle would become a key strategic element of the game.</p>
<p>But, even with the &#8220;element of chance&#8221; gone, would the AI-Coder project be free from legal trouble?</p>
<p>I don&#8217;t think so.</p>
<p>Even though it might be possible for a non-random poker variant to pass muster against the federal gambling laws, the state statues are much more varied in their construction.</p>
<p>In Florida, for example, the state Criminal Code (in <a href="http://www.leg.state.fl.us/statutes/index.cfm?mode=View%20Statutes&#038;SubMenu=1&#038;App_mode=Display_Statute&#038;URL=CH0849/Sec14.HTM">Title XLVI, Chapter 849, Section 14</a>) specifically prohibits wagering &#8220;any money or other thing of value upon the result of any trial or contest of skill, speed or power or endurance of human or beast&#8221;, making violations of the prohibition punishable as a second-degree misdemeanor.</p>
<p>Arizona doesn&#8217;t create a special prohibition against games of skill, but (in <a href="http://www.azleg.gov/ars/13/03305.htm">Title 13, Section 3305</a>) it places a blanket prohibition on any wagers concerning &#8220;the results of a race, sporting event, contest or other game of skill or chance&#8221;. A violation of this statute is a class 1 misdemeanor.</p>
<p>The Arkansas statutes (in <a href="http://www.arkleg.state.ar.us/NXT/gateway.dll/ARCode/title03839.htm/subtitle04515.htm/chapter04724.htm/section04737.htm?f=templates$fn=document-frame.htm$3.0#JD_5-66-113">Title 5, Chapter 66, Section 113</a>) prohibit &#8220;betting any money or any valuable thing on any game of hazard or skill&#8221;. The fine is $25.</p>
<p>Similar prohibitions against wagering on skill-based games exist in at least five or six other states.</p>
<p>So, I could probably operate legally, if I eliminated the element of chance from all of my games, and if I excluded residents of non-skill-gaming states from participating.</p>
<p>But the law is a slippery thing. I&#8217;d be stupid not to retain expert legal counsel, since I&#8217;m not personally capable of keeping up-to-date on the gambling laws for 50 different states, as well as the federal laws (which, by the way, automatically kick-in whenever you&#8217;re charged with a violation of a state gambling law).</p>
<p><strong>So, that&#8217;s it then?</strong></p>
<p>If it were just the legal issues, I might have the stomach to go for it.</p>
<p>I&#8217;ve found a number of attorneys who specialize in gambling laws, and I could hire them to do some research for me, giving me a set of parameters within which I could operate legally.</p>
<p>I don&#8217;t have a lot of start-up capital, but getting some sound legal advice would be a good use of that cash.</p>
<p>And if it were just a matter of learning COM interop programming with C++, I&#8217;d be up to the task. After all, I&#8217;ve learned a half-dozen different programming languages over the last few years, and I could certainly learn a few more.</p>
<p>If my only concern was providing a fun coding experience, or setting up a fast implementation, I&#8217;d take the plunge.</p>
<p>But there are too many obstacles here, and I&#8217;m afraid I&#8217;d injure my probability of succeeding by picking a project so fraught with peril.</p>
<p>Instead, I&#8217;m going back to the drawing board.</p>
<p>I&#8217;m going to re-visit my previous list of ideas and decide on a more suitable business plan. Unfortunately, despite my enthusiasm for the AI-Coder idea, this is the wrong project for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://benjismith.net/index.php/2006/08/27/ai-coder-due-diligence-why-im-changing-plans/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>AI-Coder: High Level Design</title>
		<link>http://benjismith.net/index.php/2006/08/22/ai-coder-high-level-design/</link>
		<comments>http://benjismith.net/index.php/2006/08/22/ai-coder-high-level-design/#comments</comments>
		<pubDate>Wed, 23 Aug 2006 04:37:04 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[aicoder]]></category>
		<category><![CDATA[biz]]></category>

		<guid isPermaLink="false">http://benjismith.net/index.php/2006/08/22/ai-coder-high-level-design/</guid>
		<description><![CDATA[As an artist, I&#8217;ve often started a new drawing or painting right in the medium, dipping a paintbrush into my palette and splashing something preliminary onto the canvas. Some of my favorite drawings have started with pen on paper, having no idea what the hell I was working toward, but just enjoying the process.
In many [...]]]></description>
			<content:encoded><![CDATA[<p>As an artist, I&#8217;ve often started a new drawing or painting right in the medium, dipping a paintbrush into my palette and splashing something preliminary onto the canvas. Some of my favorite drawings have started with pen on paper, having no idea what the hell I was working toward, but just enjoying the process.</p>
<p>In many cases, those first few haphazard strokes set a tone and texture that guides the rest of the creative process.</p>
<p>While the paint is still wet, and the figure is still mostly unformed, I&#8217;ll step back from the canvas and look at what I&#8217;ve done so far, asking myself &#8220;What is this? I like what I&#8217;ve got so far, but how can I turn it into something worthwhile?&#8221;</p>
<p>The decisions made during that post-improvisational stage often determine whether the painting gets hung on my wall or tossed into the dumpster.</p>
<p>I feel the same way right now, at the beginning of the AI-Coder project.</p>
<p><span id="more-71"></span>Although I&#8217;ve spent the last year or so experimenting with AI techniques and building my own game-playing software, those tasks really just give me a preliminary sketch for the overall design of the AI-Coder platform. Since I&#8217;m developing this software for widespread consumption, the design constraints and priorities are entirely different than if I was building something just for myself. And of course, as is always the case, some of the design criteria conflict with one another.</p>
<p>At this point, I&#8217;ve identified four overarching principles that should guide the whole development process. In no particular order, it&#8217;s essential that the software is:</p>
<ul>
<li>Fun</li>
<li>Secure</li>
<li>Fast</li>
<li>Legal</li>
</ul>
<p>All four of those constraints will make a big difference on the software design, and all four of them are critical to the monetary success of the project. If I fail to meet any of those criteria, I can expect big problems.</p>
<p><strong>Ahem&#8230;What?</strong></p>
<p>My parents are avid readers of this blog, and when they read that I&#8217;d chosen the AI-Coder project, they were completely perplexed. Not necessarily because I had chosen this  project above all the others. Rather, they were perplexed about what this project even <strong><em>is</em></strong>.</p>
<p>I can hardly blame them. So far, I haven&#8217;t figured out how to explain the gist of the AI-Coder project without going into a twenty minute explanation.</p>
<p>So before I dive into my four design criteria, I&#8217;m going to step back for a second and try to write a 30-second elevator pitch for AI-Coder:</p>
<div style="margin:20px;"><em><strong>AI-Coder</strong> is an ongoing contest, where smart programmers can win cash prizes by writing better AI code than their opponents, in strategy games like chess, othello, and poker.</em></div>
<p>I&#8217;m sure that the definition of the AI-Coder project will fluctuate as the software (and the business develops), but that&#8217;s the core idea. And hopefully, a description like that clearly expresses what I&#8217;m trying to accomplish without needing any further explanation.</p>
<p>Now that I&#8217;ve gotten the elevator pitch pretty solidified, I can focus on my four central design criteria.</p>
<p>Over the next week, I&#8217;ll post four follow-up articles, discussing how the elements of fun, security, speed, and legality will affect the design decisions of the AI-Coder platform.</p>
<p>And, by the end of that process, I hope to develop a clear concept (from both a business and a technical perspective) of how my preliminary sketches will develop into a fully-formed picture, one that I&#8217;ll be proud to put on public display.</p>
]]></content:encoded>
			<wfw:commentRss>http://benjismith.net/index.php/2006/08/22/ai-coder-high-level-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

