CSE 4355 W5 · Class 2 Fuzzy logic and genetic algorithms
Sakeef Sani ↗ ↓ PDF

CSE 4355Week 5Class 2Sunday · 9:30 AM – 10:50 AM80 min

Fuzzy logic and genetic algorithms

Two ideas borrowed from people and from nature: reason with 'quite hot' instead of 'hot or not', and search by breeding solutions instead of improving one. Then: what to expect in the midterm.

S present · L lecturer notes · → next step · F full screen · P presenter window
01 Cold open 0–4 min

Is 30 °C hot?

Hands up: is 30 °C hot? Is 29.9 °C?

A crisp rule "hot if T ≥ 30" says 29.9 is not hot and 30.0 is. Nobody in Dhaka thinks that. People say "a bit hot", "quite hot", "very hot".

00.531.0515202530354030 °Ctemperature (°C)degree of 'hot'
crisp: T ≥ 30fuzzy: μ_hot(T)
02 Concept 4–9 min

Fuzzy sets: membership is a degree

  • A crisp set: x is in or out, μ(x) ∈ {0, 1}. A fuzzy set A: a membership function μA: X → [0, 1].
  • A linguistic variable (temperature) has fuzzy values (cool, warm, hot) that overlap: 30 °C is 0.57 warm and 0.29 hot.
  • Shapes are a design choice; triangles and trapezoids are standard because they are cheap and easy to tune.
μtri(x; a,b,c)=max⁡ ⁣(0, min⁡(x−ab−a, c−xc−b))\mu_{tri}(x;\,a,b,c) = \max\!\Big(0,\ \min\Big(\frac{x-a}{b-a},\ \frac{c-x}{c-b}\Big)\Big)

Fuzzy ≠ probability

"0.7 chance that the glass is poisoned" and "the glass is 0.7 full of poison" are different statements. Probability is about uncertainty whether something is true; fuzziness is about the degree to which it is true, with nothing left to find out.

Pop culture

Lotfi Zadeh, an electrical engineer at Berkeley, introduced fuzzy sets in 1965. Japanese engineers made them famous in the 1980s.

03 Concept 9–13 min

Fuzzy AND, OR, NOT, and hedges

OperationZadehμA = 0.7, μB = 0.4
A AND Bmin(μA, μB)0.4
A OR Bmax(μA, μB)0.7
NOT A1 − μA0.3
very AμA²0.49
somewhat A√μA0.84
Gotcha

A AND NOT A = min(0.7, 0.3) = 0.3, not 0. A OR NOT A = 0.7, not 1. The laws of contradiction and excluded middle do not hold.

Pro tip

With μ only 0 or 1, min/max/1 − μ reduce to ordinary Boolean AND/OR/NOT: fuzzy logic contains crisp logic.

04 Worked example 13–19 min

Worked example: a fuzzy fan controller, by hand

Given

Room at 30 °C, 70 % humidity. Temperature: cool (15, 15, 20, 26), warm (20, 27, 34), hot (28, 35, 40, 40). Humidity: dry (30, 30, 45, 65), humid (50, 75, 100, 100). Fan: low (0, 0, 15, 45), medium (25, 50, 75), high (55, 85, 100, 100). Rules: R1 cool → low; R2 warm ∧ dry → low; R3 warm ∧ humid → medium; R4 hot → high.

Find

The fan speed by Mamdani inference with centroid defuzzification (sample every 10 %).

Method: Mamdani inference: fuzzify, AND = min, clip, OR = max, centroid

Solution

Predict the answer first. Then reveal one step at a time.

05 Live lab 19–26 min

Live lab: the fan controller

A fuzzy fan controller Move the temperature and humidity. Each rule fires as strongly as its weakest condition (AND = min); its output set is clipped at that height; the clipped sets are merged (max) and the centroid is the fan speed.
fan speed60.2 %
Temperature (°C)warm 0.57hot 0.29152025303540
Humidity (%)humid 0.8030405060708090100
Fan speed (%)60.2020406080100
RuleIFTHEN fanstrength
R1temp is coollow0.00
R2temp is warm AND humidity is drylow0.00
R3temp is warm AND humidity is humidmedium0.57
R4temp is hothigh0.29
Centroid60.2 %
11-point sum61.8 %
Sugeno (20/50/90)63.3 %
06 Your turn 26–30 min

Your turn · 1: a Sugeno shortcut

04:00
1 Exam CQ

Same sets and rules as the worked example, but each rule now outputs a single number (a Sugeno system): low = 20 %, medium = 50 %, high = 90 %.

Find the fan speed at 33 °C and 55 % humidity. Check with the lab's Sugeno readout.

Work it out first.
Press Solve when the class has an answer.

07 Think deeper 30–33 min

Critical thinking · why not just use an equation?

Think deeper

A fan speed of 61.8 % could also come from a formula, e.g. speed = 6(T − 20). What does fuzzy logic give you that the formula does not?

Argue it in pairs for two minutes. There may be more than one defensible answer.

08 Concept 33–38 min

