1. Objectives
- Compute fuzzy union (max), intersection (min) and complement (1 - degree) and plot them.
- Combine degrees from two different variables with fuzzy AND / OR (temperature AND humidity).
- Apply the hedges 'very' and 'somewhat' and find alpha-cuts of a fuzzy set.
- Clip, combine and defuzzify output sets with the centroid method (a first look at fuzzy inference).
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Union (OR) | Degree in A or B = the larger degree: max | Warm OR hot |
Intersection (AND) | Degree in A and B = the smaller degree: min | Hot AND humid (a sticky July day) |
Complement (NOT) | Degree not in A = 1 - degree in A | NOT hot |
Hedge | A word that changes a fuzzy set | 'very hot' = degree squared |
Alpha-cut | All values whose degree is at least alpha | Temperatures that are hot to at least 0.5 |
Defuzzification | Turning a fuzzy result into one crisp number | Fan speed 61% |
Centroid | The balance point (centre of gravity) of the area under a curve | Where a cardboard cut-out balances |
3. Background
Operations
P07a built fuzzy sets. Now we combine them. Zadeh's standard operators: union A OR B = max(μA, μB), intersection A AND B = min(μA, μB), complement NOT A = 1 - μA. For two sets on the SAME universe we apply them point by point (np.fmax, np.fmin). For two different variables (temperature and humidity) we first find each degree with interp_membership and then take min or max of the two numbers - exactly what a fuzzy rule 'IF hot AND humid' does.
Hedges and alpha-cuts
'very hot' is stricter than 'hot': μ² (0.6 becomes 0.36). 'somewhat hot' is looser: √μ (0.36 becomes 0.6). An alpha-cut turns a fuzzy set into an ordinary range: the 0.5-cut of 'hot' is every temperature that is hot to degree 0.5 or more.
A first look at defuzzification
A fuzzy controller ends with a fuzzy output, e.g. fan speed 'medium' to 0.5 and 'fast' to 0.7. Each output set is cut (clipped) at its level, the pieces are joined with max, and the centroid (balance point) of the shape gives one number for the motor. Lecture 23 and Practicals 08-09 do the full process.
Data files for this lab
No data file is needed - the programs make their own data.
4. Time Plan (50 minutes)
Minutes | Activity | Part |
|---|---|---|
0-5 | Background: max, min, 1 - μ; hedges; defuzzification idea | 1-3 |
5-10 | Guided warm-up: operations on one universe; AND/OR across two variables | Part A |
10-13 | Predict the output | Part B |
13-17 | Bug hunt | Part C |
17-47 | Graded tasks 1-3 | Part D |
47-50 | Wrap-up; post-lab questions set as homework | Part E |
Part A - Guided Warm-up (type, run, observe)
A1. Union, intersection and complement on one universe
warm and hot overlap between 28 and 40 °C. Union takes the higher curve, intersection the lower, complement flips 'hot'.
import numpy as np import skfuzzy as fuzz import matplotlib.pyplot as plt x = np.arange(0, 51, 1) warm = fuzz.trimf(x, [20, 30, 40]) hot = fuzz.trapmf(x, [28, 38, 50, 50]) union = np.fmax(warm, hot) # warm OR hot inter = np.fmin(warm, hot) # warm AND hot not_hot = 1 - hot # NOT hot fig, ax = plt.subplots(1, 3, figsize=(8, 2.3), sharey=True) for a, mf, t in zip(ax, [union, inter, not_hot], ["warm OR hot", "warm AND hot", "NOT hot"]): a.plot(x, warm, ":", c="grey"); a.plot(x, hot, ":", c="grey"); a.fill_between(x, mf, alpha=0.5); a.set_title(t) plt.tight_layout(); plt.savefig("operations.png", dpi=100) print("temp warm hot OR AND NOT hot") for t in [25, 32, 36, 45]: print(f"{t:>4} {warm[t]:.2f} {hot[t]:.2f} {union[t]:.2f} {inter[t]:.2f} {not_hot[t]:.2f}") print("Saved operations.png") |
Output temp warm hot OR AND NOT hot 25 0.50 0.00 0.50 0.00 1.00 32 0.80 0.40 0.80 0.40 0.60 36 0.40 0.80 0.80 0.40 0.20 45 0.00 1.00 1.00 0.00 0.00 Saved operations.png |
A2. AND / OR across two variables: a July afternoon
Temperature and humidity live on different universes. Find each degree first, then combine the two numbers.
import numpy as np import skfuzzy as fuzz t_x = np.arange(0, 51, 1); h_x = np.arange(0, 101, 1) hot = fuzz.trapmf(t_x, [28, 38, 50, 50]) humid = fuzz.trapmf(h_x, [50, 80, 100, 100]) temp, hum = 36, 70 # 36 C, 70% humidity mu_hot = fuzz.interp_membership(t_x, hot, temp) mu_humid = fuzz.interp_membership(h_x, humid, hum) print(f"hot = {mu_hot:.2f}, humid = {mu_humid:.2f}") print(f"hot AND humid = {min(mu_hot, mu_humid):.2f}") print(f"hot OR humid = {max(mu_hot, mu_humid):.2f}") print(f"NOT hot = {1 - mu_hot:.2f}") |
Output hot = 0.80, humid = 0.67 hot AND humid = 0.67 hot OR humid = 0.80 NOT hot = 0.20 |
Part B - Predict the Output
Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.
B1.
import numpy as np a = np.array([0.2, 0.7, 1.0]); b = np.array([0.5, 0.4, 0.0]) print(np.fmax(a, b), np.fmin(a, b)) |
My prediction |
B2.
mu = 0.6 print(round(mu ** 2, 2), round(mu ** 0.5, 2), round(1 - mu, 2)) |
My prediction |
B3.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 51, 1) warm = fuzz.trimf(x, [20, 30, 40]) cut = x[warm >= 0.5] print(cut.min(), cut.max()) |
My prediction |
Part C - Bug Hunt
Each program has ONE bug. Some crash with an error message; others run but give a wrong answer (silent logic error - the dangerous kind). Find the bug, explain it and write the fix.
C1.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 51, 1) warm = fuzz.trimf(x, [20, 30, 40]); hot = fuzz.trapmf(x, [28, 38, 50, 50]) warm_or_hot = warm + hot print("largest degree:", warm_or_hot.max()) |
It runs and prints largest degree: 1.2000000000000002 |
Bug, why it happens, and the fixed line(s) |
C2.
import numpy as np, skfuzzy as fuzz t_x = np.arange(0, 51, 1); h_x = np.arange(0, 101, 1) hot = fuzz.trapmf(t_x, [28, 38, 50, 50]); humid = fuzz.trapmf(h_x, [50, 80, 100, 100]) sticky = np.fmin(hot, humid) # hot AND humid print(sticky.max()) |
Bug, why it happens, and the fixed line(s) |
C3.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 51, 1) hot = fuzz.trapmf(x, [28, 38, 50, 50]) very_hot = 2 * hot # 'very' hedge print("very hot at 36 C:", very_hot[36], "| at 45 C:", very_hot[45]) |
It runs and prints very hot at 36 C: 1.6 | at 45 C: 2.0 |
Bug, why it happens, and the fixed line(s) |
Part D - Graded Tasks (10 marks)
Task | Level | Title | Marks |
|---|---|---|---|
T1 | Easy | Which day is the most uncomfortable? | 3 |
T2 | Medium | Hedges and alpha-cuts | 3 |
T3 | Challenge | From fuzzy to crisp: what speed should the fan run at? | 4 |
Total | 10 |
Task 1 (Easy, 3 marks): Which day is the most uncomfortable?
Use hot = trapmf [28, 38, 50, 50] on 0-50 °C and humid = trapmf [50, 80, 100, 100] on 0-100 %. For the five days (30 °C, 40 %), (34 °C, 65 %), (38 °C, 75 %), (41 °C, 55 %) and (36 °C, 90 %) print the degree of hot, humid, 'hot AND humid' (discomfort) and 'hot OR humid'. Which day is the most uncomfortable?
Sample run (your output must match):
Output day temp hum hot humid AND OR 1 30 40 0.20 0.00 0.00 0.20 2 34 65 0.60 0.50 0.50 0.60 3 38 75 1.00 0.83 0.83 1.00 4 41 55 1.00 0.17 0.17 1.00 5 36 90 0.80 1.00 0.80 1.00 Most uncomfortable: day 3 (hot AND humid = 0.83) |
Your code (or attach the file) |
Task 2 (Medium, 3 marks): Hedges and alpha-cuts
On 0-50 °C (step 0.1) with hot = trapmf [28, 38, 50, 50]: (a) make 'very hot' (μ²) and 'somewhat hot' (√μ) and plot all three (save task2.png); (b) print the degree of 33 °C in each; (c) print the 0.5-cut (lowest and highest temperature with degree ≥ 0.5) of each set. Which hedge makes the set narrower?
Sample run (your output must match):
Output somewhat hot 33 C -> 0.71 0.5-cut: 30.5 to 50.0 C hot 33 C -> 0.50 0.5-cut: 33.0 to 50.0 C very hot 33 C -> 0.25 0.5-cut: 35.1 to 50.0 C Saved task2.png |
Your code (or attach the file) |
Task 3 (Challenge, 4 marks): From fuzzy to crisp: what speed should the fan run at?
Fan speed (0-100 %, step 1) has three sets: slow = trimf [0, 0, 50], medium = trimf [25, 50, 75], fast = trimf [50, 100, 100]. Some rules have fired with strengths slow 0.2, medium 0.5, fast 0.7. Clip each set at its strength (np.fmin), join the three clipped sets with np.fmax, and plot the result (save task3.png). Compute the centroid (a) with fuzz.defuzz(x, agg, 'centroid') and (b) with your own formula Σ x·μ / Σ μ. Also print the 'mom' (mean of maximum) result. Which answer would you send to the fan motor?
Sample run (your output must match):
Output centroid (scikit-fuzzy): 61.09 % centroid (own formula): 61.27 % mean of maximum (mom): 92.50 % Saved task3.png |
Your code (or attach the file) |
Part E - Post-Lab Questions (finish as homework if time runs out)
E1. A reading is warm to 0.4 and hot to 0.7. Give warm OR hot, warm AND hot, and NOT hot.
Your answer |
E2. What do the hedges 'very' and 'somewhat' do to a degree of 0.81?
Your answer |
E3. Why do we need defuzzification in a fuzzy controller?
Your answer |
Marking Rubric (applied to Part D)
Criterion | Full | Partial | None |
|---|---|---|---|
Code runs without errors | Runs fully | Small fixable error | Does not run |
Correct operators (max, min, 1 - μ, μ², √μ) on the right universes | All correct | One slip | Several wrong |
Tables, plots, alpha-cuts and centroids | All present | Some missing | None |
Explanations (discomfort day, hedges, choice of defuzzifier) | Clear and correct | Partly correct | Missing |
Submission
- Make a folder RollNo_P07b with task1.py, task2.py, task3.py (or P07b.ipynb), task2.png and task3.png.
- Upload the zipped folder to the LMS / Google Classroom by the end of the day.
- Copy Task 1 and Task 3 code and output into your Lab Book (Practical 07b page).
- Keep your P07a membership functions - P08 reuses them.