1. Objectives
- Follow the fuzzy designer workflow: inputs, output, membership functions, rules, simulation.
- Build a room-fan controller with skfuzzy.control using the P07 temperature sets.
- Write rules with AND (&), OR (|) and NOT (~) and compute crisp fan speeds.
- Check one result of the system by hand: fuzzify, fire the rules, clip, aggregate, centroid.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Fuzzy inference system (FIS) | A program that turns crisp inputs into a crisp output using fuzzy rules | Fan controller: 36 °C -> 83 % speed |
Antecedent | An input variable of the rules (the IF part) | temperature, humidity |
Consequent | The output variable of the rules (the THEN part) | fan speed |
Term | One named fuzzy set of a variable | 'hot' for temperature |
Rule | IF condition THEN conclusion, written with terms | IF hot AND NOT dry THEN fast |
Firing strength | How true the IF part is for this input (0-1) | hot AND humid = 0.67 |
Simulation | Feeding inputs to the system and computing the output | sim.compute() |
3. Background
From sets to a system
P07a built fuzzy sets and P07b combined them and defuzzified an output that we GAVE. Today the rules decide. MATLAB's Fuzzy Logic Designer follows five steps; scikit-fuzzy's control module follows the same steps in code: (1) inputs = Antecedent, (2) output = Consequent, (3) membership functions (terms) for each, (4) rules = ctrl.Rule, (5) ControlSystem + ControlSystemSimulation: set .input, call .compute(), read .output.
What happens inside compute()
Fuzzify: find the degree of each input in each term. Fire: each rule's strength = min for AND (&), max for OR (|), 1 - μ for NOT (~). Clip each rule's output set at its strength, join the pieces with max, and defuzzify with the centroid - exactly P07b Task 3, but now the strengths come from rules. This is Mamdani inference (L23).
Today's controller
A pedestal fan for a Burewala summer: inputs temperature (0-50 °C) and humidity (0-100 %), output fan speed (0-100 %). The temperature sets are those from P07a: cold, comfortable, hot. P08b tests the system and draws its control surface.
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: five designer steps; what compute() does | 1-3 |
5-10 | Guided warm-up: variables and terms; a two-rule system | 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. Steps 1-3: inputs, output and their terms
Each variable gets a universe (the range of values) and named terms. view() draws the terms; interp_membership reads a degree.
import numpy as np import skfuzzy as fuzz from skfuzzy import control as ctrl temp = ctrl.Antecedent(np.arange(0, 51, 1), "temperature") hum = ctrl.Antecedent(np.arange(0, 101, 1), "humidity") fan = ctrl.Consequent(np.arange(0, 101, 1), "fan") temp["cold"] = fuzz.trapmf(temp.universe, [0, 0, 10, 20]) temp["comfortable"] = fuzz.trimf(temp.universe, [15, 24, 33]) temp["hot"] = fuzz.trapmf(temp.universe, [28, 38, 50, 50]) hum["dry"] = fuzz.trapmf(hum.universe, [0, 0, 20, 50]) hum["normal"] = fuzz.trimf(hum.universe, [30, 50, 70]) hum["humid"] = fuzz.trapmf(hum.universe, [50, 80, 100, 100]) fan["slow"] = fuzz.trimf(fan.universe, [0, 0, 50]) fan["medium"] = fuzz.trimf(fan.universe, [25, 50, 75]) fan["fast"] = fuzz.trimf(fan.universe, [50, 100, 100]) import matplotlib.pyplot as plt for v in (temp, hum, fan): print(f"{v.label:<12} {v.universe.min()}-{v.universe.max()} terms: {list(v.terms)}") for name in temp.terms: print(f"36 C is {name:<12} to degree {fuzz.interp_membership(temp.universe, temp[name].mf, 36):.2f}") temp.view(); plt.savefig("temperature_terms.png", dpi=90) print("Saved temperature_terms.png") |
Output temperature 0-50 terms: ['cold', 'comfortable', 'hot'] humidity 0-100 terms: ['dry', 'normal', 'humid'] fan 0-100 terms: ['slow', 'medium', 'fast'] 36 C is cold to degree 0.00 36 C is comfortable to degree 0.00 36 C is hot to degree 0.80 Saved temperature_terms.png |
A2. Steps 4-5: two rules and a simulation
The smallest possible system: IF hot THEN fast; IF NOT hot THEN slow. Set the input, compute, read the output.
# ... set-up: imports, variables and terms (copy from Part A1) ... rule1 = ctrl.Rule(temp["hot"], fan["fast"]) rule2 = ctrl.Rule(~temp["hot"], fan["slow"]) system = ctrl.ControlSystem([rule1, rule2]) sim = ctrl.ControlSystemSimulation(system) for t in (20, 33, 36, 45): sim.input["temperature"] = t sim.compute() print(f"{t} C -> fan {sim.output['fan']:.1f} %") |
Output 20 C -> fan 16.7 % 33 C -> fan 50.0 % 36 C -> fan 66.4 % 45 C -> fan 83.3 % |
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 from skfuzzy import control as ctrl quality = ctrl.Antecedent(np.arange(0, 11, 1), "quality") quality.automf(3) print(list(quality.terms)) |
My prediction |
B2.
# ... set-up: imports, variables and terms (copy from Part A1) ... system = ctrl.ControlSystem([ctrl.Rule(temp["cold"], fan["slow"]), ctrl.Rule(temp["comfortable"], fan["medium"]), ctrl.Rule(temp["hot"], fan["fast"])]) sim = ctrl.ControlSystemSimulation(system) sim.input["temperature"] = 24 sim.compute() print(round(sim.output["fan"], 2)) |
My prediction |
B3.
# ... set-up: imports, variables and terms (copy from Part A1) ... system = ctrl.ControlSystem([ctrl.Rule(temp["cold"], fan["slow"]), ctrl.Rule(temp["comfortable"], fan["medium"]), ctrl.Rule(temp["hot"], fan["fast"])]) sim = ctrl.ControlSystemSimulation(system) sim.input["temperature"] = 45 sim.compute() print(round(sim.output["fan"], 2)) |
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.
# ... set-up: imports, variables and terms (copy from Part A1) ... rule = ctrl.Rule(temp["warm"], fan["medium"]) |
Bug, why it happens, and the fixed line(s) |
C2.
# ... set-up: imports, variables and terms (copy from Part A1) ... sim = ctrl.ControlSystemSimulation(ctrl.ControlSystem([ctrl.Rule(temp["hot"], fan["fast"]), ctrl.Rule(~temp["hot"], fan["slow"])])) sim.input["temprature"] = 36 sim.compute() |
Bug, why it happens, and the fixed line(s) |
C3.
# ... set-up: imports, variables and terms (copy from Part A1) ... rules = [ctrl.Rule(temp["cold"], fan["slow"]), ctrl.Rule(temp["comfortable"] & hum["dry"], fan["slow"]), ctrl.Rule(temp["comfortable"] & ~hum["dry"], fan["medium"]), ctrl.Rule(temp["hot"] & hum["dry"], fan["medium"]), ctrl.Rule(temp["hot"] & ~hum["dry"], fan["fast"])] sim = ctrl.ControlSystemSimulation(ctrl.ControlSystem(rules)) sim.input["temperature"] = 36 sim.input["humidity"] = 0.70 # 70 % humidity sim.compute() print("fan:", round(sim.output["fan"], 2)) |
It runs and prints fan: 50.0 |
Bug, why it happens, and the fixed line(s) |
Part D - Graded Tasks (10 marks)
Task | Level | Title | Marks |
|---|---|---|---|
T1 | Easy | A temperature-only fan controller | 3 |
T2 | Medium | Adding humidity: the five-rule controller | 3 |
T3 | Challenge | Check the system by hand | 4 |
Total | 10 |
Task 1 (Easy, 3 marks): A temperature-only fan controller
Using the terms of Part A (copy the set-up code), build a system with three rules: IF cold THEN slow; IF comfortable THEN medium; IF hot THEN fast. Print the fan speed from 10 to 45 °C in steps of 5 °C with the change from the previous line. In which 5-degree step does the speed change most, and where does it not change at all?
Sample run (your output must match):
Output 10 C -> fan 16.7 % 15 C -> fan 19.4 % (change +2.8) 20 C -> fan 50.0 % (change +30.6) 25 C -> fan 50.0 % (change +0.0) 30 C -> fan 60.3 % (change +10.3) 35 C -> fan 82.2 % (change +21.9) 40 C -> fan 83.3 % (change +1.2) 45 C -> fan 83.3 % (change +0.0) |
Your code (or attach the file) |
Task 2 (Medium, 3 marks): Adding humidity: the five-rule controller
Add humidity (dry, normal, humid as in Part A) and replace the rules with these five: (1) IF cold THEN slow; (2) IF comfortable AND dry THEN slow; (3) IF comfortable AND NOT dry THEN medium; (4) IF hot AND dry THEN medium; (5) IF hot AND NOT dry THEN fast. Print the fan speed for the five P07b days (30 °C, 40 %), (34 °C, 65 %), (38 °C, 75 %), (41 °C, 55 %), (36 °C, 90 %). Which day gets the fastest fan? Is it the same day P07b found most uncomfortable (day 3)?
Sample run (your output must match):
Output day 1: 30 C, 40 % -> fan 45.0 % day 2: 34 C, 65 % -> fan 81.4 % day 3: 38 C, 75 % -> fan 83.3 % day 4: 41 C, 55 % -> fan 83.3 % day 5: 36 C, 90 % -> fan 82.8 % Fastest fan: day 3 |
Your code (or attach the file) |
Task 3 (Challenge, 4 marks): Check the system by hand
For 36 °C and 70 % with the five rules of Task 2, do the inference yourself: (a) print the degree of 36 °C in cold/comfortable/hot and of 70 % in dry; (b) print the firing strength of each rule (AND = min, NOT = 1 - μ); (c) for each output term take the strongest rule that points to it, clip the term with np.fmin, join with np.fmax and compute the centroid with fuzz.defuzz; (d) compare with sim.output['fan'].
Sample run (your output must match):
Output (a) cold 0.00, comfortable 0.00, hot 0.80; dry 0.00 (NOT dry 1.00) (b) R1 slow 0.00 (b) R2 slow 0.00 (b) R3 medium 0.00 (b) R4 medium 0.00 (b) R5 fast 0.80 (c) clip levels slow 0.00, medium 0.00, fast 0.80 -> centroid by hand 82.78 % (d) skfuzzy.control: 82.78 % |
Your code (or attach the file) |
Part E - Post-Lab Questions (finish as homework if time runs out)
E1. Name the five steps of building a fuzzy system and the scikit-fuzzy class or call used in each.
Your answer |
E2. In a rule, what do &, | and ~ compute?
Your answer |
E3. Why can the fan speed never reach exactly 100 % in our system?
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 variables, terms and rules (&, |, ~) | All correct | One slip | Several wrong |
Outputs for every test input | All present | Some missing | None |
Hand check and explanations | Clear and correct | Partly correct | Missing |
Submission
- Make a folder RollNo_P08a with task1.py, task2.py and task3.py (or P08a.ipynb).
- Upload the zipped folder to the LMS / Google Classroom by the end of the day.
- Copy Task 2 and Task 3 code and output into your Lab Book (Practical 08a page).
- Keep task2.py - P08b tests this same controller.