Genetic algorithms: search by breeding

  1. Encode a candidate solution as a string (a chromosome): bits, digits, a permutation.
  2. Start with a random population of N individuals. Score each with a fitness function.
  3. Select parents in proportion to fitness (roulette wheel) or by tournament.
  4. Crossover: cut two parents at a random point and swap tails.
  5. Mutate: change each gene with a small probability.
  6. The children form the next generation (optionally keep the best: elitism). Repeat until good enough or out of time.
Where it fits

A GA is a local search (Week 3) that keeps k states, like beam search, but makes new ones by combining two good states instead of only nudging one.

pi=fi∑jfjp_i = \frac{f_i}{\sum_j f_j}

Roulette selection: fitness must be positive; bigger slice, more children.

09 Worked example 38–44 min

Worked example: one generation by hand

Given

Maximise f(x) = x², x an integer 0–31 encoded in 5 bits. Population: 01101, 11000, 01000, 10011 (Goldberg's classic).

Find

Selection probabilities, expected copies, the children of crossover, and the new total fitness.

Method: simple GA (Goldberg, 1989)

Solution

Predict the answer first. Then reveal one step at a time.

10 Live lab 44–51 min

Live lab: evolve an 8-queens solution

A genetic algorithm solves 8-queens Each individual is 8 digits: the row of the queen in each column. Fitness = pairs of queens that do not attack each other (28 = solved). Select in proportion to fitness, cross over, mutate, repeat.
♛♛♛♛♛♛♛♛
Generation0
Best fitness26 / 28
2631685726
2357185523
6155683823
6578574622
3538282221
6858712321
07142128010203040solvedgenerationfitness
bestpopulation mean
11 Your turn 51–55 min

Your turn · 2: Russell & Norvig's 8-queens population

04:00
2 Exam CQ

Four 8-queens individuals with fitness (non-attacking pairs): 24748552 (24), 32752411 (23), 24415124 (20), 32543213 (11).

(a) Selection probability of each? (b) Cross 32752411 with 24748552 after the 3rd digit: write both children.

Work it out first.
Press Solve when the class has an answer.

12 Think deeper 55–58 min

Critical thinking · when does crossover help?

Think deeper

Crossover helped in x² and 8-queens. Can you design an encoding where it is useless, or harmful?

Argue it in pairs for two minutes. There may be more than one defensible answer.

13 Real world 58–62 min

Real world: rice cookers and space antennas

Fuzzy at home

Fuzzy-logic rice cookers, washing machines (load and dirt sensors → water and spin time) and inverter air conditioners sold in Dhaka markets all run small fuzzy controllers. The Sendai subway in Japan (1987) used fuzzy control for smoother, more energy-efficient braking.

BME corner

Fuzzy controllers are studied for anaesthesia depth and insulin dosing, where expert rules ("glucose high and rising → increase a little") are natural, and smooth action is safer than thresholds.

An evolved antenna

NASA's ST5 satellites (2006) flew an X-band antenna whose odd, bent-wire shape was designed by an evolutionary algorithm; it met the specification better than the hand design.

Think deeper

Your university class routine: rooms, teachers, sections, no clashes. Why is a GA a reasonable tool?

Argue it in pairs for two minutes. There may be more than one defensible answer.

14 Pop quiz 62–66 min

Pop quiz

Score 0

Question 1 / 5

μtall(Rafi) = 0.7. Then μnot tall(Rafi) and μtall AND not tall(Rafi) are…

15 Recap 66–69 min

Three things to remember

01

Fuzzy sets: μ ∈ [0, 1], overlapping linguistic values. AND = min, OR = max, NOT = 1 − μ; very = μ².

02

Mamdani: fuzzify → rule strength (min) → clip → aggregate (max) → centroid Σμz/Σμ. Sugeno: Σwz/Σw.

03

GA: population, fitness, roulette selection f/Σf, crossover, mutation, elitism. A k-state local search that recombines.

16 Recap 69–80 min

Midterm: what to expect

WeekBe able to…
1define AI (four approaches), write PEAS, classify environments
2formulate a problem (states, actions, goal, cost); propositional logic, forward/backward chaining, expert-system architecture
3trace BFS, DFS, UCS, DLS, IDS; compare them; hill climbing, annealing acceptance eΔE/T, beam search
4greedy and A* traces; admissible vs consistent; dominance, b*, relaxed problems; minimax
5alpha–beta cuts, move ordering, expectiminimax; fuzzy inference by hand; one GA generation

Creative questions (CQ) only, no MCQ. 30 % of the course. Each CQ has a short scenario and 3–4 parts that go from recall to analysis.

CQ Exam CQ

A practice CQ

A ward robot moves on: S–A (2), S–B (5), A–B (2), A–G (6), B–G (2). Heuristic: h(S) = 5, h(A) = 4, h(B) = 2, h(G) = 0.

(a) Is h admissible? (b) Consistent? (c) Trace A*: expansion order, path, cost. (d) What would greedy best-first return?

Work it out first.
Press Solve when the class has an answer.