Introduction to Fuzzy Logic

Davinder Singh
CodeX
Published in
10 min readOct 8, 2023
Photo by Elnaz Asadi on Unsplash

In this Article

  • Summary
  • Why Fuzzy Logics?
  • Crisp Vs. Fuzzy Sets
  • Probabilities Vs. Possibilities
  • Fuzzy Membership & Defuzzification
  • Conclusion
  • References

Summary

Unlike traditional binary or two-valued logic, fuzzy logic operates within a multi-valued logic system. This system was conceived as a more natural and effective way to capture the intricacies of human decision-making.

In this inaugural article of a three-part series, I delve into the foundational concepts that underpin Fuzzy Logic: Fuzzy Sets. I begin by exploring the motivation behind the creation of Fuzzy Logic, explaining why it was developed and how it works. Throughout the series, I will emphasise how Fuzzy Logic integrates uncertainty and fuzziness into our models, leading to a closer alignment with real-world situations. Additionally, I will discuss the possibility theory as an alternative to incorporating uncertainty into models, distinct from the idea of probability based on Boolean logic.

Regarding prerequisites, none are necessary. As this is an introductory article, I will provide explanations and guidance as we progress, ensuring a comprehensive understanding for all readers.

Why Fuzzy Logics?

Binary logic has been around for quite some time, and it is de facto for computing devices. But the real problem comes up when we try to model human decision-making. Imagine that we are trying to build a control system for a robot. In collision avoidance logic, implemented with Boolean Logic, we might have a rule: "If distSensor.dist < .8 meter, then reduce speed to 10%.” This rule says that if the distance from the object reported by the distance sensor is less than .8 meters, then reduce the speed to 10 per cent. While this rule works, it is not as flexible as we want for an AI-capable robot. The Fuzzy system can represent a similar situation with the following law: "If [object] is close, then slow down.” The first thing that is clear by looking at it is that it seems much more natural than the previous one. Also, it comes with a lot of flexibility. Using defuzzification, we can assign different meanings to both close and slow down and, when the need arises, map them to actual values.

Fuzzy Logic is well suited in situations where the precise meaning of terms like close, slow or dirty is not defined as a single value but as a degree. For example, In a binary system, clothes are either soiled or clean, but in Fuzzy logic, we can assign a degree on how dirty clothes are, whether very dirty or mildly or almost clean.

Because of this flexibility and ability to define natural but vague terms in a mathematical context, fuzzy logic is particularly good at expressing the control systems for robots or self-driving cars and autonomous medical diagnostics.

Crisp Vs. Fuzzy Sets

Now that we know why fuzzy logic was developed in the first place, we will focus on the foundation idea —Fuzzy Sets. A set is a collection of mathematical objects such as numbers or other sets. Every time a set is created, it is carved out of a universal set or set containing all possible objects of the current domain. For example, the set of all positive even numbers is cut out of all positive numbers (commonly called Natural Numbers).

Mathematically, this “carving out” is based on something called Indicator Function I, which maps every object which is a Member of a universal set to either 0 (False) or 1 (True). Let x be one such element, and I(x) means map x to one of {0,1} (curly brackets indicate either 0 or 1). In the case of even a set example, I can be defined as I(x) = x mod 2 == 0, which means return 1 if the remainder (mod) of x divided by 2 is 0 and 0 otherwise. Now replace x with any number from the set of Natural numbers; I(2) = 1 since 2 divided by 2 gives 0 remainder; therefore, 2 is in the (or member of) set of even numbers; similarly, I(3) = 0 as 3 divided by 2 leaves remainder 1. So, we can represent this set as E = {0, 2, 4,…,2n, …}. This is how every crisp set is built. The indicator function always returns either 0 or 1. What changes is how it decides.

