Artificial Intelligence Applications in Engineering (MUH-920), week 4 of 14

Fuzzy Logic and Fuzzy Control

Prof. Dr. Utku Kose, Süleyman Demirel University

Vagueness and fuzzy sets

Week 3 ended with a weakness of crisp rules: A threshold of 70 degrees makes a reading of 69.9 normal and a reading of 70.1 overheated. Engineers rarely think in such steps. A temperature is more or less high, a load is fairly heavy, a surface is quite rough. Zadeh proposed fuzzy sets to represent such graded concepts [1]. A fuzzy set A on a universe of discourse X is defined by a membership function that assigns every element x a degree between 0 and 1. A room at 23 degrees may belong to the set of comfortable rooms with degree 0.6 and to the set of warm rooms with degree 0.125.

Vagueness is not probability. A membership degree of 0.6 does not say that the room is comfortable with probability 0.6. It says that 23 degrees fits the concept "comfortable" to degree 0.6, and there is nothing random about it. Probability describes uncertainty about whether an event happens; fuzzy membership describes how well a precisely known value fits an imprecise concept. The two can be combined, but they answer different questions.

Common membership functions are triangular, defined by a left foot, a peak and a right foot; trapezoidal, with a flat top; and Gaussian, with a centre and a width. The support of a fuzzy set is where its membership is positive, the core is where it equals one, and an alpha-cut is the crisp set of elements whose membership is at least alpha. A linguistic variable, such as tank level, takes linguistic values, such as low, okay and high, each represented by a fuzzy set on the same universe [2].

A crisp set and fuzzy sets for the concept "warm room". The fuzzy sets overlap, so a temperature can belong partly to two neighbouring concepts .
Figure 4.1. A crisp set and fuzzy sets for the concept "warm room". The fuzzy sets overlap, so a temperature can belong partly to two neighbouring concepts [1].

Check your understanding. A water temperature of 58 degrees has membership 0.7 in the fuzzy set "hot". What does this mean?

Formulas: Membership functions

A fuzzy set AA on a universe of discourse XX, for example the room temperatures from 0 to 40 degrees, is defined by a membership function

μA:X→[0,1]\mu_A : X \rightarrow [0, 1]

that gives every element xx a degree of membership μA(x)\mu_A(x) between 0 and 1 [1]. The Greek letter mu, μ\mu, is the usual symbol. A crisp set is the special case in which μA(x)\mu_A(x) is either 0 or 1. Three shapes are common. A triangular set with left foot aa, peak bb and right foot cc rises linearly from aa to bb and falls linearly from bb to cc:

μ(x;a,b,c)=max⁡(min⁡(x−ab−a,c−xc−b),0)\mu(x; a, b, c) = \max\left(\min\left(\frac{x - a}{b - a}, \frac{c - x}{c - b}\right), 0\right)

The first fraction is the rising side, the second the falling side, the minimum picks the side on which xx lies, and the maximum with 0 sets the membership to zero outside the feet. When a=ba = b, the rising side is replaced by 1 and the set becomes a left shoulder, which keeps full membership at the low end of the universe; b=cb = c gives a right shoulder. A trapezoidal set has a flat top from bb to cc:

μ(x;a,b,c,d)=max⁡(min⁡(x−ab−a,1,d−xd−c),0)\mu(x; a, b, c, d) = \max\left(\min\left(\frac{x - a}{b - a}, 1, \frac{d - x}{d - c}\right), 0\right)

A Gaussian set with centre mm and width σ\sigma (sigma) is smooth everywhere:

μ(x;m,σ)=e−(x−m)2/(2σ2)\mu(x; m, \sigma) = e^{-(x - m)^2 / (2 \sigma^2)}

Three crisp sets describe a fuzzy set. The support holds the elements with positive membership, the core those with full membership, and the alpha-cut AαA_{\alpha} those with membership of at least α\alpha:

supp(A)={x:μA(x)>0},    core(A)={x:μA(x)=1},    Aα={x:μA(x)≥α}\text{supp}(A) = \{ x : \mu_A(x) > 0 \}, \;\; \text{core}(A) = \{ x : \mu_A(x) = 1 \}, \;\; A_{\alpha} = \{ x : \mu_A(x) \ge \alpha \}

Worked example: How warm is a room at 23 degrees?

Figure 4.1 and the notebook use three triangular sets on the room temperature: cool (5,12,20)(5, 12, 20), comfortable (16,21,26)(16, 21, 26) and warm (22,30,38)(22, 30, 38), each written as (left foot, peak, right foot).

Step 1. Cool. The value 23 lies above the right foot 20, so the falling side is negative, 20−2320−12=−38=−0.375\frac{20 - 23}{20 - 12} = \frac{-3}{8} = -0.375, and the maximum with 0 gives μcool(23)=0\mu_{\text{cool}}(23) = 0.

Step 2. Comfortable. Both sides are evaluated and the smaller one is kept:

23−1621−16=75=1.4,    26−2326−21=35=0.6,    μcomfortable(23)=min⁡(1.4,0.6)=0.6\frac{23 - 16}{21 - 16} = \frac{7}{5} = 1.4, \;\; \frac{26 - 23}{26 - 21} = \frac{3}{5} = 0.6, \;\; \mu_{\text{comfortable}}(23) = \min(1.4, 0.6) = 0.6

