Sie sind auf Seite 1von 10

Build Mamdani Systems (Code)

On this page Tipping Problem from the Command Line System Display Functions Building a System from Scratch FIS Evaluation The FIS Structure

Tipping Problem from the Command Line


The tipping problem is one of many Fuzzy Logic Toolbox examples of fuzzy inference systems. The FIS is alw ays cast as a MATLAB structure. To load this system, type: a=r e a d f i s ( ' t i p p e r . f i s ' ) This command returns the follow ing result: a= n a m e :' t i p p e r ' t y p e :' m a m d a n i ' a n d M e t h o d :' m i n ' o r M e t h o d :' m a x ' d e f u z z M e t h o d :' c e n t r o i d ' i m p M e t h o d :' m i n ' a g g M e t h o d :' m a x ' i n p u t :[ 1 x 2s t r u c t ] o u t p u t :[ 1 x 1s t r u c t ] r u l e :[ 1 x 3s t r u c t ] The labels on the left of this listing represent the various components of the MATLAB structure associated w ith t i p p e r . f i s . To access the various components of this structure, type the component name after aat the MATLAB prompt. For example: a . t y p e returns the follow ing result: a n s= m a m d a n i The function g e t f i s ( a ) returns almost the same structure information that typing a , alone does: N a m e =t i p p e r T y p e =m a m d a n i N u m I n p u t s=2 I n L a b e l s = s e r v i c e f o o d N u m O u t p u t s=1 O u t L a b e l s=

t i p N u m R u l e s=3 A n d M e t h o d=m i n O r M e t h o d=m a x I m p M e t h o d=m i n A g g M e t h o d=m a x D e f u z z M e t h o d=c e n t r o i d Some of these fields are not part of the structure a . Thus, you cannot get information by typing a . I n l a b e l s , but you can get it by typing g e t f i s ( a , ' I n l a b e l s ' ) Similarly, you can obtain structure information using g e t f i sin this manner. g e t f i s ( a , ' i n p u t ' , 1 ) g e t f i s ( a , ' o u t p u t ' , 1 ) g e t f i s ( a , ' i n p u t ' , 1 , ' m f ' , 1 ) The s t r u c t u r e . f i e l dsyntax also generates this information. For more information on the syntax for MATLAB structures and cell arrays, see Create a Structure Array and Create a Cell Array in the MATLAB documentation. For example, type a . i n p u t or a . i n p u t ( 1 ) . m f ( 1 ) The function g e t f i sis loosely modeled on the Handle Graphics function g e t . The function s e t f i sacts as the reciprocal to g e t f i s . It allow s you to change any property of a FIS. For example, if you w anted to change the name of this system, you could type a=s e t f i s ( a , ' n a m e ' , ' g r a t u i t y ' ) ; How ever, because ais already a MATLAB structure, you can set this information more simply by typing a . n a m e=' g r a t u i t y ' ; Now the FIS structure ahas been changed to reflect the new name. If you w ant a little more insight into this FIS structure, try s h o w f i s ( a ) This syntax returns a printout listing all the information about a . This function is intended more for debugging than anything else, but it show s all the information recorded in the FIS structure Because the variable, a ,designates the fuzzy tipping system, you can display any of the GUIs for the tipping system directly from the command line. Any of the follow ing functions w ill display the tipping system w ith the associated GUI: f u z z y ( a )displays the FIS Editor. m f e d i t ( a )displays the Membership Function Editor. r u l e e d i t ( a )displays the Rule Editor. r u l e v i e w ( a )displays the Rule View er. s u r f v i e w ( a )displays the Surface View er.

If, in addition, ais a Sugeno-type FIS, then a n f i s e d i t ( a )displays the ANFIS Editor GUI. When you open any of these GUIs, you can access any of the other GUIs using the pull-dow n menu rather than the command line.

System Display Functions


There are three functions designed to give you a high-level view of your fuzzy inference system from the command line: p l o t f i s ,p l o t m f , and g e n s u r f . The first of these displays the w hole system as a block diagram much as it w ould appear on the FIS Editor. p l o t f i s ( a )

After closing any open MATLAB figures or GUI w indow s, the function p l o t m f plots all the membership functions associated w ith a given variable as follow s. p l o t m f ( a , ' i n p u t ' , 1 ) returns the follow ing plots

p l o t m f ( a , ' o u t p u t ' , 1 )

These plots appear in the Membership Function Editor GUI, or in an open MATLAB figure, if p l o t m fis called w hile either of these is open. Finally, the function g e n s u r fplots any one or tw o inputs versus any one output of a given system. The result is either a tw o-dimensional curve, or a three-dimensional surface. When there are three or more inputs, g e n s u r fmust be generated w ith all but tw o inputs fixed, as is described in g e n s u r f . g e n s u r f ( a )

