fuzzy control chapter 14. fuzzy control fuzzy sets design of a fuzzy controller –fuzzification of...

53
Fuzzy Control Chapter 14

Post on 21-Dec-2015

260 views

Category:

Documents


4 download

TRANSCRIPT

Fuzzy Control

Chapter 14

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 Sets

The sentence on the other sideof the line is false

The sentence on the other sideof the line is false

Is this sentence true or false?

Agea1 a2

Young1

0

F igu re 14 .2 M em bersh ip func tion fo r the fuzzy se t Y oung

Table 14.1 Fuzzy Logic OperationsLogic Operation Fuzzy Logic Operation

NOT A 1 - AA AND B MIN(A,B)A OR B MAX(A,B)

Table 14.1 Fuzzy Logic OperationsLogic Operation Fuzzy Logic Operation

NOT A 1 - AA AND B MIN(A,B)A OR B MAX(A,B)

Agea1 a2

Young1

0

NOT Young

Figure 14.3 NOT Young = 1 - Young

Age

Old1

0

NOT Old

F igu re 14 .4 M em be rsh ip func tions fo r the fuzzy se ts O ld and N O T O ld

NOT OldNOT Young

Age

1

0

Age

1

0

Middle Age = NOT Young AND Not Old

(a)

(b)

Figure 14.5 Deriv ing the membership function for Middle Age

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

128 174 22082360

1

Universe of discourse2550

Figure 14.6 Example of fuzzy membership functions

INPUTS

OUTPUT

Map to Fuzzy Sets

FUZZY RULES

If A AND B then L

• • •

Defuzzification

get_inputs();

fire_rules();

find_output();

A Fuzzy Controller

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()

u1 u2 u4

1

wt

u3

slope_1 slope_2

Figure 14.10 A membership function is defined in terms of u1, u2, u3, and u4

The 68HC12 MEM Instruction

NM NS Z PS PM

128 174 22082360

1

Universe of discourse2550

Figure 14.6 Example of fuzzy membership functions

The 68HC12 MEM Instruction

A xiweight(j)

X --> point_1 Y -->point_2 Y + 1 -->slope_1slope_2

X + 4 -->

Figure 14.11 Data structure used by the 68HC12 MEM instruction

pfa 5 # mem fncs^wt pointer to weight array

X --> 0 point_172 point_2

slope_1slope_2

48 point_1116 point_2

slope_1slope_2

104 point_1152 point_2

slope_1slope_2

140 point_1208 point_2

slope_1slope_2

184 point_1255 point_2

slope_1slope_2

Figure 14.12 Data structure for storing membership function parameters

* Fill Weight Arrays* fill.weights ( xi pfa -- )FILLWT

LDY 2,X+ Y = pfaLDD 2,X+ B = xiPSHX save XTFR Y,X X = pfaLDY 2,X Y -> input weight tablePSHB save xiLDD 4,X+ B = #mem functs; X->1st memb fncPULA A = xi

FW1 MEM fuzzy membership gradeDBNE B,FW1PULX restore XRTS

Figure 14.13 68HC12 code for the WHYP word fill.weights ( xi addr -- )

fuzzy.input ball_position 0 0 36 72 >m \ neg_far 48 82 100 116 >m \ neg_close 104 128 128 152 >m \ zero_p 140 156 174 208 >m \ pos_close 184 220 255 255 >m \ pos_far

end.input

Figure 14.14 Format for entering membership functions

NM NS Z PS PM

128 174 22082360

1

Universe of discourse2550

Figure 14.6 Example of fuzzy membership functions

Listing 14.1 Fuzzy Control in WHYP -- Defining Membership Functions\ Fuzzy Logic Control Using WHYP12 File: FUZZY12.WHP\ *********************************************\ Fuzzy Control\ *********************************************

\ ********** Defining word for input **********: fuzzy.input ( +++ )

CREATE HERE 0 , 0 , DOES> ( pfa -- ) ;

: get.slopes ( u1 u2 u3 u4 -- )3 PICK C, \ store point_1DUP C, \ store point_22SWAP \ u3 u4 u1 u2SWAP - ?DUPIF 255 SWAP / C, \ store slope_1ELSE 0 C, \ vertical slopeTHEN \ u3 u4SWAP - ?DUPIF 255 SWAP / C, \ store slope_2ELSE 0 C, \ vertical slopeTHEN ;

