A Fuzzy Logic Primer Peter Elsea - Dartmouth College

12
Fuzzy Primer Peter Elsea 2/11/14 1 A Fuzzy Logic Primer Peter Elsea Fuzzy logic is a mathematical pursuit and an engineering approach. In mathematics, fuzzy logic has been highly controversial, with raging battles about the validity of the discipline at all, its appropriateness versus probability theory, and the personal habits of its proponents. Resistance is mostly about the name, which is easily misunderstood. Fuzzy logic is not logic that is fuzzy, it is a logic that deals with fuzzy issues. And when you get right down to it, nearly every real situation involves fuzzy issues. How fast is too fast? Controversy also arises from the fact that fuzzy logic admits that there is such a thing as "partially true." Classically trained logicians really hate that. The arguments are fading out though, especially since it is easy to prove that classical logic is a special case of fuzzy logic. As an engineering discipline, fuzzy is in use just about everywhere, although it first flowered in Japan. In the US it was ignored until the mid '90s. It is mostly used in control systems, things like thermostats that adjust for the time of day and angle of the sun, or autofocus in a camera. Fuzzy is also a good way to make expert systems, because it presents a very straightforward way to encode problems and make decisions. Review of Crisp Sets Fuzzy logic is developed from set theory. You should be familiar with the set, a collection of things that have some common characteristic. An example would be the set of all people in a classroom wearing coats. Another set might be the set of all those in the classroom wearing sweaters. The two sets are distinct. However, each member of each set is also a person, so the set of all persons in the classroom includes both sets. The set of people wearing coats is a subset of the set of persons in the classroom. Another subset of the set of people in the classroom is the set of those with both coats and sweaters. These people belong to two sets, and constitute the intersection of the sets. Yet another way to group people is those wearing either sweaters or coats. This is the union of the two sets. You've seen diagrams with ovals to represent all of this. Figure 1. Now consider the set of all persons in the classroom wearing tennis shoes. There is also the set of those not wearing tennis shoes. Obviously, the two sets are mutually exclusive. Mutually exclusive sets are complements of each other.

Transcript of A Fuzzy Logic Primer Peter Elsea - Dartmouth College

Fuzzy Primer

Peter Elsea 2/11/14 1

A Fuzzy Logic Primer Peter Elsea Fuzzy logic is a mathematical pursuit and an engineering approach. In mathematics, fuzzy logic has been highly controversial, with raging battles about the validity of the discipline at all, its appropriateness versus probability theory, and the personal habits of its proponents. Resistance is mostly about the name, which is easily misunderstood. Fuzzy logic is not logic that is fuzzy, it is a logic that deals with fuzzy issues. And when you get right down to it, nearly every real situation involves fuzzy issues. How fast is too fast? Controversy also arises from the fact that fuzzy logic admits that there is such a thing as "partially true." Classically trained logicians really hate that. The arguments are fading out though, especially since it is easy to prove that classical logic is a special case of fuzzy logic. As an engineering discipline, fuzzy is in use just about everywhere, although it first flowered in Japan. In the US it was ignored until the mid '90s. It is mostly used in control systems, things like thermostats that adjust for the time of day and angle of the sun, or autofocus in a camera. Fuzzy is also a good way to make expert systems, because it presents a very straightforward way to encode problems and make decisions.

Review of Crisp Sets Fuzzy logic is developed from set theory. You should be familiar with the set, a collection of things that have some common characteristic. An example would be the set of all people in a classroom wearing coats. Another set might be the set of all those in the classroom wearing sweaters. The two sets are distinct. However, each member of each set is also a person, so the set of all persons in the classroom includes both sets. The set of people wearing coats is a subset of the set of persons in the classroom. Another subset of the set of people in the classroom is the set of those with both coats and sweaters. These people belong to two sets, and constitute the intersection of the sets. Yet another way to group people is those wearing either sweaters or coats. This is the union of the two sets. You've seen diagrams with ovals to represent all of this.

Figure 1. Now consider the set of all persons in the classroom wearing tennis shoes. There is also the set of those not wearing tennis shoes. Obviously, the two sets are mutually exclusive. Mutually exclusive sets are complements of each other.

Fuzzy Primer

Peter Elsea 2/11/14 2

