Wednesday, September 21, 2011

Dynamic AI

Well, it's been a while. No questions there.

So, what have I been up to while my laptop was out of commission? Reading all day, being bored, what have you? Well, to a certain extent, yes, I must admit. But not completely.

There are currently a few different levels to artificial intelligence.

The first, and simplest, is "Bug AI" (my own terminology). Basically what this means is that the unit controlled by this form of intelligence is able to respond directly to certain conditions. In other words, if there is a higher concentration of light in one direction than in another, move towards the light. If you have fewer than X hitpoints, retreat. If an enemy is in sight, fire. I wasn't kidding when I said it was simple.

The second level is what could be termed "Animal AI" (again, my own terminology). This AI differs from Bug AI in that the unit is able to remember certain areas and outcomes, and the processes controlling it are more complex. Instead of "If X then X", you get "If X and X and X then X to X", which could be "If there is an enemy in sight and the nearest cover is less than ten feet away and allied cover is less than 3 then run to the nearest cover". It's quite a bit more complex than Bug AI, and used in the majority of video games.

I don't know if anyone has come up with it before, but I have created what I call "Dynamic AI". One day while out on a walk through the woods nearby (I really enjoy taking long walks away from people), I pondered this question: What is the difference between human intelligence and traditional AI? After some thought, I decided this: People base their actions not on true/false (binary) assumptions or facts, but on a more analog/digital type of scale. For example, we don't look outside and say, "Hey, it's sunny outside, I think I'll take a walk". Instead, we weigh a number of things in our heads. After all, it's not always 100% sunny or 100% overcast. It would be more like, "Hmm... It's a little cloudy, but the people here are annoying me somewhat and I feel like stretching my legs would do me some good. It would give me some time to think about things, too. After all, it looks like it might clear up some." And you go outside. The equation might include 70% clouds (steady decrease), 75% localized annoyance (rapid increase), 40% need for exercise (slight increase), 50% need to think clearly (stable). Instead of binary, you're now working with analog. After all, human thought is without a doubt analog. If we didn't take the increases and decreases into account, it might be termed digital.

With this in mind, I began working on a program that would demonstrate this theory of mine. I made a very simple game: four teams with four players each, firing weapons a fixed distance and gaining points for kills/damage dealt. I added a twist and made it so that if they didn't fire immediately, their weapon would fire a more powerful bolt. My goal was not only to have the AI fight efficiently, but to have it teach itself.

This of course is an area of controversy. People assume that if a robot is capable of learning a task through trial and error, then the robot will eventually become powerful enough to outdo humans in everything - and of course enslave or destroy mankind! (And here I roll my eyes.) Excellent examples of this line of thinking are movies such as Terminator and The Matrix. In each, the human race battles robots of its own creation, struggling to stay alive and wishing they'd never thunk up the doggone things. Of course, this is an impossibility. Robots (or the programs running on them, to be more precise) might be able to learn tasks they're pre-programmed for, but they can't recode themselves to introduce an entirely new set of variables into their programming. This means that if they come across something they've never seen or heard of before, they won't know what to do with it, and therefore won't be able to utilize it, or even react to it. How humans do this is beyond me, but it was part of the reason I desired to become a neurologist for quite a while.

In any case, I created the program. Above is a screenshot of how it looks while it's running. My focus on graphics was minimal.

This is how it works:
The AI for each team consists of 88 variables with ranges set from minimum to maximum. These variables relate to things such as how close the nearest enemy is, how close the average enemy is, how many weapons are within X distance, how many teammates the AI unit has left, and so on. With both a minimum and a maximum, this doubles the possible choices.
In addition, each variable end gives a fixed +/- value to a set of 24 different actions (including turn towards nearest enemy, fire, don't fire, move forwards, move backwards, etc.). This results in 4224 different combinations. The stronger the additive values are for a particular action, the more weight that action has. Example of the AI save file:


8,-30,18,-3,12,8,11,5,-1,14,-3,-20,-4,-1,-8,-22,-17,-5,29,17,-19,-11,-17,-14,
-8,22,-4,4,11,-7,-25,-8,-11,-14,11,16,27,6,10,5,23,-9,7,-3,18,27,14,-6,
7,-1,-5,15,-6,-14,-2,-4,-1,-6,11,16,14,-32,11,-3,25,20,28,34,6,-13,12,-6,
-1,-7,33,3,15,-14,1,13,6,11,-14,1,-16,-7,-4,11,16,9,-4,-7,4,-9,16,-9,


When the program begins running after the variable file is reset, each AI team is very, very slightly tweaked so that they do things differently. In the early stages of their self-development, they know absolutely nothing about how to play, and may do ridiculous things like back up constantly or fire at their own allies, or try to flee the map. After either 2 minutes or after only one team is left standing, the winning AI is used as the new base, and it is again tweaked for three of the teams. Team one always uses the best AI generated. In doing this, in 50 rounds you have an incredibly good AI, with all 4224 variables set to near-perfection.

It wasn't long before I began to encounter flaws - not in my own programming code, but in the game I had developed! The AI was discovering loopholes and problems in the game balance and exploiting them to its own advantage:

  • They would tend to go off the edge of the map to avoid getting hit, as I made getting hit a penalty. I tried to fix this by making them receive constant damage while on the edge. The AI exploited this again and almost taunted me by stopping just a pixel away from where they would begin to receive damage.
  • The charging weapons was a good idea, but they exploited this as well. The tactic became to concentrate all fully-charged firepower on a single enemy, which would destroy that enemy instantly. It became a game of "Let's see who can charge their weapons the fastest".
  • I originally had a large point bonus awarded to teams every time they killed an enemy. This resulted in them avoiding getting shot, until one of their enemies was very close to death, and all other three teams would blindly rush that enemy at once.
  • Originally, the weapons had a much farther range. This resulted in the bots never approaching each other. They would move a short distance, sit, and begin turning and firing as if they were turrets, moving only to avoid enemy weapons.
  • After fixing the above two mistakes, they created a new tactic: move out, deal a tiny bit of damage, and then flee to sit right next to the edge of the map to avoid taking any more damage. This was due largely to the penalty I placed on taking damage, so I had to remove this as well.

There's no question! Dynamic AI works. Exceedingly well. I doubt a real human player could learn so quickly, and I'm sure they couldn't exploit the game balance issues quite as well. The problems:
  1. They make me look bad. My games look horrible when they play.
  2. I can only get the AI to run a full update once a second. Once a second, they scan through all the variables and decide the best course of action. This makes the game freeze for 0.5 seconds. Dynamic AI is a very CPU-consuming thing.
  3. It takes a bit long for them to program themselves. It might take three full hours to get them up to their full potential.
  4. Many of the variables are completely ignored in the end, such as the "Fire at the nearest ally" variables.

This is still the future of AI, I'm convinced. I'm also convinced I couldn't have come up with it myself. I'm creative, but how could I have made a discovery like this? Seriously. I'm sure someone else has already come up with it. But I'm just as sure it's the future of AI. Maybe not in games, but in advanced robotics, without a doubt. Real-world applications. After all... My AI teaches itself, and can beat even their creator at his own game.

No comments:

Post a Comment