: 4DROP ( n1 n2 n3 n4 -- )2DROP 2DROP ;

: >m ( pfa u1 u2 u3 u4 -- pfa )DUP 255 >IF 4DROP CR ." ERROR: u4 > 255"ELSE \ pfa u1 u2 u3 u4 2DUP > IF 4DROP CR ." ERROR: u3 > u4" ELSE \ pfa u1 u2 u3 u4 2OVER > IF 4DROP CR ." ERROR: u1 > u2" ELSE \ pfa u1 u2 u3 u4 2 PICK 2 PICK > IF 4DROP CR ." ERROR: u2 > u3" ELSE \ pfa u1 u2 u3 u4

get.slopes THEN THEN THENTHEN1 OVER +! ; \ inc #member fncs

: end.input ( pfa -- ) VHERE OVER 2+ ! @ VALLOT ;

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

m

m

m

m

m

m

m

x1 x2

x1 x2

y

y

A1

A2

B1

B2

L1

L2

yy0

a b

w1

w2w2*L1

w1*L2

sum

w1

w2

rule 1

rule 2

L'

Figure 14.15 Fuzzy inference

m

yy0

L'

m

yy0

L'

m

yy0

L'

Maximum Sum Singletons

F ig u re 1 4 .1 6 C o m p a r in g th e M A X ru le a n d th e S U M ru le

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()

m

m

m

m

m

m

m

x1 x2

x1 x2

y

y

A1

A2

B1

B2

L1

L2

yy0

a b

w1

w2w2*L1

w1*L2

sum

w1

w2

rule 1

rule 2

L'

Figure 14.15 Fuzzy inference

Y--> neg_far 0 weight(1) in.out.arraypfa

#rules rules

neg_close 1 - - Out0 zero_p 2 - - X--> 0 x1 is neg_far

pos_close 3 - - 5 x2 is neg_fastpos_far 4 - - FE

neg_fast 5 weight(2) 15 y is pos_highneg_slow 6 - - FE

zero_s 7 - - 0 x1 is neg_farpos_slow 8 - - 6 x2 is neg_slowpos_fast 9 - - FE

10 5 Out0 15 y is pos_highneg_high 11 0 Out FEneg_low 12 0zero_m 13 0pos_low 14 0 11

pos_high 15 0 A =$FF

FF end of rules

Figure 14.18 Setup required for REV instruction

The 68HC12 REV Instruction

* Fire all rules* firerules ( in.out.array rules -- )FIRERULES

LDD 2,X+ D -> rulesLDY 2,X+ Y -> in.out.arrayPSHXTFR D,X X -> rulesINXINX X -> Out0 addrPSHY save YLDY 2,X+ Y -> Out0, X -> 1st ruleLDAB 0,Y B = #out.memb.fncs

FR0 CLR 1,Y+ clear Out arrayDBNE B, FR0PULY Y -> in.out.arrayLDAA #$FFREV rule evaluationPULXRTS

Figure 14.19 68HC12 code for WHYP word firerules ( in.out.array rules-- )

make.rules rules\ ball_position ball_speed motor_power

neg_far C, neg_fast C: pos_high C; neg_far C, neg_slow C: pos_high C; ----- ----- ----- ----- pos_far C, pos_slow C: neg_high C; pos_far C, pos_fast C: neg_high C.

end.rules

Figure 14.20 Format for defining fuzzy rules

Listing 14.2 Fuzzy Control in WHYP -- Making rules\ Fuzzy Logic Control Using WHYP12 File: FUZZY12.WHP\ ********** Defining word for making rules **********: make.rules ( +++ )

CREATE HERE 0 , \ leave room for #rules 0 , \ leave room for ^Out0DOES> ( pfa -- ) ;

HEX

C: ( pfa ix -- pfa )C, FE C, ;

C; ( pfa ix -- pfa )C, FE C,1 OVER +! ; \ inc #rules

C. ( pfa ix -- pfa )C, FF C,1 OVER +! ; \ inc #rules

DECIMAL

: end.rules ( pfa -- )DROP ; \ drop addr of #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

m

m

m

m

m

m

m

x1 x2

x1 x2

y

y

A1

A2