Step 3. Warm.

23−2230−22=18=0.125,    38−2338−30=158=1.875,    μwarm(23)=min⁡(0.125,1.875)=0.125\frac{23 - 22}{30 - 22} = \frac{1}{8} = 0.125, \;\; \frac{38 - 23}{38 - 30} = \frac{15}{8} = 1.875, \;\; \mu_{\text{warm}}(23) = \min(0.125, 1.875) = 0.125

The room is comfortable to degree 0.6 and warm to degree 0.125, while the crisp set "warm" of Figure 4.1, the interval from 22 to 30 degrees, would give it full membership.

Step 4. Other shapes. A Gaussian set "comfortable" with centre 21 and width 2.5, as in the notebook, gives

23−212.5=0.8,    0.82=0.64,    μ=e−0.64/2=e−0.32=0.7261\frac{23 - 21}{2.5} = 0.8, \;\; 0.8^2 = 0.64, \;\; \mu = e^{-0.64/2} = e^{-0.32} = 0.7261

and a trapezoidal set with feet 16 and 26 and a flat top from 20 to 22 gives

μ=max⁡(min⁡(23−1620−16,1,26−2326−22),0)=max⁡(min⁡(1.75,1,0.75),0)=0.75\mu = \max\left(\min\left(\frac{23 - 16}{20 - 16}, 1, \frac{26 - 23}{26 - 22}\right), 0\right) = \max(\min(1.75, 1, 0.75), 0) = 0.75

Step 5. Support, core and alpha-cut of the triangular set "comfortable". The support is the open interval from 16 to 26, and the core is the single temperature 21. For α=0.5\alpha = 0.5, the rising side reaches 0.5 where x−165=0.5\frac{x - 16}{5} = 0.5, that is at x=16+0.5×5=18.5x = 16 + 0.5 \times 5 = 18.5, and the falling side where 26−x5=0.5\frac{26 - x}{5} = 0.5, at x=26−0.5×5=23.5x = 26 - 0.5 \times 5 = 23.5. The alpha-cut is the interval from 18.5 to 23.5 degrees.

Operations on fuzzy sets

The classical set operations generalise. The complement NOT A has membership 1 minus the membership of A. The intersection A AND B is usually computed with the minimum of the two memberships, and the union A OR B with the maximum [1]. Other choices are possible: The product is also a valid intersection, called a t-norm, and the probabilistic sum a + b - ab is a valid union, called an s-norm. The choice changes the smoothness of the resulting controller but not the principle. Some classical laws fail on purpose: A AND NOT A need not be empty, because a temperature can be somewhat warm and somewhat not warm at the same time.

Formulas: Complement, intersection and union

For fuzzy sets AA and BB on the same universe, the standard operations of Zadeh work point by point [1]:

μ¬A(x)=1−μA(x)\mu_{\neg A}(x) = 1 - \mu_A(x)
μA∧B(x)=min⁡(μA(x),μB(x)),    μA∨B(x)=max⁡(μA(x),μB(x))\mu_{A \wedge B}(x) = \min(\mu_A(x), \mu_B(x)), \;\; \mu_{A \vee B}(x) = \max(\mu_A(x), \mu_B(x))

For degrees of 0 and 1 they reproduce the truth tables of Week 3. The minimum is one member of a family of intersections called t-norms, functions T(a,b)T(a, b) of two degrees that are commutative, associative, non-decreasing in each argument and satisfy T(a,1)=aT(a, 1) = a. The product T(a,b)=abT(a, b) = a b is another t-norm. Every t-norm has a matching union, its s-norm, obtained through the complement:

S(a,b)=1−T(1−a,1−b)S(a, b) = 1 - T(1 - a, 1 - b)

For the minimum this gives the maximum. For the product it gives the probabilistic sum,

S(a,b)=1−(1−a)(1−b)=a+b−abS(a, b) = 1 - (1 - a)(1 - b) = a + b - a b

With these pairs the laws of De Morgan hold, for example ¬(A∧B)=¬A∨¬B\neg(A \wedge B) = \neg A \vee \neg B, whereas the law of contradiction does not: min⁡(μ,1−μ)\min(\mu, 1 - \mu) is zero only when μ\mu is 0 or 1 [1, 3].

Worked example: Combining the two memberships of the room

At 23 degrees the room is comfortable with a=0.6a = 0.6 and warm with b=0.125b = 0.125.

Step 1. Complements. Not warm: 1−0.125=0.8751 - 0.125 = 0.875. Not comfortable: 1−0.6=0.41 - 0.6 = 0.4.

Step 2. Comfortable and warm. With the minimum, min⁡(0.6,0.125)=0.125\min(0.6, 0.125) = 0.125. With the product, 0.6×0.125=0.0750.6 \times 0.125 = 0.075.

Step 3. Comfortable or warm. With the maximum, max⁡(0.6,0.125)=0.6\max(0.6, 0.125) = 0.6. With the probabilistic sum, 0.6+0.125−0.075=0.650.6 + 0.125 - 0.075 = 0.65.

