AI with Fuzzy Logic
Senior 6 · ICT
AI with Fuzzy Logic
Lesson 4 of 5
25 min
Ask an experienced farmer on a school vegetable plot in Bugesera how long to water the tomatoes, and you will not get an equation. You will get rules: "If the soil is dry and the day is hot, water for a long time. If the soil is moist and the day is mild, a short watering is enough. If it is wet, hardly water at all." Each rule uses vague words, and several rules may apply at once to some degree. A fuzzy inference system takes rules exactly like these, combines them, and produces one number: how many minutes to open the valve.
Definition
Fuzzy rule: a statement of the form IF antecedent THEN consequent, where both parts use linguistic terms. Example: IF soil is dry AND temperature is hot THEN watering time is long.
Firing strength: the degree to which a rule's IF part is true for the current inputs, found with the fuzzy operators.
Rule base: the full set of rules of a system, often written as a table.
Defuzzification: turning the combined fuzzy output into one crisp number. The most common method is the centroid (centre of area):
The method used here is named after Ebrahim Mamdani, whose 1975 steam-engine controller used it. It has five steps.
Two inputs and one output, each a linguistic variable:
| Variable | Terms and membership functions |
|---|---|
| Soil moisture (%) | dry: trapezoid (0, 0, 20, 40); moist: triangle (20, 40, 60); wet: trapezoid (40, 60, 100, 100) |
| Air temperature (°C) | mild: trapezoid (0, 0, 20, 30); hot: trapezoid (20, 30, 50, 50) |
| Watering time (min) | short: triangle (0, 5, 10); medium: triangle (8, 15, 22); long: triangle (20, 25, 30) |
The rule base, written as a table (soil down the side, temperature across the top, watering time in the cells):
| Soil \ Temperature | mild | hot |
|---|---|---|
| dry | medium (R2) | long (R1) |
| moist | short (R4) | medium (R3) |
| wet | short (R5) | short (R6) |
Worked example 1: steps 1 to 4 for 26% moisture and 27 °C
Step 1, fuzzify. Soil: dry ; moist ; wet . Temperature: mild ; hot .
Step 2, firing strengths (AND = min):
| Rule | IF part | Strength | THEN |
|---|---|---|---|
| R1 | dry AND hot | long | |
| R2 | dry AND mild | medium | |
| R3 | moist AND hot | medium | |
| R4 | moist AND mild | short | |
| R5, R6 | wet AND … | 0 | short |
Step 3, clip. Each output set is cut off at its rule's strength. Two rules point to medium, both at 0.3; when several rules share an output, the strongest one counts (OR = max), so medium is clipped at 0.3. Short is clipped at 0.3 and long at 0.7.
Step 4, aggregate. The clipped sets are joined with max into one shape, shaded below.
Worked example 2: step 5, the centroid
Each clipped triangle is a trapezoid. A triangle of base clipped at height has a top of width , so its area is
Ignoring the small overlaps between neighbouring sets, the centroid is the area-weighted mean of the centres:
The program in the next lesson, which integrates the exact shape, also gives 16.9 minutes. The controller waters for about 17 minutes: longer than "medium", because the strongest rule (dry and hot) points to "long", but pulled down by the three weaker rules.
MATLAB: Fuzzy Inference System Walkthrough (Fuzzy Logic, Part 2). Follow how the inputs are fuzzified, the rules fire and clip their outputs, and the result is defuzzified; it is the same five-step process as the irrigation example.
Warning
Check that the rules cover every case. If some combination of inputs makes every rule's strength 0 (for example, a gap between two sets), the aggregated shape is empty and the centroid is , undefined. A complete rule table with overlapping sets avoids this, and a program should still guard against it.
Tip
Quick estimate, then exact. The weighted mean of the peaks, , is a fast sanity check. It differs from the centroid because the clipped areas are not proportional to the strengths, but if your centroid is far from it, look for an error.
Check: for 34% and 24 °C your strengths should be dry 0.3, moist 0.7, mild 0.6, hot 0.4, and the strongest rule should be R4 (moist AND mild, 0.6).
Practice
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.