Building a System from Scratch


You can build a fuzzy inference system using Fuzzy Logic Toolbox commands as an alternative to the GUI tools. For example, to build the tipping system entirely from the command line, you use the commands n e w f i s ,a d d v a r ,a d d m f , and a d d r u l e . Probably the most difficult part of this process is learning the shorthand that the fuzzy inference systems use for building rules. Use the command line function, a d d r u l eto do this. Each variable, input, or output, has an index number, and each membership function has an index number. The rules are built from statements such as the follow ing: If input1 is MF1 or input2 is MF3, then output1 is MF2 (weight = 0.5) This rule is turned into a structure according to the follow ing logic. If there are m inputs to a system and n outputs, then the first m vector entries of the rule structure correspond to inputs 1 through m. The entry in column 1 is the index number for the membership function associated w ith input 1. The entry in column 2 is the index number for the membership function associated w ith input 2, and so on. The next n columns w ork the same w ay for the outputs. Column m + n + 1 is the w eight associated w ith that rule (typically 1) and column m + n + 2 specifies the connective used (w here AND = 1 and OR = 2). The structure associated w ith the preceding rule is 1320 . 52 This sample code show s one w ay you can build the entire tipping system from the command line, using the MATLAB structure syntax. a = n e w f i s ( ' t i p p e r ' ) ; a . i n p u t ( 1 ) . n a m e = ' s e r v i c e ' ; a . i n p u t ( 1 ) . r a n g e = [ 01 0 ] ; a . i n p u t ( 1 ) . m f ( 1 ) . n a m e = ' p o o r ' ; a . i n p u t ( 1 ) . m f ( 1 ) . t y p e = ' g a u s s m f ' ; a . i n p u t ( 1 ) . m f ( 1 ) . p a r a m s = [ 1 . 50 ] ; a . i n p u t ( 1 ) . m f ( 2 ) . n a m e = ' g o o d ' ; a . i n p u t ( 1 ) . m f ( 2 ) . t y p e = ' g a u s s m f ' ; a . i n p u t ( 1 ) . m f ( 2 ) . p a r a m s = [ 1 . 55 ] ; a . i n p u t ( 1 ) . m f ( 3 ) . n a m e = ' e x c e l l e n t ' ;

a . i n p u t ( 1 ) . m f ( 3 ) . t y p e = ' g a u s s m f ' ; a . i n p u t ( 1 ) . m f ( 3 ) . p a r a m s = [ 1 . 51 0 ] ; a . i n p u t ( 2 ) . n a m e = ' f o o d ' ; a . i n p u t ( 2 ) . r a n g e = [ 01 0 ] ; a . i n p u t ( 2 ) . m f ( 1 ) . n a m e = ' r a n c i d ' ; a . i n p u t ( 2 ) . m f ( 1 ) . t y p e = ' t r a p m f ' ; a . i n p u t ( 2 ) . m f ( 1 ) . p a r a m s = [ 2013 ] ; a . i n p u t ( 2 ) . m f ( 2 ) . n a m e = ' d e l i c i o u s ' ; a . i n p u t ( 2 ) . m f ( 2 ) . t y p e = ' t r a p m f ' ; a . i n p u t ( 2 ) . m f ( 2 ) . p a r a m s = [ 791 01 2 ] ; a . o u t p u t ( 1 ) . n a m e = ' t i p ' ; a . o u t p u t ( 1 ) . r a n g e = [ 03 0 ] ; a . o u t p u t ( 1 ) . m f ( 1 ) . n a m e = ' c h e a p ' a . o u t p u t ( 1 ) . m f ( 1 ) . t y p e = ' t r i m f ' ; a . o u t p u t ( 1 ) . m f ( 1 ) . p a r a m s = [ 051 0 ] ; a . o u t p u t ( 1 ) . m f ( 2 ) . n a m e = ' a v e r a g e ' ; a . o u t p u t ( 1 ) . m f ( 2 ) . t y p e = ' t r i m f ' ; a . o u t p u t ( 1 ) . m f ( 2 ) . p a r a m s = [ 1 01 52 0 ] ; a . o u t p u t ( 1 ) . m f ( 3 ) . n a m e = ' g e n e r o u s ' ; a . o u t p u t ( 1 ) . m f ( 3 ) . t y p e = ' t r i m f ' ; a . o u t p u t ( 1 ) . m f ( 3 ) . p a r a m s = [ 2 02 53 0 ] ; a . r u l e ( 1 ) . a n t e c e d e n t = [ 11 ] ; a . r u l e ( 1 ) . c o n s e q u e n t = [ 1 ] ; a . r u l e ( 1 ) . w e i g h t = 1 ; a . r u l e ( 1 ) . c o n n e c t i o n = 2 ; a . r u l e ( 2 ) . a n t e c e d e n t = [ 20 ] ; a . r u l e ( 2 ) . c o n s e q u e n t = [ 2 ] ; a . r u l e ( 2 ) . w e i g h t = 1 ; a . r u l e ( 2 ) . c o n n e c t i o n = 1 ; a . r u l e ( 3 ) . a n t e c e d e n t = [ 32 ] ; a . r u l e ( 3 ) . c o n s e q u e n t = [ 3 ] ; a . r u l e ( 3 ) . w e i g h t = 1 ; a . r u l e ( 3 ) . c o n n e c t i o n = 2

