Fuzzy Final Assignment

download Fuzzy Final Assignment

of 27

Transcript of Fuzzy Final Assignment

  • A REPORT ON

    REAL LIFE APPLICATION OF FUZZY LOGIC (WITH MATLAB IMPLIMENTATION) Course: map 701 Submitted by:

    DONKESHWARAM HARAHARI

    (Entry no: 2010 MAS 7133)

    Guidance by:

    Dr. Ram laL

    DEPARTMENT OF MATHEMATICS

    INDIAN INSTITUTE OF TECHNOLOGY DELHI NOVEMBER 4, 2011.

  • DECLARATION Here in this report, I encapsulated my work on FUZZY LOGIC. So, I explained my work in sequence as given below:

    Brief introduction to fuzzy logic with small example on Air Conditioner controlling according to variations in temperatures.

    Brief explanation about a real life application example. i.e., on PLACEMENTING of a student in campus recruitments based on his

    CGPA and PERSONALITY.

    Explanation of fuzzy logic algorithm through systematic steps using

    above PLACEMENTING problem with matlab code. The matlab code for the problem without using inbuilt matlab functions

    The same problem is implemented by using GUI and compared with my own code results.

    I am grateful to Dr. Ram Lal for his constant motivation and guidance. I

    am highly obliged to him for constantly encouraging us by giving his critics

    on my work.

    Date: November 4, 2011. DONKESHWARAM HARAHARI

    (ENTRY NO: 2010 MAS7133)

  • 1 .INTRODUCTION

    Fuzzy logic is a form of many-valued logic; it deals with reasoning that is

    approximate rather than fixed and exact. In contrast with traditional logic

    theory, where binary sets have two-valued logic: true or false, fuzzy logic

    variables may have a truth value that ranges in degree between 0 and 1. Fuzzy

    logic has been extended to handle the concept of partial truth, where the truth

    value may range between completely true and completely false. Furthermore,

    when linguistic variables are used, these degrees may be managed by specific

    functions (membership functions).

    Fuzzy logic began with the 1965 proposal of fuzzy set theory by Lotfi

    Zadeh. Fuzzy logic has been applied to many fields, from control

    theory to artificial intelligence.

    Now we explain the A fuzzy logic system (FLS)/ A fuzzy inference system

    (FIS) systematically:

    A fuzzy logic system (FLS) can be defined as the nonlinear mapping of an

    Input data set to a scalar output data. A FLS consists of four main parts:

    1. Fuzzifier, 2. Rules, 3.Inference engine, and 4.Defuzzifier. These components and the general architecture of a FLS is shown in below

    Figure.

    Figure2: A Fuzzy Logic System

  • The process of fuzzy logic is explained in Algorithm given below:

    Fuzzy logic algorithm:

    1. Define the linguistic variables and terms (initialization)

    2. Construct the membership functions (initialization)

    3. Construct the rule base (initialization)

    4. Convert crisp input data to fuzzy values (fuzzification).

    using the membership functions 5. Evaluate the rules in the rule base (inference)

    Applying Fuzzy Operators (min, max, product etc.) in each rule. Applying Implication Operators (min, Larsen product etc.) in each rule.

    6. Combine the results of each rule (inference)

    By applying the Aggregation Operators. 7. Convert the output data to non-fuzzy values (defuzzification)

    By finding centroid,bisectors etc. Of fuzzy output which we got from above step.

    **The process of fuzzy logic is explained in Algorithm:

    Firstly, a crisp set of input data are gathered and converted to

    a fuzzy set using fuzzy linguistic variables, fuzzy linguistic terms and

    membership functions. This step is known as fuzzification. Afterwards, an

    inference is made based on a set of rules. Lastly, the resulting fuzzy output is

    mapped to a crisp output using the membership functions, in the defuzzification

    step.

    In order to exemplify the usage of a FLS, consider an air

    conditioner system controlled by a FLS (Figure given below). The system

    adjusts the temperature of the room according to the current temperature of the

    room and the target value.

    The fuzzy engine periodically compares the room temperature and the target

    Temperature, and produces a command to heat or cool the room.

    **Linguistic Variables:

    Linguistic variables are the input or output variables of the system whose

  • values are words or sentences from a natural language, instead of numerical

    values. A linguistic variable is generally decomposed into a set of linguistic

    terms.

    Example:

    Consider the air conditioner in Figure 2. Let temperature (t) is the

    Linguistic variable which represents the temperature of a room. To qualify the

    Temperature, terms such as hot and cold are used in real life. These are the Linguistic values of the temperature. Then, T (t) = {too-cold, cold,

    warm, hot, too-hot} can be the set of decompositions for the linguistic variable

    temperature. Each member of this decomposition is called a linguistic term and

    can cover a Portion of the overall values of the temperature.

    **Membership Functions:

    Membership functions are used in the fuzzification and defuzzification

    steps of a FLS, to map the non-fuzzy input values to fuzzy linguistic terms and

    vice versa. A membership function is used to quantify a linguistic term. For

    instance, In below Figure, membership functions for the linguistic terms of

    temperature variable are plotted. Note that, an important characteristic of fuzzy

    logic is that a numerical value does not have to be fuzzified using only one

    membership function. In other words, a value can belong to multiple sets at the

    same time. For example, according to Figure 3, a temperature value can be

    considered as cold and too-cold at the same time, with different degree of memberships.

    Membership Functions for T(temperature) = {too-cold, cold, warm, hot, too-hot} figure 3

    There are different forms of membership functions such as triangular,

    trapezoidal, piecewise linear, Gaussian, or singleton (Figure given below).

    The most common types of membership functions are triangular, trapezoidal,

  • and Gaussian shapes. The type of the membership function can be context

    dependent and it is generally chosen arbitrarily according to the user

    experience.

    Different Types of Membership Functions.

    **Fuzzy Rules :

    In a FLS, a rule base is constructed to control the output variable. A fuzzy

    Rule is a simple IF-THEN rule with a condition and a conclusion. In Table 1,

    Sample fuzzy rules for the air conditioner system in its corresponding figure

    above. Table 2 shows the matrix representation of the fuzzy rules for the said

    FLS. Row captions in the matrix contain the values that current room

    temperature can take, column captions contain the values for target

    temperature, and each cell is the resulting command when the input variables

    take the values in that row and column. For instance, the cell (3, 4) in the

    matrix can be read as follows: If temperature is cold and target is warm then

    command is heat.

  • **Fuzzy Set Operations:

    The evaluations of the fuzzy rules and the combination of the results of the

    Individual rules are performed using fuzzy set operations. The operations on

    Fuzzy sets are different than the operations on non-fuzzy sets. Let A and A

    are the membership functions for fuzzy sets A and B. Table 3 contains possible

    fuzzy operations for OR and AND operators on these sets, comparatively. The

    mostly used operations for OR and AND operators are max and min,

    respectively. For complement (NOT) operation, Eq. 1 is used for fuzzy sets.

    After evaluating the result of each rule, these results should be combined to

    Obtain a final result. This process is called inference. The results of individual

    Rules can be combined in different ways. Table 4 contains possible

    accumulation methods that are used to combine the results of individual rules.

  • The maximum algorithm is generally used for accumulation.

    **Defuzzification :

    After the inference step, the overall result is a fuzzy value. This result

    should be defuzzified to obtain a final crisp output. This is the purpose of the

    defuzzifier component of a FLS. Defuzzification is performed according to the

    membership function of the output variable. For instance, assume that we have

    the result in Figure 5 at the end of the inference. In this figure, the shaded areas

    all belong to the fuzzy result. The purpose is to obtain a crisp value,

    represented with a dot in the figure, from this fuzzy result.

    There are different algorithms for defuzzification too. The mostly used

    algorithms are listed in Table 5. The meanings of the variables used in Table 5

    are explained in Table 6.

  • 2. THE PLACEMENTING PROBLEM DESCRIPTION

    Now I will discuss about Placementing problem. The problem is if we have data called CGPA,PERSONOLITY LEVEL then we have decide that

    which type of placement he will get in his campus placements. i.e., whether

    will he get good, avg or no placement etc.

    So, I developed a fuzzy inference system for the above decision making

    problem Using different rule bases. The various components of the problem

    used in the developed fuzzy system are:

    1. Crisp inputs:

    Two 1. CGPA with rage [1, 10] 2. PERSONOLITY LEVEL with range [1,10]

    2. Crisp outputs:

    One TYPE OF THE PLACEMENT 3. Membership functions used:

    Three 1. Triangular 2. Trepezoidal

    3. Gaussian

    4. Linguistic variables:

    CGPA: = {Outstanding, Excellent, Good, Average, Poor}

    PERSONOLITY :={ Good, Average, Poor}

    PLACEMENT :={ Good, Good, No placement}

  • 5. FUZZY RULES USED:

    1. (OUTSTANDING) || (GOOD) ==>GOOD

    2. (EXCELLENT) && (AVG) ==>GOOD

    3. (GOOD) && (GOOD) ==>GOOD

    4. (EXCELLENT) || (GOOD) ==>AVG

    5. (GOOD) && (AVG) ==>AVG

    6. (AVG) && (AVG) ==>AVG

    7. (POOR) || (POOR) ==>NO PLACEMENT

    6. Fuzzy operators used :

    AND min OR max

    7. Implication method used: min

    8. Aggregation method used: max

    9.Defuzzifiaction method used: Centroid

    Using above components I have constructed the Fuzzy Inference system :

    1. By without using Inbuilt matlab functions and

    2. By without using Inbuilt matlab functions i.e., using GUI.

    And I have compared both the results from above two methods. The results are

    same i.e., are equivalent.

  • 3. MATLAB CODE FOR FUZZY LOGIC ALGORITHM

    FOR THE ABOVE PLACEMENTING PROBLEM

    (Without using inbuilt functions)

    %---------------------------------------------------------------

    %FUZZY INFERENCE SYSTEM PLACEMENTING PROBLEM

    %AUTHOR: DONKESHWARAM HARAHARI (2010MAS7133)

    %---------------------------------------------------------------

    %-----------

    % INPUTS :

    %-----------

    % 1.CGPA,2.PERSONOLITY

    %---------- % OUTPUT :

    %----------

    %1.PLACEMENT

    %---------------------- % UNIVERSE OF DICOURSE:

    %---------------------- x = 0:0.1:10; % FOR BOTH INPUTS y = 0:0.1:10; % FOR OUTPUT,'PLACEMENT'

    %-----------------------

    % LINGUISTIC VARIABLES:

    %-----------------------

    % CGPA = {outstanding, excellent, good, avg, poor}

    % PERSONOLITY = {good, average, poor}

    %PLACEMENT = {good, avg, noplacement}

    %------------------------------------- % COBNSTRUCTION MEMBERSHIP FUNCTIONS:

    %-------------------------------------

    % FOR CGPA: outstanding_mf1=trapzoid(x,[8.1 9.24 10.6 13.3]); excellent_mf1 = triangle(x,[7.041 8.02 9.43]); good_mf1 = triangle(x,[5.507 7.017 8.197]); avg_mf1=triangle(x,[4.63 5.61 7.02]); poor_mf1 = trapzoid(x,[-1.18 0.0107 2.98 3.97]); mf1 = [outstanding_mf1;excellent_mf1;good_mf1;avg_mf1;poor_mf1]; figure(1) plot(x,mf1) title('OUTSTANDING,EXCELLENT,GOOD,AVG,POOR CGPAS') xlabel('CGPA') ylabel('MEMDERSHIP') % FOR PERSONOLITY: good_mf2 = trapzoid(x,[6.5 8.5 10.3 13.7]); avg_mf2=triangle(x,[3 6 8]); poor_mf2 = trapzoid(x,[-3.13 0.0701 2 4]); mf2 = [good_mf2;avg_mf2,;poor_mf2]; figure(2) plot(x,mf2) title('GOOD,AVERAGE,POOR PERSONOLITY') xlabel('PERSONOLITY') ylabel('MEMDERSHIP') % FOR PLACEMENT: good_mf3 = trapzoid(x,[7.04 8.92 11 19]); avg_mf3 = gaussian(x,[0.902 5.816]); noplacement_mf3 = trapzoid(x,[-7.02 0.0962 2.19 3.96]); mf3 = [good_mf3;avg_mf3,;noplacement_mf3];

  • figure(3) plot(x,mf3) title('GOOD,AVERAGE,NO PLACEMENTS') xlabel('PLACEMENT') ylabel('MEMBERSHIP')

    %-------------------------------------------------

    FUZZY RULES ARE :

    %------------------------------------------------- % 1.(OUTSTANDING)||(GOOD)==>GOOD % 2.(EXCELLENT)&&(AVG)==>GOOD % 3.(GOOD)&&(GOOD)==>GOOD % 4.(EXCELLENT)||(GOOD)==>AVG % 5.(GOOD)&&(AVG)==>AVG % 6.(AVG)&&(AVG)==>AVG % 7..(POOR)||(POOR)==>NO PLACEMENT %-------------------------------------------------- %The five steps of evaluating fuzzy algorithms %-------------------------------------------------- % 1. Fuzzify the input. % 2. Apply a fuzzy operator. % 3. Apply an implication operation. % 4. Aggregate the outputs. % 5. Defuzzify the output. %--------------------------------------------------- %1.FUZZIFYING INPUT: %--------------------------------------------------- IP=input('\n ENTER THE INPUT AS ROW VECTOR (CGPA,PERSONOLITY) :'); fuzzy_input1=mf1(:,find(x'==IP(1))); fuzzy_input2=mf2(:,find(x==IP(2))); %---------------------------------------------------- % 2.APPLYING FUZZY OPERATORS(AND,OR etc.) % NOTE:Here we taken as AND-->MIN,OR-->MAX %---------------------------------------------------- % RULE1: antecedent(1)=max(fuzzy_input1(1),fuzzy_input2(1)); % RULE2: antecedent(2)=min(fuzzy_input1(2),fuzzy_input2(2)); % RULE3: antecedent(3)=min(fuzzy_input1(3),fuzzy_input2(1)); % RULE4: antecedent(4)=max(fuzzy_input1(2),fuzzy_input2(1)); % RULE5: antecedent(5)=min(fuzzy_input1(3),fuzzy_input2(2)); % RULE6: antecedent(6)=min(fuzzy_input1(4),fuzzy_input2(2)); % RULE7: antecedent(7)=min(fuzzy_input1(5),fuzzy_input2(3)); %--------------------------------------------------------- % 3.APPLYING IMPLICATION OPERATIONS % NOTE:Here we applied 'MIN' as implication operations. %--------------------------------------------------------- % RULE1: consequent1=min(antecedent(1),mf3(1,:)); figure(4);plot(y,consequent1);title('CONSEQUENT1'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); % RULE2 : consequent2=min(antecedent(2),mf3(1,:)); figure(5);plot(y,consequent2);title('CONSEQUENT2'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); % RULE3 : consequent3=min(antecedent(3),mf3(1,:));

  • figure(6);plot(y,consequent3);title('CONSEQUENT3'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); % RULE4: consequent4=min(antecedent(4),mf3(2,:)); figure(7);plot(y,consequent4);title('CONSEQUENT4'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); % RULE5 : consequent5=min(antecedent(5),mf3(2,:)); figure(8);plot(y,consequent5);title('CONSEQUENT5'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); % RULE6 : consequent6=min(antecedent(6),mf3(2,:)); figure(9);plot(y,consequent6);title('CONSEQUENT6'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); % RULE7 : consequent7=min(antecedent(7),mf3(3,:)); figure(10);plot(y,consequent7);title('CONSEQUENT7'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); %------------------------------------------------------------------------ % 4.AGGREGATING THE OUTPUTS(i.e.,CONSEQUENTS BY IMPLICATION OPERATION) % NOTE :Here we used 'MAX' as aggregating method. %------------------------------------------------------------------------ fuzzy_output=max([consequent1;consequent2;consequent3;consequent4;conseque

    nt5;consequent6;consequent7]); figure(11);plot(y,fuzzy_output);title('FUZZY OUTPUT'); xlabel('PLACEMENT');ylabel('MEMBERSHIP'); %------------------------------------------------------------------------- % 5.DEFUZZIFYING THE NONCRISP OUTPUT WHICH IS GOT FROM AGGREGIATION %------------------------------------------------------------------------- center=(y*fuzzy_output')/sum(fuzzy_output); output=center figure(12);plot(y,fuzzy_output);title('CRISP OUTPUT'); xlabel('PLACEMENT');ylabel('MEMBERSHIP');hold on; t=ones(size(fuzzy_output))*output; plot(t,fuzzy_output)

    disp('The crisp output is : '); disp(output); fprintf('SO %f percent PLACEMENT \n',output*10);

    %-------------------------------------------------------------------------

    The matlab codes for functions used in above programme are given below with

    their actual mathematical definition:

  • 1.Triangular function :

    The matlab code is for above function is:

    %------------------------------------------------------------------ % Function for constructing triangle Membership function %------------------------------------------------------------------ function y = triangle(x, params) if nargin ~= 2 error('Two arguments are required by the triangular MF.'); elseif length(params) < 3 error('The triangular MF needs at least three parameters.'); end

    a = params(1); b = params(2); c = params(3);

    if a > b, error('Illegal parameter condition: a > b'); elseif b > c, error('Illegal parameter condition: b > c'); elseif a > c, error('Illegal parameter condition: a > c'); end

    y = zeros(size(x));

    % Left and right shoulders (y = 0) index = find(x

  • end

    % Center (y = 1) index = find(x == b); y(index) = ones(size(index));

    2.Trepezoidal Function:

    The matlab code for above function :

    %------------------------------------------------------------- %FUNCTION FOR CONSTRUCTING TREPEZOIDAL FUNCTION %------------------------------------------------------------ function y = trapzoid(x, params) if nargin ~= 2 error('Two arguments are required by the trapezoidal MF.'); elseif length(params) < 4 error('The trapezoidal MF needs at least four parameters.'); end

    a = params(1); b = params(2); c = params(3); d = params(4);

    if a > b, error('Illegal parameter condition: a > b'); elseif c > d,

  • error('Illegal parameter condition: c > d'); end

    y1 = zeros(size(x)); y2 = zeros(size(x));

    % Compute y1 index = find(x >= b); if ~isempty(index), y1(index) = ones(size(index)); end index = find(x < a); if ~isempty(index), y1(index) = zeros(size(index)); end index = find(a

  • elseif params(1) == 0, error('The Gaussian MF needs a non-zero sigma.'); end

    sigma = params(1); c = params(2); y = exp(-(x - c).^2/(2*sigma^2));

    Next we will see the output for above matlab code.

    **THE OUTPUT FOR THE ABOVE MATLAB CODE:

    >> fuzzy placing

  • ENTER THE INPUT AS ROW VECTOR (CGPA,PERSONOLITY) :[6.1 8.2]

  • The crisp output is : 7.2369

    SO 72.369244 percent PLACEMENT

    >>

  • 4. FUZZY LOGIC OUTPUT FOR PLACEMENT PROBLEM

    (With using GUI Tool box of Matlab)

    >> fuzzy placing

  • 5.Conclusion:

    We can construct Fuzzy logic Inference System for various

    Real life problems involving Human like reasoning,decision

    taking and controllong systems.

    I must mention here that The Output from my own code

    and GUI Tool Box is same with 0.0001 error. So I reached to

    constructing good FIS.