University of Texas at Austin CS 378 – Game Technology Don Fussell
CS 378: Computer Game Technology
AI – Fuzzy Logic and Neural NetsSpring 2012
Today
AIFuzzy LogicNeural Nets
Fuzzy Logic
Philosophical approachDecisions based on “degree of truth” Is not a method for reasoning under uncertainty – that’s probability
Crisp Facts – distinct boundariesFuzzy Facts – imprecise boundariesProbability - incomplete factsExample – Scout reporting an enemy
“Two tanks at grid NV 54“ (Crisp)“A few tanks at grid NV 54” (Fuzzy)“There might be 2 tanks at grid NV 54 (Probabilistic)
Apply to Computer Games
Can have different characteristics of playersStrength: strong, medium, weakAggressiveness: meek, medium, nastyIf meek and attacked, run away fastIf medium and attacked, run away slowlyIf nasty and strong and attacked, attack back
Control of a vehicleShould slow down when close to car in frontShould speed up when far behind car in front
Provides smoother transitions – not a sharp boundary
Fuzzy Sets
Provides a way to write symbolic rules with terms like “medium” but evaluate them in a quantified way Classical set theory: An object is either in or not in the set
Can’t talk about non-sharp distinctionsFuzzy sets have a smooth boundary
Not completely in or out – somebody 6” is 80% in the tall set tallFuzzy set theory
An object is in a set by matter of degree 1.0 => in the set 0.0 => not in the set 0.0 < object < 1.0 => partially in the set
Example Fuzzy Variable
Each function tells us how much we consider a character in the set if it has a particular aggressiveness valueOr, how much truth to attribute to the statement: “The character is nasty (or meek, or neither)?”
Aggressiveness
Membership (Degree of Truth)
1.0
0.0 1 0.5
MediumNastyMeek
-1 -0.5
Fuzzy Set Operations: Complement
The degree to which you believe something is not in the set is 1.0 minus the degree to which you believe it is in the set
Membership
Units
1.0
0.0
FS
¬FS
Fuzzy Set Ops: Intersection (AND)
If you have x degree of faith in statement A, and y degree of faith in statement B, how much faith do you have in the statement A and B?
Eg: How much faith in “that person is about 6’ high and tall”
Does it make sense to attribute more truth than you have in one of A or B?
Membership
Height
1.0
0.0
About 6’ Tall
Fuzzy Set Ops: Intersection (AND)
Assumption: Membership in one set does not affect membership in anotherTake the min of your beliefs in each individual statementAlso works if statements are about different variables
Dangerous and injured - belief is the min of the degree to which you believe they are dangerous, and the degree to which you think they are injured
Membership
Height
1.0
0.0
About 6’ Tall
About 6’ high and tall
Fuzzy Set Ops: Union (OR)
If you have x degree of faith in statement A, and y degree of faith in statement B, how much faith do you have in the statement A or B?
Eg: How much faith in “that person is about 6’ high or tall”
Does it make sense to attribute less truth than you have in one of A or B?
Membership
Height
1.0
0.0
About 6’ Tall
Fuzzy Set Ops: Union (OR)
Take the max of your beliefs in each individual statementAlso works if statements are about different variables
Membership
Height
1.0
0.0
About 6’ Tall
About 6’ high or tall
Fuzzy Rules
“If our distance to the car in front is small, and the distance is decreasing slowly, then decelerate quite hard”
Fuzzy variables in blueFuzzy sets in redConditions are on membership in fuzzy setsActions place an output variable (decelerate) in a fuzzy set (the quite hard deceleration set)
We have a certain belief in the truth of the condition, and hence a certain strength of desire for the outcomeMultiple rules may match to some degree, so we require a means to arbitrate and choose a particular goal - defuzzification
Fuzzy Rules Example(from Game Programming Gems)
Rules for controlling a car:Variables are distance to car in front and how fast it is changing, delta, and acceleration to applySets are:
Very small, small, perfect, big, very big - for distanceShrinking fast, shrinking, stable, growing, growing fast for deltaBrake hard, slow down, none, speed up, floor it for acceleration
Rules for every combination of distance and delta sets, defining an acceleration set
Assume we have a particular numerical value for distance and delta, and we need to set a numerical value for acceleration
Extension: Allow fuzzy values for input variables (degree to which we believe the value is correct)
Set Definitions
distance
v. small small perfect big v. big
delta
< = > >>
acceleration
slow present fast fastest
<<
brake
Instance
Distance could be considered small or perfectDelta could be stable or growingWhat acceleration?
distance
v. small small perfect big v. big
delta
< = > >>
acceleration
slow present fast fastest
<<
brake
????
Matching
Relevant rules are:If distance is small and delta is growing, maintain speedIf distance is small and delta is stable, slow downIf distance is perfect and delta is growing, speed upIf distance is perfect and delta is stable, maintain speed
For first rule, distance is small has 0.75 truth, and delta is growing has 0.3 truth
So the truth of the and is 0.3Other rule strengths are 0.6, 0.1 and 0.1
Fuzzy Inference
Convert our belief into actionFor each rule, clip action fuzzy set by belief in rule
acceleration
present
acceleration
presentacceleration
slow
acceleration
fast
Defuzzification Example
Three actions (sets) we have reason to believe we should take, and each action covers a range of values (accelerations)Two options in going from current state to a single value:
Mean of Max: Take the rule we believe most strongly, and take the (weighted) average of its possible valuesCenter of Mass: Take all the rules we partially believe, and take their weighted average
In this example, we slow down either way, but we slow down more with Mean of Max
Mean of max is cheaper, but center of mass exploits more information
Evaluation of Fuzzy Logic
Does not necessarily lead to non-determinismAdvantages
Allows use of continuous valued actions while still writing “crisp” rules – can accelerate to different degreesAllows use of “fuzzy” concepts such as mediumBiggest impact is for control problems
Help avoid discontinuities in behaviorIn example problem strict rules would give discontinuous acceleration
DisadvantagesSometimes results are unexpected and hard to debugAdditional computational overheadThere are other ways to get continuous acceleration
References
Nguyen, H. T. and Walker, E. A. A First Course in Fuzzy Logic, CRC Press, 1999.Rao, V. B. and Rao, H. Y. C++ Neural Networks and Fuzzy Logic, IGD Books Worldwide, 1995.McCuskey, M. Fuzzy Logic for Video Games, in Game Programming Gems, Ed. Deloura, Charles River Media, 2000, Section 3, pp. 319-329.
Neural Networks
Inspired by natural decision making structures (real nervous systems and brains)If you connect lots of simple decision making pieces together, they can make more complex decisions
Compose simple functions to produce complex functionsNeural networks:
Take multiple numeric input variablesProduce multiple numeric output valuesNormally threshold outputs to turn them into discrete valuesMap discrete values onto classes, and you have a classifier!But, the only time I’ve used them is as approximation functions
Simulated Neuron - Perceptron
Inputs (aj) from other perceptrons with weights (Wi,j)Learning occurs by adjusting the weights
Perceptron calculates weighted sum of inputs (ini)Threshold function calculates output (ai)
Step function (if ini > t then ai = 1 else ai = 0)Sigmoid g(a) = 1/(1+e-x)
Output becomes input for next layer of perceptron aj Wi,j
aiΣ Wi,j aj = ini
ai = g(ini)
Network Structure
Single perceptron can represent AND or OR, but not XORCombinations of perceptron are more powerful
Perceptron are usually organized in layersInput layer: takes external inputHidden layer(s)Output layer: external output
Feed-forward vs. recurrentFeed-forward: outputs only connect to later layers
Learning is easierRecurrent: outputs can connect to earlier layers or same layer
Internal state
Neural network for Quake
Four input perceptron One input for each condition
Four perceptron hidden layerFully connected
Five output perceptron One output for each actionChoose action with highest outputOr, probabilistic action selection
Choose at random weighted by output
EnemySound
DeadLow Health
Attack
Retreat
Wander
ChaseSpawn
Learning Neural Networks
Learning from examplesExamples consist of input, t, and correct output, o
Learn if network’s output doesn’t match correct outputAdjust weights to reduce differenceOnly change weights a small amount (η)
Basic perceptron learningLet f be the actual output for input tWi,j = Wi,j + η(f-o)aj
If output is too high (f-o) is negative so Wi,j will be reducedIf output is too low (f-o) is positive so Wi,j will be increasedIf aj is negative the opposite happens
Neural Net Example
Single perceptron to represent ORTwo inputsOne output (1 if either inputs is 1)Step function (if weighted sum > 0.5 output a 1)
Initial state (below) gives error on (1,0) inputTraining occurs
10.1
Σ Wj aj = 0.1
g(0.1) = 0
0
0
0.6
Neural Net Example
Wj = Wj + η(f-o)aj
W1 = 0.1 + 0.1(1-0)1 = 0.2W2 = 0.6 + 0.1(1-0)0 = 0.6After this step, try (0,1)1 example
No error, so no training
00.2
Σ Wj aj = 0.6
g(0.6) = 0
1
1
0.6
Neural Net Example
Try (1,0)1 exampleStill an error, so training occurs
W1 = 0.2 + 0.1(1-0)1 = 0.3W2 = 0.6 + 0.1(1-0)0 = 0.6And so on…
10.2
Σ Wj aj = 0.2
g(0.2) = 0
0
0
0.6
Neural Networks Evaluation
AdvantagesHandle errors wellGraceful degradationCan learn novel solutions
Disadvantages“Neural networks are the second best way to do anything”Can’t understand how or why the learned network worksExamples must match real problemsNeed as many examples as possibleLearning takes lots of processing
Incremental so learning during play might be possible
References
Mitchell: Machine Learning, McGraw Hill, 1997.Russell and Norvig: Artificial Intelligence: A Modern Approach, Prentice Hall, 1995.Hertz, Krogh & Palmer: Introduction to the theory of neural computation, Addison-Wesley, 1991.Cowan & Sharp: Neural nets and artificial intelligence, Daedalus 117:85-121, 1988.