FUZZY LOGIC AND FUZZY EXPERT SYSTEMS Atmika Singh Abstract: Fuzzy logic is a superset of classical logic introduced by Dr. Lotfi Zadeh. This paper discusses a mathematical basis of fuzzy logic along with the concepts of fuzzy sets, membership functions and rules of reasoning. Fuzzy expert systems accept inputs numbers and convert them into linguistic values which are then manipulated by if-then rules given by a human expert. The concept of a fuzzy expert system with its rule-base and set membership functions is discussed in detail. After defining the rules and membership functions, the application of this knowledge to the input variables to compute the values of the output variables is studied. A case study is presented along with an implementation in MATLAB. 1. Introduction. In the classical two-valued logic, propositions can take only two truth values from the set 0,1 . It has been shown using De Morgan's algebras that the propositional logic obtained when the set of truth assignments consists of all values in the unit interval with conjunction, disjunction and negation as defined in the table 1 (Appendix C) is the same as fuzzy logic [4]. De-Morgan algebra is a structure A A, , ,0,1, such that A, , ,0,1, is a bounded distributive lattice. ¬ is a De Morgan involution such that x y x y, x x . When the classical set of truth values is extended to include a third value called 'undecided', the De Morgan algebra generated gives rise to three-valued logic. A structure 0,1 , , , , 0,1 forms a De Morgan's algebra. Using this as the algebra of truth values, classical fuzzy logic is generated. It has been shown that the equational class of all De-Morgan's algebras can be generated by both three-valued logic and fuzzy logic, hence they are the same. Fuzzy 1 logic extends propositional logic to all values in the interval [0,1]. In such a case, a statement may have a truth value that is neither completely true nor completely false. Basic fuzzy logic operations as defined by Dr. Zadeh are shown in the table 1 in appendix C. 2. Fuzzy sets and membership functions. A fuzzy set is a class of objects in which there is no sharp boundary between those objects that belong to the class and those that do not. Some common examples are the sets of all young women or the set of small cars. In a fuzzy set an object may have a grade of membership intermediate between full membership, represented by 1, and non-membership, represented by 0. Mathematically, the transition from regular sets (crisp sets) to fuzzy sets can be explained by assuming a universal set X such that A is a crisp subset of X . Define a function v : X {0,1} such that v ( x ) is 1 if x A and v ( x ) is 0 when x A . This function is the characteristic function of any crisp subset A of X . Fuzzy sets generalize the characteristic function by allowing all values in the unit interval. This is known as the membership function. Any fuzzy subset F of X is defined by its membership function : X [0,1] . Then ( x ) where x F denotes the grade of membership of x in the fuzzy set F . X is called the universe of discourse. A fuzzy subset (or fuzzy set as it is commonly referred to) is defined as a set of ordered pairs F x, ( x) | x F , F X . The membership function is denoted by F for the fuzzy set F. For example, let X be the set of people and F be the set of 'Tall' people. Each person is assigned a degree of membership in the fuzzy subset TALL. For this we define a membership function based on the person's height. The intervals of 5ft and 7ft are 2 arbitrarily chosen for the sake of the example and can be replaced by any other suitable intervals. 0, if height( x) 5 ft Tall ( x) height( x) - 5 ft / 2 ft , if 5 ft. height ( x) 7 ft 1,if height( x) 7 ft Based on the above definition if there is two individuals 3 ft and 6ft tall would have 0 and 0.5 membership grade respectively [2]. 3. Fuzzy logical operations. Some fuzzy set theorems are presented in table 2 in appendix C. The negation operation defined in table 1 is standard. The computation of AND and OR are however not standard. The AND and OR operators in fuzzy logic are generalizations from classical logic and are called t-norms and t-conorms respectively. T-norms and t-conorms are functions from [0,1] [0,1] into [0,1] [1]. If z T ( x, y ) , then x, y, z all belong to the interval [0,1] . All t-norms and t-conorms have the properties of commutativity, monotonicity, boundary and associativity. Let tv be a truth assignment, then tv( P AND Q) T (tv( P), tv(Q)) for any t-norm. The basic t-norms are: 1) Tm ( x, y ) min( x, y) (Zahedian intersection) 2) TL ( x, y) max(0, x y 1) (Bounded difference intersection) 3) Tp xy (Algebraic product) As for t-norms, If C is a t-conorm then, tv( P OR Q) C (tv( P), tv(Q)) . The basic tconorms are: 1) Cm ( x, y) max( x, y) (Standard Union) 2) CL ( x, y) min(1, x y) (Bounded sum) 3 3) C p x y xy (Algebraic sum) 4. Fuzzy propositions and truth assignments. A fuzzy proposition is a statement that takes a fuzzy truth value. It includes logical connectives like AND, OR, NOT and Implication. An atomic fuzzy proposition cannot be decomposed into simpler fuzzy propositions and are combined together to create compound propositions. For example, a fuzzy rule of the form (x is fast) is an atomic fuzzy proposition because it can not be decomposed into simpler propositions. Other examples are x and size is small. A compound fuzzy proposition consists of atomic propositions that are conjuncted, disjuncted, complemented or connected by implication. An example of a complex fuzzy proposition is ((x is A) AND (x is B) AND (y is C) AND (z is D)). The truth value of a proposition x, that simply asserts that x exists is the truth value of x. If, for example, x is assigned a value 10 with truth value 0.75, then truth value of the proposition is 0.75. Most fuzzy propositions involve only single valued data like integers or strings [1]. They are of the general form (A(comparison operator)B). For example, A :( x y ) where value of x y 3 and the membership values are x 0.8, y 0.7, comparison operator = 1. Therefore, truth value of A = min(0.8,1,0.7) = 0.7. For propositions of the form (Size is small), the truth value (tv) of the fuzzy set size, tv(size) = 1, of the comparison operator 'is' is 1. The truth value of SMALL in size is its grade of membership in size, for example 0.4. Then tv((Size is small)) = min(1,1,0.4) = 0.4. Truth values of complex propositions are obtained by evaluating the logical operators. For example [1], let P be a complex proposition, P A AND B OR NOT C . The truth values of A, B and C are 0.6, 0.8 and 4 0.3 respectively. Then, tv(A)= 0.6, tv(A AND B) = min(0.6,0.8) = 0.6, tv(NOT C) = 1 0.3 = 0.7. Therefore, tv((A AND B) OR (NOT C)) = max(0.6,0.7) = 0.7. 5. Linguistic variables and linguistic values. A linguistic variable is a variable whose value is expressed in terms of spoken language. These terms are imprecise and are represented by fuzzy sets. Let t be a variable that denotes temperature over an interval [0, T]. Let X be the domain of real numbers. Let there be three fuzzy sets L, M and H that have the member ship functions L , M , H respectively. Each of these fuzzy sets can be referred to as LOW, MEDIUM and HIGH. TEMPERATURE can be treated as a linguistic variable. LOW, MEDIUM and HIGH are linguistic values of the linguistic variable TEMPERATURE. Therefore t can be LOW, MEDIUM or HIGH. 6. Fuzzy rules and inference. Inference involves the modification of the values or truth values of data using rules [1]. A fuzzy rule is a proposition in the if-then format. It contains a main implication connective and fuzzy propositions. These can be of the form, ((x is A) AND (x is B) AND (y is C) AND (z is D)) => (r is Q) where x, y and z are input variables, r is an output variable, A,B and C are membership functions defined on x, y and z respectively and Q is a membership function defined on r. The rule's premise (left hand side) is called the antecedent and it describes to what degree the rule applies, while the conclusion (right hand side) is called the consequent. This assigns a membership function to each of one or more output variables. The set of rules 5 used for inferencing in a fuzzy expert system is known as the rule base or knowledge base. The most common tool used for fuzzy logical inference is approximate reasoning, used to infer new logical propositions from old ones. Approximate reasoning employs the generalized modus ponens. Classical modus ponens is if A then B. The fuzzy version is formulated as: If X is A then Y is B, from X = A' infer that Y = B'. Here A and A' are fuzzy sets belonging to the same universe and B and B' are also defined on the same universe which maybe different from that of A and A'. 7. Fuzzy expert systems. An expert system involves the collection and encoding of human knowledge about prediction and classification, together with an inference engine for evaluating the rule base for a given set of inputs [1]. The basic components of a fuzzy expert system are a set of fuzzy rules, a set of fuzzy set membership functions and a fuzzy inference engine [2]. They are used in several wideranging fields including, linear and nonlinear control, pattern recognition, and financial systems and data analysis. The fuzzy inference engine contains a scheduler that selects the rules in a sequence for processing. A scheduler is needed because certain rules must be checked before others. Some rules fire and make their consequents true. These consequents are the antecedents in the certain rules. Thus a firing order is needed implemented by a scheduler. It also contains a rule processor that examines the left hand side of a rule to check the truth values of the conditions. It uses these truth values to fire the rule. A set membership builder builds an approximate membership function for the output from the firing of the rules using approximate reasoning. A defuzzifier then 6 converts the inferred fuzzy values of the consequents to a non-fuzzy value. The operation of the expert system proceeds as follows. 1. Fuzzification: This process involves finding grades of membership of the linguistic values of a linguistic variable corresponding to an input value given by the user or process. The degree of truth for each rule antecedent is calculated by applying the membership functions to the input values. 2. Inference: The if–then rules are implemented and the truth value of each rule is computed using the truth values of the linguistic values. Approximate reasoning as describe earlier is used. Commonly used inference rules are min and product. In min inferencing, the output membership function is determined by either the truth value of the implemented rule or the original value of the membership function, whichever is lower (fuzzy logic AND). In product inferencing, the output membership function is scaled by the rule premise's truth value. 3. Composition: All of the fuzzy subsets assigned to each output variable are combined together to form a single fuzzy subset for each output linguistic variable. The methods used are max and sum. In max composition, the fuzzy sets for the output variable are ORed with each other so that the maximum value from all the fuzzy subsets is used (fuzzy logic OR). In sum composition, the fuzzy subsets associated with the output value are summed to generate a single fuzzy subset. 4. Defuzzification: This is the process of calculating a scalar value from the fuzzy output. From composition we obtain a single fuzzy set. Defuzzification aggregates the set into a single value. The common techniques used are the centroid and maximum methods. In the centroid method, the scalar value of the output variable is computed by finding the 7 variable value of the center of gravity of the membership function for the fuzzy value. a The general formula is X (centroid ) x ( x)dx where ( x)dx b a [a, b] is the interval of the b aggregated membership function. In the maximum method, the value at which the fuzzy subset has its maximum truth value is chosen as the value for the output variable. 8. Implementation of a simple fuzzy expert system. To demonstrate the use of fuzzy expert systems, a fuzzy washing machine is implemented. The details of the system were obtained from reference [5]. A detailed implementation of the expert system is provided in appendix A. 1. Developing membership functions: The washing machine receives input from the user in the form of scalar values on a scale of 1 to 10. These correspond to two linguistic variables, "degree of dirtiness" and "type of dirt". Each has 3 linguistic values, Degree of dirtiness (Dd) = {Low, Medium, High}= LOW , MED , HI and Type of dirt (Dt) = {Not greasy, Medium greasy, Greasy}= NG , MG , G . The expected output from the expert system is the linguistic variable "duration of the wash cycle (Wd)" ranging from 0 to 60 minutes. The linguistic values for the output are Wd = {Very short, Short, Medium, Long, Very long} = VL , L , M , S , VS . The linguistic variables are the fuzzy sets and each of the 11 linguistic values (6 for the inputs and 5 for the outputs) are fuzzy membership functions. The function definitions are listed in appendix A. 2. Developing the rule-base: We have 2 inputs comprising of 3 membership functions each. Thus we will have 3x3 = 9 rules of inference comprising the rule base. 8 If degree of dirtiness is High and type of dirt is Greasy, then duration of wash cycle is Very long; If degree of dirtiness is Medium and type of dirt is Greasy, then duration of wash cycle is Long; If degree of dirtiness is Low and type of dirt is Greasy, then duration of wash cycle is Long; If degree of dirtiness is High and type of dirt is Medium, then duration of wash cycle is Long; If degree of dirtiness is Medium and type of dirt is Medium, then duration of wash cycle is Medium; If degree of dirtiness is Low and type of dirt is Medium, then duration of wash cycle is Medium; If degree of dirtiness is High and type of dirt is not greasy, then duration of wash cycle is Medium; If degree of dirtiness is Medium and type of dirt is not greasy, then duration of wash cycle is Short; If degree of dirtiness is Low and type of dirt is not greasy, then duration of wash cycle is Very short Since none of the above rules depend on the firing on any of the other rules, a scheduler for the firing of rules is not needed. 3. The expert system algorithm: 1. Accept user inputs from an interface. These are values on a scale of 1 to 10. 2. Fuzzify the inputs, i.e. determine the membership values in each of the 6 membership functions for both inputs. 3. Apply the membership values of the inputs to the rule-base to generate the truth values of the consequents of the 9 rules. 4. Use MIN inferencing to generate new output membership functions. The new membership values are compared point wise to the original membership functions to get the modified output membership functions 5. Combine the new membership functions to form a single membership function using MAX composition method. In this all the newly generated membership 9 functions are combined with each other using the fuzzy OR and the maximum possible membership value is chosen for each output value. 6. Perform defuzzification using the centroid method to get a scalar output value for the actual duration of the wash cycle. 9. Conclusion. Fuzzy expert systems provide a convenient way of handling uncertain values. They are used in several wide-ranging fields including, linear and nonlinear control, pattern recognition, and financial systems and data analysis. A table of the results obtained for the fuzzy washing machine expert system is attached in appendix A. The performance can be improved by using better defined rules of inference and membership functions. References: [1] – Siler, W; Buckley, J. J., Fuzzy expert systems and fuzzy reasoning, John Wiley & Sons, Inc., ISBN 0-471-38859-9, 2005. [2] - Kantrowitz, M., Horstkotte, E., Joslyn, J.,"FAQ: Fuzzy Logic and Fuzzy Expert Systems 1/1", http://www.faqs.org/faqs/fuzzy-logic/part1, March 1997 [3] - Looney, C. G., "Fuzzy Rules and Approximate Reasoning.", http://ultima.cse.unr.edu/cs791j/unit2791j.htm, January 2004 [4] - Gehrke, M; Walker, Cl; Walker, E., "A mathematical setting for fuzzy logics. ", Internat. J. Uncertain. Fuzziness Knowledge-Based Systems 5 (1997), no. 3, 223--238. [5] – Zhang, W (Aptronix Inc.), "Controller for washing machine", http://www.aptronix.com/fuzzynet/fiusource/wash.htm, July 1992 10 APPENDIX A: Implementation of the fuzzy expert system for a fuzzy washing machine. 1. Define membership functions: Degree of dirtiness (Dd) = {Low, Medium, High}= LOW , MED , HI Type of dirt (Dt) = {Not greasy, Medium greasy, Greasy}= NG , MG , G Duration of the wash cycle (Wd) = {Very short, Short, Medium, Long, Very long}= VL , L , M , S , VS The function definitions for the input and output membership functions: LOW and NOT GREASY 0.2 x 1, if 0 x 5 0, for x 0 LOW ( x) NG ( x) 0.2 x, if 0 x 5 MEDIUM and MEDIUM GREASY MED ( x) MG ( x) 0.2 x 2, for 5 x 10 HIGH and GREASY VERY SMALL CYCLE SMALL CYCLE 0, if 0 x 5 0.2 x 1, for 5 x 10 HI ( x) G ( x) 1 x 1, 0 x 15 S 15 0, x 15 1 15 x, 0 x 15 1 VS x 2,15 x 30 15 0, x 30 11 MEDIUM CYCLE LONG CYCLE VERY LONG CYCLE 1 15 x 1,15 x 30 1 M x 3,30 x 45 15 0, otherwise 1 15 x 2,30 x 45 1 L x 4, 45 x 60 15 0, otherwise 0, 0 x 45 VL 1 x 3, 45 x 60 15 Figure 1: Plots of the membership functions for the inputs 12 Figure 2: Plot of membership functions of the output. 3. Working of the expert system for randomly selected set of inputs: Let Dd = 8 and Dt = 4. Fuzzify the inputs, i.e. determine the membership values in each of the 6 membership functions for both inputs. The values for the given inputs are LOW 0 MED 0.4 HI 0.6 NG 0.2 MG 0.8 G 0 Generate the truth values of the consequents of the 9 rules. For the current inputs, the inferred values are, Rule 1: If degree of dirtiness is High and type of dirt is Greasy, then duration of wash cycle is Very long = min[ HI , G ] min[0.6,0] VL 0 Similarly for the other rules, Rule 2: min[ MED , G ] min[0.4, 0] L 0 Rule 3: min[ LOW , G ] min[0, 0] L 0 Rule 4: min[ HI , MG ] min[0.6,0.8] L 0.6 Rule 5: min[ MED , MG ] min[0.4,0.8] M 0.4 13 Rule 6: min[ LOW , MG ] min[0,0.8] M 0 Rule 7: min[ HI , NG ] min[0.6,0.2] M 0.2 Rule 8: min[MED , NG ] min[0.4,0.2] S 0.2 Rule 9: min[ LOW , NG ] min[0,0.2] VS 0 The new membership values are compared point wise to the original membership functions to get the following modified output membership functions denoted by a superscript m. From Rule 1: m VL 0,0 x 60 From Rule 2: Lm 0, 0 x 60 From Rule 3: Lm 0, 0 x 60 From Rule 4: 1 15 x 2,30 x 39 0.6,39 x 51 m L 1 x 4,51 x 60 15 0, otherwise From Rule 5: 1 15 x 1,15 x 21 0.4, 21 x 39 Mm 1 x 3,39 x 45 15 0, otherwise From Rule 6: Mm 0, 0 x 60 From Rule 7: 1 15 x 1,15 x 18 0.4,18 x 42 m M 1 x 3, 42 x 45 15 0, otherwise From Rule 8: 1 15 x, 0 x 3 0.2,3 x 27 m S 1 x 2, 27 x 30 15 0, x 30 From Rule 9: m VS 0, 0 x 60 14 Figure 3: New output membership functions after inferencing The new membership functions generated by inferencing are now combined to form a single membership function for the output duration of wash cycle. For this max composition is used. In this all the newly generated membership functions are combined with each other using the fuzzy OR and the maximum possible membership value is chosen for each output value. The final function is defined as, FINAL 1 15 x, 0 x 3 0.2,3 x 18 1 x 1,18 x 21 15 0.4, 21 x 36 1 x 2,36 x 39 15 0.6,39 x 51 1 x 4,51 x 60 15 15 Defuzzification is performed using the centroid method to get a scalar output value for the actual duration of the wash cycle from the output function obtained. 60 Wash time = X (centroid ) x ( x) 1 60 ( x) = 34 minutes. 1 The table shows the wash times obtained for different input values. Degree of dirtiness Type of dirt Duration of wash cycle 2 2 19 2 6 34 8 4 34 9 9 45 APPENDIX B: Source code for fuzzy washing machine File: Fuzzycontroller.m % IMPLEMENTATION OF A BASIC FUZZY RULE BASED CONTROLLER USED IN A WASHING MACHINE TO % DETERMINE LENGTH OF WASH CYCLE. % PROJECT MATH 354 % ATMIKA SINGH clear all; close all; 16 clc; % Accept User Input disp('ENTER INTEGER VALUES FROM A 0-10 INTERVAL'); Dirtlevel=input('Enter the degree of dirtiness and press enter'); Dirttype=input('Enter the type of dirt and press enter'); % STEP 1: DEFINE AND INITIALIZE THE INPUT AND OUTPUT VARIABLES AND % MEMBERSHIP FUNCTIONS. % INPUT: Degree of dirt and Type of dirt with values % ranging between 0 and 10. Specified by user. % Output ranging from 0 to 60 minutes. Dd = round(Dirtlevel)+1; % Matlab does not work with zero index Td = round(Dirttype)+1; rangeout = 0:1:60; rangein = 0:1:10; % STEP 2: DEFINE MEMBERSHIP FUNCTIONS % LOW, MEDIUM AND LARGE FOR DEGREE OF DIRT % NOT GREASY, MEDIUM, GREASY FOR TYPE OF DIRT % Very small, small, medium, long and very long for output % Membership matrix for input variables and output variables. % Input membership matrix Min = [1 0.8 0.6 0.4 0.2 0 0 0 0 0 0; 0 0.2 0.4 0.6 0.8 1 0.8 0.6 0.4 0.2 0;... 0 0 0 0 0 0 0.2 0.4 0.6 0.8 1]; % Output membership matrix Mout = zeros(5,61); for i=1:1:16 x = 0:1:15; Mout(1,i)=(-x(i)/15)+1; y = 45:1:60; Mout(5,i+45)=(y(i)/15)-3; end Mout(2,1:16)=Mout(5,46:61); Mout(2,16:31)=Mout(1,1:16); Mout(3,16:31)=Mout(5,46:61); Mout(3,31:46)=Mout(1,1:16); Mout(4,31:46)=Mout(5,46:61); Mout(4,46:61)=Mout(1,1:16); %Plot membership functions hold on; plot(rangein,Min(1,:)'); plot(rangein,Min(2,:)','r:'); plot(rangein,Min(3,:)','g--'); hold off; title('Input membership functions for degree of dirtiness'); legend('Low','Medium','High'); xlabel('Degree of dirtiness'); figure; hold on; plot(rangein,Min(1,:)'); plot(rangein,Min(2,:)','r:'); plot(rangein,Min(3,:)','g--'); hold off; 17 title('Input membership functions for type of dirt'); legend(' Not Greasy','Medium','Greasy'); xlabel('Type of dirt'); figure; hold on; plot(rangeout,Mout(1,:)'); plot(rangeout,Mout(2,:)','r:'); plot(rangeout,Mout(3,:)','g--'); plot(rangeout,Mout(4,:)','cd'); plot(rangeout,Mout(5,:)','m.-'); hold off; title('Output membership functions for wash duration'); legend('Very Small','Small','Medium','Long','Very Long'); xlabel('Duration of wash cycle'); % STEP 3: Fuzzification of input Tlow = Min(1,Td); Tmed = Min(2,Td); Thi = Min(3,Td); Dlow = Min(1,Dd); Dmed = Min(2,Dd); Dhi = Min(3,Dd); % STEP 4: Implementation of rule base % rules one to nine WTvl = min(Thi,Dhi); WTl = min(Thi,Dmed); WTl1 = min(Thi,Dlow); WTl2 = min(Tmed,Dhi); WTm = min(Tmed,Dmed); WTm1 = min(Tmed,Dlow); WTm2 = min(Tlow,Dhi); WTs = min(Tlow,Dmed); WTvs = min(Tlow,Dlow); % STEP 5: Mrule(1,:) Mrule(2,:) Mrule(3,:) Mrule(4,:) Mrule(5,:) Mrule(6,:) Mrule(7,:) Mrule(8,:) Mrule(9,:) Inferencing using MIN reasoning. = min(Mout(1,:), WTvs); = min(Mout(2,:), WTs); = min(Mout(3,:), WTm); = min(Mout(3,:), WTm1); = min(Mout(3,:), WTm2); = min(Mout(4,:), WTl); = min(Mout(4,:), WTl1); = min(Mout(4,:), WTl2); = min(Mout(5,:), WTvl); figure; hold on; plot(rangeout,Mrule'); title('new output membership functions after inferencing'); xlabel('Duration of wash cycle'); hold off; % Step 6: Composition using MAX reasoning Mmax = max(Mrule); figure; 18 plot(rangeout, Mmax) title('Final output membership function after composition'); xlabel('Duration of wash cycle'); % STEP 7: Defuzzification using center of gravity method y=sum(Mmax.*rangeout)/sum(Mmax); disp('Duration of wash cycle '); wash_time = y APPENDIX C: Tables x y x AND y xy x OR y NOT x a b min{a,b} max{a,b} max{1-a, min{a,b}} 1-a Table 1 Basic operations in fuzzy logic. A A A, A A A, (A) A Commutativity A B B A, A B B A Associativity A B C A B C , A B C A B C Distributivity A ( B C ) ( A B) ( A C ), A ( B C ) ( A B) ( A C ) De Morgan's ( A B) A B, ( A B) A B Laws Absorption A ( A B) A , A ( A B) A Zero Law A X X, A Identity Law A A, A X A Table 2: Fuzzy Set Theorems 19