A Tunable Fuzzy Logic Engine
Spring 1997, Dr. Dobb's Journal
by Richard Sevenich and Paul Leathers
/* declare an input domain */
input domain TIME
/* define sets within TIME domain */
TIME EARLY_AM is {0 0 6 7}
TIME MORNING is {6 7 11 12}
TIME AFTERNOON is {11 12 16 17}
TIME EVENING is {16 17 19 20}
TIME NIGHT is {19 20 24 24}
/* declare another input domain */
input domain TEMPERATURE
/* define sets within TEMPERATURE domain */
COLD is {0 0 40 50}
COOL is {40 50 60 70}
WARM is {60 70 80 90}
HOT is {80 90 100 100}
/* declare an output domain */
output domain FAN_SPEED
/* define sets within FAN_SPEED */
OFF is {-5 0 0 5}
LOW is {0 5 10 20}
MEDIUM is {10 20 30 40}
HIGH is {30 40 50 50}
/* some possible rules associated with the */
/* preceding inputs and outputs - not a */
/* complete selection of rules, merely examples */
if TEMPERATURE is HOT or TEMPERATURE is very WARM
then FAN_SPEED is very HIGH
if TEMPERATURE is COLD or (TEMPERATURE is COOL and TIME is
EVENING)then FAN_SPEED is OFF
if TEMPERATURE is COOL or TIME is MORNING
then FAN_SPEED is LOW
if TEMPERATURE is WARM or (TEMPERATURE is COOL and TIME is
MORNING) then FAN_SPEED is MEDIUM
...
if TEMPERATURE is HOT or TEMPERATURE is very WARM
then FAN_SPEED is very HIGH
input domain TEMPERATURE
COLD is {0 0 40 50}
COOL is {40 50 60 70}
WARM is {60 70 80 90}
HOT is {80 90 100 100}
...
Listing Three
ERROR at line 25, Unknown domain: <>
COOL is {40 50 60 30}
WARM is {70 60 80 90}
Listing Five
ERROR at line 13, set <> begins at 40, ends at 30
ERROR at line 14, set <> begins at 70, max at 60
if TEMPERATURE is HOT or TEMPERATURE is very HIGH
then FAN_SPEED is very HIGH
ERROR at line 21, Unknown <> set: <>
if TEMPERATURE is HOT then FAN_SPEED is HIGH
if TEMPERATURE is HOT AND FAN_SPEED is HIGH
then DELTA_TEMPERATURE is NEGATIVE_SMALL
input domain temperature
cold is {20 40 50 60}
cool is {50 60 65 75}
warm is {65 75 80 90}
hot is {80 90 100 100}
output domain fan_speed
off is {-10 0 0 10}
low is {0 10 10 20}
med is {10 20 20 30}
high is {20 30 30 40}
if temperature is cold then fan_speed is off
if temperature is cool then fan_speed is low
if temperature is warm then fan_speed is med
if temperature is hot then fan_speed is high
input domain temperature
cold is {20 40 50 60}
cool is {50 60 65 75}
warm is {65 75 80 90}
hot is {80 90 100 100}
input domain fan_speed
off is {0 0 0 0}
low is {0 10 10 20}
med is {10 20 20 30}
high is {20 30 30 40}
output domain delta_temperature
nm is {-15 -10 -10 -5}
zero is {-5 0 0 5}
ps is {0 5 5 10}
pm is {5 10 10 15}
pl is {10 15 20 20}
if temperature is cold and fan_speed is off
then delta_temperature is ps
if temperature is cold and
(fan_speed is low or fan_speed is med or fan_speed is high)
then delta_temperature is zero
if temperature is cool and fan_speed is off
then delta_temperature is pm
if temperature is cool and fan_speed is low
then delta_temperature is ps
if temperature is cool and (fan_speed is med or fan_speed is high)
then delta_temperature is zero
if (temperature is warm or temperature is hot) and fan_speed is off
then delta_temperature is pl
if (temperature is warm or temperature is hot) and fan_speed is low
then delta_temperature is pm
if (temperature is warm or temperature is hot) and fan_speed is med
then delta_temperature is ps
if (temperature is warm or temperature is hot) and fan_speed is high
then delta_temperature is nm
End Listings