AI with Fuzzy Logic
Senior 6 · ICT
AI with Fuzzy Logic
Lesson 2 of 5
25 min
A school buys a fan controller for a hot classroom in Bugesera. Before it can decide what to do, it needs to know what "cool", "comfortable" and "hot" mean, in numbers. Each word becomes a fuzzy set on the temperature scale, and each set is described by a membership function: a graph that gives, for every temperature, how fully that temperature belongs to the word. Choosing these functions is the first design step of every fuzzy system.
Definition
Linguistic variable: a quantity whose values are words. Example: classroom temperature, with the values (called terms) cool, comfortable and hot.
Universe of discourse: the full range of values the quantity can take. Example: 10 °C to 40 °C.
Membership function : a function that gives, for each in the universe, the degree to which belongs to the fuzzy set , a number in .
Fuzzification: turning a measured number into its degrees of membership in each term. Example: 21 °C becomes cool 0.2, comfortable 0.6, hot 0.
Triangular, with feet at and and peak at :
Trapezoidal, with feet at and and a flat top from to :
A trapezoid with at the left end of the universe (or at the right end) is called a shoulder: it stays at 1 all the way to the edge, as "cool" and "hot" do below.
Gaussian (bell-shaped), centred on with width :
Triangles and trapezoids are quick to compute and easy to explain, so most teaching examples and many small controllers use them. Gaussian curves are smooth everywhere, which some optimisation methods need.
Definition
Features of a fuzzy set :
The fan controller uses these three sets. Notice that neighbouring sets overlap: between 18 °C and 22 °C a temperature is partly cool and partly comfortable. Overlap is what makes the controller change smoothly; with no overlap it would jump from one behaviour to the next like a crisp switch.
| Term | Shape and parameters | Core | Support |
|---|---|---|---|
| cool | Left shoulder (10, 10, 17, 22) | 10 to 17 °C | 10 to just under 22 °C |
| comfortable | Triangle (18, 23, 28) | 23 °C only | between 18 and 28 °C |
| hot | Right shoulder (24, 30, 40, 40) | 30 to 40 °C | above 24 °C |
Worked example 1: fuzzify 21 °C and 26 °C
At 21 °C.
At 26 °C.
The memberships at one temperature need not add up to 1: at 21 °C they add to 0.8, and at 26 °C to about 0.73. Fuzzy memberships are not shares of a whole.
Worked example 2: "about 25 °C" as a bell
A Gaussian set "about 25 °C" has and . Find the membership of 27 °C and 29 °C.
Twice as far from the centre gives far less than half the membership: the bell falls off quickly in its tails.
The same calculations in Python, tested with python3. The functions follow the formulas above; fuzzify returns the three memberships for a temperature.
import math
def tri(x, a, b, c):
"""Triangle: 0 at a, rises to 1 at b, falls to 0 at c."""
return max(min((x - a) / (b - a), (c - x) / (c - b)), 0.0)
def trap(x, a, b, c, d):
"""Trapezoid: 0 at a, 1 from b to c, 0 at d. Use a == b or c == d for a shoulder."""
rise = (x - a) / (b - a) if b > a else 1.0
fall = (d - x) / (d - c) if d > c else 1.0
return max(min(rise, 1.0, fall), 0.0)
def gauss(x, c, s):
"""Gaussian bell centred on c with width s."""
return math.exp(-((x - c) ** 2) / (2 * s ** 2))
def fuzzify(t):
return {"cool": round(trap(t, 10, 10, 17, 22), 3),
"comfortable": round(tri(t, 18, 23, 28), 3),
"hot": round(trap(t, 24, 30, 40, 40), 3)}
for t in (15, 21, 23, 26, 32):
print(t, fuzzify(t))
print(round(gauss(27, 25, 2), 3), round(gauss(29, 25, 2), 3))
Output:
15 {'cool': 1.0, 'comfortable': 0.0, 'hot': 0.0}
21 {'cool': 0.2, 'comfortable': 0.6, 'hot': 0.0}
23 {'cool': 0.0, 'comfortable': 1.0, 'hot': 0.0}
26 {'cool': 0.0, 'comfortable': 0.4, 'hot': 0.333}
32 {'cool': 0.0, 'comfortable': 0.0, 'hot': 1.0}
0.607 0.135The printed values match the worked examples: 0.2 and 0.6 at 21 °C, 0.4 and 0.333 at 26 °C, and 0.607 and 0.135 for the bell.
MATLAB: What Is Fuzzy Logic? (Fuzzy Logic, Part 1). Watch how a crisp input is fuzzified using overlapping membership functions, and notice the shapes used; they are the same triangles and trapezoids as in this lesson.
Warning
Read the right side of the triangle. The most common error is using the rising formula on the falling side. Always check first where is: left of the peak uses the rising side, right of the peak uses the falling side, and outside the feet the value is 0. A membership value above 1 or below 0 means you used the wrong side.
Tip
Sketch before you compute. Draw the sets, mark your with a vertical line, and see which sets it cuts and roughly how high. Then calculate. The sketch tells you which formula to use and whether your answer is sensible.
fuzzify to use your sets. Run it for five journey times, including one in each overlap.Check: every value between 0 and 120 minutes belongs to at least one set with membership above 0, and your hand calculations match the program.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.