Fuzzy logic in practice

A standard article on fuzzy logic usually does two things:

  1. In 99% of cases, the article deals exclusively with the use of fuzzy logic in the context of fuzzy sets, or rather fuzzy inference, and even more precisely the Mamdani algorithm. One gets the impression that only in this way fuzzy logic can be applied, but this is not so.
  2. Almost always, an article is written in mathematical language. Great, but programmers use a different language with different notation. Therefore, it turns out that the article is simply incomprehensible to those who, it would seem, should be useful.

All this is sad, because fuzzy logic is one of the greatest achievements of mathematics of the 20th century, if practical criteria are taken as a criterion. In this article I will try to show how simple and powerful this programming tool is - as simple, but much more powerful than a system of ordinary logical operations.

The most remarkable fact about fuzzy logic is that it is primarily logic . From the beginnings of math logic it is known that any logical function can be represented by a disjunctive or conjunctivenormal form, which implies that to implement the propositional calculus, only three operations are enough: conjunctions (&&), disjunctions (||) and negation (!). In classical logic, each of these operations is given by a truth table:

ab || ab && a!
-------- -------- ----
0 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, in contrast to classical logic, instead of true and false values, the degree of truth is used , which takes any values ​​from an infinite set from 0 to 1 inclusive. Therefore, logical operations can no longer be represented in a table. In fuzzy logic, they are set by functions.

There are two ways to implement disjunction and conjunction:

# Maximin approach:
a || b => max (a, b)
a && b => min (a, b)
#Colorometric approach:
a || b => a + b - a * b
a && b => a * b

Negation is set in a unique way (it is not difficult to guess):

! a => 1 - a

It is easy to verify that for extreme cases - when the values ​​of the variables are exclusively 1 or 0 - the above functions give truth tables of operations of classical logic. Done! Now we have an extended logic with incredible power, simplicity and at the same time fully 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 you always have to get 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 a quantum state into probability and rolls dice, but generally speaking, the methods of defuzzification are different. I will not delve into this topic, because its volume draws on a separate article. I’ll just mention that the defazzification method should be chosen,

For example, imagine a missile control system that uses fuzzy logic to avoid obstacles. Imagine that a rocket flies exactly uphill, and the control system calculates the solution: fly right - 0.5, fly left - 0.5. If you use defuzzification by the center of mass method, 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 probability of 50%.

In the simplest case, when you need to make a decision based on the degree of truth, you can split the set [0,1] into intervals and use if-else-if.

If fuzzy logic is used to search by 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 some minimum degree of equality, the values ​​below which we are not interested in; for the remaining elements, the degree of equality will be relevant, in decreasing order of which we will sort the results, and let the user decide which result is correct.

As an example, I will use the fuzzy logic to solve the problem that I had fun at the institute - this is the task of finding the Chinese character in the image.

I immediately rejected the idea of ​​recognizing any doodles drawn by the user on the screen (then it was a PDA screen). Instead, the program suggested choosing the type of trait from the order of 23 defined by the rules of Japanese calligraphy. By selecting the type of stroke, the user drew a rectangle into which the stroke fit. In fact, the character - 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 this representation? To begin with, we formulate a criterion in a clear statement:

Hieroglyphs A and B are equal if and only if for each trait in A there is an equal trait in B and for each trait in B there is an equal trait in A.

It is implicitly assumed that the hieroglyphs do not contain duplicate features, that is, if some feature coincides with a feature in another character, then it cannot coincide with any other feature in the same character.

The equality of features 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 that 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 traits, and the operator '==' calculates their fuzzy equality.
# It is assumed that both characters have the same number of traits - n.

Then we close 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, colorimetry gives too small values ​​for conjunctions. If we recall that max is a disjunction, it turns out that we are calculating the statement that the ith line of A is equal to the first line of B or the second or third, etc. Thus, M is the coincidence vector 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 # (ratio of the sum of 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 is a philosophical question.

A couple of 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. Features of some types are very similar. Entering, the user can easily confuse them, so it’s worth having a similarity table, the values ​​of which will reflect how much the line i looks like the line j (on the main diagonal, of course, there will be units). As the degree of equality of the rectangles, we can take the ratio of the area of ​​their intersection to the area of ​​the larger of the rectangles.

In general, the scope of fuzzy logic is very extensive. In any algorithm, in any system of rules, try replacing truth and falsehood with a degree of truth, and perhaps this system of rules or algorithm will more accurately reflect reality. After all, we live in a world that is fundamentally fuzzy.

Also popular now: