66 lines
1.5 KiB
Plaintext
66 lines
1.5 KiB
Plaintext
|
// Block definition (there may be more than one block per file)
|
||
|
FUNCTION_BLOCK tipper
|
||
|
|
||
|
// Define input variables
|
||
|
VAR_INPUT
|
||
|
service : REAL;
|
||
|
food : REAL;
|
||
|
END_VAR
|
||
|
|
||
|
// Define output variable
|
||
|
VAR_OUTPUT
|
||
|
tip : REAL;
|
||
|
END_VAR
|
||
|
|
||
|
// Fuzzify input variable 'service'
|
||
|
|
||
|
FUZZIFY service
|
||
|
TERM poor := (0, 1) (4, 0) ;
|
||
|
TERM good := (1, 0) (4,1) (6,1) (9,0);
|
||
|
TERM excellent := (6, 0) (9, 1);
|
||
|
RANGE := (0.0 .. 9.0); // Added range for service
|
||
|
END_FUZZIFY
|
||
|
|
||
|
// Fuzzify input variable 'food'
|
||
|
|
||
|
FUZZIFY food
|
||
|
TERM rancid := (0, 1) (1, 1) (3,0) ;
|
||
|
TERM delicious := (7,0) (9,1);
|
||
|
RANGE := (0.0 .. 9.0); // Added range for food
|
||
|
END_FUZZIFY
|
||
|
|
||
|
// Defzzzify output variable 'tip'
|
||
|
|
||
|
DEFUZZIFY tip
|
||
|
TERM cheap := (0,0) (5,1) (10,0);
|
||
|
TERM average := (10,0) (15,1) (20,0);
|
||
|
TERM generous := (20,0) (25,1) (30,0);
|
||
|
// Use 'Center Of Gravity' defuzzification method
|
||
|
METHOD : COG;
|
||
|
// Default value is 0 (if no rule activates defuzzifier)
|
||
|
DEFAULT := 0;
|
||
|
RANGE := (0.0 .. 30.0); // Added range for tip
|
||
|
END_DEFUZZIFY
|
||
|
|
||
|
RULEBLOCK No1
|
||
|
// Use 'min' for 'and' (also implicit use 'max'
|
||
|
// for 'or' to fulfill DeMorgan's Law)
|
||
|
AND : MIN;
|
||
|
// Use 'min' activation method
|
||
|
ACT : MIN;
|
||
|
// Use 'max' accumulation method
|
||
|
ACCU : MAX;
|
||
|
|
||
|
RULE 1 : IF service IS poor OR food IS rancid
|
||
|
THEN tip IS cheap;
|
||
|
|
||
|
RULE 2 : IF service IS good
|
||
|
THEN tip IS average;
|
||
|
|
||
|
RULE 3 : IF service IS excellent AND food IS delicious
|
||
|
THEN tip is generous;
|
||
|
END_RULEBLOCK
|
||
|
|
||
|
END_FUNCTION_BLOCK
|
||
|
|