Fuzzy Control

Fuzzy Control
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
Fuzzy Logic
" So far as the laws of mathematics refer to reality, they are not certain,
And so far as they are certain, they do not refer to reality."
Albert Einstein
Geometrie und Erfahrung
Fuzzy Sets
Is this sentence true or false?
The sentence on the other side
of the line is false
The sentence on the other side
of the line is false
Young
1
0
a1
a2
Age
Figure 14.2 Membership function for the fuzzy set Young
Table 14.1 Fuzzy Logic Operations
Logic Operation
Fuzzy Logic Operation
NOT A
1 - A
A AND B
MIN(A,B)
A OR B
MAX(A,B)
Young
NOT Young
1
0
a1
a2
Age
Figure 14.3 NOT Young = 1 - Young
NOT Old
Old
1
0
Age
Figure 14.4 Membership functions for the fuzzy sets Old and NOT Old
1
NOT Young
NOT Old
0
(a)
Age
1
Middle Age =
NOT Young AND Not Old
0
(b)
Age
Figure 14.5 Deriving the membership function for Middle Age
Probabiltiy vs. Fuzziness
Probability describes the uncertainty of an
event occurrence.
Fuzziness describes event ambiguity.
Whether an event occurs is RANDOM.
To what degree it occurs is FUZZY.
Probability:
There is a 50% chance of an apple
being in the refrigerator.
Fuzzy:
There is a half an apple in the
refrigerator.
Fuzzy logic acknowledges
and exploits the tolerance for
uncertainty and imprecision.
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
NM
NS
Z
PS
PM
1
0
0
36
82
12 8
17 4
Univers e of dis cou rse
22 0 25 5
Figure 14.6 Example of fuzzy membership functions
A Fuzzy Controller
INPUTS
Map to Fuzzy Sets
FUZZY RULES
get_inputs();
fire_rules();
If A AND B then L
•
•
•
Defuzzification
OUTPUT
find_output();
do_forever
{
get_inputs();
fire_rules();
find_output();
}
Figure 14.8 Overall algorithm of a fuzzy controller
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
get_inputs()
for i = 1, num_inputs
{
get_x(i);
fill_weight(xi, Mi);
}
Figure 14.9 Pseudocode for get_inputs()
wt
1
sl ope_ 2
sl ope_ 1
u1
u2
u3
u4
Figure 14.10 A membership function is defined in terms of u1, u2, u3, and u4
NM
NS
Z
PS
PM
1
0
0
36
82
12 8
17 4
Univers e of dis cou rse
22 0 25 5
Figure 14.6 Example of fuzzy membership functions
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
Fuzzy Inference
if x1 is A1 and x2 is B1 then y is L1
rule 1
if x1 is A2 and x2 is B2 then y is L2
rule 2
Given the fact that
x1 is A' and x2 is B'
fact
the problem is to find the conclusion
y is L'
conclusion
A1
m
B1
m
rule 1
w1
m
L1
w2
w2*L1
x2
x1
m
m
A2
B2
w2
y
rule 2
m
L2
w1
a
w1*L2
x1
b
x2
y
sum
m
L'
y0
Figure 14.15 Fuzzy inference
y
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
fire_rules()
clear Out array;
for j = 1, num_rules
{
min_wt = 1;
for i = 1, num_inputs
{
wt = weighti[Aij]
if wt < min_wt
min_wt = wt;
}
Out[Lj] = MAX(Out[Lj], min_wt);
}
Figure 14.17 Pseudocode for fire_rules()
A1
m
B1
m
rule 1
w1
m
L1
w2
w2*L1
x2
x1
m
m
A2
B2
w2
y
rule 2
m
L2
w1
a
w1*L2
x1
b
x2
y
sum
m
L'
y0
Figure 14.15 Fuzzy inference
y
Rules
make.rules
rules
\
ball_position
neg_far
C,
neg_far
C,
--------pos_far
C,
pos_far
C,
end.rules
ball_speed
neg_fast
C:
neg_slow
C:
pos_slow
pos_fast
C:
C:
motor_power
pos_high C;
pos_high C;
--------neg_high C;
neg_high C.
Figure 14.20 Format for defining fuzzy rules
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
A1
m
B1
m
rule 1
w1
m
L1
w2
w2*L1
x2
x1
m
m
A2
B2
w2
y
rule 2
m
L2
w1
a
w1*L2
x1
b
x2
y
sum
m
L'
y0
Figure 14.15 Fuzzy inference
y
14.2.4 Centroid Defuzzification
The last step in the fuzzy controller shown in Figure 14.7 is defuzzification. This
involves finding the centroid of the net output fuzzy set L' shown in Figures 14.15 and
14.16. Although we have used the MIN-MAX rule in the previous section we will begin
by deriving the centroid equation for the sum rule shown in Figure 14.16. This will
illuminate the assumptions made in deriving the defuzzification equation that we will
actually use in the fuzzy controller.
Let Li(y) be the original output membership function associated with rule i where y
is the output universe of discourse (see Figure 14.15.). After applying rule i this
membership function will be reduced to the value
mi(y) = wiLi(y)
(14.1)
where wi is the minimum weight found by applying rule i. The sum of these reduced
output membership functions over all rules is then given by
N
M(y) =
mi(y)
i=1
where N is the number of rules.
(14.2)
The crisp output value y0 is then given by the centroid of M(y) from the equation
yM(y)dy

