A standard article on fuzzy logic usually suffers from two things:
- In 99% of cases, the article concerns only the application of fuzzy logic in the context of fuzzy sets, or more precisely, fuzzy inference, and even more precisely the Mamdani algorithm. It seems that only in this way fuzzy logic can be applied, but this is not so.
- Almost always the article is written in mathematical language. Great, but programmers use a different language with different designations. Therefore, it turns out that the article is simply incomprehensible to those who, it would seem, should be helpful.
All this is sad, because fuzzy logic is one of the greatest achievements of the mathematics of the 20th century, if we take practical use of the criteria. In this article I will try to show how simple and powerful the programming tool is, as simple as, but far more powerful, than the system of ordinary logical operations.
The most remarkable fact about fuzzy logic is that it is primarily
logic . From the beginnings of mathematics, it is known that any logical function can be represented by a
disjunctive or
conjunctive normal form, which means that only three operations are sufficient to realize the calculus of statements: conjunction (&&), disjunction (||) and negation (!). In classical logic, each of these operations is given by a truth table:
ab || ab && a!
-------- -------- ----
0 0 0 0 0 0 1
0 1 1 0 1 0 1 0
1 0 1 1 0 0
1 1 1 1 1 1
In fuzzy logic, as opposed to classical logic, instead of
true and
false values, the
degree of truth is used , taking any values from an infinite set from 0 to 1 inclusive. Consequently, logical operations can no longer be presented in table. In fuzzy logic, they are given by functions.
')
There are two ways to implement disjunctions and conjunctions:
# Maximin approach:
a || b => max (a, b)
a && b => min (a, b)
# Colorimetric approach:
a || b => a + b - a * b
a && b => a * b
The negation is given in the only way (not hard to guess):
! a => 1 - a
It is easy to check that for extreme cases - when the values of variables are exclusively 1 or 0 - the above functions give truth tables of classical logic operations. Done! Now we have advanced logic, which has incredible power, simplicity and at the same time is completely compatible with classical logic in extreme cases. So wherever we [programmers] use logical expressions, can we use fuzzy logic expressions? Not really.
The fact is that all operators of programming languages require clear conditions, so at some point it is always necessary to obtain a clear trigger criterion from a fuzzy degree of truth. This is similar to what happens in the quantum world: as long as the system evolves according to the Schrödinger equation, its quantum state changes deterministically and continuously, but as soon as we touch the system, a quantum jump occurs and the system falls into one of discrete states. In fuzzy logic, this is called defuzzification. Nature simply turns the quantum state into probability and throws the bones, but generally speaking defuzzification methods are different. I will not delve into this topic, because its volume pulls into a separate article. I will only mention that the defuzzification method should be chosen, taking into account the semantics of the problem.
For example, imagine a rocket control system that uses fuzzy logic to bypass obstacles. Imagine that the rocket is flying exactly uphill, and the control system calculates the solution: fly to the right - 0.5, fly to the left - 0.5. If you use the defuzzification method of the center of mass, the control system will give the command - to fly straight. Boom! Obviously, in this case, the right decision is to roll the dice and get the command “left” or “right” with a 50% probability.
In the simplest case, when you need to make a decision based on the degree of truth, you can divide the set [0,1] into intervals and use if-else-if.
If fuzzy logic is used to search for fuzzy criteria, then defuzzification may not be necessary at all. Making comparisons, we will get some value of the degree of equality for each element of the search space. We can define a certain minimum degree of equality, values below which we are not interested in; for the remaining elements, the degree of equality will be relevance, in descending order of which we will sort the results, and let the user decide which result is correct.
As an example, let me give you the use of fuzzy logic to solve the problem, which I had fun while still at the institute - this is the task of searching for the Chinese character in the image.
I immediately dropped the idea to recognize any karakul, drawn by the user on the screen (then it was the PDA screen). Instead, the program offered to choose the type of feature from about 23, defined by the rules of Japanese calligraphy. By selecting a stroke type, the user drew a rectangle into which the stroke fit. In fact, the hieroglyph - both entered and stored in the dictionary - was represented as a set of rectangles for which the type was defined.
How to determine the equality of hieroglyphs in such a representation? To begin with, we formulate the criterion in a clear statement:
Hieroglyphs A and B are equal if and only if for each line in A there is a line equal to it in B and for each line in B there is a line equal to it in A.It is implicitly assumed that the hieroglyphs do not contain duplicate traits, that is, if a certain trait coincided with a trait in another hieroglyph, it cannot coincide with any other trait in the same hieroglyph.
Equality of traits can be defined as follows:
Features are equal if and only if they are of the same type and their rectangles occupy the same area.These two definitions give us a statement system, which is enough to implement the search algorithm.
To begin with, we construct the matrix E [n, n] as follows:
for i in 1..n
for j in 1..n
E [i, j] = A [i] == B [j]
end
end
#A and B are hieroglyphs; A [i] and B [j] are their features, and the '==' operator computes their fuzzy equality.
# It is assumed that both hieroglyphs have the same number of features - n.
Then we join this matrix into the vector M [n]:
for i in 1..n
M [i] = E.max_in_row (i)
end
# The max_in_row method calculates the maximum value in the matrix row.
I use the maximin approach because, in practice, the colorimetric gives too small values for conjunctions. If we recall that max is a disjunction, it turns out that we calculate the statement that the i-th line A is equal to the first line B or the second or third line, and so on. Thus, M is a vector of coincidence of traits A with traits B.
Next, we need to turn the coincidence vector into one single value, and this can be done in two ways:
# Just a fuzzy conjunction.
e = M.min
# Or so:
e = M.sum / M.length # (the ratio of the sum of the elements to the length of the vector).
Both methods work, but in different ways, and the second method works even if you compare the features clearly. Which one is more correct - a philosophical question.
A few more words should be said about the comparison of features. According to the definition, equality of features is a conjunction of two conditions: equality of types and equality of rectangles. The traits of some types are very similar. When entering, the user can easily confuse them, so it’s worth having a similarity table, the values of which will reflect how much i’s a line is like a j’s line (there will naturally be ones on the main diagonal). As the degree of equality of rectangles, one can take the ratio of the area of their intersection to the area of the larger rectangle.
In general, the scope of fuzzy logic is very extensive. In any algorithm, in any rule system, try replacing truth and falsehood with a degree of truth, and perhaps this rule system or algorithm will more accurately reflect reality. After all, we live in a world that is fundamentally fuzzy.