{"id":4496,"date":"2009-08-18T09:00:09","date_gmt":"2009-08-18T13:00:09","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=4496"},"modified":"2009-08-20T15:39:44","modified_gmt":"2009-08-20T19:39:44","slug":"ai-follies-interesting-behavior","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=4496","title":{"rendered":"AI Follies: Interesting Behavior"},"content":{"rendered":"<p>Here are some thoughts on the goals of an AI programmer, which should have gone at the beginning of this series. Let me put them up now before we go any further.<\/p>\n<p>My first computer (which you can read about <a href=\"?p=213\">here<\/a>)  was the Tandy MC-10.  4k of memory. Less than a single megahertz of speed.  Once I got my programming legs under me, the first thing I tried to do was <del datetime=\"2009-08-16T09:31:49+00:00\">make a game<\/del> <em>clone Pac-Man<\/em>.  For reference, here is what the original looked like:<br \/>\n<!--more--><br \/>\n<table width='336'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><a href='http:\/\/en.wikipedia.org\/wiki\/Pac-Man'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/pac-man.jpg' class='insetimage' width='336' alt='wakka wakka wakka!' title='wakka wakka wakka!'\/><\/a><\/td><\/tr><tr><td class='insetcaption'>The original.<\/td><\/tr><\/table><\/p>\n<p>As a programmer, displaying something like that on the Tandy was about as likely as getting Crysis running on a 486.  If I&#8217;d wanted to display my ghosts with the same level of detail, they would have needed to take up most of the screen. On the MC-10 the screen was 40 characters wide by 20 lines tall.  It had a fixed character set.  Character codes 0 through 127 were standard ASCII.  128 through 255 were a bunch of stupid shapes that were, in theory, for doing graphics but which didn&#8217;t really look very interesting and were more hassle than they were worth.<\/p>\n<p>So my Pac-Maze ended up being made mostly of ASCII characters.  Periods for dots.  Asterisks for power pills.  A solid block of pixels for the walls. I&#8217;ll bet a <em>lot<\/em> of thirty and forty-somethings out there authored something very, very similar.  Here is a very close re-creation of what it looked like, according to my memories:<\/p>\n<p><table width='320'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/mc10_pacman.jpg' class='insetimage' width='320' alt='wakka wakka wakka!' title='wakka wakka wakka!'\/><\/td><\/tr><tr><td class='insetcaption'>The difference in maze layout is due to the fact that the original Pac-Maze can&#8217;t actually exist on a 40&#215;20 grid.  This was as close as I could come.<\/td><\/tr><\/table><\/p>\n<p>Well, that was the platonic ideal anyway.  In reality, the computer was hooked up to a television, and ended up looking more or less like this:<\/p>\n<p><table width='320'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/mc10_pacman_tv.jpg' class='insetimage' width='320' alt='wakka wakka wakka!' title='wakka wakka wakka!'\/><\/td><\/tr><\/table><\/p>\n<p>All those hours spent looking at my TV from sixteen inches away. Ouch. <\/p>\n<p>Anyway, this was my first attempt at AI pathfinding.  It ended in disaster, but I think it was an admirable effort for a junior-high school kid.  I can still remember the rules that the ghosts used:<\/p>\n<p>Each ghost can move in one of four directions. Note that &#8220;moving&#8221; here is not a smooth thing.  The ghosts did not slide smoothly around the board like they did in the arcade.  They could only occupy one of the given character positions. And so they blinked around, hopping from one dot to the next like pieces on a chess board.  A single turn would take a full second.  Imagine counting one Mississippi, two Mississippi, etc.  That&#8217;s how long it took poor Pac-man to jump from one dot to the next.  (The game ran much smoother without the dot-eating sound effect, but I couldn&#8217;t give that up!) <\/p>\n<p>Originally, they all ranked each direction the same.  (That is, they would consider moving up before they considered moving left.)  But this uniformity of rules led to uniformity of behavior.  If two ghosts ever got close to each other, they would keep making the same decisions and thus move together.  Eventually the whole gang would pile up and it was pretty much like running from one ghost for the rest of the board.<\/p>\n<p>If I wanted different behavior, I needed different rules for each ghost.  I made it so that each had their own order in which they &#8220;preferred&#8221; each of these directions.  So, ghost #1 might rank the four directions:<\/p>\n<p>1) Up<br \/>\n2) Down<br \/>\n3) Left<br \/>\n4) Right<\/p>\n<p>That is, if given a choice between going down or left, it will prefer down more than left, left more than right, etc. Another might prefer:<\/p>\n<p>1) Right<br \/>\n2) Left<br \/>\n3) Up<br \/>\n4) Down<\/p>\n<p>Two ghosts preferred vertical movement first, the other two preferred horizontal movement first.  The rules they used were:<\/p>\n<p>1) Can&#8217;t pass through walls.<br \/>\n2) You may NEVER reverse direction.<br \/>\n3) You MUST move.<br \/>\n4) Check your direction priority, and see if you can move TOWARDS the player without violating rules 1 through 3.<br \/>\n5) If you can&#8217;t move towards the player, just select the highest-rated direction that doesn&#8217;t violate rule 1 through 3.<\/p>\n<p>The upshot of these rules was that if you removed the player from the game entirely, the ghosts would fan out, each heading to a different corner.  They would then run in circles around the wall in that corner forever.  Two would go clockwise around their little wall, the other two would go un-clockwise. This was really cool.  Diverse behavior! I was so proud of myself.<\/p>\n<p>But if the player was in the game, the ghosts would fan out and envelop poor Pac-Man from all sides within seconds.  The game was essentially impossible. I discovered that &#8220;interesting&#8221; behavior was not the same thing as &#8220;efficient&#8221; behavior, and that &#8220;interesting&#8221; was way harder.<\/p>\n<p>My solution was to slow them down.  Ghosts only got to move half as often as the player, which meant you could escape their grasp.  It was still much harder than the arcade, and a lot less interesting.  You could stay alive if you stayed near the middle of the board where you had lots of options, but it was fatal to go after the dots in the corners because they would manage to cover all of your escape routes before you could get back out.  If I made them move one-third the speed of the player, it became too easy.  You could ditch them on one side of the board and go work on the other. They were a constant nuisance, but they weren&#8217;t a threat and they were always moving towards you.  (They sort of &#8220;camped&#8221; the middle, which made dots close to the center almost impossible to grab.  Not that &#8220;camping&#8221; was part of anyone&#8217;s gaming vocabulary yet.) I began to gain an appreciation for just how much thought had gone into the original game.  It was easy to make the game either effortless or impossible, and the designer managed to find the precise balance between the two, a narrow band of behaviors that could be labeled as &#8220;fun&#8221;.<\/p>\n<p>Sometimes AI is used to make the game seem less smart by making it miss more, ignore you at the right time, or as with my example above, chase you less efficiently.  Sometimes AI is used to make the game &#8220;smarter&#8221;.  So the goal of AI is not to simply increase intelligence. And it&#8217;s not to make the computer opponents more efficient.  The goal of AI is to make the computer an <em>interesting<\/em> opponent. <\/p>\n<p>It turns out that this is a complete pain in the ass.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are some thoughts on the goals of an AI programmer, which should have gone at the beginning of this series. Let me put them up now before we go any further. My first computer (which you can read about here) was the Tandy MC-10. 4k of memory. Less than a single megahertz of speed. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-4496","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/4496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4496"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/4496\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}