2000 
IMPLEMENTING ARTIFICIAL INTELLIGENCE
By Jonathan Nix

Fuzzy Logic

Neural Networks

Genetic Algorithms

Sample Code

Certainly the computer has been the greatest benefactor to games of all the last thousand years. Even in the last couple decades, we’ve seen the greatest evolution take place. Just as two-dimensional games were saturating the market, the advent of 3D-technology really made the concept of a game world come to the mainstream. But 3D itself is now regulated by what hardware vendors are willing to support, and our dreams far outweigh what’s possible with today’s technology. It’s the same way with global-networking, which is only now emerging in terms of the performance necessary for games. When it matures, we expect it to have an even greater impact on the industry than 3D did, and of course the biggest impacts are those that have yet taken place.

Major advances in the early part of the next century will certainly include player interactivity and artificial intelligence. The number of connected users will invariably increase, and the level of scenarios required to keep them entertained will have to cater to the group rather than the individual. A game’s story line must no longer consist of one primary character. It must consist of many, all playing an important role in the conflict resolution of their virtual world. This puts a heavy demand on the artificial intelligence required to operate non-player characters. It doesn’t make sense to place a player in a photo-realistic environment with a script-driven barkeep if it looks and behaves like everything else. Of course creatures must no longer be written so as to react based on a single player, and the realness of their behavior and uniqueness of their tactics will be a more important feature than it has been in the past.

In addition to improving upon what once was, advances in artificial intelligence would likely open the industry up to a wider variety of games and give developers a competitive differentiation in a market saturated with common technology. The popular games of tomorrow may very well include features like real-time interrogation of suspects, dynamic movement, and richness of behavior. We may also see an increased interaction between creatures and their environment, no longer separated from the consequence of their own actions.

These and other features are only possible with advances in artificial intelligence. It’s the same science that enables optical character recognition (OCR) and voice to text dictation to work. It’s also the broadest and most obscure science imaginable. There’s no boundary that says intelligence must be logical, biological, mathematical, mechanical, or spiritual. It leaves open to implementation any possible method that may achieve results. While AI is by no means simple to master, there are some important concepts to consider: fuzzy logic, neural networks, genetic algorithms, and combinations thereof.

Fuzzy Logic

In the 1960s, Dr. Lotfi Zadeh of the University of California at Berkeley pioneered the use of fuzzy logic for the purpose of linguistic analysis. Since then it has found widespread use in control systems, expert systems, and neural networks. It’s an extension of the mathematical system invented by George Boole in the mid 1800s. Nearly a century after Boole, scientists found that Boolean logic would allow an electrical system to count, add, multiply, and perform logical operations – all the necessary functions of a computer. It’s still the method that modern computers use today, and will continue to use for the first great part of this century. Fuzzy logic extends those concepts by representing true and false as a range [1,0] or [1,-1]. So the fuzzy degree of truth tells you not only whether a criteria is true or not, but how true it is.

To illustrate this, suppose you’re holding a red ball that a camera must keep centered in its view at all times. Without getting mathematical, it’s obvious that the camera will rotate depending on the position of the ball. It might also zoom depending on the ball’s apparent size. Boolean logic alone tells the camera whether or not to move. Fuzzy logic can tell the camera by how much, and simply by answering whether the ball is centered vertically, horizontally, or spatially. If the ball is far to the right, then the degree of truth to the statement is the ball to the right? will be quite large, and cause the camera to converge upon its goal much quicker than the Boolean counterpart.

Fuzzy logic of course has a mathematical foundation and can also be represented in Boolean terms. Fuzzy logical operations that match their Boolean counterparts on the set {0,1} can also be derived quite easily, see figure.

