The idea: rules in words, numbers in and out

A fan should run faster when the room is hot or humid. A person can say that in words, but a controller needs numbers. Fuzzy inference is the bridge. In ordinary logic a temperature is either "warm" or not. In fuzzy logic it is warm to a degree between 0 and 1. The page shows the most common design, a Mamdani controller. It takes two crisp (exact) inputs, temperature T and humidity H, and gives one crisp output, the fan speed S. In between it works in four steps: fuzzify, fire the rules, clip and aggregate, defuzzify.

Five boxes joined by arrows. Crisp inputs T = 24 °C and H = 40 %. Step 1 fuzzify: warm 0.60, hot 0.40, dry 0.75, humid 0.25. Step 2 fire rules: R1 0, R2 min = 0.60, R3 max = 0.40. Step 3 clip and max: one fuzzy set drawn as a shape with two flat tops. Step 4 defuzzify: centroid, fan = 62.9 %. A band under the boxes marks the first and last as numbers and the three in the middle as degrees between 0 and 1
Crisp numbers go in and come out; in the middle everything is a degree between 0 and 1.

The controller on this page has three rules:

ruleifthenstrength
R1temp is coldfan is lowcold
R2temp is warm AND air is dryfan is mediummin(warm, dry)
R3temp is hot OR air is humidfan is highmax(hot, humid)

Membership functions: triangles and trapezoids

A fuzzy set such as "warm" is described by a membership function μ. For every value x it gives the degree of membership μ(x): 0 means not in the set, 1 means fully in, and anything between means partly in. Almost all controllers use two simple shapes. A triangle (a, b, c) rises from 0 at a to 1 at b and falls back to 0 at c. A trapezoid (a, b, c, d) has a flat top: it is 1 from b to c. The sets at the two ends of an axis are trapezoids that stay at 1 to the edge, often called shoulders: anything below 10 °C is fully cold.

Three small plots of a degree from 0 to 1. A green triangle with corners a = 10, b = 20, c = 30, the set warm, with a dashed line at x = 24 reading μ(24) = 0.60. A purple trapezoid with corners 10, 15, 25, 30 and a flat top from b to c. A blue shoulder that is 1 from 0 to 10 and falls to 0 at 20, the set cold = (0, 0, 10, 20)
A triangle is given by 3 corner points and a trapezoid by 4; reading a degree is one straight-line calculation.
variablesetshapecorners
temperature T, 0–40 °Ccoldtrapezoid (left shoulder)0, 0, 10, 20
warmtriangle10, 20, 30
hottrapezoid (right shoulder)20, 30, 40, 40
humidity H, 0–100 %drytrapezoid (left shoulder)0, 0, 30, 70
humidtrapezoid (right shoulder)30, 70, 100, 100
fan speed S, 0–100 %lowtriangle, peak at 00, 0, 50
mediumtriangle25, 50, 75
hightriangle, peak at 10050, 100, 100

Step 1: fuzzify

Fuzzification reads each crisp input on every set of its axis. At T = 24 °C the line is on the falling side of warm, so warm = (30 − 24) / 10 = 0.60, and on the rising side of hot, so hot = (24 − 20) / 10 = 0.40. Cold is 0. At H = 40 %, dry = (70 − 40) / 40 = 0.75 and humid = (40 − 30) / 40 = 0.25. Neighbouring sets overlap on purpose, so one value usually belongs to two sets at once (Demo: the four steps, T = 24 °C, H = 40 %). These degrees are not probabilities: 24 °C is not "warm with a 60 % chance". It is simply warm to degree 0.60.

Step 2: fire the rules with min and max

Each rule gets a firing strength from the degrees of its premises. AND takes the minimum: a rule is only as true as its weakest premise. OR takes the maximum: one strong premise is enough. With degrees of exactly 0 and 1 these give the ordinary truth tables, so fuzzy logic contains ordinary logic as a special case. For the inputs above, R2 = min(0.60, 0.75) = 0.60 and R3 = max(0.40, 0.25) = 0.40. R1 has strength 0 and does not fire. Unlike crisp rules, several rules fire at the same time, each to its own degree.

At T = 20 °C the room is fully warm, but at H = 60 % the air is dry only to 0.25, so R2 = min(1, 0.25) = 0.25 (Demo: AND takes the min). At T = 28 °C and H = 60 %, R3 = max(0.80, 0.75) = 0.80 (Demo: OR takes the max).

