F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy...

17
FUZZY LOGIC Steve Foster

Transcript of F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy...

Page 1: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

FUZZY LOGIC

Steve Foster

Page 2: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

INTRODUCTION

Today we will be discussing Fuzzy Logic What is fuzzy logic?

The difference between crisp and fuzzy logic

Making a decision using fuzzy logic

Programming AI using fuzzy logic

Page 3: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

FUZZY LOGIC Historical Origins:

Derived from fuzzy set theory Developed by Prof. Lotfi Zadeh at the University of

California, Berkeley in 1965 Fuzzy set theory is in contrast to traditional

(“crisp”) set theory Traditional set theory: An object either belongs to a set or

not. Fuzzy set theory: An object can “partially belong” to a set Example: To classify people into two sets: Tall and Short In traditional set theory, an arbitrary cut-off is set at, say, 6

feet. Any person taller than 6 feet is in the Tall set. Any person less than or equal to 6 feet is in the Short set.

In fuzzy set theory, for example, a person taller than 6’2” is considered to be 100% in the Tall set, a person at 5’11” is considered to have 20% membership in the Tall set etc.

Fuzzy logic often mimics real life Can be used to improve the rules used in FSM

Page 4: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

FUZZY SET MEMBERSHIP

Traditional Set Theory: An object is either a member of a set or not a member of a set

Fuzzy Set Theory: An object is either in a set, not in a set, or partially in a setThis is denoted by a number from 0.0 to 1.0Membership number m of Object A in Set S:

m = 0.0 means that Object A is not in Set S 0.0 < m < 1.0 means that Object A is partially in

Set S, for example m = 0.2 means that Object A is 20% in Set S, a rather weak membership

m = 1.0 means that Object A is completely in Set S

Page 5: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

MEMBERSHIP FUNCTIONS To determine an object’s membership value in a

set, refer to membership functions of the object’s attribute(s)

For example, we may define our membership functions for the three sets Short, Medium and Tall Attribute is height An object whose height is 1.55m would be a 0.25

member of Short, a 0.75 member of Medium, and a 0.0 member (i.e. non-member) of Tall

0.0

1.0

Membership

Height (meters)

0.5

1.4 1.5 1.6 1.8 1.9 2.0

Short

Medium

Tall

1.55

Page 6: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

CRISP LOGIC OPERATIONS

We consider set operators AND, OR and NOT

Remember traditional (crisp) logic: Represent TRUE as 1, FALSE as 0 We then have the truth tables to

the right For example, look at the third line

in the first table. It says that if Statement A is True, and Statement B is false, then the Statement “A and B” must be fals

A B A and B

0 0 0

0 1 0

1 0 0

1 1 1

A B A or B

0 0 0

0 1 1

1 0 1

1 1 1

A not A

0 1

1 0

Page 7: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

FUZZY LOGIC OPERATIONS

NOT: If Fuzzy Statement A is m true, then the statement “Not

A” is (1.0 – m) true (where m is a number between 0.0 and 1.0 inclusive)

Equivalent Set Theory operation: If an object A has m membership in Fuzzy Set S, then it must have membership (1.0 – m) in Fuzzy Set Not-S

AND: If Fuzzy Statement A is m true, and Fuzzy Statement B is

n true, then the Fuzzy Statement “A and B” is k true, where k = min(m,n) (Here, m, n and k are numbers between 0.0 and 1.0 inclusive)

OR: If Fuzzy Statement A is m true, and Fuzzy Statement B is

n true, then the Fuzzy Statement “A or B” is k true, where k = max(m,n) (Here, m, n and k are numbers between 0.0 and 1.0 inclusive)

Page 8: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

RULES Crisp rule:

Example: “If Self is Tall and Enemy is Short, then Attack.” The Condition of a Rule:

The condition for this rule is: “If Self is Tall and Enemy is Short”

To check the condition is easy, just check self for membership in the set Tall, and enemy for membership in the set Short.