y0 =
M(y)dy

(14.3)
Note that the centroid of membership function Li(y) is given by

yLi(y)dy
ci =

Li(y)dy
(14.4)
But
Ii = 
Li(y)dy
(14.5)
is just the area of membership function Li(y). Substituting (14.5) into (14.4) we can write

yLi(y)dy = ciIi
(14.6)
Using Eqs. (14.1) and (14.2) we can write the numerator of (14.3) as
yM(y)dy

 N
= y wiLi(y) dy

 i=1
N
=
ywiLi(y) dy
i=1
N
=
 wiciIi
i=1
where (14.6) was used in the last step.
(14.7)
Similarly, using (14.1) and (14.2) the denominator of (14.3) can be written as
M(y)dy

N
=  wiLi(y) dy

i=1
N
=
wiLi(y) dy
i=1
N
=
 wiIi
(14.8)
i=1
where (14.5) was used in the last step. Substituting (14.7) and (14.8) into (14.3) we can
write the crisp output of the fuzzy controller as
N
 wiciIi
y0 =
i=1
N
(14.9)
 wiIi
i=1
Eq. (14.9) says that we can compute the output centroid from the centroids, ci, of the
individual output membership functions.
Note in Eq. (14.9) the summation is over all N rules. But the number of output
membership functions, Q, will, in general, be less than the number of rules, N. This means
that in the sums in Eq. (14.9) there will be many terms that will have the same values of ci
and Ii. For example, suppose that rules 2, 3, and 4 in the sum all have the output
membership function Lk as the consequent. This means that in the sum
w2c2I2 + w3c3I3 + w4c4I4
the values ci and Ii are the same values ck and Ik because they are just the centroid and
area of the kth output membership function. These three terms would then contribute the
value
(w2 + w3 + w4)ckIk = WkckIk
to the sum, where
Wk = (w2 + w3 + w4)
is the sum of all weights from rules whose consequent is output membership function Lk.
This means that the equation for the output value, y0, given by (14.9) can be rewritten as
Q
 WkckIk
y0 =
k=1
Q
(14.10)
 WkIk
k=1
If the area of all output membership functions, Ik are equal, then Eq. (14.10) reduces to
Q
 Wkck
y0 =
k=1
Q
(14.11)
 Wk
k=1
Eqs. (14.10) and (14.11) show that the output crisp value of a fuzzy controller can be
computed by summing over only the number of output membership functions rather than
over all fuzzy rules. Also, if we use Eq. (14.11) to compute the output crisp value, then
we need to specify only the centroids, ck, of the output fuzzy membership functions. This
is equivalent to assuming singleton fuzzy sets for the output.
We will always use singleton fuzzy sets for the output represented by the
centroids, ck. We will also use the MIN-MAX inference rule described in the previous
section. It should be clear from Figure 14.16 that in this case the centroid y0 will still be
given by Eq. (14.11) where Wk is now the output array, Out(k), shown in Figure 14.18
and computed by the word firerules given in Figure 14.19.
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
find_output()
numer = 0;
denom = 0;
for k = 1, Q
if Out[k] != 0
{
numer = numer + Out[k]*c[k];
denom = denom + Out[k];
}
y0 = numer/denom;
Figure 14.21 Pseudocode for find_output()
Fuzzy Control
• Fuzzy Sets
• Design of a Fuzzy Controller
–
–
–
–
–
–
Fuzzification of inputs: get_inputs()
Fuzzy Inference
Processing the Rules: find_rules()
Centroid Defuzzification
Output Defuzzification: find_output()
A Fuzzy Control Example -• Floating Ping-Pong Ball
68HC12
JStamp
…
ball_speed
pos_fast
neg_far
zero_m
ball_position
neg_close
zero_p
neg_low
neg_high
pos_close
neg_high
pos_far
neg_high
pos_slow
pos_low
zero_m
neg_low
neg_high
neg_high
zero_s
pos_high
pos_low
zero_m
neg_low
neg_high
neg_slow
pos_high
pos_high
pos_low
zero_m
neg_low
neg_fast
pos_high
pos_high
pos_high
pos_low
zero_m
Figure 14.23 Fuzzy K-map for floating ping-pong ball
Fuzzy k-map for floating ping-pong ball
Implementations
• High-level Languages
–C
– Java
• Assembly
– Assembly
– Assembly Built-in Fuzzy Control Instructions
• HC12
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)
Designing a Robot (Example)