B1

B2

L1

L2

yy0

a b

w1

w2w2*L1

w1*L2

sum

w1

w2

rule 1

rule 2

L'

Figure 14.15 Fuzzy inference

14.2.4 Centroid Defuzzification

The last step in the fuzzy controller shown in Figure 14.7 is defuzzification. Thisinvolves finding the centroid of the net output fuzzy set L' shown in Figures 14.15 and14.16. Although we have used the MIN-MAX rule in the previous section we will beginby deriving the centroid equation for the sum rule shown in Figure 14.16. This willilluminate the assumptions made in deriving the defuzzification equation that we willactually use in the fuzzy controller.

Let Li(y) be the original output membership function associated with rule i where yis the output universe of discourse (see Figure 14.15.). After applying rule i thismembership 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 reducedoutput membership functions over all rules is then given by

M(y) = i=1

Nmi(y) (14.2)

where N is the number of rules.

The crisp output value y0 is then given by the centroid of M(y) from the equation

y0 = yM(y)dy

M(y)dy (14.3)

Note that the centroid of membership function Li(y) is given by

ci = yLi(y)dy

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 = y

i=1

NwiLi(y) dy

= i=1

N

ywiLi(y) dy

= i=1

N wiciIi (14.7)

where (14.6) was used in the last step.

Similarly, using (14.1) and (14.2) the denominator of (14.3) can be written as

M(y)dy =

i=1

NwiLi(y) dy

= i=1

N

wiLi(y) dy

= i=1

N wiIi (14.8)

where (14.5) was used in the last step. Substituting (14.7) and (14.8) into (14.3) we canwrite the crisp output of the fuzzy controller as

y0 =

i=1

N wiciIi

i=1

N wiIi

(14.9)

Eq. (14.9) says that we can compute the output centroid from the centroids, ci, of theindividual output membership functions.

Note in Eq. (14.9) the summation is over all N rules. But the number of outputmembership functions, Q, will, in general, be less than the number of rules, N. This meansthat in the sums in Eq. (14.9) there will be many terms that will have the same values of ciand Ii. For example, suppose that rules 2, 3, and 4 in the sum all have the outputmembership 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 andarea of the kth output membership function. These three terms would then contribute thevalue