Fuzzy sets are quite the generalised version of crisp sets. Rather than having indicator functions, we have the concept of degrees in them. What are degrees, you ask? A degree is any value between 0 and 1 or mathematically [0,1] (square brackets mean 0 or 1 or any value between these two); for example, 0.1, 0.00001, 0.5 or 1, any of the infinite values are possible. For instance, you want to create a set of all the students in a class who are tall. The first problem that comes up is how to define tall. Should a person be at least 5 feet and 8 inches to be classified as tall? or 6 feet? This way of thinking is Boolean, meaning if a person qualifies for some height criteria, they are considered tall or short. This way, we are losing a lot of information; say, somebody is taller than all the people not in the tall set but 1 inch shorter than the criteria. Does that mean they are not tall? At least not according to constricted thinking imparted by Boolean logic. Using a fuzzy set, we can represent this problem in a much more flexible and comprehensive way. Here’s one such scheme: Suppose person ‘m’ is taller than all and has a height of 5.8 feet; assign degree 1 to m (‘m’:1), ‘j’ is shorter than m by 4 inches, assign degree 1-(height(m)-height(j)) divided by 5 which is the degree of freedoms that you want. This can be visualised with the following plot.

Plot of a typical membership function for Tall Fuzzy Set F(x) = 1 if x ≥ 5.8 else 1-(x-5.8)/5

See, the lower the height is from 5.8, the lower the student's degree in the set. The degree is how much a member is part of the set. Had we used the Indicator function I(x) = x ≥ 5.8 to assign degrees, we would have gotten a crisp set, proving that Crisp sets are a particular case of Fuzzy sets. Let’s move on to another vital concept where fuzzy logic shines: describing vague uncertainties.

Probabilities Vs. Possibilities

Uncertainty is an essential topic in AI applications. AI systems often have to deal with incomplete or inaccurate information, and they need to be able to make decisions even when they are not sure of the outcome. A robot, for example, might have to face a lot of uncertainties in the ever-changing environment. A cleaning robot might have to anticipate human presence in the room, their footsteps, so that it doesn’t collide. Or what if the soffa was just moved, and now the robot has to update the internal map to reflect this?

The traditional approach of quantifying uncertainties is Probability Theory. The probability of an event E happening is the number of scenarios in which E can occur divided by all possible scenarios. For example, When a 6-sided dice is rolled, possible outcomes are S = {1,2,3,4,5,6}; this is the sample space of the experiment. And If somebody asks how likely it is that 6 comes up on rolling the dice — the answer is the size({6})/size({1,2,3,4,5,6}), which is 1/6. This ratio means that as the dice is rolled repeatedly, the number of times 6 comes up is, on average, 1/6.

Now that you understand probability's basic idea let’s move on to a more real-world example. Retake the example of a robot. This time, it is travelling along a path, and suddenly, the sensor detects a possibly moving body B that will cross the robot's path if it (the robot) keeps travelling the same way — resulting in a possible collision. Collision is not definite, as the body might change its direction or speed — uncertainty. Since we know probability, we deal with the situation using probability. Immediately, however, we encounter a problem as the likelihood is based on inferring historical data, and this is a new situation; the robot can only make a guess based on the current data available, and this is no better than making a random guess, i.e. performing a coin toss.

Possibility theory, however, is well suited for situations like this. The same problem in probability theory would be handled in the following way: Initially, in the fuzzy set D (decision), we shall have keep_moving and change_direction assigned degrees 0.5 (representing the current scenario), and then the robot will keep tracking the body adding new information updating the degrees along the way. After every track-update cycle, defuzzification is performed to determine the best action that can be taken, and this keeps happening till the defuzzification process results in either change_path or keep_moving_forward. So, a vague and uncertain situation is handled with the help of possibility theory, fuzzy logic and delayed decision-making more logically and naturally compared to the probabilistic approach.

Fuzzy Membership & Defuzzification

I have already given you a glimpse of what membership means regarding fuzzy sets. In this article, we will dive deeper into the concept and look at Defuzzification, a way of turning a fuzzy set back into a crisp one so that a computer can work with it.

