AI with Fuzzy Logic
Senior 6 · ICT
AI with Fuzzy Logic
Lesson 1 of 5
22 min
Ask a class in Kigali "Is today hot?" At 31 °C everyone says yes, and at 19 °C everyone says no. At 27 °C the class splits: some say "a bit", some say "not really", and nobody is wrong. Now try to program it. A conventional program needs a cut-off: if temperature >= 28: hot = True. Then 27.9 °C is "not hot" and 28.0 °C is "hot", although nobody can feel a tenth of a degree. The word hot has no sharp edge, and forcing one onto it makes the program behave strangely near the edge.
Fuzzy logic deals with this by letting a statement be true to a degree. At 27 °C, "the day is hot" can be 0.5 true. This unit shows how computers reason with such degrees, and why the idea, more than sixty years old, still runs inside rice cookers, washing machines, camera autofocus and train controllers.
Definition
Crisp set (classical set): a set that each element either belongs to or does not; membership is 0 or 1. Example: the set of students in S6.
Fuzzy set: a set in which each element has a degree of membership, written , any number from 0 (not at all a member) to 1 (fully a member). Example: the set of hot days, in which a 27 °C day has .
Fuzzy logic: a way of reasoning in which statements have degrees of truth between 0 and 1, rather than only true or false. It was introduced by Lotfi A. Zadeh in the paper "Fuzzy Sets" (1965).
The crisp definition is a step: at 27 °C a day is not hot at all; one degree later it is fully hot. The fuzzy definition is a ramp: membership rises by for every degree from 24 °C to 30 °C, so
A fan controlled by the crisp rule switches fully on at 28 °C and fully off at 27.9 °C, and on a day that hovers around 28 °C it keeps clicking on and off. A fan controlled by the fuzzy value can run at half speed at 27 °C and speed up smoothly as the room warms. This smoothness is the main practical reason engineers use fuzzy logic in control.
Both use numbers from 0 to 1, so they are easily confused, but they answer different questions.
| Degree of membership (fuzzy) | Probability | |
|---|---|---|
| Question it answers | How far does this thing fit a vague description? | How likely is an uncertain event? |
| "0.7" means | The day is hot to degree 0.7, now, with nothing unknown | There is a 70% chance it will be hot; it will turn out fully hot or fully not |
| After you look | Still 0.7: the vagueness is in the word | Becomes 0 or 1: the uncertainty disappears |
| Must values add to 1? | No: a day can be 0.5 hot and 0.6 humid | Yes, over all possible outcomes |
A water example makes the difference sharp. Bottle A is known to be 0.9 a member of "safe to drink": the water is slightly cloudy but harmless. Bottle B has a probability of 0.9 of being safe: nine bottles in ten from that source are clean, and one in ten is contaminated. Bottle A will never make you seriously ill; bottle B might.
Artificial intelligence has two broad traditions. Knowledge-based (symbolic) AI writes human knowledge as rules that a program applies. Machine learning lets a program find patterns in data. Fuzzy logic belongs to the first tradition: an expert writes rules in words, such as "if the soil is dry and the day is hot, water for a long time", and fuzzy logic turns those words into numbers a machine can compute with. It does not learn by itself, although it can be combined with learning methods (for example, neuro-fuzzy systems tune their membership functions from data).
| Year | Milestone |
|---|---|
| 1965 | Lotfi A. Zadeh, a professor at the University of California, Berkeley, publishes "Fuzzy Sets" in the journal Information and Control. |
| 1975 | Ebrahim Mamdani and Sedrak Assilian in London control a laboratory steam engine with fuzzy rules: the first fuzzy controller. |
| 1987 | The Namboku Line of the Sendai subway in Japan opens with fuzzy control of acceleration and braking, built by Hitachi. |
| Late 1980s onwards | Fuzzy control appears in consumer products: washing machines, rice cookers, air conditioners and camera autofocus. |
Computerphile: Fuzzy Logic. Watch for the difference between a crisp and a fuzzy set, and for how the degrees of membership are drawn as graphs; compare them with the "hot day" graph above.
Worked example 1: crisp and fuzzy "full"
A school's water tank is "full" in a crisp program when the level is at least 90%. A fuzzy version says membership in "full" rises from 0 at 70% to 1 at 95%. Find both values at 80% and at 92%.
A pump controller using the fuzzy value can slow the pump as the tank nears full instead of slamming it off at exactly 90%.
Worked example 2: a membership curve from people
Ten students each give the temperature at which a Kigali day becomes "hot" for them (illustrative data): 25, 26, 26, 27, 27, 27, 28, 28, 29, 30 °C. A simple membership value for "hot" at temperature is the fraction of students whose threshold is at most .
This short Python program, run with python3, does the same count for every temperature:
thresholds = [25, 26, 26, 27, 27, 27, 28, 28, 29, 30]
def mu_hot(t):
return sum(1 for h in thresholds if h <= t) / len(thresholds)
for t in range(24, 31):
print(t, mu_hot(t))24 0.0
25 0.1
26 0.3
27 0.6
28 0.8
29 0.9
30 1.0The values rise from 0 to 1 without a jump: a fuzzy set built from how people actually use the word.
Warning
Fuzzy does not mean careless. Fuzzy logic is exact mathematics about vague words. The membership values are precise numbers, the rules are written down, and the result is the same every time for the same inputs. What is "fuzzy" is the boundary of the word, not the calculation.
Tip
Two questions tell them apart. "How well does it fit the word?" is fuzzy membership. "How likely is it to happen?" is probability. If the number would turn into a plain 0 or 1 once you look, it was a probability.
thresholds, and change the range to suit your quantity. Run it.Check: your curve rises from 0 to 1, and you can read off the membership at any value and explain why the crisp line gives a strange answer just below the cut-off.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.