You are on page 1of 50

AI CS364

Fuzzy Logic

Fuzzy Tutorial

10th October 2006


Dr Bogdan L. Vrusias
b.vrusias@surrey.ac.uk
AI CS364
Fuzzy Logic

Contents
Recap
Example: Air Conditioner
Example: Cart Pole Problem
Case Study: Building a Fuzzy Expert System
Summary

10th October 2006 Bogdan L. Vrusias 2006 2


AI CS364
Fuzzy Logic

Recap
Process of developing a fuzzy expert system:

Specify the problem; define linguistic variables.


Determine fuzzy sets.
Elicit/deduce and construct fuzzy rules.
Encode the fuzzy sets, fuzzy rules and procedures to perform
fuzzy inference into the expert system.
Evaluate and tune the system.

10th October 2006 Bogdan L. Vrusias 2006 3


AI CS364
Fuzzy Logic

Recap
Operation of a fuzzy expert system:

Fuzzification: definition of fuzzy sets, and determination of the


degree of membership of crisp inputs in appropriate fuzzy sets.

Inference: evaluation of fuzzy rules to produce an output for each


rule.

Composition: aggregation or combination of the outputs of all


rules.

Defuzzification: computation of crisp output

10th October 2006 Bogdan L. Vrusias 2006 4


AI CS364
Fuzzy Logic

Example: Air Conditioner


1a. Specify the problem
Air-conditioning involves the delivery of air, which can be warmed or cooled
and have its humidity raised or lowered.
An air-conditioner is an apparatus for controlling, especially lowering, the
temperature and humidity of an enclosed space. An air-conditioner typically
has a fan which blows/cools/circulates fresh air and has a cooler. The cooler
is controlled by a thermostat. Generally, the amount of air being compressed
is proportional to the ambient/close temperature.

1b. Define linguistic variables


- Ambient Temperature
- Air-conditioner Fan Speed

10th October 2006 Bogdan L. Vrusias 2006 5


AI CS364
Fuzzy Logic

Example: Air Conditioner


2. Determine Fuzzy Sets: Temperature
Temp (0C). COLD COOL PLEASANT WARM HOT

0< (T)<1 0 Y* N N N N
5 Y Y N N N
10 N Y N N N
12.5 N Y* N N N
15 N Y N N N (T)=1
17.5 N N Y* N N
20 N N N Y N
22.5 N N N Y* N
25 N N N Y N
27.5 N N N N Y
30 N N N N Y*

(T)=0

10th October 2006 Bogdan L. Vrusias 2006 6


AI CS364
Fuzzy Logic

Example: Air Conditioner


2. Determine Fuzzy Sets: Temperature

Temperature Fuzzy Sets


Temperature Fuzzy Sets

1 1
0.9 0.9
0.8 0.8 Cold
Cold
Truth Value
Truth Value

0.7 0.7
0.6 0.6 Cool
Cool
0.5 0.5 Pleasent
Pleasent
0.4 0.4
0.3 0.3
Warm
Warm
0.2 0.2 Hot
Hot
0.1 0.1
0 0
0 0 5 5 1010 15
15 20
20 2525 3030

Temperature
Temperature Degrees
Degrees CC
10th October 2006 Bogdan L. Vrusias 2006 7
AI CS364
Fuzzy Logic

Example: Air Conditioner


2. Determine Fuzzy Sets: Fan Speed
Rev/sec MINIMAL SLOW MEDIUM FAST BLAST
(RPM)
0 Y* N N N N

10 Y N N N N

20 Y Y N N N

30 N Y* N N N

40 N Y N N N

50 N N Y* N N

60 N N N Y N

70 N N N Y* N

80 N N N Y Y

90 N N N N Y

100 N N N N Y*

10th October 2006 Bogdan L. Vrusias 2006 8


AI CS364
Fuzzy Logic

Example: Air Conditioner


2. Determine Fuzzy Sets: Fan Speed

Speed Fuzzy Sets

1
0.8 MINIMAL
Truth Value

0.6 SLOW
MEDIUM
0.4
FAST
0.2 BLAST
0
0 10 20 30 40 50 60 70 80 90 100
Speed

10th October 2006 Bogdan L. Vrusias 2006 9


AI CS364
Fuzzy Logic

Example: Air Conditioner


3. Elicit and construct fuzzy rules

