Fuzzy inference system for decision making
This example demonstrates the process of creating a fuzzy inference system. Such systems formalise human reasoning and expert knowledge in the form of fuzzy sets, their membership functions and decision rules.
Installing and connecting libraries
To create a fuzzy inference system, we will use the Julia programming language library called FuzzyLogic. To install it in Engee, you need to execute the following code cell:
Pkg.add(["FuzzyLogic"])
Pkg.add("FuzzyLogic")
Connecting the installed FuzzyLogic library:
using FuzzyLogic
Building a fuzzy inference system
In the code below, a Mamdani fuzzy inference system is built using the @mamfis macro. This system involves defining membership functions for input and output variables.
At the beginning of the code, we declare a tipper function that takes two arguments: service and food. These variables represent the input data that will be used to estimate the tip that a restaurant customer wants to leave.
For the service variable we define three fuzzy sets using Gaussian membership functions:
- poor with centre 0 and standard deviation 1.5
- good with centre 5 and standard deviation 1.5
- excellent with centre 10 and standard deviation 1.5
For the variable food we define two fuzzy sets using trapezoidal membership functions:
- rancid with coordinates (-2, 0, 1, 3)
- delicious with coordinates (7, 9, 10, 12)
The variable tip has three fuzzy sets defined by triangular membership functions:
- cheap (small) with coordinates (0, 5, 10)
- average with coordinates (10, 15, 20)
- generous with coordinates (20, 25, 30)
Next, fuzzy logic rules are defined, linking the quality of service and food with the tip. For example, if the service is bad or the food is of poor quality, the tip will be low, if the service is good and the quality of the food is satisfactory, the tip will be average, and so on.
fis = @mamfis function tipper(service, food)::tip
service := begin
domain = 0:10
poor = GaussianMF(0.0, 1.5)
good = GaussianMF(5.0, 1.5)
excellent = GaussianMF(10.0, 1.5)
end
food := begin
domain = 0:10
rancid = TrapezoidalMF(-2, 0, 1, 3)
delicious = TrapezoidalMF(7, 9, 10, 12)
end
tip := begin
domain = 0:30
cheap = TriangularMF(0, 5, 10)
average = TriangularMF(10, 15, 20)
generous = TriangularMF(20, 25, 30)
end
service == poor || food == rancid --> tip == cheap
service == good --> tip == average
service == excellent || food == delicious --> tip == generous
end
An example of using a fuzzy inference system in the form of a function fis
:
fis(service=2, food=4)
Generating a library-independent function using compilefis
:
fis_ex = compilefis(fis)
The function obtained by code generation can be written to a file. To do this, the variable fis_ex
, which has the format Expr
, is converted to the format String
, for further writing:
text_function = string(fis_ex)
Writing a function in string format to a file:
f = open("fis.jl","w") # создание файла
write(f, text_function) # запись в файл
close(f) # закрытие файла
Define the function tipper
, in the Engee workspace using the function include
from the created file:
include("fis.jl")
Using a function from a .jl format file.
tipper(2, 3)
The obtained function can be used in other projects without connecting FuzzyLogic library.
Visualisation of membership functions
Connecting a library for visualising graphs:
using Plots
Display membership functions for the variable service
:
plot(fis, :service)
Display the membership functions for the variable food
:
plot(fis, :food)
Display the membership functions for the variable tip
:
plot(fis, :tip)
Constructing the response surface
If a fuzzy output system has 2 inputs, we can construct its response surface. This is a surface that shows how the output data varies depending on the input data:
# Определяем векторы
s = collect(0:0.2:10) # Вектор s от 0 до 10
f = collect(0:0.2:10) # Вектор f от 0 до 10
# Создаем матрицы значений
tip_matrix = zeros(length(s), length(f))
# Заполняем матрицу значениями отклика
for (i, service) in enumerate(s)
for (j, food) in enumerate(f)
tip_matrix[i, j] = tipper(service, food)
end
end
# Построение поверхности отклика
surface(s, f, tip_matrix, xlabel="Service", ylabel="Food", zlabel="Tip", title="Поверхность отклика", color=:inferno)
Conclusion
In this example the functions of FuzzyLogic library were considered. They were used to build a fuzzy inference system that allows modelling and processing of uncertain and imprecise data using fuzzy logic. In the context of decision-making and computing, the fuzzy inference system formalises human reasoning and expert knowledge, allowing more flexible and adaptive decision-making under uncertainty than traditional methods.