Tip You can also build the FIS structure using MATLAB w orkspace variables. For example, to specify the range of the input, type: r 1 =[ 01 0 ] a . i n p u t ( 1 ) . r a n g e=r 1 ;

Alternatively, you can build the entire tipping system from the command line using Fuzzy Logic Toolbox commands. These commands are in the m k t i p p e r . mfile. a = n e w f i s ( ' t i p p e r ' ) ; a = a d d v a r ( a , ' i n p u t ' , ' s e r v i c e ' , [ 01 0 ] ) ; a = a d d m f ( a , ' i n p u t ' , 1 , ' p o o r ' , ' g a u s s m f ' , [ 1 . 50 ] ) ; a = a d d m f ( a , ' i n p u t ' , 1 , ' g o o d ' , ' g a u s s m f ' , [ 1 . 55 ] ) ; a = a d d m f ( a , ' i n p u t ' , 1 , ' e x c e l l e n t ' , ' g a u s s m f ' , [ 1 . 51 0 ] ) ; a = a d d v a r ( a , ' i n p u t ' , ' f o o d ' , [ 01 0 ] ) ; a = a d d m f ( a , ' i n p u t ' , 2 , ' r a n c i d ' , ' t r a p m f ' , [ 2013 ] ) ; a = a d d m f ( a , ' i n p u t ' , 2 , ' d e l i c i o u s ' , ' t r a p m f ' , [ 791 01 2 ] ) ; a = a d d v a r ( a , ' o u t p u t ' , ' t i p ' , [ 03 0 ] ) ; a = a d d m f ( a , ' o u t p u t ' , 1 , ' c h e a p ' , ' t r i m f ' , [ 051 0 ] ) ; a = a d d m f ( a , ' o u t p u t ' , 1 , ' a v e r a g e ' , ' t r i m f ' , [ 1 01 52 0 ] ) ; a = a d d m f ( a , ' o u t p u t ' , 1 , ' g e n e r o u s ' , ' t r i m f ' , [ 2 02 53 0 ] ) ; r u l e L i s t = [. . .

11112 20211 32312] ; a = a d d r u l e ( a , r u l e L i s t ) ; Specifying Custom Mem bership and Inference Functions You can create custom membership and inference functions as described in Specifying Custom Membership Functions, and Specifying Custom Inference Functions, and specify them for building fuzzy inference systems at the command line. To include a custom membership function, specify the name of the custom membership function, as show n in the follow ing example: a = a d d m f ( a , ' i n p u t ' , 1 , ' c u s t o m M F 1 ' , ' c u s t m f 1 ' , [ 01246891 0 ] ) ; To include a custom inference function, specify the name of the custom inference function, as show n in the follow ing example: a . d e f u z z M e t h o d = ' c u s t o m d e f u z z ' ;

FIS Evaluation
To evaluate the output of a fuzzy system for a given input, use the function e v a l f i s . For example, the follow ing script evaluates t i p p e rat the input, [1 2]. a=r e a d f i s ( ' t i p p e r ' ) ; e v a l f i s ( [ 12 ] ,a ) a n s= 5 . 5 5 8 6 This function can also be used for multiple collections of inputs, because different input vectors are represented in different parts of the input structure. e v a l f i s ( [ 35 ;27 ] ,a ) a n s= 1 2 . 2 1 8 4 7 . 7 8 8 5

The FIS Structure


The FIS structure is the MATLAB object that contains all the fuzzy inference system information. This structure is stored inside each GUI tool. Access functions such as g e t f i sand s e t f i smake it easy to examine this structure. All the information for a given fuzzy inference system is contained in the FIS structure, including variable names, membership function definitions, and so on. This structure can itself be thought of as a hierarchy of structures, as show n in the follow ing diagram.