RULE 1: IF temp is cold THEN speed is minimal


RULE 2: IF temp is cool THEN speed is slow
RULE 3: IF temp is pleasant THEN speed is medium
RULE 4: IF temp is warm THEN speed is fast
RULE 5: IF temp is hot THEN speed is blast

10th October 2006 Bogdan L. Vrusias 2006 10


AI CS364
Fuzzy Logic

Example: Air Conditioner


4. Encode into an Expert System

5. Evaluate and tune the system

Consider a temperature of 16oC, use the system to compute the optimal fan
speed.

Operation of a Fuzzy Expert System

Fuzzification
Inference
Composition
Defuzzification

10th October 2006 Bogdan L. Vrusias 2006 11


AI CS364
Fuzzy Logic

Example: Air Conditioner


Fuzzification
Affected Temperature (T) fuzzy sets: COOL and PLEASANT

COOL(T) = T / 5 + 3.5 PLSNT(T) = T /2.5 - 6


= 16 / 5 + 3.5 = 16 /2.5 - 6
= 0.3 = 0.4

Temp=16 COLD COOL PLEASANT WARM HOT

0 0.3 0.4 0 0

10th October 2006 Bogdan L. Vrusias 2006 12


AI CS364
Fuzzy Logic

Example: Air Conditioner


Fuzzification
Affected Speed (S) fuzzy sets: SLOW and
MEDIUM

SLOW(S) = S / 20 - 0.5 MEDIUM(S) = -S / 10 + 6


= 12.5 / 20 - 0.5 = -57.5 / 10 + 6
= 0.125 = 0.25

Speed=12.5 MINIM SLOW MEDIUM FAST BLAST

0 0.125 0 0 0

10th October 2006 Bogdan L. Vrusias 2006 13


AI CS364
Fuzzy Logic

Example: Air Conditioner


Inference

RULE 1: IF temp is cold THEN speed is minimal


RULE 2: IF temp is cool THEN speed is slow
RULE 3: IF temp is pleasant THEN speed is medium
RULE 4: IF temp is warm THEN speed is fast
RULE 5: IF temp is hot THEN speed is blast

10th October 2006 Bogdan L. Vrusias 2006 14


AI CS364
Fuzzy Logic

Example: Air Conditioner


Inference

RULE 2: IF temp is cool (0.3) THEN speed is slow (0.3)

RULE 3: IF temp is pleasant (0.4) THEN speed is medium (0.4)

10th October 2006 Bogdan L. Vrusias 2006 15


AI CS364
Fuzzy Logic

Example: Air Conditioner


Composition

speed is slow (0.3) + speed is medium (0.4)


10th October 2006 Bogdan L. Vrusias 2006 16


AI CS364
Fuzzy Logic

Example: Air Conditioner


Defuzzification

COG = 0.125(12.5) + 0.25(15) + 0.3(17.5+20++40+42.5) + 0.4(45+47.5++52.5+55) + 0.25(57.5)


0.125 + 0.25 + 0.3(11) + 0.4(5) + 0.25

= 45.54rpm

10th October 2006 Bogdan L. Vrusias 2006 17


AI CS364
Fuzzy Logic

Example: Cart Pole Problem

m

w
g

The problem is to balance an upright pole, with a mass m at its head


and mass M at its base. A weightless shaft/rod connects these two
masses. The base can be moved on a horizontal axis. The task is to
determine the force (F) necessary to balance the pole. The calculation
of the force F involves the measurement of the angle and the
angular velocity of the pole.
10th October 2006 Bogdan L. Vrusias 2006 18
AI CS364
Fuzzy Logic

Example: Cart Pole Problem



nb nm ns az ps pm pb
nb ps pb
nm pm

w ns nm ns ps
az nb nm ns az ps pm pb
ps ns ps pm
pm nm
pb nb ns
nb: negative big, nm: negative medium, ns: negative small az: approximately zero
ps: positive small, pm: positive medium, pb: positive big

IF is negative medium
AND w is approximately zero
THEN F is negative medium
10th October 2006 Bogdan L. Vrusias 2006 19
AI CS364
Fuzzy Logic

Example: Cart Pole Problem


The fuzzy sets for , and F are based on the linear equation
(x)=ax + b, and are defined based on the following table:

(w)=1 if
w
w (w)=1 if
w
w=
(w)=0 if
w

10th October 2006 Bogdan L. Vrusias 2006 20