When we are organizing people (or anything) into sets, the usual approach is to simply list them. I prefer to look at sets slightly differently, from the point of view of one person at a time. If I have four things that could describe one potential set member1, I can make a list of the descriptions and just check the ones that apply. Someone wearing a hat and coat, but not tennis shoes or a sweater would get a list like table 1. The check mark indicates they are a member of the particular set. People -> W/hat W/tennies W/coat W/sweater membership-> √ √ Table 1. Being mathematically inclined, I'd probably use a 1 to indicate a check and a 0 to represent no check. Then I could describe what someone is wearing with a list of numbers. Ralph's clothes => {1 0 1 0} Of course, this only works if I remember what the numbers stand for, but that is not a problem in a computer program. At this point, the sets become useful, because we can arrange them in a table, and make observations about how many folks wear hats and tennis shoes together. People -> W/hats W/tennies W/coat W/sweater Cindy 0 0 0 1 George 1 1 1 0 Ralph 1 0 1 0 Sonia 1 1 1 1 Table 2. From the table, we can construct a set that represents people wearing hats: Hats => {0 1 1 1} Each person is a member of the set {hats} if they have a 1 in the hats column in table 2. If not, we say their membership in the set {hats} is 0. We can go further and use Boolean logic to construct intersections and unions of these sets. Tennis shoes and sweater => {0 0 0 1} Tennis shoes or sweater => {1 1 0 1} The intersection is created with the Boolean AND operation, and the union with OR. Here is a reminder of the Boolean rules: 1 AND 1 = 1 1 OR 1 = 1 1 AND 0 = 0 1 OR 0 = 1 0 AND 1 = 0 0 OR 1 = 1

1 The set of everything that could possibly be a member of any of the sets I am talking about is called the Universe.

Fuzzy Primer

Peter Elsea 2/11/14 3

0 AND 0 = 0 0 OR 0 = 0 Boolean Rules

