basic fuzzy rules

21
Basic Fuzzy Basic Fuzzy Rules Rules (Quick and Dirty?) (Quick and Dirty?)

Upload: bruce-olson

Post on 31-Dec-2015

55 views

Category:

Documents


1 download

DESCRIPTION

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.) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Basic Fuzzy Rules

Basic Fuzzy Basic Fuzzy RulesRules

(Quick and Dirty?)(Quick and Dirty?)

Page 2: Basic Fuzzy Rules

Basic FunctionsBasic Functions

Define the four basic set functions. Define the four basic set functions. ( Code is on p. 198-199 of text.)( Code is on p. 198-199 of text.)

Define the three logical operators. Define the three logical operators. (Code is on p. 201 of text.)(Code is on p. 201 of text.)

For hedges you can just use For hedges you can just use additional sets instead of modifiers. additional sets instead of modifiers. ((You don’t have to do this. You can square etc. You don’t have to do this. You can square etc. for hedges. If so, document your hedges.)for hedges. If so, document your hedges.)

Page 3: Basic Fuzzy Rules

Make Sections for RulesMake Sections for Rules

Comment these sections clearly.Comment these sections clearly. Values SectionValues Section

This calculates fuzzy values for rules.This calculates fuzzy values for rules. Has input and output values.Has input and output values.

Rules SectionRules Section List rules in 2 parts.List rules in 2 parts.

Result SectionResult Section One line (weighted average).One line (weighted average).

Page 4: Basic Fuzzy Rules

ProcedureProcedure

Decide on base variables. Decide on base variables. (You will define fuzzy linguistic variables.)(You will define fuzzy linguistic variables.)

Write the rules as comments using Write the rules as comments using fuzzy values. fuzzy values.

Define required fuzzy sets.Define required fuzzy sets. Write comments for the other sections.Write comments for the other sections. Fill in rules then go back and fill in the Fill in rules then go back and fill in the

other sections.other sections.

Page 5: Basic Fuzzy Rules

Example: Deciding on a Example: Deciding on a TipTip

What are input variables?What are input variables? Can we define base value for each Can we define base value for each

variable we choose?variable we choose? Taste of food?Taste of food? Speed of service?Speed of service? Price?Price?

What is output?What is output? Tip as percent of bill.Tip as percent of bill.

Page 6: Basic Fuzzy Rules

Define rules as Define rules as commentscomments

If price is high and speed is slow If price is high and speed is slow then tip is lousy. then tip is lousy.

If taste is poor and speed is medium If taste is poor and speed is medium then tip is okay. then tip is okay.

If price is very low or speed is highIf price is very low or speed is high then tip is very good. then tip is very good.

Page 7: Basic Fuzzy Rules

CommentsComments

// #1 If price is high and speed is slow// #1 If price is high and speed is slow

……

// then tip is lousy// then tip is lousy

…… Number rules and leave room for Number rules and leave room for

code.code. Note this rule means we must define Note this rule means we must define

high price, slow speed, and lousy tip.high price, slow speed, and lousy tip.

Page 8: Basic Fuzzy Rules

// Rules// Rules

// #1 If price is high and speed is slow// #1 If price is high and speed is slow // then tip is lousy // then tip is lousy

// #2 if taste is poor and speed is not // #2 if taste is poor and speed is not slowslow // then tip is okay // then tip is okay

// #3 if price is low or speed is high// #3 if price is low or speed is high // then tip is very good. // then tip is very good.

Page 9: Basic Fuzzy Rules

Define Fuzzy SetsDefine Fuzzy Sets

double highPrice = double highPrice =

Fuz.grade( price, Fuz.grade( price, 8.5,10.0 );8.5,10.0 );

highPrice will replace “price is high” in the highPrice will replace “price is high” in the rule. This is degree “price is high” is true.rule. This is degree “price is high” is true.

price is base variable – value passed in.price is base variable – value passed in. Membership is 0 at 8.5 and rises to 1 at 10.0.Membership is 0 at 8.5 and rises to 1 at 10.0. You may need to graph by hand to see if set You may need to graph by hand to see if set

is okay.is okay.

Page 10: Basic Fuzzy Rules

//Inputs//Inputsdouble highPrice = double highPrice = Fuz.grade(price, 8.5, 10.0);Fuz.grade(price, 8.5, 10.0);double lowPrice = double lowPrice = Fuz.reverseGrade(price, 2.0,2.5);Fuz.reverseGrade(price, 2.0,2.5);double slowSpeed = double slowSpeed = Fuz.reverseGrade(speed,3.0, 5.0);Fuz.reverseGrade(speed,3.0, 5.0);double mediumSpeed = double mediumSpeed = Fuz.Trapezoid(speed, 2.0, 4.0, 6.0, Fuz.Trapezoid(speed, 2.0, 4.0, 6.0,

8.0);8.0);double highSpeed = double highSpeed = Fuz.grade(speed, 6.0, 8.0);Fuz.grade(speed, 6.0, 8.0);double poorTaste = double poorTaste = Fuz.reverseGrade(taste, 2.0, 3.0);Fuz.reverseGrade(taste, 2.0, 3.0);

Page 11: Basic Fuzzy Rules

// Outputs// Outputs

final double TIP_LOUSY = 5.0;final double TIP_LOUSY = 5.0;

final double TIP_OKAY = 15.0;final double TIP_OKAY = 15.0;

final double TIP_VERY_GOOD = 20.0;final double TIP_VERY_GOOD = 20.0;

