AI with Fuzzy Logic
Senior 6 · ICT
AI with Fuzzy Logic
Lesson 3 of 5
22 min
Real rules rarely test one thing. A nurse at a health centre in Nyagatare decides how urgently to see a child by combining several observations: the fever is high AND the child is very sleepy; the cough is long-lasting OR the breathing is fast. Each observation is a matter of degree. To build such rules into a machine we need fuzzy versions of AND, OR and NOT that work on degrees between 0 and 1, and that still give the ordinary answers when the degrees are exactly 0 or 1.
Definition
For degrees of membership and , the standard (Zadeh) operators are:
Example: if an afternoon is hot to degree 0.7 and humid to degree 0.4, then "hot AND humid" is .
A chain is only as strong as its weakest link: a statement "A and B" cannot be truer than its less true part, so AND takes the minimum. "A or B" is as true as its truer part, so OR takes the maximum. NOT turns "fully true" into "fully false" and leaves 0.5 where it is.
These operators extend ordinary logic. When the inputs are only 0 or 1 they give exactly the Boolean truth table:
| : AND | : OR | : NOT a | ||
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 |
| 0.7 | 0.4 | 0.4 | 0.7 | 0.3 |
The last row is something Boolean logic cannot express at all.
On a graph, AND is the region under both curves (the lower envelope, shaded), and OR is the region under either curve (the upper envelope, the grey band). The two curves cross at 20 °C, where gives and , so the peak of "cool AND comfortable" is 0.4.
NOT flips each curve upside down about : wherever comfortable is high, NOT comfortable is low, and the two always add to 1.
Worked example 1: a Kigali afternoon
At 14:00 the afternoon is hot to degree 0.7 and humid to degree 0.4. Evaluate four conditions.
So "hot but dry" (0.6) describes this afternoon better than "hot and humid" (0.4), which matches the numbers: it is clearly hot and not very humid.
Worked example 2: the fan's conditions at 26 °C
From lesson 2, at 26 °C: cool 0, comfortable 0.4, hot . Evaluate "comfortable OR hot" and "comfortable AND NOT hot".
Both conditions are true to degree 0.4. A rule such as "IF comfortable AND NOT hot THEN fan low" will therefore act at strength 0.4.
Most laws of ordinary logic still hold for min, max and : AND and OR are commutative and associative, and De Morgan's laws hold:
Two famous laws do not hold. In Boolean logic, "A OR NOT A" is always true and "A AND NOT A" is always false. In fuzzy logic, with :
A 25 °C day can be hot to degree 0.5 and not hot to degree 0.5 at the same time. This is not a mistake: it is exactly the borderline case that a vague word creates.
Min and max are not the only possibilities. The product is another common AND, and the probabilistic sum is another OR. For hot 0.7 and humid 0.4 they give 0.28 and 0.82. Product AND lets every input affect the result, while min ignores the larger input completely. Whichever pair you choose, use it consistently throughout one system.
This program, run with python3, computes the worked example, the alternative operators, and checks De Morgan's law on every pair of values from 0 to 1 in steps of 0.1.
from itertools import product
def AND(a, b): return min(a, b) # Zadeh intersection
def OR(a, b): return max(a, b) # Zadeh union
def NOT(a): return 1 - a # complement
hot, humid = 0.7, 0.4
print("hot AND humid ", AND(hot, humid))
print("hot OR humid ", OR(hot, humid))
print("NOT humid ", round(NOT(humid), 2))
print("hot AND NOT humid ", round(AND(hot, NOT(humid)), 2))
print("product AND ", round(hot * humid, 2))
print("probabilistic OR ", round(hot + humid - hot * humid, 2))
# De Morgan: NOT(a AND b) == NOT a OR NOT b, for every pair on a 0.1 grid
grid = [i / 10 for i in range(11)]
print("De Morgan holds:", all(abs(NOT(AND(a, b)) - OR(NOT(a), NOT(b))) < 1e-12
for a, b in product(grid, grid)))
# Excluded middle: a OR NOT a is not always 1
print("0.5 OR NOT 0.5 =", OR(0.5, NOT(0.5)))
Output:
hot AND humid 0.4
hot OR humid 0.7
NOT humid 0.6
hot AND NOT humid 0.6
product AND 0.28
probabilistic OR 0.82
De Morgan holds: True
0.5 OR NOT 0.5 = 0.5Topperly: Operations on Classical and Fuzzy Sets. Watch for the union, intersection and complement drawn as graphs, and check that they match the min, max and 1 − a rules in this lesson.
Warning
Do not add or multiply by habit. Students often add degrees for OR (0.7 + 0.4 = 1.1, which is not a degree at all) or average them for AND. With the standard operators, AND is the smaller value and OR is the larger one. A result above 1 or below 0 always means an error.
Tip
AND is pessimistic, OR is optimistic. AND believes the weaker evidence, so it can never be higher than either input. OR believes the stronger evidence, so it can never be lower than either input.
AND_prod(a, b) and test whether De Morgan's law holds when product AND is paired with probabilistic-sum OR.Check: the program prints True for both De Morgan laws with min and max, and you can state for which "A OR NOT A" is least true.
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.