P. 1
AS-74_3115_anfis

AS-74_3115_anfis

|Views: 91|Likes:
Published by Fajar Maulana

More info:

Published by: Fajar Maulana on Jan 23, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/23/2011

pdf

text

original

ANFIS (Adaptive Neuro-Fuzzy Inference System

)
By Heikki Koivo1 © 2008

1

Heikki Koivo © 2008

1

A fuzzy system (FIS in MATLAB) can be considered to be a parameterized nonlinear map, called f. This point will be made clearer later on in the unified view of soft computing, but let's write here explicitly the expression of f.

f ( x) =

∑ y l (∏ µ Al ( xi ))
l =1 m

m

n

∑ (∏ µ
l =1 i =1

i =1 n

i

Ail

( xi ))

where y l is a place of output singleton if Mamdani reasoning is applied or a constant if Sugeno reasoning is applied. The membership function µ Al ( xi )
i

corresponds to the input x = [ x1 , , xn ] of the rule l . The and connective in the premise is carried out by a product and defuzzification by the center-of-gravity method. This can be further written as
f (x) = ∑ wl bl (x)
l =1 m

where wl = y l and

b j ( x) =

∏µ
i =1 m

n

Ail

( xi )

⎛ n ⎞ ∑ ⎜ ∏ µ Ail ( xi ) ⎟ l =1 ⎝ i =1 ⎠

If F is a continuous, nonlinear map on a compact set, then f can approximate F to any desired accuracy, i.e.
F ≈ f FS

.

2