Step 4. De Morgan. On the left side, 1−min⁡(0.6,0.125)=1−0.125=0.8751 - \min(0.6, 0.125) = 1 - 0.125 = 0.875. On the right side, max⁡(1−0.6,1−0.125)=max⁡(0.4,0.875)=0.875\max(1 - 0.6, 1 - 0.125) = \max(0.4, 0.875) = 0.875. The two sides agree.

Step 5. A set and its complement. Comfortable and not comfortable: min⁡(0.6,0.4)=0.4\min(0.6, 0.4) = 0.4, not 0. Comfortable or not comfortable: max⁡(0.6,0.4)=0.6\max(0.6, 0.4) = 0.6, not 1. The room is partly comfortable and partly not, which is exactly the vagueness the fuzzy set expresses. Part A of the interactive lab evaluates triangular, trapezoidal and Gaussian sets and these operations for any input.

Mamdani inference

A fuzzy rule has linguistic conditions and a linguistic conclusion: IF level error is low AND level rate is none THEN valve change is open fast. Mamdani inference evaluates a rule base in five steps [4, 5]. First, fuzzification computes the membership of each crisp input in each of its fuzzy sets. Second, each rule receives a firing strength, the minimum of the memberships in its conditions. Third, implication clips the output fuzzy set of each rule at its firing strength. Fourth, aggregation combines the clipped sets of all rules with the maximum. Fifth, defuzzification turns the aggregated fuzzy set into one crisp output, most often with the centroid, the centre of gravity of the area under the aggregated membership function.

Animation: Inside a Mamdani controller

Move the two inputs of a tank level controller. The bars show fuzzification, the clipped shapes show the rules that fire, and the vertical line marks the centroid that becomes the valve command.

Level error (m)
Level rate (m/s)
Valve change: clipped sets and centroid

The controller defined by a rule base is a nonlinear function from inputs to output, and its control surface can be drawn for two inputs. Overlapping membership functions make the surface smooth; more sets and rules add detail. A rule table, with one input along the rows and the other along the columns, is the usual design tool: Every cell holds the conclusion for one combination of linguistic values, and gaps in the table leave parts of the input space without any action. Symmetry of the table usually leads to symmetric behaviour of the loop.

Check your understanding. Two rules fire with strengths 0.3 and 0.8 and conclude "open slow" and "open fast". What happens in Mamdani inference before defuzzification?

Formulas: The five steps of Mamdani inference

Consider rules with two inputs, IF xx is AkA_k AND yy is BkB_k THEN zz is CkC_k, for k=1,…,Kk = 1, \ldots, K, and crisp inputs x0x_0 and y0y_0 [4, 5].

1. Fuzzification computes the memberships μAk(x0)\mu_{A_k}(x_0) and μBk(y0)\mu_{B_k}(y_0) of the inputs in every set used by the rules.

2. Firing strength. The AND of a rule is the minimum of its memberships:

wk=min⁡(μAk(x0),μBk(y0))w_k = \min(\mu_{A_k}(x_0), \mu_{B_k}(y_0))

3. Implication clips the output set of each rule at its firing strength:

μk′(z)=min⁡(wk,μCk(z))\mu'_k(z) = \min(w_k, \mu_{C_k}(z))

4. Aggregation joins the clipped sets of all rules with the maximum:

μ(z)=max⁡kμk′(z)\mu(z) = \max_{k} \mu'_k(z)

5. Defuzzification by the centroid returns the centre of gravity of the area under μ(z)\mu(z). On a grid of output values z1,z2,…,zNz_1, z_2, \ldots, z_N the integrals become sums:

z⋆=∫z μ(z) dz∫μ(z) dz≈∑j=1Nzj μ(zj)∑j=1Nμ(zj)z^{\star} = \frac{\int z\, \mu(z)\, dz}{\int \mu(z)\, dz} \approx \frac{\sum_{j=1}^{N} z_j\, \mu(z_j)}{\sum_{j=1}^{N} \mu(z_j)}

The integral sign ∫\int adds up infinitely many thin strips; the sum on the right adds up the values at the grid points, and a finer grid brings it closer to the integral.

Worked example: The tank level controller of the animation

The controller of the animation and the notebook has the level error ee (set point minus level, in metres) and the level rate rr (in metres per second) as inputs and the change of the valve opening per second as output. Its sets, written as (left foot, peak, right foot), are high (−1,−1,0)(-1, -1, 0), okay (−0.4,0,0.4)(-0.4, 0, 0.4) and low (0,1,1)(0, 1, 1) for the error; negative (−0.1,−0.1,0)(-0.1, -0.1, 0), none (−0.03,0,0.03)(-0.03, 0, 0.03) and positive (0,0.1,0.1)(0, 0.1, 0.1) for the rate; and close fast (−0.07,−0.05,−0.03)(-0.07, -0.05, -0.03), close slow (−0.035,−0.02,−0.005)(-0.035, -0.02, -0.005), no change (−0.012,0,0.012)(-0.012, 0, 0.012), open slow (0.005,0.02,0.035)(0.005, 0.02, 0.035) and open fast (0.03,0.05,0.07)(0.03, 0.05, 0.07) for the output. The five rules are: R1 IF ee is low THEN open fast; R2 IF ee is high THEN close fast; R3 IF ee is okay AND rr is none THEN no change; R4 IF ee is okay AND rr is positive THEN close slow; R5 IF ee is okay AND rr is negative THEN open slow. The inputs are e=0.25e = 0.25 m, the level is a quarter of a metre below the set point, and r=0.02r = 0.02 m/s, the level is rising.

