## 5. Fuzzy Logic Fuzzy Logic 5.1 Fuzzy Logic Guide Fuzzy Logic ...

“People also do not require precise numerical input”. • These slides are based on “Fuzzy Logic Tutorial” by. Encoder Newsletter of the Seattle Robotics Society.
5. Fuzzy Logic

Fuzzy Logic

• Introduced by Lotfi Zadeh, UC Berkeley • Process data allowed partial set membership instead of “crisp membership” • Deals with noisy, imprecise,vague, ambiguous data • Higher reliability • “People also do not require precise numerical input” • These slides are based on “Fuzzy Logic Tutorial” by Encoder Newsletter of the Seattle Robotics Society

• Empirical model • Simple rule-based approach: IF x THEN y

Bräunl 2003

Here no numbers but fuzzy set memberships, e.g.: IF (temp is too_cool) AND (time is progressed) THEN (cool fast)

• • • • 1

Imprecise, but descriptive Mimic human control logic Robust, requires little tuning Can model non-linear systems that are mathematically very difficult (or impossible) to model

Bräunl 2003

2

5.1 Fuzzy Logic Guide

Fuzzy Logic Example

• Consider inputs, outputs, system control, failure modes • Determine input/output relationships • Use minimum number of input variables, e.g.

Fuzzy logic temperature control of a physical process

– error (control difference) – change-of-error (error derivative)

Target temp.

• Set up system as a number of IF-THEN rules • Create membership functions, giving meaning to input/output terms • Create pre-/post-processing terms • Test system, evaluate, tune, and test again Bräunl 2003

Fuzzy Logic

Heater Cooler

Physical Process

Feedback temperature

3

Bräunl 2003

source: Encoder, Seattle Robotics Soc.

4

Fuzzy Logic Example Fuzzy Logic

• • • •

Heater Cooler

Target input: Error feedback: Error-dot feedback: Fuzzy logic output:

Linguistic Variables

Physical Process

Introduction of linguistic variables: • Input 1: • Input 2: • Output:

temperature negative, zero, positive negative, zero, positive heat, cool, or “do nothing”

Bräunl 2003

“error”: positive (P), zero (Z), negative (N) “error-dot”: positive (P), zero (Z), negative (N) heat (H), do_nothing (–), cool (C)

Error = target – feedback (P = too cold, Z = just right, N = too hot) ErrorDot = d Error / dt (P = getting colder, Z = no change, N = getting hotter) Output H = activate heater, “–” = do nothing, C = activate cooler 5

Bräunl 2003

5.2 Linguistic Rules

Linguistic Rules

Set up linguistic rules:

1. 2. 3.

IF too_hot AND getting_colder THEN ?? IF just_right AND getting_colder THEN ?? IF too_cold AND getting_colder THEN ??

4. 5. 6.

IF too_hot AND no_change THEN ?? IF just_right AND no_change THEN ?? IF too_cold AND no_change THEN ??

7. 8. 9.

IF too_hot AND getting_hotter THEN ?? IF just_right AND getting_hotter THEN ?? IF too_ cold AND getting_hotter THEN ??

IF-THEN (antecedent and consequent blocks) Example: IF too_hot AND getting_hotter THEN cool

target-feedback=N

Bräunl 2003

d(target-feedback)/dt=N

output = C

7

Bräunl 2003

6

8

Linguistic Rules

Rule Matrix

1. 2. 3.

cool IF too_hot AND getting_colder THEN IF just_right AND getting_colder THEN heat IF too_cold AND getting_colder THEN heat

4. 5. 6.

IF too_hot AND no_change THEN IF just_right AND no_change THEN IF too_cold AND no_change THEN

7. 8. 9.

cool IF too_hot AND getting_hotter THEN IF just_right AND getting_hotter THEN cool IF too_ cold AND getting_hotter THEN heat

Build rule matrix from linguistic rules

cool do_nothing heat

Bräunl 2003

9

too cold

just right

too hot

getting colder

cool

heat

heat

no change

cool

do nothing

heat

getting hotter

cool

cool

heat

Bräunl 2003