You can generate a listing of information on the FIS using the s h o w f i scommand, as show n in the follow ing code sample: s h o w f i s ( a ) 1 . N a m e 2 . T y p e 3 . I n p u t s / O u t p u t s 4 . N u m I n p u t M F s 5 . N u m O u t p u t M F s 6 . N u m R u l e s 7 . A n d M e t h o d 8 . O r M e t h o d 9 . I m p M e t h o d 1 0 .A g g M e t h o d 1 1 .D e f u z z M e t h o d 1 2 .I n L a b e l s 1 3 . 1 4 .O u t L a b e l s 1 5 .I n R a n g e 1 6 . 1 7 .O u t R a n g e 1 8 .I n M F L a b e l s 1 9 . 2 0 . 2 1 . 2 2 . 2 3 .O u t M F L a b e l s 2 4 . 2 5 . 2 6 .I n M F T y p e s 2 7 . 2 8 . 2 9 . 3 0 . 3 1 .O u t M F T y p e s 3 2 . 3 3 . 3 4 .I n M F P a r a m s 3 5 . 3 6 . t i p p e r m a m d a n i [21] [32] 3 3 m i n m a x m i n m a x c e n t r o i d s e r v i c e f o o d t i p [01 0] [01 0] [03 0] p o o r g o o d e x c e l l e n t r a n c i d d e l i c i o u s c h e a p a v e r a g e g e n e r o u s g a u s s m f g a u s s m f g a u s s m f t r a p m f t r a p m f t r i m f t r i m f t r i m f [1 . 5000] [1 . 5500] [1 . 51 000]

3 7 . 3 8 . 3 9 .O u t M F P a r a m s 4 0 . 4 1 . 4 3 . 4 4 . 4 3 . 4 4 . 4 2 .R u l eW e i g h t 4 3 . 4 4 . 4 3 . 4 4 .

[0013] [791 01 0] [051 00] [1 01 52 00] [2 02 53 00] [20] [32] 2 3 1 1 1 1 2

4 2 .R u l eA n t e c e d e n t [11]

4 2 .R u l eC o n s e q u e n t 1

4 2 .R u l eC o n n e c t i o n 2

The list of command-line functions associated w ith FIS construction includes g e t f i s ,s e t f i s ,s h o w f i s ,a d d v a r ,a d d m f ,a d d r u l e ,r m v a r , and r m m f . Saving FIS Files A specialized text file format is used for saving fuzzy inference systems. The functions r e a d f i sand w r i t e f i sare used for reading and w riting these files. If you prefer, you can modify the FIS by editing its . f i stext file rather than using any of the GUIs. You should be aw are, how ever, that changing one entry may oblige you to change another. For example, if you delete a membership function using this method, you also need to make certain that any rules requiring this membership function are also deleted. The rules appear in indexed format in a . f i stext file. The follow ing sample show s the file t i p p e r . f i s . [ S y s t e m ] N a m e = ' t i p p e r ' T y p e = ' m a m d a n i ' N u m I n p u t s = 2 N u m O u t p u t s = 1 N u m R u l e s = 3 A n d M e t h o d = ' m i n ' O r M e t h o d = ' m a x ' I m p M e t h o d = ' m i n ' A g g M e t h o d = ' m a x ' D e f u z z M e t h o d = ' c e n t r o i d ' [ I n p u t 1 ] N a m e = ' s e r v i c e ' R a n g e = [ 01 0 ] N u m M F s = 3 M F 1 = ' p o o r ' : ' g a u s s m f ' , [ 1 . 50 ] M F 2 = ' g o o d ' : ' g a u s s m f ' , [ 1 . 55 ] M F 3 = ' e x c e l l e n t ' : ' g a u s s m f ' , [ 1 . 51 0 ] [ I n p u t 2 ] N a m e = ' f o o d ' R a n g e = [ 01 0 ] N u m M F s = 2 M F 1 = ' r a n c i d ' : ' t r a p m f ' , [ 0013 ] M F 2 = ' d e l i c i o u s ' : ' t r a p m f ' , [ 791 01 0 ]

[ O u t p u t 1 ] N a m e = ' t i p ' R a n g e = [ 03 0 ] N u m M F s = 3 M F 1 = ' c h e a p ' : ' t r i m f ' , [ 051 0 ] M F 2 = ' a v e r a g e ' : ' t r i m f ' , [ 1 01 52 0 ] M F 3 = ' g e n e r o u s ' : ' t r i m f ' , [ 2 02 53 0 ] [ R u l e s ] 11 ,1( 1 ):2 20 ,2( 1 ):1 32 ,3( 1 ):2

Das könnte Ihnen auch gefallen