AI CS364
Fuzzy Logic

Example: Cart Pole Problem


Based on the fuzzy sets table draw three graphs showing the fuzzy
sets (nb, nm, ns, az, ps, pm, pb) for each , , and F individually.

Consider the case when the input variables are: = 50, = -5.

Use the rule base, execute each of the four tasks to compute the force
F necessary to balance the pole using the Centre of Gravity in the
Defuzzification task.

10th October 2006 Bogdan L. Vrusias 2006 21


AI CS364
Fuzzy Logic

Example: Cart Pole Problem


Fuzzification

i) Determine where and the angular velocity fall in the table


: pm, pb
: az

ii) Formulate possible rules from linguistic values obtained

IF is pm AND is az THEN F is pm
IF is pb AND is az THEN F is pb

10th October 2006 Bogdan L. Vrusias 2006 22


AI CS364
Fuzzy Logic

Example: Cart Pole Problem


Fuzzification
iii) Compute membership functions

1

1
1/22.5 = /17.5

: pm = 0.78
22.5 45 50 67.5 45 50 67.5

1
1/22.5 = /5
: pb
= 0.22
45 50 67.5

1
w: az 1/11.25 = /6.25

-11.25 -5 0 10
= 0.56

10th October 2006 Bogdan L. Vrusias 2006 23


AI CS364
Fuzzy Logic

Example: Cart Pole Problem


Inference

1. IF is pm AND is az THEN F is pm

The two premises in rule are conjunctive


minimum of the two: min{0.78, 0.56} = 0.56

2. IF is pb AND is az THEN F is pb
The two premises in RULE 2 are conjunctive
minimum of the two: min{0.22, 0.56} = 0.22

10th October 2006 Bogdan L. Vrusias 2006 24


AI CS364
Fuzzy Logic

Example: Cart Pole Problem


Composition

ps

pb

Defuzzification
3 0.2 (4 5 6) 0.56 (7 8) 0.22
SoG =
CoG = 5.30
0.2 0.56 0.56 0.56 0.22 0.22

10th October 2006 Bogdan L. Vrusias 2006 25


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Step 1: Specify the problem and define linguistic variables

A service centre keeps spare parts and repairs failed ones. A customer
brings a failed item and receives a spare of the same type. Failed
parts are repaired, placed on the shelf, and thus become spares.

The objective is to advise a manager of the service centre on certain


decision policies to keep the customers satisfied.

There are four main linguistic variables: average waiting time (mean
delay) m, repair utilisation factor of the service centre p, number of
servers s, and initial number of spare parts n.

10th October 2006 Bogdan L. Vrusias 2006 26


AI CS364
Fuzzy Logic
Linguistic Variable: Mean Delay, m
Linguistic Value Notation Numerical Range (normalised)
Very Short VS [0, 0.3]
Short S [0.1, 0.5]
Medium M [0.4, 0.7]
Linguistic Variable: Number of Servers, s
Linguistic Value Notation Numerical Range (normalised)
Small S [0, 0.35]
Medium M [0.30, 0.70]
Large L [0.60, 1]
Linguistic
Linguistic Variable:
Variable: Repair
Repair Utilisation Factor,
Utilisation Factor,
Linguistic Value Notation Numerical Range
Low L [0, 0.6]
Medium M [0.4, 0.8]
High H [0.6, 1]
Linguistic Variable: Number of Spares, n
Linguistic Value Notation Numerical Range (normalised)
Very Small VS [0, 0.30]
Small S [0, 0.40]
Rather Small RS [0.25, 0.45]
Medium M [0.30, 0.70]
Rather Large RL [0.55, 0.75]
Large L [0.60, 1]
Very Large VL [0.70, 1]
10th October 2006 Bogdan L. Vrusias 2006 27
AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Step 2: Determine fuzzy sets

Fuzzy sets can have a variety of shapes. However, a triangle or a


trapezoid can often provide an adequate representation of the expert
knowledge, and at the same time, significantly simplifies the process
of computation.

10th October 2006 Bogdan L. Vrusias 2006 28


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Fuzzy sets of Mean Delay m

Degree of
Membership
1.0
0.8 VS S M
0.6
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Mean Delay (normalised)

10th October 2006 Bogdan L. Vrusias 2006 29


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Fuzzy sets of Number of Servers s