As a game character, a fuzzy intelligent system may have several goals that it must achieve. By assigning each of them a fuzzy priority, you can control the behavior of the system. Some goals might remain in a low-priority state until an event occurs, while others might remain in a high-priority state until satisfied. Underneath each of them may be sub-goals, until eventually there are operations that should result in satisfaction. Suppose that some include follow-food’s-scent, dodge-obstacles, and run-from-danger. The creature can give power to its behavior based on the fuzzy value associated with a goal, so if a predator approaches, the character can work at satisfying run-from-danger, but it won’t forget about dodging obstacles or following the scent of food. So fuzzy logic applied to a state-based behavior system can blend the concept of state membership. As it distances itself from the predator, the character’s run-from-danger goal is gradually satisfied, until that goal has a negligible effect on behavior. So if the predator was situated between the character and a food-source, it’s likely that the character would run around the predator in order to get its food. If the fuzzy goal priority of follow-food’s-scent increased as the character approached food, perhaps influenced by hunger, you might see it risk grabbing the food despite an approaching predator. Nevertheless, these behaviors are a side effect of using a fuzzy logic implementation.

Suppose a creature has a find-food of 0.75, and a run-from-danger of 0.112. Any non-zero goals can activate a goal-satisfaction function, and in this case movement satisfies the goal so the functions would each calculate a movement vector. By multiplying each vector by its current priority (0.75, and 0.112), and then adding them up, you get the actual movement that’ll satisfy all of the goals. This gives your characters a level of freedom and a richness of behavior that games haven’t seen.

Traditionally a character’s path has been calculated based on its location, destination, and obstacles in between. It implies a certain omnipotence that defeats realism, and reduces complexity for performance purposes. With 3D acceleration though, there are a lot of resources open for fuzzy logic to take advantage of. A character’s goals as activated by events can be defined in a level-editor, but the engine’s code has to have the necessary tools and ability to figure out how to achieve those goals. By breaking goals down into sub-goals with simple solutions, the complex behavior is possible without hard-coding the engine for each scenario.

A character participating in combat may have a series of high-level tactical goals that would certainly include find-ammo,  find-health, and seek-shelter. As ammo or health decrease, the find-ammo and find-health priorities increase, causing the character’s behavior to change. If there are two nearby shelters, the goals will guide the character to choose whichever one it needs more, ammo or health. The intelligence is a consequence of prioritizing goals and occupying multiple behavior states at once.

Hopefully that’s a good introduction and gives you a few ideas. It should be enough at least to prepare you for the next section, which deals with neurofuzzy systems.

Neural Networks           

It's common knowledge that a computer is a finite state machine: Its next state is a function of its current state and all external input. Regardless of whether the machine modifies its own behavior, starting from an initial state it will always progress in a calculable pattern based on input and current state. Hence forth from any initial state all future states are implied. (Input is also calculable but that is a different course of thought.)

Is the process any different for an insect?  Some may in fact convert the energy of stimulus directly into the energy of response using food to amplify the signal. Consequently brains of such insects are hard coded with instinct. They will not respond differently under the same set of stimuli and brain state. Yet isn't their next state calculable from their stimulus and current state anyway?  If an electron had a second chance could it choose to go a different yet equally likely path?

Particularly a neural network coupled with the appropriate training algorithm simulated in a computer would have a greater potential of intelligence than the primitive insect as described above. Could that neural network be considered alive?  Stimulus isn't reality, it's perceived, right?  If an insect were fed stimulus from a computer simulated environment, and its response used to modify the environment, it's unlikely that it could tell the difference. So what if it's not a real insect?

Since two programs with identical behavior, one running mechanically, one running electronically, are considered the same program, life too might be independent of implementation details. Indeed modern artificial neural networks are capable of learning, generalizing, and adapting. So to some extent, it’s possible that computer’s can simulate creatures that are even more lifelike than those that once existed.

An artificial neural network is simply a datastructure with several variables that play a part in determining the behavior of the system. By changing the variables, you change behavior. The trick is to gradually tune behavior and verify that it’s making positive progress toward the satisfaction of a goal. A very simple description of this is that of a perceptron network, where each weight variable is the degree to which a stimulus affects a response. It’s possible to hardcode behavior into these weights, train the network to exhibit desired behavior, or train it during runtime when a response causes pain.