These are singleton outputs.These are singleton outputs. This is called the Sugeno Method.This is called the Sugeno Method.

Page 12: Basic Fuzzy Rules

Rule 1Rule 1

// #1 If price is high and speed is slow// #1 If price is high and speed is slow

double degree1 = double degree1 =

Fuz.and(highPrice,slowSpeed);Fuz.and(highPrice,slowSpeed);

// then tip is lousy// then tip is lousy

double tip1 = TIP_LOUSY;double tip1 = TIP_LOUSY;

Use degree1 and tip1 for rule 1.Use degree1 and tip1 for rule 1.

Page 13: Basic Fuzzy Rules

Rule 2Rule 2

// #2 if taste is poor and speed is not slow// #2 if taste is poor and speed is not slow

double degree2 = double degree2 =

Fuz.and( poorTaste,Fuz.not(slowSpeed) Fuz.and( poorTaste,Fuz.not(slowSpeed) ););

// then tip is okay// then tip is okay

double tip2 = TIP_OKAY;double tip2 = TIP_OKAY;

Page 14: Basic Fuzzy Rules

Rule 3Rule 3

// #3 if price is low or speed is high// #3 if price is low or speed is high

double degree3 = double degree3 =

Fuz.or( lowPrice,highSpeed );Fuz.or( lowPrice,highSpeed );

// then tip is very good.// then tip is very good.

double tip3 = TIP_VERY_GOOD;double tip3 = TIP_VERY_GOOD;

Page 15: Basic Fuzzy Rules

// Result// Result

double result = ( degree1 * tip1 + double result = ( degree1 * tip1 +

degree2 * tip2 + degree2 * tip2 +

degree3 * tip3 )degree3 * tip3 )

/ (degree1 + degree2 + / (degree1 + degree2 + degree3);degree3);

Note this is just weighted average.Note this is just weighted average.

Page 16: Basic Fuzzy Rules

Another Output MethodAnother Output Method

More complicated than singletonMore complicated than singleton Approximates continuous output Approximates continuous output

method.method. Demands uniform steps.Demands uniform steps. Exact if all fuzzy sets have straight Exact if all fuzzy sets have straight

lines between the specified values.lines between the specified values. This is a simplified Mamdani This is a simplified Mamdani

Method.Method.

Page 17: Basic Fuzzy Rules

MethodMethod

Define a scale.Define a scale. Outputs are an array of valuesOutputs are an array of values Outputs are evenly spacedOutputs are evenly spaced For example with 5% scale:For example with 5% scale:

TIP_LOUSY[0] is degree for 0% tipTIP_LOUSY[0] is degree for 0% tip TIP_LOUSY[1] is degree for 5% tipTIP_LOUSY[1] is degree for 5% tip TIP_LOUSY[2] is degree for 10% tipTIP_LOUSY[2] is degree for 10% tip Etc.Etc.

Page 18: Basic Fuzzy Rules

Outputs, each step is 5%Outputs, each step is 5%

final double scale = 5.0; final double scale = 5.0;

final double[] TIP_LOUSY = final double[] TIP_LOUSY =

{1.0, 1.0, 0.3,0.0, 0.0, 0.0};{1.0, 1.0, 0.3,0.0, 0.0, 0.0};

final double[] TIP_OKAY = final double[] TIP_OKAY =

{0.0, 0.0, 0.2,1.0, 0.5, 0.0 };{0.0, 0.0, 0.2,1.0, 0.5, 0.0 };

final double[] TIP_VERY_GOOD = final double[] TIP_VERY_GOOD =

{0.0, 0.0, 0.0, 0.3, 1.0, 1.0 };{0.0, 0.0, 0.0, 0.3, 1.0, 1.0 };

Page 19: Basic Fuzzy Rules

Clipping of Rule OutputClipping of Rule Output

// then tip is lousy (result of rule #1)// then tip is lousy (result of rule #1) double[] tip1 =double[] tip1 =

Fuz.clip(degree1,TIP_LOUSY);Fuz.clip(degree1,TIP_LOUSY);

Output is a clipped array.Output is a clipped array. degree1 was used for clipping.degree1 was used for clipping. Thus output is weighted by degree Thus output is weighted by degree

rule is true.rule is true.

Page 20: Basic Fuzzy Rules

Results: Combine Results: Combine OutputsOutputs

Make an array with max of outputs.Make an array with max of outputs.

double[] maxSet = new double[6];double[] maxSet = new double[6];Fuz.max(tip1,tip2,maxSet);Fuz.max(tip1,tip2,maxSet);Fuz.max(tip3,maxSet,maxSet);Fuz.max(tip3,maxSet,maxSet);

Fuz.max is basically:Fuz.max is basically:for (int i=0; i< set1.length; i++){for (int i=0; i< set1.length; i++){

theMax[i] = theMax[i] = Math.max(set1[i],set2[i]);Math.max(set1[i],set2[i]);

}}

Page 21: Basic Fuzzy Rules

Results: Find CentroidResults: Find Centroid

double result = Fuz.centroid(scale, double result = Fuz.centroid(scale, maxSet);maxSet);

public static double centroid(double scale, double [] public static double centroid(double scale, double [] set){set){

//assumes set is not all zeros.//assumes set is not all zeros.double sum = 0;double sum = 0;double weightedSum = 0;double weightedSum = 0;for(int i=0; i< set.length; i++){for(int i=0; i< set.length; i++){

sum += set[i];sum += set[i];weightedSum += i * set[i];weightedSum += i * set[i];

}}return scale * (weightedSum)/sum;return scale * (weightedSum)/sum;