Degree of
Membership
1.0
0.8 S M L
0.6
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Number of Servers (normalised)

10th October 2006 Bogdan L. Vrusias 2006 30


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Fuzzy sets of Repair Utilisation Factor p

Degree of
Membership
1.0
0.8 L M H
0.6
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Repair Utilisation Factor

10th October 2006 Bogdan L. Vrusias 2006 31


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Fuzzy sets of Number of Spares n

Degree of
Membership
1.0
0.8 VS S RS M RL L VL
0.6
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Number of Spares (normalised)

10th October 2006 Bogdan L. Vrusias 2006 32


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Step 3: Elicit/deduce and construct fuzzy rules

To accomplish this task, we might ask the expert to describe how the
problem can be solved using the fuzzy linguistic variables defined
previously.

Required knowledge also can be collected from other sources such as


books, computer databases, flow diagrams and observed human
behaviour.

10th October 2006 Bogdan L. Vrusias 2006 33


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


The square FAM (Fuzzy Associative Memory) representation of n
(number of spares)

s = number
of servers

m = delay
10th October 2006 Bogdan L. Vrusias 2006 34
AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


The rule table Rule Base 2 (slide 37 )

Rule m s p n Rule m s p n Rule m s p n


1 VS S L VS 10 VS S M S 19 VS S H VL
2 S S L VS 11 S S M VS 20 S S H L
3 M S L VS 12 M S M VS 21 M S H M
4 VS M L VS 13 VS M M RS 22 VS M H M
5 S M L VS 14 S M M S 23 S M H M
6 M M L VS 15 M M M VS 24 M M H S
7 VS L L S 16 VS L M M 25 VS L H RL
8 S L L S 17 S L M RS 26 S L H M
9 M L L VS 18 M L M S 27 M L H RS

10th October 2006 Bogdan L. Vrusias 2006 35


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Rule Base 2

1. If (mean_delay is VS) and (no_of_servers is S) and (util_fact is L) then (no_of_spares is VS)


2. If (mean_delay is S) and (no_of_servers is S) and (util_fact is L) then (no_of_spares is VS)
3. If (mean_delay is M) and (no_of_servers is S) and (util_fact is L) then (no_of_spares is VS)

4. If (mean_delay is VS) and (no_of_servers is M) and (util_fact is L) then (no_of_spares is VS)


5. If (mean_delay is S) and (no_of_servers is M) and (util_fact is L) then (no_of_spares is VS)
6. If (mean_delay is M) and (no_of_servers is M) and (util_fact is L) then (no_of_spares is VS)

7. If (mean_delay is VS) and (no_of_servers is L) and (util_fact is L) then (no_of_spares is S)


8. If (mean_delay is S) and (no_of_servers is L) and (util_fact is L) then (no_of_spares is S)
9. If (mean_delay is M) and (no_of_servers is L) and (util_fact is L) then (no_of_spares is VS)

10. If (mean_delay is VS) and (no_of_servers is S) and (util_fact is M) then (no_of_spares is S)


11. If (mean_delay is S) and (no_of_servers is S) and (util_fact is M) then (no_of_spares is VS)
12. If (mean_delay is M) and (no_of_servers is S) and (util_fact is M) then (no_of_spares is VS)

10th October 2006 Bogdan L. Vrusias 2006 36


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Cube FAM of Rule Base 2 (number of spares = n)
s
s
VS VS VS L RL M RS
VS VS VS
L S S VS
VS VS VS M M M S
VS VS VS
M VS VS VS
VS VS VS S VL L M
VS VS VS
S VS VS VS H VS S M H
M
L s
VS S M m
L M RS S
m
M RS S VS

S S VS VS
M
VS S M
s
m
L S S VS

M VS VS VS

S VS VS VS
L
VS S M
m
10th October 2006 Bogdan L. Vrusias 2006 37
AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Step 4: Encode the fuzzy sets, fuzzy rules and procedures to perform
fuzzy inference into the expert system

To accomplish this task, we may choose one of two options: to build


our system using a programming language such as C/C++ or Pascal,
or to apply a fuzzy logic development tool such as MATLAB Fuzzy
Logic Toolbox or Fuzzy Knowledge Builder.

10th October 2006 Bogdan L. Vrusias 2006 38


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Step 5: Evaluate and tune the system

The last, and the most laborious, task is to evaluate and tune the
system. We want to see whether our fuzzy system meets the
requirements specified at the beginning.