Step 1. Fuzzification of the error.

μhigh(0.25)=max⁡(min⁡(1,0−0.250−(−1)),0)=max⁡(−0.25,0)=0\mu_{\text{high}}(0.25) = \max\left(\min\left(1, \frac{0 - 0.25}{0 - (-1)}\right), 0\right) = \max(-0.25, 0) = 0
μokay(0.25)=max⁡(min⁡(0.25+0.40.4,0.4−0.250.4),0)=min⁡(1.625,0.375)=0.375\mu_{\text{okay}}(0.25) = \max\left(\min\left(\frac{0.25 + 0.4}{0.4}, \frac{0.4 - 0.25}{0.4}\right), 0\right) = \min(1.625, 0.375) = 0.375
μlow(0.25)=max⁡(min⁡(0.25−01−0,1),0)=0.25\mu_{\text{low}}(0.25) = \max\left(\min\left(\frac{0.25 - 0}{1 - 0}, 1\right), 0\right) = 0.25

Step 2. Fuzzification of the rate.

μnegative(0.02)=max⁡(min⁡(1,0−0.020.1),0)=max⁡(−0.2,0)=0\mu_{\text{negative}}(0.02) = \max\left(\min\left(1, \frac{0 - 0.02}{0.1}\right), 0\right) = \max(-0.2, 0) = 0
μnone(0.02)=max⁡(min⁡(0.02+0.030.03,0.03−0.020.03),0)=min⁡(1.6667,0.3333)=0.3333\mu_{\text{none}}(0.02) = \max\left(\min\left(\frac{0.02 + 0.03}{0.03}, \frac{0.03 - 0.02}{0.03}\right), 0\right) = \min(1.6667, 0.3333) = 0.3333
μpositive(0.02)=max⁡(min⁡(0.020.1,1),0)=0.2\mu_{\text{positive}}(0.02) = \max\left(\min\left(\frac{0.02}{0.1}, 1\right), 0\right) = 0.2

Step 3. Firing strengths. R1: w1=0.25w_1 = 0.25. R2: w2=0w_2 = 0. R3: w3=min⁡(0.375,0.3333)=0.3333w_3 = \min(0.375, 0.3333) = 0.3333. R4: w4=min⁡(0.375,0.2)=0.2w_4 = \min(0.375, 0.2) = 0.2. R5: w5=min⁡(0.375,0)=0w_5 = \min(0.375, 0) = 0. Rules R2 and R5 do not fire.

Step 4. Implication on a grid. The output universe from −0.07-0.07 to 0.070.07 is sampled every 0.01, which gives 15 grid points. Close slow is positive at three of them: −0.03+0.0350.015=0.3333\frac{-0.03 + 0.035}{0.015} = 0.3333 at −0.03-0.03, 1 at −0.02-0.02 and −0.005−(−0.01)0.015=0.3333\frac{-0.005 - (-0.01)}{0.015} = 0.3333 at −0.01-0.01. Clipped at w4=0.2w_4 = 0.2, all three become min⁡(0.2,0.3333)=min⁡(0.2,1)=0.2\min(0.2, 0.3333) = \min(0.2, 1) = 0.2. No change is positive at −0.01-0.01 with −0.01+0.0120.012=0.1667\frac{-0.01 + 0.012}{0.012} = 0.1667, at 0 with 1 and at 0.01 with 0.012−0.010.012=0.1667\frac{0.012 - 0.01}{0.012} = 0.1667; clipped at w3=0.3333w_3 = 0.3333 these become 0.1667, 0.3333 and 0.1667. Open fast is positive at 0.04 with 0.04−0.030.02=0.5\frac{0.04 - 0.03}{0.02} = 0.5, at 0.05 with 1 and at 0.06 with 0.07−0.060.02=0.5\frac{0.07 - 0.06}{0.02} = 0.5; clipped at w1=0.25w_1 = 0.25 all three become 0.25.

Step 5. Aggregation and the products for the centroid. The column μ(z)\mu(z) holds the maximum of the three clipped sets; at z=−0.01z = -0.01, for example, max⁡(0.2,0.1667,0)=0.2\max(0.2, 0.1667, 0) = 0.2.

