210 likes | 287 Views
Basic Fuzzy Rules. (Quick and Dirty?). Basic Functions. Define the four basic set functions. ( Code is on p. 198-199 of text.) Define the three logical operators. (Code is on p. 201 of text.)
E N D
Basic Fuzzy Rules (Quick and Dirty?)
Basic Functions • Define the four basic set functions. ( Code is on p. 198-199 of text.) • Define the three logical operators. (Code is on p. 201 of text.) • For hedges you can just use additional sets instead of modifiers. (You don’t have to do this. You can square etc. for hedges. If so, document your hedges.)
Make Sections for Rules • Comment these sections clearly. • Values Section • This calculates fuzzy values for rules. • Has input and output values. • Rules Section • List rules in 2 parts. • Result Section • One line (weighted average).
Procedure • Decide on base variables. • (You will define fuzzy linguistic variables.) • Write the rules as comments using fuzzy values. • Define required fuzzy sets. • Write comments for the other sections. • Fill in rules then go back and fill in the other sections.
Example: Deciding on a Tip • What are input variables? • Can we define base value for each variable we choose? • Taste of food? • Speed of service? • Price? • What is output? • Tip as percent of bill.
Define rules as comments • If price is high and speed is slow then tip is lousy. • If taste is poor and speed is medium then tip is okay. • If price is very low or speed is high then tip is very good.
Comments // #1 If price is high and speed is slow … // then tip is lousy … • Number rules and leave room for code. • Note this rule means we must define high price, slow speed, and lousy tip.
// Rules // #1 If price is high and speed is slow // then tip is lousy // #2 if taste is poor and speed is not slow // then tip is okay // #3 if price is low or speed is high // then tip is very good.
Define Fuzzy Sets double highPrice = Fuz.grade( price, 8.5,10.0 ); • highPrice will replace “price is high” in the rule. This is degree “price is high” is true. • price is base variable – value passed in. • Membership is 0 at 8.5 and rises to 1 at 10.0. • You may need to graph by hand to see if set is okay.
//Inputs double highPrice = Fuz.grade(price, 8.5, 10.0); double lowPrice = Fuz.reverseGrade(price, 2.0,2.5); double slowSpeed = Fuz.reverseGrade(speed,3.0, 5.0); double mediumSpeed = Fuz.Trapezoid(speed, 2.0, 4.0, 6.0, 8.0); double highSpeed = Fuz.grade(speed, 6.0, 8.0); double poorTaste = Fuz.reverseGrade(taste, 2.0, 3.0);
// Outputs final double TIP_LOUSY = 5.0; final double TIP_OKAY = 15.0; final double TIP_VERY_GOOD = 20.0; • These are singleton outputs. • This is called the Sugeno Method.
Rule 1 // #1 If price is high and speed is slow double degree1 = Fuz.and(highPrice,slowSpeed); // then tip is lousy double tip1 = TIP_LOUSY; • Use degree1 and tip1 for rule 1.
Rule 2 // #2 if taste is poor and speed is not slow double degree2 = Fuz.and( poorTaste,Fuz.not(slowSpeed) ); // then tip is okay double tip2 = TIP_OKAY;
Rule 3 // #3 if price is low or speed is high double degree3 = Fuz.or( lowPrice,highSpeed ); // then tip is very good. double tip3 = TIP_VERY_GOOD;
// Result double result = ( degree1 * tip1 + degree2 * tip2 + degree3 * tip3 ) / (degree1 + degree2 + degree3); • Note this is just weighted average.
Another Output Method • More complicated than singleton • Approximates continuous output method. • Demands uniform steps. • Exact if all fuzzy sets have straight lines between the specified values. • This is a simplified Mamdani Method.
Method • Define a scale. • Outputs are an array of values • Outputs are evenly spaced • For example with 5% scale: • TIP_LOUSY[0] is degree for 0% tip • TIP_LOUSY[1] is degree for 5% tip • TIP_LOUSY[2] is degree for 10% tip • Etc.
Outputs, each step is 5% final double scale = 5.0; final double[] TIP_LOUSY = {1.0, 1.0, 0.3,0.0, 0.0, 0.0}; final double[] TIP_OKAY = {0.0, 0.0, 0.2,1.0, 0.5, 0.0 }; final double[] TIP_VERY_GOOD = {0.0, 0.0, 0.0, 0.3, 1.0, 1.0 };
Clipping of Rule Output // then tip is lousy (result of rule #1) double[] tip1 = Fuz.clip(degree1,TIP_LOUSY); • Output is a clipped array. • degree1 was used for clipping. • Thus output is weighted by degree rule is true.
Results: Combine Outputs • Make an array with max of outputs. double[] maxSet = new double[6]; Fuz.max(tip1,tip2,maxSet); Fuz.max(tip3,maxSet,maxSet); Fuz.max is basically: for (int i=0; i< set1.length; i++){ theMax[i] = Math.max(set1[i],set2[i]); }
Results: Find Centroid double result = Fuz.centroid(scale, maxSet); public static double centroid(double scale, double [] set){ //assumes set is not all zeros. double sum = 0; double weightedSum = 0; for(int i=0; i< set.length; i++){ sum += set[i]; weightedSum += i * set[i]; } return scale * (weightedSum)/sum;