1. Introduction and JUnit
Fuzzy Logic
1
1
Fuzzy Expert Systems: Learning Objectives
Exploit fuzziness in data
Create fuzzy rules
Apply fuzzy rules
Adapted from Savich
2
What Can We Expect Machines to Learn?
3
Much of what needs to be learned carries uncertainty.
(e.g., X is a dining room because it contains a table and several chairs)
Fuzzy is a useful approach to uncertainty.
Key motivation for fuzzy: humans’ use of language.
Fuzzy Logic
4
Introduction; Fuzzy Linguistic Variables
Fuzzy Rules
Applying fuzzy rules to crisp input
Demonstration
Applications
Tools
Conclusion
Advantages over Conventional E.S.
Fewer rules
Easier to express
Amenable to parallelism
More robust
As we will see, fuzzy expert systems (“ES”) tend to need fewer rules than conventional ES’s. Fuzzy rules can operate in parallel, and their “fuzziness” can make them less brittle than conventional rules.
Fuzzy Disadvantages
No chaining
No guarantee of stability
Few theoretical results
Verification requires extensive testing
Adapted from Munakata & Jani CACM March 1995
The main disadvantage of fuzzy rules is that they do not facilitate chains of reasoning. The other listed disadvantages concern the fact that they are empirical. However, this is true for almost all machine learning techniques—with the possible exception on Bayesian reasoning. Also, automated chain reasoning from data seems to be a long way off in any case.
6
When to Apply Conventional/Fuzzy
Logically deep: Conventional
e.g. radar repair
e.g. business prognosis with multiple branching
Small number of rules: Fuzzy
e. g., semi-trailer backup
e.g. rough business prognosis
The bullet points contrast examples of conventional vs. fuzzy expert systems. The operator of a semitrailer uses a relatively small set of rules (“If I am in … position and … then I take … action”) rather than a rule that concludes an intermediate statement, and thus facilitates chains of reasoning in which one application of a rule produces results that are then used in another rule application.
Common Visualization of Fuzzy Set S
0
1
Universal set represented here
x
degree to which
x belongs to F
Lotfi Zadeh and students
A set in mathematics is something for which it is possible to state, for anything, whether that thing belongs or not. An example is the set of chairs in Manhattan on January 1, 2017 at 3 pm. The universal set is the set consisting of everything.
Lotfi Zadeh, a prominent electrical engineer, speculated in the 1950’s about the mismatch between the concreteness of sets and how this mismatched the real world. He invented the idea of a fuzzy set (let’s call an example F) for which one can state, for anything, the degree to which that thing belongs to F.
The figure shows a way to visualize a fuzzy set in two dimensions. It imagines the universal set as spread out on the x axis, and a typical element x. The degree to which x belongs to F is represented by a point whose y coordinate is between 0 (equivalent to not belonging at all in the conventional sense and 1 (belonging entirely). Since the sets we will be concerned with involve only numbers, this visualization proves to be adequate.
8
Example: The Fuzzy Set Young
0
1
age
25
The degree to which 25 belongs to Young is 0.7 (70%).
45
0.7
The definition of a fuzzy set depends on the context. For example, in the context of a nursery school application, a 10-year-old would belong the the fuzzy set Young to the degree 0. For an insurance application, the fuzzy set Young might be defined as in the figure. In this particular definition, the number -1 is not considered to belong to this set—more properly, we say that its degree of belonging to Young is zero.
9
Visualization of Fuzzy Sets
0
1
Universal set “spread” out on x-axis
s1 s2 s3 s4
As an exercise in reinforcing the fuzzy idea, the figure concerns a system in which the universal set consists of just four elements, labeled s1, s2, s3, and s4. We imagine them as four positive numbers.
10
Visualization of “S” & “T”
0
1
s1 s2 s3 s4
S
T
This figure shows two fuzzy sets in this space. There is an extensive theory about intersecting fuzzy sets etc. but we will not need most of it for machine learning in this course.
11
Fuzzy Linguistic Variable
A variable
whose values are fuzzy sets
over a common range of numbers.
Example: Temperature,
with values VLo, Lo, Med, Hi, VHi
In fuzzy work, we often work with variables (such as Temperature) whose values are fuzzy sets (such as VeryLow, Low, etc.). This is like a variable s in Java of type String—the values of s are strings. Variables whose values are fuzzy sets are called fuzzy linguistic variables.
12
(Fuzzy Linguistic Variable Temperature)
One of its Values: High
1
0C
50C
80C
60C
degree to which 60C is High
0
High
Here is a picture of the fuzzy set High, a value of the fuzzy linguistic variable Temperature.
13
v hi
v lo
med
low
hi
Common Fuzzy Values
1
The figure shows the general shape of commonly used fuzzy values.
14
Common Place for Fuzzy Use
Output
pendulum position / velocity
Decision mechanism
Which way to move
e. g.
Model
Input
The layman often imagines that fuzzy expert systems are used when the input or output is fuzzy but this is not generally true: it is the model that expresses fuzziness. Inputs and outputs are more often crisp.
15
Fuzzy Logic
16
Introduction; Fuzzy Linguistic Variables
Fuzzy Rules
Applying fuzzy rules to crisp input
Demonstration
Applications
Tools
Conclusion
Now that we have defined fuzzy linguistic variables, we can define rules with them in the same way that we defined expert system rules. Recall that the latter are like
IF strength is >80 units and speed is >70 units THEN recruit is promising.
For classical expert systems, this refers to values of strength and speed—ordinary, (“crisp”) variables. This example carries over easily to fuzzy variables Strength and Speed whose values are fuzzy sets like Fast, Slow, High Low, etc., as explained in this section.
Fuzzy Control Model
Made up of controller rules (model)
IF
AND
…
THEN
The figure shows the form of a fuzzy rule. Notice that we are not allowing for an OR conclusion.
17
Fuzzy Control Model
t
t’
v
t +
+
t’ +
Sign convention
Angle
Rotation speed
The classical example of a fuzzy rules set is one that balances a stick (formally referred to as an “inverted pendulum”). Three fuzzy variables are involved:
Angle (i.e., with the vertical), shortened to t,
Angular Velocity (e.g., measured in degrees per second), shortened to t’, and
Velocity (e.g., measured in degrees per second), shortened to t’.
. The figure shows the directions that are taken as positive and negative.
18
Fuzzy Control Model
t
t’
Example:
IF t is Positive Large AND t’ is Negative Large
THEN v Positive Large
v
t +
+
t’ +
The figure shows an example of a fuzzy rule. You can check for yourself that if the pendulum (stick) were leaning to the right and rotating clockwise fast, you would need to move the bottom of the stick to the right at high speed in order to keep it balanced.
19
Truck Backing Example
t = truck angle
w = wheel angle
IF
location left quadrant
& t negative large
& w left medium
THEN
turn wheel clock large
+
left quadrant
left
back
front
–
right
The idea of fuzzy linguistic rules codifies useful rules-of-thumb such as those used when backing up a truck to a loading bay—or even more complex ones such as backing up a semi-trailer.
20
Pendulum Values for t and t’
NL Negative Large
NM Negative Medium
NS Negative Small
ZE Zero
PS Positive Small
PM Positive Medium
PL Positive Large
The fuzzy values of one fuzzy linguistic variable are not necessarily the same as those of another; however, they often have names such as “Large.” The “Large” fuzzy value for one variable is unlikely to be the same as for another, though.
In the inverted pendulum example, t and t’ share values with the same names, as in the figure. Positive Small, for example, has different for t as for t’, however.
21
Fuzzy Value Example: Zero (ZE)
-20o
20o
1
Consider the (fuzzy set) value Zero of a Temperature variable. Certainly, 0o qualifies fully as belonging to Zero. On the other hand, 50o almost certainly does not. One version of Zero is shown in the figure.
22
Pendulum Fuzzy Associative Memory (FAM)
for Velocity (of base)
ZE
NS
PS
NM
NL
PM
PL
NM
PM
NS
NM
NL
PS
PM
PL
t
t’
NL
NM
NS
ZE
PS
PM
PL
NL
NM
NS
ZE
PS
PM
PL
t
t’
v
+
+
+
Example shown
The rules of a fuzzy expert system with two variables can be conveniently expressed by a matrix, as in the figure. For example, the rule PM is “if t is Positive Small and t’ is Negative Small (i.e., counter-clockwise), then velocity is Positive Medium.” This particular rule is illustrated in the figure.
23
Uncertainty and Fuzzy Logic
24
Introduction; Fuzzy Linguistic Variables
Fuzzy Rules
Applying fuzzy rules to crisp input
Demonstration
Applications
Tools
Conclusion
We can express fuzzy rules. In this section, we will describe what a fuzzy expert system does with an input. The inputs will be assumed to be an ordinary numbers. The term crisp is used to emphasize this.
Applying “A => B” to Input: Example
A: Temp == Medium
B: Alert level == Normal
0
0
Medium
Normal
100
Imagine a rule for which the input (the IF part) is Temperature and the output (THEN part) is Alert Level on a scale from 0 to 100.
25
Implementing “A => B”: Example
Given: furnace temp 90o …..
90o
Consequent value via rule?
Temp
Alert level
0
Medium
Normal
0o
100
Suppose that we have the rule “IF Temp == Medium THEN Alert Level == Normal.” (We are using ‘==‘ to emphasize that the variable has the value, although we usually use just “=“.)
Suppose that, in addition to this rule, we also know that the temperature is measured at 90o. The question is what does the fuzzy system do with crisp input?
26
Implementing “A => B”
Given: a crisp value in range of A
a
A
B
Fuzzy consequence of a
degree of A
degree of B
When a crisp value a is observed, it conditions the premise A (IF part); in other words a and A determine the extent to which the premise is true. We are asking to what extent is B true? It is natural to say that if A implies B and A is true some particular extent then B is true to the same extent. That is what the diagram shows in red—the extent to which the fuzzy set B is true.
27
Implementing “A => B”
Rule’s consequence of 90o
alert level
90o
Temp
Alert level
Medium
Normal
100
The consequence of the input 90o is thus the fuzzy value shown in red. It is probably not equivalent to any single name like “large,” “somewhat large” etc. but it is nevertheless well-defined.
28
Implementing A&B => C
IF t = PS AND t’ = ZE THEN v = NS
t = 15
t’ = -10
The figure shows the consequence (in red) of
The rule IF t = PS AND t’ = ZE THEN v = NS
The input t = 15, and
The input t’ = -10
The lower of the two horizontal lines isusually chosen.
29
Summing Consequents
Applying rules to input thus produces a set of fuzzy (set) values—often trapezoidal in shape. What we actually want is a concrete (“crisp”) result. This is usually done by treating the fuzzy sets as, in effect, metal plates* and selecting as the crisp value, the center of gravity (“centroid”) of the result.
* Plates with uniform mass, in fact
30
Summing Consequents
Center of gravity of combination
To find the center of gravity of a set of shapes, you can replace each with a weighted point at its own center of gravity. Then you find the weighted average of the point weights. This is demonstrated in the figure.
31
Fuzzy Logic
32
Introduction; Fuzzy Linguistic Variables
Fuzzy Rules
Applying fuzzy rules to crisp input
Demonstration
Applications
Tools
Conclusion
http://www.cs.dartmouth.edu/~spl/publications/fuzzy%20talk/FuzzyPendulum.html
The figure shows the rules (in green) that provide nonzero contribution to the velocity of the base (actually, the torque, which is equivalent here). You can select a single rule—Zero and Zero implies Zero in the figure—that shows the way that rule contributes.
The pendulum is “knocked” now and then to get it going.
33
Inverted Pendulum
34
The figure shows essentially the same example. The current status of the rule Theta (we called it t) NM (negative medium) AND dTheta (we called this t’) Z (zero) IMPLIES Current (equivalent to velocity) PM (positive medium).
35
On-Line Demo’s
Fuzzy Shower demo – java only code (simple shower control simulation)
Fuzzy Shower demo – Java/Jess hybrid code (simple shower control simulation)
Fuzzy Compiler demo – java only code (tabular output of a set of fuzzy rules run over a range of inputs)
Fuzzy Truck demo – java only code (park a truck using fuzzy rules)
The are various demonstrations of fuzzy control online, as in the figure, although many required applet capability which browsers generally don’t support. These are good for visualization, although our main interest for machine learning is to extract fuzzy rule so that they can be used for and to explain predictions base on the data.
36
Fuzzy Logic
37
Introduction; Fuzzy Linguistic Variables
Fuzzy Rules
Applying fuzzy rules to crisp input
Demonstration
Applications
Tools
Conclusion
Rule 1: If protein is low and energy is low
then feed is high
Rule 2: If protein is low and energy is high then feed is low
Rule 3: If protein is high and energy is low then feed is high
Rule 4: If protein is high and energy is high then feed is low
http://cals.arizona.edu/AREC/fuzzy/example.html
Example: Supplemental Feeding for Range Cows
The figure shows fuzzy rules for a feedstock application. They are understandable.
38
Example: Power System Peak Load Forecasting
Forecasts daily load curve’s two minima and two maxima, for each season
Tested using historical load and temperature data of Greek interconnected power system.
Results: Can forecast future loads with accuracy comparable to neural networks,
Can also incorporate classic rules and expert’s opinion.
A fuzzy expert system for peak load forecasting. Application to the Greek power system; Kiartzis, S.J.; Bakirtzis, A.G.; Theocharis, J.B.; Tsagas, G.Electrotechnical Conference, 2000. MELECON 2000. 10th Mediterranean Volume 3, Issue , 29-31 May 2000 Page(s): 1097 – 1100 vol.3
The example outlined in the figure supports the advantages of gleaning explainable rules from data. There have been many applications since that time.
39
Fuzzy Logic
40
Introduction; Fuzzy Linguistic Variables
Fuzzy Rules
Applying fuzzy rules to crisp input
Demonstration
Applications
Tools
Conclusion
There are several tools available to process fuzzy rules once they have been extracted. This section will outline their form.
Scikit-fuzzy Python API
41
https://media.readthedocs.org/pdf/scikit-fuzzy/stable/scikit-fuzzy.pdf
42
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
age = ctrl.Antecedent(np.arange(0, 100, 1), ‘age’)
height = ctrl.Consequent(np.arange(0, 95, 1), ‘height’)
# Fuzzy
age[‘low’] = fuzz.trimf(age.universe, [2, 4, 8])
height[‘small’] = fuzz.trimf(height.universe, [15, 30, 35])
age.view()
height.view()
# Define a rule
rule1 = ctrl.Rule(age[‘low’], height[‘small’])
control = ctrl.ControlSystem([rule1])
control_simulation = ctrl.ControlSystemSimulation(control)
control_simulation.input[‘age’] = 5
control_simulation.compute()
height.view(sim=control_simulation)
Example (Ruoting Wang)
Scikit Fuzzy Example
43
# Based on
# https://pythonhosted.org/scikit-fuzzy/auto_examples/plot_tipping_problem_newapi.html
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
# Fuzzy variables quality, … defined
quality = ctrl.Antecedent(np.arange(0, 11, 1), ‘quality’)
…
https://pythonhosted.org/scikit-fuzzy/auto_examples/plot_tipping_problem_newapi.html
Scikit Fuzzy Example
44
# Based on
# https://pythonhosted.org/scikit-fuzzy/auto_examples/plot_tipping_problem_newapi.html
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
# Fuzzy variables quality, service, and tip are defined
quality = ctrl.Antecedent(np.arange(0, 11, 1), ‘quality’) # FLV quality has only 1 value
service = ctrl.Antecedent(np.arange(0, 11, 1), ‘service’)
tip = ctrl.Consequent(np.arange(0, 26, 1), ‘tip’)
# Auto-membership function options 3, 5, or 7
quality.automf(3) # on [0, 11] creates standard ‘low’, ‘medium’, and ‘high’
service.automf(3)
Scikit Fuzzy Example
45
quality.automf(3) # on [0, 11] creates standard ‘low’, ‘medium’, and ‘high’
46
# Custom fuzzy values for FLV tip
tip[‘low’] = fuzz.trimf(tip.universe, [0, 0, 13])
tip[‘medium’] = fuzz.trimf(tip.universe, [0, 13, 25])
tip[‘high’] = fuzz.trimf(tip.universe, [13, 25, 25])
rule1 = ctrl.Rule(quality[‘poor’] | service[‘poor’], tip[‘low’])
rule2 = ctrl.Rule(service[‘average’], tip[‘medium’])
rule3 = ctrl.Rule(service[‘good’] | quality[‘good’], tip[‘high’])
# Control system consisting of these rules
tipping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
# Ready to run
tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
47
# Example
tipping.input[‘quality’] = 6.5
tipping.input[‘service’] = 9.8
# Calculate output
tipping.compute()
print(tipping.output[‘tip’])
tip.view(sim=tipping)
Executing
48
# Example
tipping.input[‘quality’] = 6.5
tipping.input[‘service’] = 9.8
# Calculate output
tipping.compute()
print(tipping.output[‘tip’])
tip.view(sim=tipping)
19.8…
FuzzyJ API
public class FuzzyRule
extends java.lang.Object
A FuzzyRule holds three sets FuzzyValues for the antecedents, conclusions and input values of a rule. A rule might be written as follows:
if antecedent1 and antecedent2 and … antecedentn then conclusion1 and conclusion2 and … conclusionm
http://rorchard.github.io/FuzzyJ/
FuzzyRule is a class as described, part of the FuzzyJ library.
49
Example:
if temperature is hot
then pressure is low or medium
FuzzyJ Example 1 http://www.iit.nrc.ca/IR_public/fuzzy/fuzzyJDocs/FuzzyRule.html
Rule:
: If antecedent1 and
antecedent2 and
…
antecedentn
then
consequent and
consequent2 and
…
consequentm
consequentn allows or’s
Recall that the values of the variables (antecedents and consequents—called “conclusions” here).
50
FuzzyJ Example 2 http://rorchard.github.io/FuzzyJ/
// some values used to describe the fuzzy terms in the temperature FuzzyVariable
double xHot[] = {25, 35};
double yHot[] = {0, 1};
double xCold[] = {5, 15};
double yCold[] = {1, 0};
// define our temperature FuzzyVariable with terms hot,
// cold, very hot and medium
FuzzyVariable temp = new FuzzyVariable(“temperature”, 0, 100, “C”);
temp.addTerm(“hot”, xHot, yHot, 2);
temp.addTerm(“cold”, xCold, yCold, 2);
temp.addTerm(“veryHot”, “very hot”);
temp.addTerm(“medium”, “(not hot and (not cold))”);
The values of a fuzzy variable are fuzzy sets. In FuzzyJ, they are represented in shorthand. For example, hot is a value of the fuzzy variable temp. The fuzzy set hot is the trapezoid with corners (25, 0) and (35, 1) in the xy plane.
51
FuzzyJ Example 3 http://rorchard.github.io/FuzzyJ/
// define our pressure FuzzyVariable with terms low, medium and high
FuzzyVariable pressure =
new FuzzyVariable(“pressure”, 0, 10, “kilo-pascals”);
pressure.addTerm(“low”, new ZFuzzySet(2.0, 5.0));
pressure.addTerm(“medium”, new PIFuzzySet(5.0, 2.5));
pressure.addTerm(“high”, new SFuzzySet(5.0, 8.0));
FuzzyJ has classes that facilitate the definition of fuzzy sets in pre-formed shapes such as ZFuzzySet (a flattened z-shape).
52
FuzzyJ Example 4 http://rorchard.github.io/FuzzyJ/
// build a rule —
FuzzyRule rule1 = new FuzzyRule();
FuzzyValue antecedentFval = new FuzzyValue(temp, “hot”);
FuzzyValue conclusionFval = new FuzzyValue(pressure, “low or medium”);
FuzzyValue inputFval = new FuzzyValue(temp, “very medium”);
rule1.addAntecedent(antecedentFval);
rule1.addConclusion(conclusionFval);
rule1.addInput(inputFval);
Given fuzzy linguistic variables, it becomes possible to create rules as in the figure.
53
FuzzyJ Example 5 http://rorchard.github.io/FuzzyJ/
// execute this simple rule with a single antecedent and
// a single consequent using default rule executor —
// MamdaniMinMaxMinRuleExecutor
FuzzyValueVector fvv = rule1.execute();
// show the results using the plotting methods for FuzzyValues
FuzzyValue fvals[] = new FuzzyValue[2];
fvals[0] = antecedentFval;
fvals[1] = inputFval;
System.out.println(FuzzyValue.plotFuzzyValues(“*+”, 0, 50, fvals));
System.out.println(fval2.plotFuzzyValue(“*”, 0, 10));
System.out.println(fvv.fuzzyValueAt(0).plotFuzzyValue(“*”, 0, 10));
FuzzyJ includes tools to inspect the operation of individual rules.
54
Fuzzy Logic
55
Introduction; Fuzzy Linguistic Variables
Fuzzy Rules
Applying fuzzy rules to crisp input
Demonstration
Applications
Tools
Conclusion
Number of Applications
1985 8
1987 15
1988 50
1989 100
1990 150
1991 300
1992 800
1500
2007
The number of applications worldwide became essentially uncountable in 2007.
56
Example of Recent Application
57
Evaluating the Service Quality of Hospital Using Topsis with Interval Type-2 Fuzzy Sets
2017 International Conference on Fuzzy Theory and Its Applications
The ease with which fuzzy rules can be formulated continues to encourage their use and formulation from data. The figure shows a recent application.
57
Impact
Apps require fewer specifics from users – adjust automatically with fuzzy logic
washing machines
cameras
transportation
Engineering of more inexact applications possible
automated car steering and/or warning
applications to safety
The impact of fuzzy applications has waxed and waned over the past decade.
58
Summary
Introduce fuzzy linguistic variables
Define fuzzy rules
Use fuzzy expert systems where possible
When fuzzy rules can be obtained and used, their operation is easy to understand.
tip_adviser.py
# Based on
# https://pythonhosted.org/scikit-fuzzy/auto_examples/plot_tipping_problem_newapi.html
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
# Fuzzy variables quality, service, and tip are defined
quality = ctrl.Antecedent(np.arange(0, 11, 1), ‘quality’) # FLV quality has only 1 value
service = ctrl.Antecedent(np.arange(0, 11, 1), ‘service’)
tip = ctrl.Consequent(np.arange(0, 26, 1), ‘tip’)
# Auto-membership function population is possible with .automf(3, 5, or 7)
quality.automf(3) # on [0, 11] creates standard ‘low’, ‘medium’, and ‘high’
service.automf(3)
# Custom fuzzy values for FLV tip
tip[‘low’] = fuzz.trimf(tip.universe, [0, 0, 13])
tip[‘medium’] = fuzz.trimf(tip.universe, [0, 13, 25])
tip[‘high’] = fuzz.trimf(tip.universe, [13, 25, 25])
rule1 = ctrl.Rule(quality[‘poor’] | service[‘poor’], tip[‘low’])
rule2 = ctrl.Rule(service[‘average’], tip[‘medium’])
rule3 = ctrl.Rule(service[‘good’] | quality[‘good’], tip[‘high’])
# Control system consisting of these rules
tipping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
# Ready to run
tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
# Example
tipping.input[‘quality’] = 6.5
tipping.input[‘service’] = 9.8
# Calculate output
tipping.compute()
print(tipping.output[‘tip’])
tip.view(sim=tipping)
/docProps/thumbnail.jpeg