Fuzzy inference system for decision making
This example demonstrates the process of creating a fuzzy inference system. Such systems formalize 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, let's 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
The code below builds the Mamdani fuzzy inference system using the @mamfis macro. This system includes the definition of membership functions for input and output variables.
At the beginning of the code, we declare the tipper function, which takes two arguments: service and food. These variables represent the input data that will be used to estimate the tip that a restaurant visitor wants to leave.
For the service variable, we define three fuzzy sets using Gaussian membership functions:
- poor with a center of 0 and a standard deviation of 1.5
- good with a center of 5 and a standard deviation of 1.5
- excellent with a center of 10 and a standard deviation of 1.5
For the food variable, we define two fuzzy sets using trapezoidal membership functions.:
- rancid (rotten) with coordinates (-2, 0, 1, 3)
- delicious with coordinates (7, 9, 10, 12)
The tip variable 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, the rules of fuzzy logic are defined, linking the quality of service and food with tips. For example, if the service is poor or the food is substandard, tips will be low, if the service is good and the quality of the food is satisfactory, tips 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. For this, the variable fis_ex having the format Expr, is converted to the format String, for further recording:
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) # закрытие файла
Function definition tipper, in the Engee workspace using the function include from the created file:
include("fis.jl")
Using a function from a .jl file.
tipper(2, 3)
The resulting function can be used in other projects without connecting the FuzzyLogic library.
Visualization of membership functions
Connecting the graph visualization library:
using Plots
Displaying membership functions for a variable service:
plot(fis, :service)
Displaying membership functions for a variable food:
plot(fis, :food)
Displaying membership functions for a variable tip:
plot(fis, :tip)
Building a response surface
If the fuzzy output system has 2 inputs, then its response surface can be constructed. This is a surface that displays how the output changes 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 the FuzzyLogic library were examined. They were used to build a fuzzy inference system that allows you to model and process uncertain and inaccurate data using fuzzy logic. In the context of decision-making and computing, the fuzzy inference system formalizes human reasoning and expert knowledge, allowing for more flexible and adaptive decisions in the face of uncertainty compared to traditional methods.