zz close slow no change open fast μ(z)\mu(z) z μ(z)z\, \mu(z)
−0.07-0.07 0 0 0 0 00
−0.06-0.06 0 0 0 0 00
−0.05-0.05 0 0 0 0 00
−0.04-0.04 0 0 0 0 00
−0.03-0.03 0.2 0 0 0.2 −0.006-0.006
−0.02-0.02 0.2 0 0 0.2 −0.004-0.004
−0.01-0.01 0.2 0.1667 0 0.2 −0.002-0.002
00 0 0.3333 0 0.3333 00
0.010.01 0 0.1667 0 0.1667 0.0016670.001667
0.020.02 0 0 0 0 00
0.030.03 0 0 0 0 00
0.040.04 0 0 0.25 0.25 0.010.01
0.050.05 0 0 0.25 0.25 0.01250.0125
0.060.06 0 0 0.25 0.25 0.0150.015
0.070.07 0 0 0 0 00

Step 6. Centroid.

∑jμ(zj)=0.2+0.2+0.2+0.3333+0.1667+0.25+0.25+0.25=1.85\sum_j \mu(z_j) = 0.2 + 0.2 + 0.2 + 0.3333 + 0.1667 + 0.25 + 0.25 + 0.25 = 1.85
∑jzj μ(zj)=(−0.006)+(−0.004)+(−0.002)+0.001667+0.01+0.0125+0.015=0.027167\sum_j z_j\, \mu(z_j) = (-0.006) + (-0.004) + (-0.002) + 0.001667 + 0.01 + 0.0125 + 0.015 = 0.027167
z⋆=0.0271671.85=0.014685z^{\star} = \frac{0.027167}{1.85} = 0.014685

The valve opens by about 0.0147 of its range per second: The level is low, which calls for opening, and the rising level holds the action back. The grid of 15 points is coarse. A grid every 0.005, with 29 points, gives 0.0177, and the 561 points of the notebook give 0.0169, the value that the integral approaches. Part B of the interactive lab repeats all steps for any inputs and grid.

Takagi-Sugeno models and learning fuzzy systems

Takagi and Sugeno replaced the fuzzy conclusion by a function of the inputs: IF x is A AND y is B THEN z = p x + q y + r [6]. The output is the average of the rule outputs weighted by their firing strengths, so no defuzzification of a shape is needed. A zero-order Sugeno model has constant conclusions and behaves much like a Mamdani model with narrow output sets; a first-order model blends local linear models, which makes it attractive for modelling and for gain scheduling of controllers.

Because the Sugeno output is a differentiable function of its parameters, the parameters can be learned from data. ANFIS represents a Sugeno system as a five-layer adaptive network and trains the premise parameters with gradient descent and the linear consequent parameters with least squares [7]. Metaheuristic training is an alternative when gradients are unreliable: In one study, ANFIS supported by the vortex optimisation algorithm forecast chaotic electroencephalogram time series [8]. Such neuro-fuzzy systems link this week to the optimisation of Weeks 5 and 6 and to the neural networks of Week 10.

Check your understanding. What distinguishes a first-order Takagi-Sugeno rule from a Mamdani rule?

Formulas: Takagi-Sugeno models and ANFIS

A first-order Takagi-Sugeno rule has a linear function as its conclusion [6]:

IF x is Ak AND y is Bk THEN zk=pkx+qky+rk\text{IF } x \text{ is } A_k \text{ AND } y \text{ is } B_k \text{ THEN } z_k = p_k x + q_k y + r_k

A zero-order rule has a constant conclusion, zk=rkz_k = r_k. The firing strength wkw_k is computed as in Mamdani inference, with the minimum or with the product of the memberships, and the output is the weighted average of the rule outputs:

z=∑k=1Kwkzk∑k=1Kwk=∑k=1Kw‾kzk,    w‾k=wk∑j=1Kwjz = \frac{\sum_{k=1}^{K} w_k z_k}{\sum_{k=1}^{K} w_k} = \sum_{k=1}^{K} \overline{w}_k z_k, \;\; \overline{w}_k = \frac{w_k}{\sum_{j=1}^{K} w_j}

The normalised strengths w‾k\overline{w}_k add up to one. ANFIS computes the same output with a five-layer network: Layer 1 evaluates the membership functions, layer 2 multiplies them into the strengths wkw_k, layer 3 normalises them, layer 4 forms w‾kzk\overline{w}_k z_k, and layer 5 adds these up [7]. Its hybrid learning rule uses two facts. First, once the membership functions are fixed, zz is linear in the consequent parameters, so these follow from a least-squares fit to the training pairs (xi,ti)(x_i, t_i). Second, the membership parameters, such as the centre cc of a Gaussian set, are improved by gradient descent on the squared error E=12(z−t)2E = \frac{1}{2}(z - t)^2 with a learning rate η\eta (eta):

c←c−η ∂E∂cc \leftarrow c - \eta\, \frac{\partial E}{\partial c}

The symbol ∂E/∂c\partial E / \partial c is the partial derivative, the rate at which EE changes when only cc changes. For two rules with Gaussian sets of width σ\sigma, the chain rule gives

∂E∂c1=(z−t)×(z1−z2)×w‾1w‾2×x−c1σ2\frac{\partial E}{\partial c_1} = (z - t) \times (z_1 - z_2) \times \overline{w}_1 \overline{w}_2 \times \frac{x - c_1}{\sigma^2}