FUZZY SETS Fuzzy logic is used for qualities that are not either/or. For instance, consider the set of all tall persons in the classroom. What do I mean by tall? In the ordinary logic system I have to pick an arbitrary height, say 5' 6.0000", and consider everyone above that to be tall. That means some people could jump from the tall to the not tall set by combing their hair. Anyone who has received a B+ grade understands the awkwardness that can arise from sharp and arbitrary divisions. For a vague description like tall, it makes more sense to use a fuzzy set. A fuzzy set allows partial membership. You don't have to be tall or not tall, you can be 0.5 tall. We can easily construct a table that assigns a value of tallness for each likely height2. To begin, we find a value we consider definitely tall, like 6'. We assign a person 6' tall or taller a membership of 1. We also find a height that is clearly not tall (say 5'), and give that a zero. We fill in the spots between these points along a straight line or some sort of curve, resulting in a membership table like 3: Height 5'0 5'1 5'2 5'3 5'4 5'5 5'6 5'7 5'8 5'9 5'10 5'11 6' Tall 0 .02 .08 .2 .3 .4 .5 .6 .7 .8 .92 .97 1 Table 3. Now we can describe someone who is 5'8" as having 0.7 membership in the set of tall people. That may seem odd, but if you consider it "fairly tall", it makes sense. We can assume a simple relationship between the numbers shown. That is, a person 5 feet six and a half inches in height has a membership in {Tall} of 0.55. Everyone above 6'0" has a membership of 1 and everyone shorter than 5'0" has a membership of 0. We can also create a fuzzy set for "heavy". I'll let you fill in the numbers in table 4. weight heavy 0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1

Table 4. Now we can describe someone in terms of weight and height with a bit of precision, as in table 5.

heavy tall Jack .8 .2 George .4 .7

Table 5. Fuzzy Intersection and Union If Jack has a membership in {heavy} of 0.8 and in {tall} of 0.2, his membership in the set {heavy and tall} is 0.2. How is that? Membership in the intersection of two fuzzy sets is 2 The quality that we can actually measure to determine a membership is called the Domain.

Fuzzy Primer

Peter Elsea 2/11/14 4

equal to the minimum membership in either. Remember, everyone has a membership value in all of the sets, even if that value is 0. If someone is 4'9", they have a membership in {tall} of 0, so their membership in {heavy and tall} is also 0. Note that the use of the word "and" is the clue to the fact that we are looking for an intersection. Likewise, membership in the union of two fuzzy sets is equal to maximum of the membership in either. Unions are suggested by the word "or".

heavy tall H and T H or T Jack .8 .2 .2 .8 George .4 .7 .4 .7

Table 6. If George has a membership in heavy of 0.4 and in tall of 0.7, his membership in {heavy and tall} is 0.4 and membership in {heavy or tall} is 0.7. Inference What's the point? Well, we now have a mathematical way of making the statement "If a person is heavy and tall, he is strong". How strong? If we define {strong} as the same as {heavy and tall}, Jack's membership in {strong} is 0.2 and George's membership in {strong} is 0.4. Of course height and weight alone don't determine strength. We need to know more about Jack and George. George goes to the gym five times a week, and Jack spends most of his time at a computer. So let's change the statement to "If a person is heavy or tall and works out, he is strong". Assigning a membership in {works out} of .9 for George and 0.1 for Jack (he walks to class), we get: George: {strong} = {{0.4 heavy} or {0.7 tall}} and {0.9 works out} = 0.7 Jack: {strong} = {{0.8 heavy} or {0.2 tall}} and {0.1 works out} = 0.1 You can see that the fact that Jack does not work out limits his strength, whereas George can probably ease up a bit. You can also see that it should not be much effort to modify the formula to include other factors like health and nutrition. The ability to easily add factors is one of the best features of the fuzzy system. Fuzzy Complements So what is Jack's membership in the fuzzy set of short people? People who are short are not tall, and not implies the complement set. The complement of a fuzzy set is found by subtracting the membership from 1.0. Thus Jack's membership in {short} is 0.8, and George's is 0.3. Here's a diagram that explores the concept:

Fuzzy Primer

Peter Elsea 2/11/14 5

Figure 2. Complimentary fuzzy sets Drawing shapes like figure 2 is a popular way of describing fuzzy sets. The domain is shown across the bottom, and the graph of membership follows a triangle, trapezoid, or some kind of curve. The highest membership in the set is always 1. The shape provides an overview of the way the memberships work, and is often called the membership function. Some mathematicians describe membership functions with equations, but that is not really necessary. Fuzzy Hedges Some fuzzy mathematicians emphasize the close affinity between fuzzy logic and ordinary language. We have seen this already in statements like "if Ralph is smart and studies hard he will get good grades." By defining sets for {smart} and {studies hard}, we can make an educated prediction of Ralph's membership in the set {gets good grades}. This has been expanded to handle statements like "if Harry is very smart and studies hard he will get good grades". The fuzzy set {very smart} is derived from the set {smart} by squaring the membership values, a process known as concentrating. As you can see in figure 3, this affects the transition zone, reducing all membership values (since they are lower than 1). Words like "very" and "fairly" are known as hedges. The set {fairly smart} would be derived by taking the square roots of the membership values in {smart}, a dilation process.

Figure 3. Fuzzy concentration and dilation. Truncation and Weighting There are situations in which we will want to reduce the importance of a set in some operation. There are two ways to do this. One is to multiply the membership value obtained from the set by a number less than one-- this is called weighting. The other is to create a new set by clipping all memberships to some maximum value, say 0.5. This produces a truncated set as shown in figure 4.

Fuzzy Primer

Peter Elsea 2/11/14 6

Figure 4. Truncation

Sets of sets The set {tall} is a good illustration of the fuzzy concept, but it is not a complete description of the concept of height. To cover the entire range of possible heights, we must also consider short and average people as well as outliers at either end:

Figure 5. Figure 5 uses five sets to cover the entire range of height. You will notice that the sets overlap-- a person at the high end of the average range may be considered to fit the concept of both average and tall to some degree. Every person will have a membership value in all five of these sets, but the value will be 0 in at least three of them. Singletons Sometimes we use a set in which only one value has a membership. That membership is usually one and represents a single number or one of several mutually exclusive states. Figure 6 represents number 3 or the note D#.

Figure 6.

Fuzzy Primer

Peter Elsea 2/11/14 7

Fuzzy Numbers Fuzzy numbers are a way to express imprecise measurements like "around 7". These are sets too, along the domain of real numbers with a membership peak at the official value and falling membership nearby.

Figure 7. There are similar concepts for "nearly 7" or "just above 7". Some people do math with fuzzy numbers, but we'll just use them as input to reasoning systems.

Fuzzy Primer

Peter Elsea 2/11/14 8

Working with Fuzzy Logic The Main application of Fuzzy Logic is solving problems that involve imprecise information or imprecise relationships between input and output. Designing a fuzzy algorithm generally follows these steps:

• State the problem as a set of rules in ordinary language. • Create membership functions as implied by the rules. These can be very rough to

begin with, because they can easily be refined later. • Restate the rules using these functions. Note that the results of each rule are fuzzy

sets. • Combine the result sets from each rule into a grand result, probably a union. • Extract a single value from the final result using one of several processes of

"defuzzification". We'll explore the process with some simple examples. Monotonic reasoning The simplest rule is one where there is a direct relationship between one factor and another. For instance, you could easily implement the rule "if the car is going too slow, apply more gas." In this case, you would have two fuzzy sets, one representing "too slow", another representing " more gas".

Figure 8. You can see from figure 8 how the two sets would relate. There would be some starting gas setting, probably associated with the speed limit. (Let's assume the target speed is the 0 mark on the set too slow.) As the speed dropped into the too slow range, the membership value for the speed would determine how much extra gas to add. The extra amount of gas is found by reading the domain value for {more gas} at the membership value obtained from {too slow}. The relationship is direct, but it doesn't have to be simple. Note that the concept of too slow has a curved membership function. Of course, speeding up is only half the story. Imagine a similar system that implements the rule "if the car is going too fast, let up on the gas." This is shown in figure 9. Most control applications have complimentary rules like this. We always test both rules. One asks for more gas under certain circumstances, the other asks for less gas under different circumstances. The resulting action comes from adding the results together. The interesting thing is that this works even if the zones for {too fast} and {too slow} overlap!

Fuzzy Primer

Peter Elsea 2/11/14 9

Figure 9. Multiple Factors The target speed in the above example can depend on a variety of factors. Here are some factors to consider:

• Speed limit ( a fuzzy number if I ever saw one) • Being late • Weather • Presence of police

We can codify this into a set of rules

• Drive at a speed near the speed limit. • If late, speed up • If road is wet, slow down • If police have been seen, slow down.

Figure 10 shows how this works. Each rule has a predicate and a consequent set. For each, we find the membership of the relevant quantity and clip the consequent set to the membership value. The clipped result sets are shown as shaded. Note that "around the speed limit" is always true, so the whole consequent is taken. From the clipped sets, we generate a composite solution set. This is a union of all rule results. Once we have a solution set, we must "defuzzify" it to extract a single value. There are may ways of doing this, but the most common is to find the centroid or center of gravity of the solution set. The formula for a solution set R is:

In English, this means the sum of the memberships at each index times that index is divided by the sum of all of the memberships.

Fuzzy Primer

Peter Elsea 2/11/14 10

Speed Limit

Late?

Wet?

Police?

Slower

Slower

Faster

Around the Speed Limit

Center of Gravity Figure 10. Predicates on the left, consequents on the right. One of the best features of this system is the ease of adding new rules. They are constructed the same way, and the result set included in the union for the solution. You can easily imagine how to add a rule such as "if music is cool, drive faster". Not all rules have the same importance. You can control how much effect each rule has in the operation by multiplying the membership found in the predicate set by a weighting number between 0 and 1. You can also adjust how rules work by modifying the predicate sets-- this process is called tuning the sets. Complex Predicate Sometimes, of course, you have to shift gears to go faster. You would control the transmission with a group of rules like "If the speed is slower than you want to go and the engine rpm is too high, shift up". The word "and" in that statement tells you what to do -- we need to be satisfying both criteria to get a result, so we are asking for the intersection. That will be the lowest membership value found for a particular speed and RPM. The

Fuzzy Primer

Peter Elsea 2/11/14 11

complementary rule would be "if you are slowing down and the rpm too low shift down". There is a third option, which is covered by the rule "if rpm is optimum, do nothing". In addition, there should be rules that force an upshift if rpm is excessive. To summarize the rules for shifting gears:

1. If accelerating and rpm is high, shift up. 2. If decelerating and rpm is low, shift down. 3. If rpm is optimum, do not shift. 4. If rpm is dangerously high, shift up. 5. If rpm is too low, shift down.

The index into {acceleration} and {deceleration} will be based on the difference between two successive speed measurements. Figure 11 shows some likely sets.

Figure 11. The rpm sets will cover the entire range from 0 to the red line as shown in figure 12. Note the bell-like shape of the optimum membership function and the exponential shapes of the danger areas.

Figure 12. Since this code generates choices rather than continuous controls, the result sets are singletons:

Figure 13. Each rule can be thought of as a vote for a particular option. To evaluate the first rule, we take the minimum of the current memberships in {acceleration} and {high rpm} and truncate {shift up} for the result. To evaluate rule 3, we truncate {no shift} to the membership in optimum. The solution set is a union of the various shift options generated. Figure 14 illustrates a simple situation, where only rules 1 and 3 will apply.

Fuzzy Primer

Peter Elsea 2/11/14 12

Figure 14. The result of all of this is a solution set with 0 membership for shift down, 0.7 membership at no shift, and 0.4 membership at shift up. The desired action is simply the option with the highest membership. In case, no shift. The solution set in these situations can be created either by generating a union of al rule results or by adding the memberships of results. The two approaches are subtly different

Effectiveness These techniques and a few others can be used to give some kind of result in nearly any situation. The question is, are these the right results. The following points have a bearing: • A fuzzy system is defined by its rules. If a problem is well understood, a fuzzy model

is relatively easy to construct. If a problem is only partially understood, a fuzzy approximation is easy to expand and refine.

• The sets need to be appropriate. The sets usually need a fair amount of adjustment to tune the system. In some cases, fuzzy systems can learn automatically. They are sometimes combined with neural nets, for instance.

• A working system needs to be monitored. After it is working well, rules should be selectively removed to evaluate their contribution. Generally you want only enough rules to get the job done.

• On the other hand, the system needs to be tested at the limits of stability, to ensure that it will not break catastrophically. Often extra rules are needed to control the worst case scenarios.

The best place to find rules is to look at working models. Fuzzy excels at creating expert systems, where experienced operators are available to describe what they do. This is the point of using fuzzy logic in music: we can be our own experts and build our own systems that work the way we want them to.