(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

y0 =

k=1

Q

WkckIk

k=1

Q

WkIk

(14.10)

If the area of all output membership functions, Ik are equal, then Eq. (14.10) reduces to

y0 =

k=1

Q

Wkck

k=1

Q

Wk

(14.11)

Eqs. (14.10) and (14.11) show that the output crisp value of a fuzzy controller can becomputed by summing over only the number of output membership functions rather thanover all fuzzy rules. Also, if we use Eq. (14.11) to compute the output crisp value, thenwe need to specify only the centroids, ck, of the output fuzzy membership functions. Thisis equivalent to assuming singleton fuzzy sets for the output.

We will always use singleton fuzzy sets for the output represented by thecentroids, ck. We will also use the MIN-MAX inference rule described in the previoussection. It should be clear from Figure 14.16 that in this case the centroid y0 will still begiven by Eq. (14.11) where Wk is now the output array, Out(k), shown in Figure 14.18and 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()

The 68HC12 WAV InstructionThe values of numer and denom in Figure 14.21 can easily be calculated using the

68HC12 WAV instruction. If index register X points to c[k], index register Y points toOut[k], and accumulator B contains the number of output membership functions, Q, thenthe 68HC12 WAV instruction will compute a 24-bit value for numer and store the resultin Y:D and compute a 16-bit value for denom and store this result in X. Therefore, if theWAV instruction is followed by the instruction EDIV (see Section 3.32) then the centroidvalue y0 will be left in Y.

The WHYP word calc.output ( rules cent_addr -- output_value ) will expectthe address of rules (see Figure 14.18) and the address of the centroid array, c[k], on thedata stack and will return the crisp output value, y0. The 68HC12 code for the wordcalc.output is shown in Figure 14.22.

* Find Output value* calc.output ( rules cent_addr -- output_value )FINDOUT

LDD 2,X+ D -> centLDY 2,X+ Y -> rulesPSHXTFR D,X X -> cent arrayLDY 2,Y Y -> Out0LDAB 0,Y B = #out.memb.fncsINY Y -> Out arrayWAVEDIV Y = quotientTFR Y,D D = quotient A = 0, B = outputPULXSTD 2,-XRTS

Figure 14.22 68HC12 code for calc.output ( rules cent_addr -- output_value )

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

ball_position neg_far neg_close zero_p pos_close pos_far

pos_fast zero_m neg_low neg_high neg_high neg_high

pos_slow pos_low zero_m neg_low neg_high neg_high

ball_speed 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

Listing 14.3 Fuzzy Control Example -- Ping-Pong Ball\ Fuzzy Control Example File: PINGPONG.WHP\ Floating ping-pong ball

LOAD FUZZY12.WHP\ ******************************************************\ Define inputs\ ******************************************************

\ Ball position0 CONSTANT neg_far1 CONSTANT neg_close2 CONSTANT zero_p3 CONSTANT pos_close4 CONSTANT pos_far

\ Ball speed5 CONSTANT neg_fast6 CONSTANT neg_slow7 CONSTANT zero_s8 CONSTANT pos_slow9 CONSTANT pos_fast

\ Define input membership functions

fuzzy.input ball_position 0 0 30 50 >m \ neg_far 40 60 80 110 >m \ neg_close 100 120 135 165 >m \ zero_p 155 175 185 210 >m \ pos_close 200 220 255 255 >m \ pos_far

end.input

fuzzy.input ball_speed 0 0 20 60 >m \ neg_fast 50 70 90 120 >m \ neg_slow 100 125 135 155 >m \ zero_s 140 170 180 200 >m \ pos_slow 190 210 255 255 >m \ pos_fast

end.input

\ ******************************************************\ Define output\ ******************************************************5 CONSTANT #out.member.fncs \ No. of output membership functionsCREATE cent \ output centroid array

5 C, 65 C, 128 C, 175 C, 200 C,\ |neg_high|neg_low|zero_m|pos_low|pos_high|

10 CONSTANT Out0\ Power percent change to motor11 CONSTANT neg_high12 CONSTANT neg_low13 CONSTANT zero_m14 CONSTANT pos_low15 CONSTANT pos_high

ball_position @ 2- VALLOT \ input 1 weight arrayball_speed @ VALLOT \ input 1 weight array#out.member.fncs 1+ VALLOT \ controller output weights

\ ******************************************************\ Define rules\ ******************************************************

make.rules rules\ ball_position ball_speed motor_power

neg_far C, neg_fast C: pos_high C; neg_far C, neg_slow C: pos_high C; neg_far C, zero_s C: pos_high C; neg_far C, pos_slow C: pos_low C; neg_far C, pos_fast C: zero_m C; neg_close C, neg_fast C: pos_high C; neg_close C, neg_slow C: pos_high C; neg_close C, zero_s C: pos_low C; neg_close C, pos_slow C: zero_m C; neg_close C, pos_fast C: neg_low C; zero_p C, neg_fast C: pos_high C; zero_p C, neg_slow C: pos_low C; zero_p C, zero_s C: zero_m C; zero_p C, pos_slow C: neg_low C; zero_p C, pos_fast C: neg_high C; pos_close C, neg_fast C: pos_low C; pos_close C, neg_slow C: zero_m C; pos_close C, zero_s C: neg_low C; pos_close C, pos_slow C: neg_high C; pos_close C, pos_fast C: neg_high C; pos_far C, neg_fast C: zero_m C; pos_far C, neg_slow C: neg_low C; pos_far C, zero_s C: neg_high C; pos_far C, pos_slow C: neg_high C; pos_far C, pos_fast C: neg_high C.

end.rules

\ ******************************************************\ Implement fuzzy controller\ ******************************************************

: init.Out.array ( -- )#out.member.fncs \ #in.out.array Out0 + \ # Out0DUP rules 2+ ! \ store Out0 @rules+2C! ; \ store # @Out0

\ Compute output power given input pos and speed: fuzzy ( pos speed -- power )

ball_speed fill.weightsball_position fill.weightsin.out.array rules firerulesrules cent calc.output ;

: fuzzy_control ( -- )init.Out.arrayBEGIN

\ get.position \ pos (user defined) \ get.speed \ pos speed (user defined)

fuzzy \ power \ adjust.motor \ (user defined)

AGAIN ;