source: Encoder, Seattle Robotics Soc.

5.3 Membership Functions

Membership Functions

• Membership function is a graphical representation of “magnitude of participation” of each input • Associates weighting with each input • Defines overlaps between inputs • Determines output response

• Shape of membership function is usually triangle (but could also be trapezoidal or other) • Height usually normalized to 1 (so in total: [0..1]) • Width of the base of function depends on number of functions • Shouldering is used to lock height in an outer function (leftmost and rightmost functions evaluate to 1 to the outsides) • Center points of functions evaluate to 1 • Overlap is usually 50% at base of function

• Fuzzification: • Defuzzification:

Bräunl 2003

Translating input values to fuzzy set memberships Translating fuzzy output back to crisp system output (e.g. a number) 11

Bräunl 2003

10

12

Membership Functions

too hot (shoulder) height

too hot just right too cold

too cold (shoulder)

too hot

just right

h&r

too cold

r&c

width Bräunl 2003

13

Bräunl 2003

14

• Add values, here: for error (temperature difference in deg. Celsius)

• Add values, here for error_dot (temperature change in deg. Celsius / minute)

1.0

1.0

too hot (shoulder) too hot

0.5

Degree of Membership

Membership Functions

Degree of Membership

Membership Functions

too cold (shoulder)

just right

h&r

too cold

r&c

0.0

getting hotter (shoulder) getting hotter

0.5

getting colder (shoulder) no change

gh & nc

getting colder

nc & gc

0.0 -4

Bräunl 2003

-2

0

+2

+4 Error in degrees Celsius

-10 15

Bräunl 2003

-5

0

+5

+10 Error_dot in degrees Celsius / minute 16

Membership Functions Degree of Membership

Degree of Membership

Membership Functions 1.0

0.5

too hot

just right too cold h&r

0.0 -4

-2

r&c 0

+2 +4 Error in degrees Celsius

Example • Error = –1 ⇒ Membership(too_hot) ⇒ Membership(just_right) ⇒ Membership(too_cold)

0.5

too hot

just right too cold h&r

0.0 -4

-2

r&c 0

+2 +4 Error in degrees Celsius

Example • Error = +5 ⇒ Membership(too_hot) ⇒ Membership(just_right) ⇒ Membership(too_cold)

= 0.5 = 0.5 = 0.0

Bräunl 2003

1.0

17

= 0.0 = 0.0 = 1.0

Bräunl 2003

Membership Functions

5.4 Inferences

Note

• Deviation from Boolean logic (true, false or 1, 0) • Now we have continuous membership values [0..1] • Build logical sums using membership values in combination with rule matrix • Use minimum of all antecedent values (AND-part) for output

• Sum of all membership values for a certain input value is always 1.0 • Different value range for different input variables (e.g. error and error_dot) • Membership values (antecedents) are evaluated for the produced conclusion (consequent)

Bräunl 2003

19

Bräunl 2003

18

20

Inferences

Inferences • •

too_hot = 0.5, just_right = 0.5, too_cold = 0.0 getting_hotter = 0.5, no_change = 0.5, getting_colder = 0.0

= –1 = –2.5

1. 2. 3.

IF too_hot AND getting_colder THEN cool IF just_right AND getting_ colder THEN heat IF too_cold AND getting_ colder THEN heat

Membership error:

4. 5. 6.

IF too_hot AND no_change THEN cool IF just_right AND no_change THEN do_nothing IF too_cold AND no_change THEN heat

7. 8. 9.

IF too_hot AND getting_hotter THEN cool IF just_right AND getting_hotter THEN cool IF too_ cold AND getting_hotter THEN heat

Example: error error_dot

too_hot = 0.5, just_right = 0.5, too_cold = 0.0

Membership error_dot getting_hotter = 0.5, no_change = 0.5, getting_colder = 0.0 Bräunl 2003

21

Inferences

Bräunl 2003

22

Inferences use minimum

• •

too_hot = 0.5, just_right = 0.5, too_cold = 0.0 getting_hotter = 0.0, no_change = 0.5, getting_colder = 0.5

use minimum

• •