Prentice Hall. Theorem 3. b ] .F. Theorem 2. If F ∈ C [ a. b ] . Theorem 1. can be extended to fuzzy systems (e. then for any n ≥ 0 . pn which minimizes ∑ F ( x ) − p( x ) i i i =1 k 2 over all polynomials of degree ≤ n . there exists a best approximating polynomial π n of degree ≤ n such that F −πn ∞ ≤ F−p ∞ over all polynomials p of degree ≤ n . b ] and E = { x1 .J. if the mathematical machinery of Hilbert spaces is not familiar. We can also consider the simpler problem of approximating at finitely many points. xk } a set of points in [ a. The same can also be said of trigonometric functions. Curtain and A. . b ] . b ] and E = { x1 . k Then there exists the least squares polynomial of degree ≤ n . . Well-known theorems from Approximation theory for polynomials. Remark: The message of Theorem 2 is that polynomials are dense in the space of continuous functions in Hilbert space. a set of points in [ a. which minimizes max F ( xi ) − p ( xi ) 0≤i ≤ k 3 . Let F be a bounded function on [ a. 1997). The following theorems are found in R.g. k then there exists a best approximating polynomial π n of degree ≤ n .The reader can skip this part without loss of continuity. Wang: A course in fuzzy systems and control. Pritchard: Functional Analysis in Modern Applied Mathematics as Corollaries of Orthogonal Projection Theorem. If F is a bounded function on [ a. xk } .

2. i=1. If x is A2 and y is B2. Evaluating the rule premises results in wi = µ A ( x) µ B ( y ). choose product for T-norm (logical and). If x is A1 and y is B1. ANFIS STRUCTURE Consider a Sugeno type of fuzzy system having the rule base 1. Bi. Evaluating the implication and the rule consequences gives f ( x. y ) f 2 ( x. w1 + w2 This can be separated to phases by first defining wi = wi .over all polynomials of degree ≤ n . 4 . w1 ( x. i i In evaluating the rules. then f1=p1x+q1y+r1 2. be µ A . y ) + w2 ( x. 1. y ) = w1 ( x. Then f can be written as f = w1 f1 + w2 f 2 . y ) f1 ( x. then f2=p2x+q2y+r2 Let the membership functions of fuzzy sets Ai. w1 + w2 These are called normalized firing strengths. 2. y ) . y ) Or leaving the arguments out f = w1 f1 + w2 f 2 . y ) + w2 ( x. i = 1. µ B . i i 2.

5 . It is not a unique presentation.All computations can be presented in a diagram form. x y A1 x A2 B1 y B2 Π w2 Π w1 N w1 w 1 f1 f Σ N w2 x y w2 f 2 This network is called adaptive by Jang is functionally equivalent to Sugeno type of a fuzzy system.

Validate With independent data 6 . Initialize the fuzzy system Use genfis1 or genfis2 commands Give other parameters for learning Important are: Number of iterations (epochs) Tolerance (error) Start learning process Use command anfis Stop when tolerance is achieved.Basic flow diagram of computations in ANFIS.

1. GENFIS1(DATA. using grid partition style. If MF_N is a number and/or IN_MF_TYPE is a single string. data = [rand(NumData. default input membership function type is 'gbellmf'. GENFIS1 and ANFIS Commands It is not clear at the beginning. 'genfis1'. IN_MF_TYPE is a string array where each row specifies the MF type of an input variable.1)]. This provides an optimization scheme to find the parameters in the fuzzy system that best fit the data. It is explained in the Toolbox manual that since most (not all) optimization algorithms require computation of the gradient. MfType = str2mat('pimf'. can be used. For example: % Generate random data NumData = 1000. Then. say those in the MATLAB Optimization Toolbox. IN_MF_TYPE) generates a fismatrix from training data DATA. they will be used for all inputs. % Plot the membership functions figure('name'. 7 . 'numbertitle'. command genfis1 may be used.In Fuzzy Control Toolbox a useful command called anfis exists. in principle. the type and number of membership functions. GENFIS1 Generate FIS matrix using generic method. 'off'). Default number of membership function MF_N is 2. what the initial fuzzy system should be. NumMf. 2) . Note that MF_N and IN_MF_TYPE are pass directly to GENPARAM for generating input MF parameters. that is. % Specify number and type of membership functions NumMf = [3 7]. this is done with a neural network. 'trimf'). This command will go over the data in a crude way and find a good starting system.1)-5 rand(NumData. any of the optimization schemes. MF_N. MF_N is a vector specifying the number of membership functions on all inputs. MfType). NumInput = size(data.1) 10*rand(NumData. % Use GENFIS1 to generate initial membership functions FisMatrix = genfis1(data.

1] consisting of sum of three sinusoidal functions y = 0. plotmf(FisMatrix. i). subplot(NumInput. EXAMPLE 1. Consider fitting a fuzzy system on a nonlinear function on [-1. 'input'. ANFIS. :) ')']). end See also GENPARAM. SUMMARY: Use genfis1 to generate initial FIS system.3 sin(3πx) + 0. 1. Use anfis to generate the best FIS system.for i = 1:NumInput.6 sin(πx) + 0. SOLUTION: 8 .1 sin(5πx) . Anfis uses the result from genfis1 to start optimization. xlabel(['input ' num2str(i) ' (' MfType(i. i).

ylabel('Measured data for y').numPts)'.:). 9 . use part for training part for checking data=[x y].3*sin(3*pi*x)+0.6*sin(pi*x)+0.1).chkdata(:.1.1*sin(5*pi*x). % checking data set % Plot training data (o) and checking data (x) plot (trndata(:.1).chkdata(:. 'o'. % Output data y y=0.The problem is solved using MATLAB and Fuzzy Logic Toolbox. % training data set chkdata = data(2:2:numPts.x=linspace(-1.trndata(:.2). % Next apply genfis1 to generate initial set of membership functions.'x') grid title('Training data (x) and checking data (o)') xlabel('x').2). % Store data in matrix called data. % total data set trndata = data(1:2:numPts.:). we first generate an input-output data set % Number of input data points numPts = 51 and input data x numPts=51. Since we are not provided data.

% membership functions type is generalized bell fismat = genfis1(trndata.nummfs=5.'input'. 10 . You can see the plot of the initial membership functions by opening anfis under FIS editor and then looking at the input membership functions or plotting them with command plotmf(fismat. % number of membership functions mftype='gbellmf'. First open the GUI for Fuzzy Logic Toolbox by typing command fuzzy Then in the FIS Editor choose New Sugano system from File menu.nummfs.mftype). Another way to proceed is to use Anfis GUI.1).

11 .Then in View menu choose Edit Anfis This will result in the following display.

3. 1. Here you also have to be careful of where the data are located. or Demo. Suppose you have generated data on MATLAB command side.Anfis Editor display is divided into four main subdisplays: 1. 2. In our example it is in workspace so you have to change the default value 12 . Load data Generate FIS Train FIS Test FIS Let us consider them in more detail. Checking. It should be in a matrix form. 4. Testing. Data can be either stored on disk or it is in workspace only. because you just generated it. For your first effort stick with Training data. Load data Here you can choose Type of data according to the purpose: Training (default choice). where the first columns consist of input data and the last of output data.

13 . Click Generate FIS and the following window opens. Easiest option is to leave the default choice. 2. General FIS Once data have been loaded. Click Load data and you are asked to type Input variable name to the space provided. Remember that the data are in matrix form.disk to workspace. Grid partition on. an initial fuzzy system can be generated using Generate FIS.

3. 14 . Epochs means number of iterations. so change this to. say 0. a complete fit is not achieved. It is not unusual to have hundreds or even thousands of iterations. Pick 100. Click Train now.01. During training you can see how Training error develops. Here hybrid algorithm is used. The default value 3 is hardly ever enough. Train FIS The main computing occurs in the Training block. Now you are ready for training. The Error tolerance is 0. In practice.As default 4 gbellmf-memebrship functions are chosen.

Default value is training data. testing or checking data. Click Test now and you will see the following display.You can improve the situation by introducing more membership functions. 15 . Test FIS Once the result is satisfactory. you can test the performance of FIS against either training. 4.

anfis(trndata.. NaN. fismat2.MATLAB Command side Now start the actual optimization procedure by calling anfis. ANFIS info: Number of nodes: 24 Number of linear parameters: 10 Number of nonlinear parameters: 15 Total number of parameters: 25 Number of training data pairs: 26 16 . numepochs.. ss. % Determine number of iterations numepochs = 40. trnerr. % Start the optimization [fismat1. chkerr]= . fismat. chkdata).

0494663 0.0464298 Step size decreases to 0.0117976 Designated epoch number reached --> ANFIS training completed at epoch 40.010380 Minimal checking RMSE = 0.0571745 0.1) 17 ...Number of checking data pairs: 25 Number of fuzzy rules: 5 Start training ANFIS .058193 0.0110313 0.0480347 0.0541812 0.0524169 0. Minimal training RMSE = 0.0511445 0. 1 2 3 4 5 …… 0.0601596 0.'input'.0112159 Next check the final membership functions again by plotmf or using FIS Editor plotmf(fismat1.0553133 0. 40 0.010567 after epoch 39.

grid 18 .1). % Plots out vs x plot(chkdata(:.plot(chkdata(:.2)).1).1).% Plots y vs x (training data) plot(x.y).chkdata(:.% Evaluates the output of the fuzzy system fismat1 .Draw the fitted curve and checking data in the same figure: out=evalfis(chkdata(:.fismat1).input checking hold.out).

19 .

Study also other case when you add noise to your measurement data set.Unity weight for each rule 20 . EXERCISE 2.EXERCISE 1. Choose different value for number of membership functions and different type of membership functions. we can call ANFIS with from 2 to 4 input arguments and it returns from 1 to 3 output arguments . It can only be used for Sugeno systems and further: .Single output derived by weighted average defuzzification In MATLAB help anfis the following is said When a specific Sugeno fuzzy inference system is used for fitting. REMARK: Anfis command has limitations. Repeat Example 1 and Exercise 1 for humps(x) function.Constant and linear output membership functions only .

'genfis1'. MfType). 1. 2) . end See also GENPARAM.APPENDIX To generate the initial FIS matrix use genfis1. ANFIS Training routine for Sugeno-type FIS (MEX only). using grid partition style. data = [rand(NumData. for i = 1:NumInput. xlabel(['input ' num2str(i) ' (' MfType(i. 'numbertitle'. Type help genfis1 in MATLAB to see the following. Note that MF_N and IN_MF_TYPE are pass directly to GENPARAM for generating input MF parameters.1) 10*rand(NumData. :) ')']).1. 'trimf'). 'input'. Default number of membership function MF_N is 2. plotmf(FisMatrix. If MF_N is a number and/or IN_MF_TYPE is a single string. IN_MF_TYPE is a string array where each row specifies the MF type of an input variable. ANFIS.1)-5 rand(NumData. NumMf. i). To determine the best FIS system use anfis. MF_N is a vector specifying the number of membership functions on all inputs. % Use GENFIS1 to generate initial membership functions FisMatrix = genfis1(data. 'off'). MF_N. GENFIS1 Generate FIS matrix using generic method. NumInput = size(data. GENFIS1(DATA. 21 .1)]. i). For example: % Generate random data NumData = 1000. IN_MF_TYPE) generates a fismatrix from training data DATA. MfType = str2mat('pimf'. default input membership function type is 'gbellmf'. subplot(NumInput. % Specify number and type of membership functions NumMf = [3 7]. % Plot the membership functions figure('name'. they will be used for all inputs.

22 . with output at the last column. IN_FISMAT: input FIS matrix to specify the structure and initial parameters for fitting. FISMAT: output FIS matrix. This can be generated from data directly using the command GENFIS. respectively. in a Sugeno-type fuzzy inference systems. T_OPT(1): training epoch number (default: 10) T_OPT(2): training error goal (default: 0) T_OPT(3): initial step size (default: 0.ANFIS applies the least-squares method and the back-propagation gradient descent for identifying linear and nonlinear parameters. FISMAT = ANFIS(TRN_DATA) attempts to fit a training data TRN_DATA using a Sugeno FIS with 2^N rules. a null matrix. When a specific Sugeno fuzzy inference system is used for fitting. If T_OPT itself is missing. D_OPT(1): display ANFIS information (default: 1) D_OPT(2): display error measure (default: 1) D_OPT(3): display step size (default: 1) D_OPT(4): display final results (default: 1) The parsing of D_OPT is the same as T_OPT. where N is the number of inputs. we can call ANFIS with from 2 to 4 input arguments and it returns from 1 to 3 output arguments: [FISMAT. The training process stops when the designated epoch number is reached or when the training goal is achieved. then it will also take default value. STEPSIZE] = ANFIS(TRN_DATA. which is the FIS matrix corresponding to the minimum training error.9) T_OPT(5): step size increase rate (default: 1. or an NaN. (This is recommended only when N is less than 7.) The final fuzzy inference system is returned as a FIS matrix FISMAT. IN_FISMAT. ERROR: array of root mean squared errors. The format of TRN_DATA is described below. D_OPT: display options.1) If any element of T_OPT is NaN (not a number).01) T_OPT(4): step size decrease rate (default: 0. The step size is decreased (by multiplying the decrease rate) if the error measure undergoes two consecutive combinations of increase and decrease. where each row is a desired input/output data pairs. then the default value is used. ERROR. Default values can be changed directly by modifying this file. T_OPT: training options. STEPSIZE: array of step sizes. T_OPT. increased (by multiplying the increase rate) if the error measure undergoes four consecutive decreases. D_OPT) TRN_DATA: training data matrix.

/exp(x/5) + randn(size(x))/30. plot(x. OUTPUT_STACK is a stack of output (row) vectors. T_ERROR: array of root mean squared training errors. NumEpochs = 20. input = [xx(:) yy(:)]. OutputFismat = anfis(TrainData. x.. fismat = readfis('mam21'). NumMfs. with the same format as TRN_DATA. FISMAT2. T_OPT: training options.2:10)'.. 'x'. For example: [xx. MfType). training error. y. CHK_DATA) TRN_DATA: training data matrix. see above. IN_FISMAT. StepSize = 0. 'o'. [NumEpochs nan StepSize]). yy. FISMAT1: output FIS matrix corresponding to the min. see above. 23 . ANFIS(TRN_DATA. OutputFismat). y = sin(2*x). InputFismat. FISMAT1: output FIS matrix corresponding to the min. EVALFIS Evaluation of a fuzzy inference system. x. InputFismat = genfis1(TrainData. yy. or a matrix where each row specifies an input vector. checking error. D_OPT: display options. NumMfs = 5. IN_FISMAT: input FIS matrix. 'y'. x. legend('Training Data'. INPUT_STACK can be a vector specifying the input vector.If checking data is involved in the training process. see above. T_OPT. yy = evalfis(x. fismat).1. C_ERROR: array of root mean squared checking errors. CHK_DATA: checking data matrix. see above. FISMATRIX) computes the output of a fuzzy inference system specified by FISMATRIX. C_ERROR] = . ANFISMEX. 'ANFIS Output'). D_OPT. OUTPUT_STACK = EVALFIS(INPUT_STACK. y. MfType = 'gbellmf'. yy] = meshgrid(-5:5). STEPSIZE. T_ERROR. out = evalfis(input. 'm'). then ANFIS should be called with 5 input arguments and it returns from 1 to 5 output arguments: [FISMAT1. For example: x = (0:0. STEPSIZE: array of step sizes. See also GENFIS1. TrainData = [x y]. To determine the output of the FIS system for given input use evalfis.

surf(xx. reshape(out. 11)) title('evalfis') produces 24 . yy. 11.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->