A perceptron is an example of a feed forward network. The inputs can be from environmental stimulus, or from an internal brain state. Each input neuron is linked to each output neuron or to neurons in what’s called a hidden layer. If hidden layer neurons are used, then input neurons link to them and the hidden neurons link to the output neurons. To calculate the response, the numerical values from input are multiplied by the connection weight of each link, and hidden or output neurons will sum the values of all their inputs. Then each receiving neuron has its own value that’s passed through a threshold function to restrict its range if that’s required. This new value can be submitted to another network or used to determine a creature’s response. If a threshold function restricts a value to the range [0,1] or [-1,1], then you can treat it as fuzzy degree of truth. If this isn’t crystal clear, you can study the CBackprop or CFuzzyLayer sample code.

Most neural models require a goal response in order to calculate the weight changes that will result in more desirable behavior. By operating over a set of input patterns, and training the network’s behavior, scientists have discovered that neural networks are able to recognize blurry or incomplete input patterns, and exhibit brain-like memory that’s distributed across the network instead of residing at a centralized location as once thought. Networks like this are frequently pre-trained with basic behavior, and then modified during runtime by using pain as a measurement of error in response. A very common and powerful method is the gradient decent method of error back propagation, and is demonstrated in the sample application using a multi-layered neural network of 32 neurons. Using this method, a sample creature could train itself so as to discourage responses that have resulted in pain when issued during a particular stimulus. After a while, such a creature will learn to avoid responses which have resulted in pain, and consequently learn to not bump into walls or get too close to predators.

Another way to learn in real-time is to seek goal stimulus. A creature that needs food may desire for its smell of food to be increased. It can find the response that will bring food closer by modifying its behavior until the smell increases. This is accomplished by reinforcing a previous response if it has brought about a desirable change in future stimulus. Again, this would be possible with feed forward neural networks that train based on the error in response. In this case a response is in error if the creature’s future stimulus does not approach goal stimulus. The error in response (a set of numbers) then implies the error in the connection weights (behavior variables), and they can be changed so that next time this stimulus is applied to the creature (sensory input) the response will generate less of an error (as measured by the difference in actual and goal stimulus).

The goal stimulus can be calculated by another neural network, this time one that takes the emotional state of the creature into consideration. It’s possible for this new network to output a goal stimulus that desires an increased smell of food, by taking the creature’s hunger and other variables as input. Whenever attempting to train a network in real time, it’s necessary to determine how to measure the suitability of response, or the suitability of stimulus. These two concepts are crucial for producing a creature that can actually learn and consequently exhibit unexpected although sometimes undesirable behavior. 

Genetic Algorithms

As the name implies, genetic algorithms simulate the evolution of a gene pool. Unlike real-world creatures, the genes used in genetic algorithms aren’t restricted to defining a biological system. Each gene represents a variable passed to a suitability function that evaluates the gene sequence. So from a pool of random or calculated gene sequences, a top percentage can be extracted, mutated, and bred (mixed together), to form other gene sequences. Eventually the process converges upon a sequence that obtains a high suitability rating. So from a set of random points, for example, the suitability function might return a Boolean value for whether that point is on a sphere. The genes with false returns die off. The true returns breed together, and eventually a large population resides solely on a sphere.

The suitability function itself can be the environment, and the gene sequences can portray the behavior of a character – either the weights in a neural network or the properties of a fuzzy state based system. The creatures that survive in the environment pass on their genes, and certainly if a player’s trying to kill them then the ones who successfully escape death are bred. So genetic algorithms make it possible for creatures to adapt to the player’s unique style, thereby increasing in difficulty.

Here’s an example of how genetic algorithms can complement fuzzy logic. Suppose an event activates a creature’s run-from-danger goal, and it calculates the goal’s priority each frame. The calculation of priority can be a decreasing linear function, such that the priority will be highest if the creature is closest. Depending on the line’s slope, there will be some point where the predator is far enough away that the creature doesn’t have to worry. By setting the slope to a random value for the first population, the surviving population with apparently suitable slopes will be bred. The resulting slope for a creature will depend largely on the predator’s tactics. If a particular predator kills creatures who approach too close, then only those with a small slope will survive to pass on their genes. Similarly, only the predators with suitable variables will pass on their own genes. So you see the entire system can be complicated.

 

GIGnews is a publication of GIGnews.com, Inc.
"Get In the Game" is a registered trademark used with permission.

© 1
999- 2005 GIGnews.com, Inc.
Legal