Several test situations depend on the mean delay, number of servers


and repair utilisation factor.

The Fuzzy Logic Toolbox can generate surface to help us analyse the
systems performance.

10th October 2006 Bogdan L. Vrusias 2006 39


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Three-dimensional plots for Rule Base 1

0.6
number_of_spares

0.5

0.4

0.3

0.2
0

0.2 1
0.8
0.4 0.6
0.4
0.6 0.2
mean_delay 0 utilisation_factor

10th October 2006 Bogdan L. Vrusias 2006 40


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Three-dimensional plots for Rule Base 2

0.35
number_of_spares

0.3

0.25

0.2

0.15

0.2 1
0.8
0.4 0.6
0.4
0.6 0.2
mean_delay 0 number_of_servers

10th October 2006 Bogdan L. Vrusias 2006 41


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Three-dimensional plots for Rule Base 2

0.5

0.4
number_of_spares

0.3

0.2

0.2 1
0.8
0.4 0.6
0.4
0.6 0.2
mean_delay 0 utilisation_factor

10th October 2006 Bogdan L. Vrusias 2006 42


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Tune the system

However, even now, the expert might not be satisfied with the system
performance.

To improve the system performance, we may use additional sets


Rather Small and Rather Large on the universe of discourse Number
of Servers, and then extend the rule base.

10th October 2006 Bogdan L. Vrusias 2006 43


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Modified fuzzy sets of Number of Servers s

Degree of
Membership
1.0
0.8 S RS M RL L
0.6
0.4
0.2
0.0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Number of Servers (normalised)

10th October 2006 Bogdan L. Vrusias 2006 44


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Cube FAM of Rule Base 3
s
s
VS VS VS L L M RS
VS VS VS
L S S VS
VS VS VS RL RL M RS
VS VS VS
RL S S VS
VS VS VS M M M S
VS VS VS
M VS VS VS
VS VS VS s RS VL RL RS
VS VS VS
RS VS VS VS
VS VS VS L M RS S S VL L M
VS VS VS
S VS VS VS H VS S M H
M RL M RS S
L m
VS S M
m M RS S VS

s RS S VS VS

L S S VS S S VS VS
M
VS S M
RL S S VS
m
M VS VS VS

RS VS VS VS

S VS VS VS
L
VS S M
m
10th October 2006 Bogdan L. Vrusias 2006 45
AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Three-dimensional plots for Rule Base 3

0.35

0.3
number_of_spares

0.25

0.2

0.15

0.2 1
0.8
0.4 0.6
0.4
0.6 0.2
mean_delay 0 number_of_servers

10th October 2006 Bogdan L. Vrusias 2006 46


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Three-dimensional plots for Rule Base 3

0.5

0.4
number_of_spares

0.3

0.2

0.2 1
0.8
0.4 0.6
0.4
0.6 0.2
mean_delay 0 utilisation_factor

10th October 2006 Bogdan L. Vrusias 2006 47


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


Tuning fuzzy systems

1. Review model input and output variables, and if required redefine


their ranges.

2. Review the fuzzy sets, and if required define additional sets on the
universe of discourse. The use of wide fuzzy sets may cause the fuzzy
system to perform roughly.

3. Provide sufficient overlap between neighbouring sets. It is suggested


that triangle-to-triangle and trapezoid-to-triangle fuzzy sets should
overlap between 25% to 50% of their bases.

10th October 2006 Bogdan L. Vrusias 2006 48


AI CS364
Fuzzy Logic

Case Study: Building a Fuzzy System


4. Review the existing rules, and if required add new rules to the rule
base.

5. Examine the rule base for opportunities to write hedge rules to


capture the pathological/diseased behaviour of the system.

6. Adjust the rule execution weights. Most fuzzy logic tools allow
control of the importance of rules by changing a weight multiplier.

7. Revise shapes of the fuzzy sets. In most cases, fuzzy systems are
highly tolerant of a shape approximation.

10th October 2006 Bogdan L. Vrusias 2006 49


AI CS364
Fuzzy Logic

Summary
Process of developing a fuzzy expert system

Operation of a fuzzy expert system

Examples: Air Conditioner; Cart Pole Problem

Case Study: Building a Fuzzy Expert System

10th October 2006 Bogdan L. Vrusias 2006 50

You might also like