because z=w‾1z1+(1−w‾1)z2z = \overline{w}_1 z_1 + (1 - \overline{w}_1) z_2 changes by z1−z2z_1 - z_2 per unit of w‾1\overline{w}_1, and w‾1\overline{w}_1 changes by w‾1w‾2(x−c1)/σ2\overline{w}_1 \overline{w}_2 (x - c_1)/\sigma^2 per unit of c1c_1.

Worked example: A zero-order Sugeno version of the tank controller

Replacing each output set of the controller by its peak gives the constants close fast −0.05-0.05, close slow −0.02-0.02, no change 0, open slow 0.02 and open fast 0.05. With the firing strengths of the Mamdani example, w=(0.25,0,0.3333,0.2,0)w = (0.25, 0, 0.3333, 0.2, 0):

z=0.25×0.05+0×(−0.05)+0.3333×0+0.2×(−0.02)+0×0.020.25+0+0.3333+0.2+0z = \frac{0.25 \times 0.05 + 0 \times (-0.05) + 0.3333 \times 0 + 0.2 \times (-0.02) + 0 \times 0.02}{0.25 + 0 + 0.3333 + 0.2 + 0}
z=0.0125+0+0−0.004+00.7833=0.00850.7833=0.010852z = \frac{0.0125 + 0 + 0 - 0.004 + 0}{0.7833} = \frac{0.0085}{0.7833} = 0.010852

The Sugeno controller needs no output shapes and no grid, and its output, 0.010852, is close to the Mamdani centroid of 0.0169. The difference comes from the widths and overlaps of the Mamdani output sets.

Worked example: One learning cycle of a small ANFIS

A sensor has the input xx and the measured output tt at three points, (2,3)(2, 3), (5,5)(5, 5) and (8,8)(8, 8). A model with two zero-order rules, IF xx is small THEN z=k1z = k_1 and IF xx is large THEN z=k2z = k_2, uses Gaussian sets with centres c1=0c_1 = 0 and c2=10c_2 = 10 and width σ=4\sigma = 4, so that 2σ2=322\sigma^2 = 32.

Step 1. Layers 1 to 3 for each training point. For x=2x = 2: w1=e−(2−0)2/32=e−0.125w_1 = e^{-(2 - 0)^2/32} = e^{-0.125} and w2=e−(2−10)2/32=e−2w_2 = e^{-(2 - 10)^2/32} = e^{-2}. All three points:

xx tt w1w_1 w2w_2 w‾1\overline{w}_1 w‾2\overline{w}_2
2 3 e−0.125=0.882497e^{-0.125} = 0.882497 e−2=0.135335e^{-2} = 0.135335 0.867036 0.132964
5 5 e−0.7812=0.457856e^{-0.7812} = 0.457856 e−0.7812=0.457856e^{-0.7812} = 0.457856 0.5 0.5
8 8 e−2=0.135335e^{-2} = 0.135335 e−0.125=0.882497e^{-0.125} = 0.882497 0.132964 0.867036

For x=2x = 2, for example, w‾1=0.882497/(0.882497+0.135335)=0.882497/1.017832=0.867036\overline{w}_1 = 0.882497/(0.882497 + 0.135335) = 0.882497/1.017832 = 0.867036.

Step 2. Least squares for the consequents. The model output is zi=w‾1ik1+w‾2ik2z_i = \overline{w}_{1i} k_1 + \overline{w}_{2i} k_2. Setting the derivatives of ∑i(zi−ti)2\sum_i (z_i - t_i)^2 with respect to k1k_1 and k2k_2 to zero gives the normal equations

(∑iw‾1i2)k1+(∑iw‾1iw‾2i)k2=∑iw‾1iti\left(\sum_i \overline{w}_{1i}^2\right) k_1 + \left(\sum_i \overline{w}_{1i} \overline{w}_{2i}\right) k_2 = \sum_i \overline{w}_{1i} t_i
(∑iw‾1iw‾2i)k1+(∑iw‾2i2)k2=∑iw‾2iti\left(\sum_i \overline{w}_{1i} \overline{w}_{2i}\right) k_1 + \left(\sum_i \overline{w}_{2i}^2\right) k_2 = \sum_i \overline{w}_{2i} t_i

The sums over the three points are

∑iw‾1i2=0.8670362+0.52+0.1329642=1.01943,    ∑iw‾2i2=1.01943\sum_i \overline{w}_{1i}^2 = 0.867036^2 + 0.5^2 + 0.132964^2 = 1.01943, \;\; \sum_i \overline{w}_{2i}^2 = 1.01943
∑iw‾1iw‾2i=0.867036×0.132964+0.5×0.5+0.132964×0.867036=0.48057\sum_i \overline{w}_{1i} \overline{w}_{2i} = 0.867036 \times 0.132964 + 0.5 \times 0.5 + 0.132964 \times 0.867036 = 0.48057
∑iw‾1iti=0.867036×3+0.5×5+0.132964×8=6.16482\sum_i \overline{w}_{1i} t_i = 0.867036 \times 3 + 0.5 \times 5 + 0.132964 \times 8 = 6.16482
∑iw‾2iti=0.132964×3+0.5×5+0.867036×8=9.83518\sum_i \overline{w}_{2i} t_i = 0.132964 \times 3 + 0.5 \times 5 + 0.867036 \times 8 = 9.83518

Cramer's rule solves the two equations with the determinant

D=1.01943×1.01943−0.480572=0.80829D = 1.01943 \times 1.01943 - 0.48057^2 = 0.80829
k1=6.16482×1.01943−0.48057×9.83518D=1.558110.80829=1.927662k_1 = \frac{6.16482 \times 1.01943 - 0.48057 \times 9.83518}{D} = \frac{1.55811}{0.80829} = 1.927662
k2=1.01943×9.83518−0.48057×6.16482D=7.063650.80829=8.739005k_2 = \frac{1.01943 \times 9.83518 - 0.48057 \times 6.16482}{D} = \frac{7.06365}{0.80829} = 8.739005

The fitted outputs are 2.833325, 5.333334 and 7.833342 for the targets 3, 5 and 8.

Step 3. One gradient step for the centre c1c_1 at the first point. At x=2x = 2 the error is z−t=2.833325−3=−0.166675z - t = 2.833325 - 3 = -0.166675. The factors of the chain rule are z1−z2=k1−k2=1.927662−8.739005=−6.811343z_1 - z_2 = k_1 - k_2 = 1.927662 - 8.739005 = -6.811343, w‾1w‾2=0.867036×0.132964=0.115285\overline{w}_1 \overline{w}_2 = 0.867036 \times 0.132964 = 0.115285 and (x−c1)/σ2=2/16=0.125(x - c_1)/\sigma^2 = 2/16 = 0.125:

∂E∂c1=−0.166675×(−6.811343)×0.115285×0.125=0.01636\frac{\partial E}{\partial c_1} = -0.166675 \times (-6.811343) \times 0.115285 \times 0.125 = 0.01636

With η=0.5\eta = 0.5 the centre moves to c1=0−0.5×0.01636=−0.00818c_1 = 0 - 0.5 \times 0.01636 = -0.00818. The step is small because the first rule already fits this point well. ANFIS repeats such least-squares and gradient steps over all points until the error stops falling [7]. Part C of the interactive lab runs these cycles on editable data.

Fuzzy control in engineering

The first industrial success was the control of a cement kiln in Denmark, where operators' rules for burning zone temperature and oxygen were encoded as fuzzy rules [9]. The Sendai subway in Japan used predictive fuzzy control for automatic train operation, which improved riding comfort and stopping accuracy [10]. Fuzzy controllers later appeared in washing machines, cameras, heating systems, water treatment dosing and vehicle subsystems, and textbooks collect applications across civil, mechanical, chemical and electrical engineering [3].

Fuzzy control is most useful when a good mathematical model is missing but operator experience exists, when the process is nonlinear, and when smooth, explainable behaviour matters. It does not remove the need for analysis: Stability, robustness to disturbances and the effect of measurement noise must still be checked, for example by simulation over the operating range, as in the notebook. When a reliable model exists, classical or model-based control is often the better engineering choice.

Formulas: The tank under fuzzy control

The tank of the notebook and the lab has the cross-section A=2A = 2 m². The inflow is qin=qmax⁡vq_{\text{in}} = q_{\max} v with the valve opening vv between 0 and 1 and qmax⁡=1q_{\max} = 1 m³/s, and the outflow through the outlet follows Torricelli's law, qout=khq_{\text{out}} = k \sqrt{h} with k=0.6k = 0.6. The mass balance gives the rate of change of the level hh:

Adhdt=qmax⁡v−khA \frac{dh}{dt} = q_{\max} v - k \sqrt{h}

The level is steady when inflow and outflow are equal, which requires the valve opening

vss=khqmax⁡v_{\text{ss}} = \frac{k \sqrt{h}}{q_{\max}}

The fuzzy controller does not set the valve directly: It sets the change of the opening per second, Δv\Delta v, so that in each time step of length Δt\Delta t

vn+1=vn+Δv×Δt,    hn+1=hn+qmax⁡vn+1−khnA Δtv_{n+1} = v_n + \Delta v \times \Delta t, \;\; h_{n+1} = h_n + \frac{q_{\max} v_{n+1} - k \sqrt{h_n}}{A}\, \Delta t

where the opening is kept between 0 and 1. The level equation is the Euler step of Week 1.

Worked example: The first control step after a change of the set point

The tank is at h=1h = 1 m with v=0.6v = 0.6, which is steady, because 0.6×1/1=0.60.6 \times \sqrt{1}/1 = 0.6. The set point now jumps to 1.5 m.

Step 1. The target opening. To hold 1.5 m the valve must reach vss=0.6×1.5/1=0.6×1.224745=0.7348v_{\text{ss}} = 0.6 \times \sqrt{1.5}/1 = 0.6 \times 1.224745 = 0.7348.

Step 2. The inputs of the controller. The error is e=1.5−1=0.5e = 1.5 - 1 = 0.5 m, and the level does not move yet, so r=0r = 0.