too_hot = 0.5, just_right = 0.5, too_cold = 0.0 getting_hotter = 0.5, no_change = 0.5, getting_colder = 0.0

1. 2. 3.

IF too_hot AND getting_colder THEN cool IF just_right AND getting_ colder THEN heat IF too_cold AND getting_ colder THEN heat

0.5 & 0.0 = 0.0 0.5 & 0.0 = 0.0 0.0 & 0.0 = 0.0

1. 2. 3.

IF too_hot AND getting_colder THEN cool IF just_right AND getting_ colder THEN heat IF too_cold AND getting_ colder THEN heat

4. 5. 6.

IF too_hot AND no_change THEN cool 0.5 & 0.5 = 0.5 IF just_right AND no_change THEN do_nothing 0.5 & 0.5 = 0.5 IF too_cold AND no_change THEN heat 0.0 & 0.5 = 0.0

4. 5. 6.

IF too_hot AND no_change THEN cool 0.5 & 0.5 = 0.5 IF just_right AND no_change THEN do_nothing 0.5 & 0.5 = 0.5 IF too_cold AND no_change THEN heat

7. 8. 9.

IF too_hot AND getting_hotter THEN cool IF just_right AND getting_hotter THEN cool IF too_ cold AND getting_hotter THEN heat

7. 8. 9.

IF too_hot AND getting_hotter THEN cool IF just_right AND getting_hotter THEN cool IF too_ cold AND getting_hotter THEN heat

Bräunl 2003

0.5 & 0.5 = 0.5 0.5 & 0.5 = 0.5 0.0 & 0.5 = 0.0 23

Bräunl 2003

0.5 & 0.5 = 0.5 0.5 & 0.5 = 0.5 24

5.5 Output Combination

Output Combination

• Find out “firing strength” of each rule • Combine logical outputs for each rule must be combined before defuzzification • Combining rule outputs

MAX-MIN • Test magnitude of all rules – select highest one Use horizontal coordinate of “fuzzy centroid” as output Note: This method does not combine individual results! MAX-DOT • Scale each member function to fit under its peak value, take the horizontal coordinate of the fuzzy centroid of the composite area as output. This shrinks all member functions to their peaks equal the magnitude of the respective function. Note: This method prodruces a smooth, continuous output combining all active rules

– – – –

MAX-MIN MAX-DOT (MAX-PRODUCT) AVERAGING ROOT-SUM-SQUARE

Bräunl 2003

25

Output Combination

26

Output Combination

AVERAGING • Each function is clipped at the average value and the fuzzy centroid of the composite area is computer Note: This method does not give increased weight if multiple rules generate the same output member! ROOT-SUM-SQUARE • Combination of several approaches: Scale functions to respective magnitudes with root of sum of squares, compute fuzzy centroid of composite area. Note: This method gives a good weighted influence to all firing rules. Bräunl 2003

Bräunl 2003

27

1. 2. 3.

IF too_hot AND getting_colder THEN cool IF just_right AND getting_ colder THEN heat IF too_cold AND getting_ colder THEN heat

0 0 0

4. 5. 6.

IF too_hot AND no_change THEN cool 0.5 IF just_right AND no_change THEN do_nothing 0.5 IF too_cold AND no_change THEN heat 0

7. 8. 9.

IF too_hot AND getting_hotter THEN cool IF just_right AND getting_hotter THEN cool IF too_ cold AND getting_hotter THEN heat

0,5 0.5 0

Output membership functions: “cool” = √(rule12 + rule42 + rule72 + rule82 ) = √(0 + 0.52 +0.52 + 0.52) = √0.75 = 0.866 Bräunl 2003

28

Output Combination 1. 2. 3.

IF too_hot AND getting_colder THEN cool IF just_right AND getting_ colder THEN heat IF too_cold AND getting_ colder THEN heat

4. 5. 6. 7. 8. 9.

Output Combination 0 0 0

1. 2. 3.

IF too_hot AND getting_colder THEN cool IF just_right AND getting_ colder THEN heat IF too_cold AND getting_ colder THEN heat

