Lab 2: Fuzzy Systems¶
Fuzzy Systems¶
Objective¶
- to construct a Mamdani fuzzy system using the
scikit-fuzzyPython library - to evaluate the result of the constructed fuzzy system
Note¶
Install the scikit-fuzzy Python library in your environment before proceeding with the lab.
1 | |
Fuzzy control system for a train¶
-
Consider a fuzzy control system to control the brake and throttle of a train based on the speed of the train and the distance of the train to the next stop.
-
Import the
skfuzzy,skfuzzy.control, andnumpy.1 2 3
import numpy as np from skfuzzy import control as ctrl from skfuzzy import membership as mf
Initialise inputs and outputs¶
-
Speed and distance are the inputs of the system whereas brake and throttle are the outputs.
-
The ranges for the variables are:
Variable Range Speed 0 - 85 km/h Distance 0 - 3000 m Brake 0 - 100% Throttle 0 - 100% -
As the inputs will be the antecedents of the rules, construct the variables
speedanddistanceasskfuzzy.control.Antecedentobjects.1speed = ctrl.Antecedent(np.arange(0, 85, 0.1), 'speed') -
The initialisation function for
skfuzzy.control.Antecedentobject takes 2 arguments, the first is the universe of the variable, i.e. the values the variables can take, the second is the label of the variable. The initialisation function forskfuzzy.control.Consequentis similar. -
The label and the range of the variable can be accessed using
.labeland.universerespectively.
Task: Initialise the variables distance as Antecedent object, and brake and throttle as Consequent objects. (Outputs of the system will be consequents of the rules)
Define membership functions for fuzzy sets of variables¶
-
The fit vectors of the fuzzy sets for the linguistic variables are given as follows:
-
speed (0 to 85 km/h)
Linguistic value Fit vector Stopped (1/0, 0/2) Very slow (0/1, 1/2.5, 0/4) Slow (0/2.5, 1/6.5, 0/10.5) Medium fast (0/6.5, 1/26.5, 0/46.5) Fast (0/26.5, 1/70, 1/85) -
distance (0 to 3000 m)
Linguistic value Fit vector At (1/0, 0/2) Very near (0/1, 1/3, 0/5) Near (0/3, 1/101.5, 0/200) Medium far (0/100, 1/1550, 0/3000) Far (0/1500, 1/2250, 1/3000) -
brake (0 to 100%)
Linguistic value Fit vector No (1/0, 0/40) Very slight (0/20, 1/50, 0/80) Slight (0/70, 1/83.5, 0/97) Medium (0/95, 1/97, 0/99) Full (0/98, 1/100) -
throttle (0 to 100%)
Linguistic value Fit vector No (1/0, 0/2) Very slight (0/1, 1/3, 0/5) Slight (0/3, 1/16.5, 0/30) Medium (0/20, 1/50, 0/80) Full (0/60, 1/80, 1/100)
-
-
The
skfuzzy.membershipmodule provides the following membership functions:Membership function Description skfuzzy.membership.dsigmf(x, b1, c1, b2, c2)Difference of two fuzzy sigmoid membership functions skfuzzy.membership.gauss2mf(x, mean1, ...)Gaussian fuzzy membership function of two combined Gaussians skfuzzy.membership.gaussmf(x, mean, sigma)Gaussian fuzzy membership function skfuzzy.membership.gbellmf(x, a, b, c)Generalized Bell function fuzzy membership generator skfuzzy.membership.piecemf(x, abc)Piecewise linear membership function (particularly used in FIRE filters) skfuzzy.membership.pimf(x, a, b, c, d)Pi-function fuzzy membership generator skfuzzy.membership.psigmf(x, b1, c1, b2, c2)Product of two sigmoid membership functions skfuzzy.membership.sigmf(x, b, c)The basic sigmoid membership function generator skfuzzy.membership.smf(x, a, b)S-function fuzzy membership generator skfuzzy.membership.trapmf(x, abcd)Trapezoidal membership function generator skfuzzy.membership.trimf(x, abc)Triangular membership function generator skfuzzy.membership.zmf(x, a, b)Z-function fuzzy membership generator -
The fit vector of a linguitic value can be assigned to a linguistic variable using
1 2
speed['stopped'] = mf.trimf(speed.universe, [0, 0, 2]) speed['very slow'] = mf.trimf(speed.universe, [1, 2.5, 4])Task: Assign all fuzzy sets to the linguistic variables.
-
The fuzzy set diagram of a linguistic variable can be viewed using
.view()1speed.view()Task: Check if the fuzzy set diagrams match the fit vectors.
Define rules¶
-
The rules for this system are displayed in the following fuzzy association memory (FAM) representaion table.
Distance At Very near Near Medium far Far Speed Stopped Full brake
No throttleFull brake
Very slight throttleVery slow Full brake
No throttleMedium brake
Very slight throttleSlight brake
Very slight throttleSlow Full brake
No throttleMedium brake
Very slight throttleVery slight brake
Slight throttleMedium fast Very slight brake
Medium throttleNo brake
Full throttleFast Very slight brake
Medium throttleNo brake
Full throttle -
Rule can be defined using
skfuzzy.control.Rule(antecedent, consequent, label). To define the first rule, i.e. if distance is 'at' and speed is 'stopped', then full brake and no throttle,1rule1 = ctrl.Rule(distance['at'] & speed['stopped'], (brake['full'], throttle['no']))If the antecedent consists of multiple parts, they can be combined using operators
|(OR),&(AND), and~(NOT).If the consequent consists of multiple parts, they can be combined as a
list/tuple.Task: Define all the rules. Then combine all the rules in a
list, i.e.rules = [rule1, rule2, ...].
Construct the fuzzy control system¶
-
The train control system can be constructed with
1train_ctrl = ctrl.ControlSystem(rules=rules) -
A
skfuzzy.control.ControlSystemSimulationobject is needed to simulate the control system to obtain the outputs given certain inputs.1train = ctrl.ControlSystemSimulation(control_system=train_ctrl) -
To obtain the values for
brakeandthrottlegiven thatspeedis 30 km/h anddistanceis 6 m,1 2 3 4 5 6 7 8 9 10 11 12
# define the values for the inputs train.input['speed'] = 30 train.input['distance'] = 2000 # compute the outputs train.compute() # print the output values print(train.output) # to extract one of the outputs print(train.output['brake']) -
To view the results in the graph,
1 2
brake.view(sim=train) throttle.view(sim=train)
View the control/output space¶
-
The control/output space allows us to identify if the outputs fit our expectation.
-
Construct an empty 3D space with 100-by-100 x-y grid.
1 2 3 4
x, y = np.meshgrid(np.linspace(speed.universe.min(), speed.universe.max(), 100), np.linspace(distance.universe.min(), distance.universe.max(), 100)) z_brake = np.zeros_like(x, dtype=float) z_throttle = np.zeros_like(x, dtype=float) -
Loop through every point and identify the value of brake and throttle of each point. As the specified rules are not exhaustive, i.e. some input combinations do not activate any rule, we will set the output of such input combinations to be
float('inf').1 2 3 4 5 6 7 8 9 10 11
for i,r in enumerate(x): for j,c in enumerate(r): train.input['speed'] = x[i,j] train.input['distance'] = y[i,j] try: train.compute() except: z_brake[i,j] = float('inf') z_throttle[i,j] = float('inf') z_brake[i,j] = train.output['brake'] z_throttle[i,j] = train.output['throttle'] -
Plot the result in a 3D graph using the
matplotlib.pyplotlibrary.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def plot3d(x,y,z): fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='viridis', linewidth=0.4, antialiased=True) ax.contourf(x, y, z, zdir='z', offset=-2.5, cmap='viridis', alpha=0.5) ax.contourf(x, y, z, zdir='x', offset=x.max()*1.5, cmap='viridis', alpha=0.5) ax.contourf(x, y, z, zdir='y', offset=y.max()*1.5, cmap='viridis', alpha=0.5) ax.view_init(30, 200) plot3d(x, y, z_brake) plot3d(x, y, z_throttle)
Fuzzy tipping recommendation system¶
-
A fuzzy expert system is designed to identify the percentage of tips a customer will give based on the service and the food the customer received.
-
The system has service and food as inputs, and tips as output.
-
The fit vectors of the fuzzy sets for the linguistic variables are given as follows:
-
service (0 to 10)
Linguistic value Fit vector Poor (1/0, 0/5) Average (0/0, 1/5, 0/10) Good (0/5, 1/10) -
food (0 to 10)
Linguistic value Fit vector Poor (1/0, 0/5) Average (0/0, 1/5, 0/10) Good (0/5, 1/10) -
tips (0 to 30%)
Linguistic value Fit vector Low (1/0, 0/15) Medium (0/0, 1/15, 0/30) High (0/15, 1/30)
-
-
The rules are displayed in the following fuzzy association memory (FAM) representaion table.
Food Poor Average Good Service Poor low tips low tips medium tips Average low tips medium tips high tips Good medium tips high tips high tips
Task: Construct the fuzzy inference system.
Task: Modify the membership functions of the input 'service' to
| Linguistic value | Fit vector |
|---|---|
| Poor | (1/0, 0/3) |
| Average | (0/2, 1/5, 0/8) |
| Good | (0/6, 1/10) |