Membership in a Fuzzy set is defined in terms of degrees, and this degree is computed by a Fuzzy Membership function instead of an Indicator function in the case of a crisp set. This concept might look simple, but it gives you so much power: simplicity lies in the fact that, in the end, a degree is just a number between 0 and 1, but beauty lies in how this number is computed. We can have a straight linear function that increases the degree as the object gets closer to some criteria, or we can have a Gaussian curve to represent the two-way relation. For example, imagine a robot seeking target T; as the robot gets closer to the target, the speed of the robot decreases and eventually approaches 0; on the other hand, it increases as the target moves farther away from the robot. Imagine representing this with Boolean logic; you simply cannot. Here is a plot to visualise this particular membership function.

Gaussian Fuzzy Membership (negative distance is when the robot is chasing the target)

1 represents the robot standing still, while any degree less than 1 represents speed percentage. This way, any problem can be defined as a fuzzy set; the only variable is how you decide the membership function.

Defuzzification is the reverse of membership. It takes in a fuzzy set and produces a crisp set. Let’s take an example of a smart washing machine. Sensors checking the clothes condition and available water reported the following fuzzy set F = {soil: 0.3, oil: 0.5} and W = {water_temp: .6, water_hardness: .4}. Our job is to build a defuzzification function D that takes in sets F and W and results in the following values: wash time and quantity of detergent to use. One possible deduction can be when water is hard, we require more detergent, but with hot water, the amount can be reduced as hot water can dissolve oil and other dirt effectively. The second deduction regarding the wash time is that the more dirty the clothes are, the harder the water, so we require more wash time. A straightforward approach would be to assign weights and then compute weighted averages. For this approach to work, we must have a base cloth wash time and detergent quantity; say, wash time is 45 minutes, and detergent is 100 gm.

D(F, W) can be defined as follows

wash_time = 45 + (180–45) * (F(oil) + F(soil)) / 2

detergent_quantity = 100 * (1-W(water_temp) )* (1 — W(water_hardness))

Plots for Detergent Quantity vs water Temperature and Hardness and Wash Time vs oil and Soil Level

As clothes become oily or soiled, the amount of wash time increases. This is done by computing the average of these two quantities. This can be made a little more complex by adding preference, K, such that K is higher for oil quantity than soil (K*F(soil) + (1-K)*F(oil))/2. Similarly, we are computing detergent quantity as water becomes colder or the hardness of water increases; more detergent is required (lighter colour in the graph).

Fuzzy logic, thus, comes very handy when working with these vague quantities and then defuzzification turns this information back into a crisp set to be used by computing devices. Usually, the workflow is like this:

(Sensor Data) → (Fuzzifier) → (Fuzzy Inference or Rules) → (Defuzzifier) →..

Conclusion

While I’ve tried to touch on many topics in the theory of Fuzzy Logic, I’ve also tried to follow an easy-to-understand writing style (which I’ll continue throughout the series) while not doing injustice to the gist of the subject. Of course, it is a large subject and hence requires a lot of articles discussing sub-topics like the selection of membership function, fuzzy algebra, Defuzzification, Control Systems, etc.

Thank you for taking the time to read this introductory article on fuzzy logic. I look forward to delving deeper into the subject in upcoming articles. Also, I look forward to your suggestions and questions. Stay tuned, and happy learning!

References

  1. A First Course in Fuzzy Logic

Postscript

  1. Matplotlib with XKCD-style for Graphs
  2. Note: The graph generated above representing the Gaussian relationship between robot speed and its distance from the target is not precisely Gaussian because it has been cut from tails at -2 and +2 Km. Gaussian, on the other hand, goes to infinities in both directions.
  3. Note: The section where I have explained probability theory vs possibility theory. In probability theory, we can do the same thing using the Bayesian Model, but Fuzzy Logic and the Possibility theory provide a more natural and flexible approach.

Revision: 1.0

--

--

Davinder Singh
CodeX
Writer for

Davinder Singh a.k.a Dav Vendator is a hobbyist programmer with never ending hunger for knowledge in field of AI, Quantum and Science!