IF too_hot AND no_change THEN cool 0.5 IF just_right AND no_change THEN do_nothing 0.5 IF too_cold AND no_change THEN heat 0

4. 5. 6.

IF too_hot AND no_change THEN cool 0.5 IF just_right AND no_change THEN do_nothing 0.5 IF too_cold AND no_change THEN heat 0

IF too_hot AND getting_hotter THEN cool IF just_right AND getting_hotter THEN cool IF too_ cold AND getting_hotter THEN heat

7. 8. 9.

IF too_hot AND getting_hotter THEN cool IF just_right AND getting_hotter THEN cool IF too_ cold AND getting_hotter THEN heat

0,5 0.5 0

Output membership functions: “do_nothing” = √(rule52) = √(0.52) = 0.5 Bräunl 2003

0 0 0

0,5 0.5 0

Output membership functions: “heat” = √(rule22 + rule32 + rule62 + rule92) = √(0 + 0 + 0 + 0) = 0.0 29

5.6 Defuzzification

Bräunl 2003

30

Defuzzification • Output Membership Function (action to be taken)

• • •

Defuzzification of combined results of inference process Result will be a crisp (numeric) output Calculation of “fuzzy centroid”:

1.

Multiply the weighted strength of each output member function by the respective member function center points Add these values Divide area by the sum of the weighted member function strength

Bräunl 2003

31

Degree of Membership

2. 3.

cool

do_nothing

heat

1.0

0.5

c&n 0.0 -100

Bräunl 2003

-50

n&h 0

+50 +100 Percent output (-100..-1 cooling, +1..+100 heating) 32

Defuzzification

Defuzzification

• Output Membership Function (action to be taken)

• Output Membership Function (action to be taken)

do_nothing

heat

cool Combined rule results cool = 0.866 do_nothing = 0.5 heat = 0.0

0.5

0.0 -100

-50

0

+50 +100 Percent output (-100..-1 cooling, +1..+100 heating)

Bräunl 2003

33

Degree of Membership

Degree of Membership

cool 1.0 0.87

do_nothing

Combined rule results cool = 0.866 do_nothing = 0.5 heat = 0.0

0.5

Calculate fuzzy centroid 0.0 -100

-50 -63.4

0

+50 +100 Percent output (-100..-1 cooling, +1..+100 heating)

Bräunl 2003

34

Defuzzification

Defuzzification

• Input:

• How to calculate centroid

– error = –1 – error_dot = –2.5

heat

1.0 0.87

– graphically: intuitively – numerically: see formula below N

• Output:

Output =

– activate cooling at 63.4%

∑ (center ⋅ strength ) i

i =1

N

∑ strengthi

i

“Moment” N is number of output members (here: 3)

i =1

Bräunl 2003

35

Bräunl 2003

36

1.0 0.87

Defuzzification

1.0 0.87

Defuzzification

0.5

0.5 0.0 -100

1.0 0.87

0.0 -100

0.5

-50

0

with shoulders:

0.0 -100

+50

+100

1.0 0.87 -50

0

+50

Output =

0

+50

+100

centercool ⋅ strengthcool + centernothing ⋅ strengthnothing + centerheat ⋅ strengthheat

Output =

+100

-50

without shoulders: simple formula

strengthcool + strengthnothing + strengthheat − 100 ⋅ 0.866 + 0 ⋅ 0.5 + 100 ⋅ 0 = −63.4 0.866 + 0.5 + 0.0

0.5 N

Output =

∑ (center ⋅ strength ) i

i =1

i

Result: Activate cooling at 63.4%

N

∑ strength i =1

i

0.0 -100

-50

0

Bräunl 2003

+50

+100 37

Bräunl 2003

38

5.7 Tuning

5.8 Applications

Tuning of a fuzzy system can be accomplished by:

Fuzzy control can be applied to a number of control problems for example: • Motor control • Driving • Balancing • Walking • …

• Changing rule antecedents • Changing rule conclusions • Changing centers of the input/output membership functions • Adding additional membership functions e.g. negative, low_neg, zero, low_pos, positive → additional rules

Bräunl 2003

39

Bräunl 2003

40