Suppose that Self is not Tall, then the decision is to not attack.

Fuzzy rule: Example: “If Self is Tall and Enemy is Short, then Attack.” The condition of the rule once again is: “If Self is Tall and

Enemy is Short” Suppose that Self is 0.3 Tall, and Enemy is 0.6 Short, then

this condition is 0.3 True. So, should we attack?

Page 9: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

MAKING A DECISION

We can simply decide the arbitrary cut-off to be 0.5. If a condition is at least 0.5 true, then perform the action. In the preceding example, the condition is only 0.3 true,

so the decision is to not attack

Or, we can make a random decision with the probability equal to the value of the condition. In the preceding example, attack with the probability 0.3

Page 10: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

MAKING A DECISION Most of the time, there are many rules

For example: Rule 1: If Self is Tall and Enemy is Short, then Attack. Rule 2: If Self is Big and Enemy is small, then Attack. Rule 3: If Self’s Power is greater than 10, and Self’s Health is

greater than 5, then Attack.

In this case, there are a few options to make a decision whether to attack or not. Option 1: Take the majority decision.

For the example above: If at least two of the above conditions are at least 0.5, then Attack. Otherwise don’t attack.

Option 2: Take the average. For the example above: If condition 1 is 0.6, condition 2 is

0.6, and condition 3 is 0.0, then the average is 0.4. 0.4 is less than 0.5, so the decision is not to attack.

Option 3: Make a random decision with the probability equal to the average.

Page 11: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

USING FUZZY LOGIC IN GAMES

You can use fuzzy logic in games in a variety of ways For example, you can use fuzzy logic to control

bots or other non-player character units

You can also use it for assessing threats posed by players

It can be used for classification E.g. Classifying a player as poor / average / elite

Page 12: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

FUZZY GAME EXAMPLE

Suppose we are programming a simple AI system for monsters within a game

Things happen in the game world that the monsters must react to

E.g. Health After being damaged, should the monster fight or

run-away? Change to a defensive stance?

With only one variable a simple if-else system would be fine

As more variables are introduced the system becomes more and more complex

Page 13: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

FUZZY GAME EXAMPLE Using two variables, we may have the following

system:

DetermineAction() {

if (health < 10) {

if(ammo < 5) return ESCAPE if(ammo >= 5 && ammo < 20) return CAUTIOUS else return NORMAL

} else if (health >= 10 && health < 20) {

if(ammo < 5) return CAUTIOUS if(ammo >= 5 && ammo < 20) return NORMAL else return ATTACK

{else {

if(ammo < 5) return NORMAL if(ammo >= 5 && ammo < 20) return ATTACK else return FULL_ATTACK

}}

Page 14: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

A FUZZY GAME SYSTEM As more variables are added, the if-else system

becomes un-manageable A fuzzy system is much simpler Output needed: Three possible states called

Escape, Defence and Attack. Input to system: Ammunition, Health, Enemies in

sight Start with Ammunition

Three states Low, Medium and High Make a table with the behaviour we want for the

monster

Low Medium High

Ammo Escape Defence Attack

Page 15: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

A FUZZY GAME SYSTEM

We know that: Low = Escape Medium = Defence High = Attack

So we can map these output functions to each level

Page 16: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

A FUZZY GAME SYSTEM

We could then added more attributes to the system

By combining the fuzzy output of each attribute we can generate an appropriate action

Low Medium High

Health Escape Defence Attack

Many Some Few

Enemies in sight

Escape Defence Attack

Page 17: F UZZY L OGIC Steve Foster. I NTRODUCTION Today we will be discussing Fuzzy Logic What is fuzzy logic? The difference between crisp and fuzzy logic Making.

A FUZZY GAME SYSTEM

Some inputs might be more important than others

To handle this you can assign a coefficient for some inputs, so their weight will be more or less than the others

Low Medium High

Ammo 1.5 Escape Defence Attack