Step 3. Fuzzification and rules. For the error, μlow(0.5)=0.5\mu_{\text{low}}(0.5) = 0.5 and μokay(0.5)=max⁡(min⁡(0.5+0.40.4,0.4−0.50.4),0)=max⁡(min⁡(2.25,−0.25),0)=0\mu_{\text{okay}}(0.5) = \max(\min(\frac{0.5 + 0.4}{0.4}, \frac{0.4 - 0.5}{0.4}), 0) = \max(\min(2.25, -0.25), 0) = 0; for the rate, μnone(0)=1\mu_{\text{none}}(0) = 1. Only R1 fires, with w1=0.5w_1 = 0.5; R3 gets min⁡(0,1)=0\min(0, 1) = 0.

Step 4. Defuzzification. The aggregated set is open fast clipped at 0.5, a trapezoid that is symmetric about the peak 0.05, so its centroid is Δv=0.05\Delta v = 0.05 per second.

Step 5. One time step of Δt=0.5\Delta t = 0.5 s.

v1=0.6+0.05×0.5=0.625v_1 = 0.6 + 0.05 \times 0.5 = 0.625
h1=1+1×0.625−0.6×12×0.5=1+0.625−0.62×0.5=1+0.00625=1.00625 mh_1 = 1 + \frac{1 \times 0.625 - 0.6 \times \sqrt{1}}{2} \times 0.5 = 1 + \frac{0.625 - 0.6}{2} \times 0.5 = 1 + 0.00625 = 1.00625 \text{ m}

The level starts to rise. As it approaches the set point the error enters the set okay, rules R3 and R4 take over and the valve settles near 0.7348. Part D of the interactive lab simulates the whole response.

Python step 4: NumPy arrays and first plots

The Python step of this week is part of the Colab notebook, where every explanation stands next to a cell that runs it and the step closes with a quick check and exercises with immediate feedback. The printable lecture notes contain the same step together with the outputs of its code. The Colab button below opens the notebook at the step. Run the setup cell just above the step first, since the exercises use its check function.

Open Python step 4 in Colab View the notebook on GitHub

Review cards

Select a card to turn it over.

Membership function
Assigns each element of the universe a degree of belonging between 0 and 1 [1].
Linguistic variable
A variable whose values are words, such as low, okay and high, each represented by a fuzzy set [2].
Firing strength
The degree to which a rule's conditions hold; with AND as minimum, the smallest membership among its conditions.
Centroid defuzzification
The centre of gravity of the aggregated output membership function.
Takagi-Sugeno rule
IF x is A AND y is B THEN z = p x + q y + r; outputs are averaged with firing strengths as weights [6].
np.linspace
Creates evenly spaced values over an interval, the usual discretised universe of a fuzzy variable.

Continue the week

The week continues with the simulation and the self-assessment of the interactive lab and with the Python step and the hands-on work of the Colab notebook. The week overview lists the discipline challenges, the weekly task and the research assignment.

Interactive lab Colab notebook Self-assessment Week overview and tasks

References

[1] Zadeh, L. A. (1965). Fuzzy sets. Information and Control, 8(3), 338-353. https://doi.org/10.1016/S0019-9958(65)90241-X

[2] Zadeh, L. A. (1973). Outline of a new approach to the analysis of complex systems and decision processes. IEEE Transactions on Systems, Man, and Cybernetics, SMC-3(1), 28-44. https://doi.org/10.1109/TSMC.1973.5408575

[3] Ross, T. J. (2017). Fuzzy Logic with Engineering Applications (4th ed.). Wiley.

[4] Mamdani, E. H., & Assilian, S. (1975). An experiment in linguistic synthesis with a fuzzy logic controller. International Journal of Man-Machine Studies, 7(1), 1-13. https://doi.org/10.1016/S0020-7373(75)80002-2

[5] Lee, C. C. (1990). Fuzzy logic in control systems: Fuzzy logic controller, Part I. IEEE Transactions on Systems, Man, and Cybernetics, 20(2), 404-418. https://doi.org/10.1109/21.52551

[6] Takagi, T., & Sugeno, M. (1985). Fuzzy identification of systems and its applications to modeling and control. IEEE Transactions on Systems, Man, and Cybernetics, SMC-15(1), 116-132. https://doi.org/10.1109/TSMC.1985.6313399

[7] Jang, J.-S. R. (1993). ANFIS: Adaptive-network-based fuzzy inference system. IEEE Transactions on Systems, Man, and Cybernetics, 23(3), 665-685. https://doi.org/10.1109/21.256541

[8] Kose, U., & Arslan, A. (2017). Forecasting chaotic time series via ANFIS supported by vortex optimization algorithm: Applications on electroencephalogram time series. Arabian Journal for Science and Engineering, 42(8), 3103-3114. https://doi.org/10.1007/s13369-016-2279-z

[9] Holmblad, L. P., & Østergaard, J.-J. (1982). Control of a cement kiln by fuzzy logic. In Fuzzy Information and Decision Processes (M. M. Gupta & E. Sanchez, Eds.) (pp. 389-399). North-Holland.

[10] Yasunobu, S., & Miyamoto, S. (1985). Automatic train operation system by predictive fuzzy control. In Industrial Applications of Fuzzy Control (M. Sugeno, Ed.) (pp. 1-18). North-Holland.