See also Truth Tables and Sets for AND and OR on crisp values, and Forward vs Backward Chaining for if-then rules that are simply true or false.

Step 3: clip and aggregate

A rule's conclusion is a fuzzy set too ("fan is medium"). The rule's strength limits it: the output set is clipped, cut off flat, at that height. A triangle clipped at 0.60 becomes a trapezoid of height 0.60. This is implication by min, the choice that makes a controller a Mamdani controller. Then the clipped sets of all rules are aggregated into one shape by taking, at every speed s, the largest of them (max). The result is a fuzzy set that says how much each fan speed is supported by the rules together.

Rules can pull in opposite directions. At T = 5 °C and H = 80 % the room is fully cold (R1 = 1, fan low) and fully humid (R3 = 1, fan high). Both output sets are kept whole and the aggregate has two peaks (Demo: two rules disagree, cold but humid).

Step 4: defuzzify with the centroid

The fan needs one number. The usual choice is the centroid (centre of gravity) of the aggregated shape: the point on the axis where the shape would balance. The page computes it at the 101 whole speeds 0, 1, …, 100:

S=∑s·μ(s)∑μ(s)

For T = 24 °C and H = 40 % the sums are 2077.6 / 33.04, so the fan runs at 62.9 %. When two rules disagree fully the shape is symmetric and the centroid is the compromise, exactly 50.0 %. The centroid also has a limit: at T = 35 °C only R3 fires, with strength 1, and the answer is the centroid of the whole "high" triangle, 83.7 %, not 100 % (Demo: one rule at full strength, still not 100 %). In the same way the lowest speed this controller can give is 16.3 %. Designers fix this by placing the outer output sets beyond the range they want to reach.

demo inputsR1 lowR2 mediumR3 highfan speed
T = 24 °C, H = 40 %0min(0.60, 0.75) = 0.60max(0.40, 0.25) = 0.4062.9 %
T = 20 °C, H = 60 %0min(1, 0.25) = 0.25max(0, 0.75) = 0.7573.6 %
T = 28 °C, H = 60 %0min(0.20, 0.25) = 0.20max(0.80, 0.75) = 0.8075.4 %
T = 5 °C, H = 80 %1min(0, 0) = 0max(0, 1) = 150.0 %
T = 35 °C, H = 20 %0min(0, 1) = 0max(1, 0) = 183.7 %

Why bother: a smooth control curve

A controller with crisp rules ("below 15 °C run at 20 %, up to 25 °C at 50 %, above that at 85 %") jumps at each threshold, so the fan changes speed with a jolt and may flip back and forth when the temperature sits near a border. In the fuzzy controller the sets overlap, so as the temperature rises one rule fades out while the next fades in, and the centroid slides across. At H = 40 % the speed goes 35.1 → 43.4 → 58.5 → 65.7 → 83.7 → 83.7 % for T = 10, 15, 20, 25, 30, 35 °C (Demo: sweep the temperature, the fan speeds up smoothly). The rules stay readable, and tuning the controller means moving corner points of the sets.

A chart of fan speed against temperature from 5 to 35 °C at humidity 40 %. A grey staircase for crisp rules sits at 20 %, jumps to 50 % at 15 °C and to 85 % at 25 °C. An orange curve for the fuzzy rules rises without jumps through the points 35.1, 43.4, 58.5, 65.7, 83.7 and 83.7 % at 10, 15, 20, 25, 30 and 35 °C
The same idea as three crisp speeds, but the fuzzy controller moves between them without a jump.

A learned set of crisp thresholds is what Decision Tree Learning builds. For reasoning with probabilities, which measure how likely something is and not how much it belongs to a set, see Bayesian Network Inference.

What the page leaves out

Real controllers often have more inputs (for example the error and its rate of change), 5 or 7 sets per variable and a full rule table, and they run the four steps again at every sample time. The page shows only min and max; other choices exist, such as product for AND, scaling the output set by the strength where the page clips it, and adding where the page takes the max. Other defuzzifiers are the bisector, the mean of maxima and the weighted average. A Sugeno (TSK) controller replaces the output sets with constants or linear functions, so step 4 is a simple weighted average. The page also skips smooth shapes such as Gaussian and bell membership functions, rule weights, NOT (1 − μ), and how sets and rules are tuned or learned from data (ANFIS). The centroid here is a sum over 101 points; an exact integral gives slightly different last digits.

On the mind map

This page on the AI: Search, Games & Reasoning mind map · 18 pages in 7 branches · open it to see the whole map and test yourself in recall mode.