“Simplicity
is the hallmark of excellent.” - LD
~~~//~~~
Simpler
method may not the best way to solve every problem. But certainly, it
gives and an alternative solution that work well for certain situation like fuzzy logic.
It is an interesting building block of technical tools to solve an engineering problem.
gives and an alternative solution that work well for certain situation like fuzzy logic.
It is an interesting building block of technical tools to solve an engineering problem.
There are many applications of fuzzy logic in different fields. One of them is an implementation of fuzzy sets in a basic Fuzzy Logic System to maintain room temperature at a certain set point (python code will discuss later on).
Normally, a
target or set point temperature is entering as command to FL controller. The
system then checks temperature sensor at some time interval to verify
temperature different - error, and rate of temp change - error rate (very
similar to P-proportional and D-derivative in conventional PID controller).
Depend on how fuzzy rules activate and its defuzzification outcome, the output
signal is going to be the percentage of power sending to either heater or fan in
attempt to bring room temperature closer to set point temperature.
The Fuzzy
Logic Algorithm:
1. Define linguistic variables {Neg, Zero, Pos}, {cool, warm,
hot}, etc.
of input and output fuzzy sets.
of input and output fuzzy sets.
2. Map out the membership function using
triangle or trapezoid shapes
(Domain : x ==> Function : f(x) ==> Range : y ----- as in math).
(Domain : x ==> Function : f(x) ==> Range : y ----- as in math).
3. Define the rule base structure (IF-THEN
block) and rule matrix.
4. Convert crisp input data to
membership values using membership
functions – Fuzzification step i.e.
(Temp : x ==> Triangle/Trapezoid function : f(x) ==> [0,1] interval : y).
functions – Fuzzification step i.e.
(Temp : x ==> Triangle/Trapezoid function : f(x) ==> [0,1] interval : y).
5. Activate rules from the rule base to
get degree of membership outputs.
6. Clipped output fuzzy sets with these membership
values.
7. Accumulate the clipped output fuzzy
set of each rule.
8. Convert aggregated fuzzy sets
(clipped outputs) to crisp output
number – Defuzzification (center of gravity, weighted average, ...)
number – Defuzzification (center of gravity, weighted average, ...)
Fuzzy variables {Negative: N, Zero: Z, Positive: P} will be selected to span the domain of two inputs error and error rate of temperature. These are more generic names. They are also meaningful when using with cooler or evaporator element instead of heater unit – we only care about error and error rate (positive, zero, or negative values).
This idea applies just about every situation you encounter in time dependent controller. It took science for a while to develop the
PID controller that is widely
used in control engineering.
used in control engineering.
For output
fuzzy set, the variables are going to be {Cool: C, No Change: NC,
Heat: H}. The domain will cover -100 to zero to 100 percent of power. One
hundred percent is the normal power output range for most system.
Heat: H}. The domain will cover -100 to zero to 100 percent of power. One
hundred percent is the normal power output range for most system.
NOTE:
In designing a FL system, it is
best just to draw membership diagram first. You start out with three 50%
overlapped triangle fuzzy sets for each input and output. The labeling of fuzzy
variables and their domain will follow. You would be surprised how fast
everything falls in place in a short time.
A picture is worth a thousand words.
In this case, we have just two inputs and one outputs. With three diagrams, you
can switch back and forth between those, adjusting or modifying until it makes
technical sense. Your thought process will be clearer, and the design will take
on a more concrete form faster.
Another thing worthwhile to mention is the use of
membership interval [0, 1] and the overlapping of fuzzy sets (the cornerstone
ideas that a lot of texts lacking in explaining. Fuzzy logic is not possible
without them).
In mathematics or multivariate conditions, there is a
need to use dimensionless scale to compare two or more variables. There are many applications for this
fundamental technique. Such as the standard scores, also known as z-values or
t-scores convert measures on different units to a common scale, enabling
further analysis such as mahalanobis distance, PCA (principle component
analysis), SVD (singular value decomposition).
The [0, 1] membership interval establishes a common
denominator of scale for fuzzy
sets of input and output, enabling comparisons and combinations of those
membership values to a meaningful result.
The overlapping between fuzzy variables is also
another fundamental concept that provide smooth transition between states. Most
species, materials, or conditions would not endure well with an abrupt changed
from hot to cold or vice versa. Nature play favoritism to this phenomenon under
normal condition.
“My crystal ball is fuzzy” ― Lotfi A. Zadeh, reflects reality
change in the gray or fuzzy area before reaching some equilibrium state as he
develops fuzzy logic.
Below are the two inputs err, errRate
and one output outPower fuzzy sets. The result of power output shows at
lower right figure. The computed value uses RSS and COG methods.
Input
variables define:
Fuzzy sets = {Negative: N, Zero: Z, Positive: P}
err
= Target Temp – Sensor Temp (sampled at some time interval)
Neg
= hot room, Zero = on target temp, Pos = cold room
errRate
= err/ ΔT (error
rate of change at certain time interval)
Neg = room getting cool, Pos = room getting hot
Zero = no change in room
temperature
Output
variables define:
Fuzzy sets = {Cool: C, No Change: NC, Heat: H}
outPower
= C – activate
cooling , NC – no change
H – activate
heating
The
resolution to 1 degree would be good enough for input err. So an interval of 4 is reasonable for each variable domain.
Temperature
different when the system first start may be around 10 to 15 degree. Input errRate domain of ten for each fuzzy
set could be used.
Since one
critical component is the temperature sensor. You should do a quick check with some sensors on the
market to make sure it fit the design intent, look like several inexpensive
ones available that can detect 1 degree different - Always do a reality check
on important area or data.
Fuzzy Rules
Define:
The basic FL
system has 2 inputs (x). Each has three fuzzy set regions (L). Just like in DOE – factorial design, the number of combination is LX = 32 =
9 (All rules are not always utilized, depending on actual practice).
The rule
base IF condition THEN conclusion format is used to describe
relationship between sets. Antecedent block is the condition of inputs. The resulting command output is the consequent block.
1. IF err = Neg AND errRate = Neg THEN
outPower = Cool
2. IF err = Zero
AND errRate = Neg THEN outPower = Heat
3. IF err = Pos AND errRate = Neg THEN
outPower = Heat
4. IF err = Neg AND errRate = Zero THEN
outPower = Cool
5. IF err = Zero
AND errRate = Zero THEN outPower = No Change
6. IF err = Pos AND errRate = Zero THEN
outPower = Heat
7. IF err = Neg AND errRate = Pos THEN outPower = Cool
8. IF err = Zero
AND errRate = Pos THEN
outPower = Cool
9. IF err = Pos AND errRate = Pos THEN
outPower = Heat
Rule Matrix
– a more compact table format of outPower result:
outPower result is at the intersection of row
and column inputs.
Fuzzy Set
Operations Define:
A AND B = min (A, B) = A ∩ B (intersection)
A AND B = min (A, B) = A ∩ B (intersection)
A OR B = max
(A, B) =
A ∪
B (union)
Ā = 1 – A (complement of
A)
Example
case of err = 1.0, errRate = -2.5 :
Remember we have to convert the two input values to membership number (normalized data to a common scale) before any attempt to do comparison or computation. From err and errRate graphs we have:
Remember we have to convert the two input values to membership number (normalized data to a common scale) before any attempt to do comparison or computation. From err and errRate graphs we have:
err = 1.0 => err_Z = 0.5, err_P = 0.5
errRate = -2.5 => errRate_N = 0.5, errRate_Z = 0.5
Using rule
base section to activate all rules:
rule1 = min (err_N = 0.0 , errRate_N
= 0.5) ==> outPower_C = 0
rule2 = min (err_Z = 0.5 , errRate_N = 0.5) ==> outPower_H = 0.5
rule3 = min (err_P = 0.5 , errRate_N = 0.5) ==> outPower_H = 0.5
rule4 = min (err_N = 0.0 , errRate_Z = 0.5) ==> outPower_C = 0
rule2 = min (err_Z = 0.5 , errRate_N = 0.5) ==> outPower_H = 0.5
rule3 = min (err_P = 0.5 , errRate_N = 0.5) ==> outPower_H = 0.5
rule4 = min (err_N = 0.0 , errRate_Z = 0.5) ==> outPower_C = 0
rule5 = min
(err_Z = 0.5 , errRate_Z
= 0.5) ==> outPower_NC = 0.5
rule6 = min
(err_P = 0.5 , errRate_Z
= 0.5) ==> outPower_H = 0.5rule7 = min (err_N = 0.0 , errRate_P = 0.0) ==> outPower_C = 0
rule8 = min (err_Z = 0.5 , errRate_P = 0.0) ==> outPower_C = 0
rule9 = min
(err_P = 0.5 , errRate_P
= 0.0) ==> outPower_H = 0
-----------------------------------------------------------------------
Root Sum Square (a statistical tolerance or error analysis method) is used to aggregate same fuzzy set membership outputs in some of the calculations:
outPower _C = (rule1**2 + rule4**2 + rule7**2 + rule8**2)**0.5
outPower _NC = (rule5**2)**0.5
outPower _H
= (rule2**2 + rule3**2 +
rule6**2 + rule9**2)**0.5
Either membership output values come from firing all rules or from RSS method, the results will be used to clip output fuzzy sets. After that, the aggregated clipped fuzzy sets are used for centroid calculation to get crisp value - defuzzification.
-----------------------------------------------------------------------
Either membership output values come from firing all rules or from RSS method, the results will be used to clip output fuzzy sets. After that, the aggregated clipped fuzzy sets are used for centroid calculation to get crisp value - defuzzification.
-----------------------------------------------------------------------
We have discussed so far the same process for both Mamdani and Takagi-
Sugeno-Kang Fuzzy
Inference Systems. Next, we will go over each process
in the final computation that set them apart – even though both
results should
be close.
Mamdani Fuzzy Inference Systems:
Mamdani Fuzzy Inference Systems:
Mamdani inference requires finding the centroid of a 2-D shape.
It is more
intuitive to use and widely accepted.
We will use two methods:
Center of Gravity or Weighted Average – Center of individual areas.
Centroid of Area – Centroid of composited-continuous areas.
Interesting Observations:
Center of things is a curious attribute-property that human, science, and nature always search for, or gravitate to. You can see it in the central limit theorem,
mean or center of bell curve, black hole at center of galaxy, regression line etc. ,
E = M*C**2 the amount of energy a mass need to reach critical transition state between energy and matter.
And of course, weighted average formula describes the distribution of mass about a balancing point (moment in physic). Fuzzy logic in a way describe the fuzzy crossing zone between sets.
One may want to throw in the question of, where is the center of the soul or center of being? Maybe a scientist, theologian, or philosopher figure it out?
There are four "python programs" included using this technique:
FuzzyControl.py – uses new API of scikit-fuzzy (a.k.a. skfuzzy).
This offers the cleanest code work flow, but lacking granular control.
FuzzyCTRL.py – utilizes regular API skfuzzy, it is follow the Mamdani Fuzzy Inference closely. We will explain it in detail with comments along the way.
FuzzyRSS_COG.py – utilizes RSS and centroid of combined-continuous areas to calculate power output percentage.
FuzzyRSS_WA.py – uses RSS and weighted average of discrete areas to calculate percentage of power output.
FuzzyCTRL.py – utilizes regular API skfuzzy, it is follow the Mamdani Fuzzy Inference closely. We will explain it in detail with comments along the way.
FuzzyRSS_COG.py – utilizes RSS and centroid of combined-continuous areas to calculate power output percentage.
FuzzyRSS_WA.py – uses RSS and weighted average of discrete areas to calculate percentage of power output.
Takagi-Sugeno-Kang Fuzzy Inference Systems:
TSK output membership functions are either linear or constant. It is useful in dynamic nonlinear systems to provide smooth transition by interpolating multiple linear models or weighting membership function overlap. TSK method is more computational efficient.
A typical rule in a TSK fuzzy model has the form:
If Input_1 = x and Input_2 = y, then Output is z = ax + by + c (first order).
i.e. x = err, y = errRate => outPower = a*err + b*errRate + c
For a zero-order TSK model, the output level z is a constant (a = b = 0).
The python program is “FuzzyTSK.py”. The power output result is using weighted average method with singletons. Values come from the center of each output fuzzy set, two of them can be the outer edges of the trapezoids. RSS method is used to calculate membership output values.
You can download and install python software from "python.org" website. It is open source and widely used in machine learning and data analytics among other things. Source codes are hosted on "github.com/LSW1980/FuzzyZones". Below is the code and comments of FuzzyCTRL.py - It follows standard Mamdani Fuzzy Inference method :
----------------------------- PYTHON SESSION -------------------------------
# import all modules need
import matplotlib.pyplot as plt
import numpy as np, skfuzzy as fuzz
from skfuzzy import control as ctrl
# define domain interval and resolution
# of fuzzy sets inputs-output
x_err = np.arange(-4,4, 0.1)
x_errRate = np.arange(-10,10, 0.1)
x_outPower = np.arange(-100,100, 0.1)
# define fuzzy set shapes or membership
functions
err_N = fuzz.trapmf(x_err,[-4,-4,-2,0])
err_Z = fuzz.trimf(x_err,[-2,0,2])
err_P = fuzz.trapmf(x_err,[0,2,4,4])
errRate_N = fuzz.trapmf(x_errRate,[-10,-10,-5,0])
errRate_Z = fuzz.trimf(x_errRate,[-5,0,5])
errRate_P = fuzz.trapmf(x_errRate,[0,5,10,10])
outPower_C = fuzz.trapmf(x_outPower, [-100,-100,-50,0])
outPower_NC = fuzz.trimf(x_outPower, [-50,0,50])
outPower_H = fuzz.trapmf(x_outPower, [0,50,100,100])
# Visualize membership functions
---------------------
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2,figsize = (11, 7))
ax1.plot(x_err, err_N, 'b', lw=2, label='err_N')
ax1.plot(x_err, err_P, 'r', lw=2, label='err_P')
ax1.plot(x_err, err_Z, 'orange', lw=2, label='err_Z')
ax1.set_xlabel('err', fontsize=10)
ax1.set_ylabel('Membership', fontsize=10)
ax1.set_title('Temp Different
Fuzzy Sets')
ax1.legend()
ax2.plot(x_errRate, errRate_N, 'b', lw=2, label='errRate_N')
ax2.plot(x_errRate, errRate_P, 'r', lw=2, label='errRate_P')
ax2.plot(x_errRate, errRate_Z, 'orange', lw=2,label='errRate_Z')
ax2.set_xlabel('errRate', fontsize=10)
ax2.set_ylabel('Membership', fontsize=10)
ax2.set_title('Rate of Temp
Change Fuzzy Sets')
ax2.legend()
ax3.plot(x_outPower, outPower_C, 'b', lw=2, label='outPower_C')
ax3.plot(x_outPower, outPower_H, 'r', lw=2,label='outPower_H')
ax3.plot(x_outPower, outPower_NC, 'orange', lw=2,label='outPower_NC')
ax3.set_xlabel('outPower', fontsize=10)
ax3.set_ylabel('Membership', fontsize=10)
ax3.set_title("outPower
Fuzzy Sets")
ax3.legend()
plt.tight_layout(pad=0.4, w_pad=2, h_pad=2)
#------------------------------------------------------------
# err, errRate = eval(input("Enter 'err, errRate' values: "))
# err, errRate = eval(input("Enter 'err, errRate' values: "))
# specifying crisp input values
err, errRate = 1.0, -2.5
# interpolate to get membership values
# plot vertical dashed line at output value
ax4.plot([powerLevel, powerLevel], [0, 1], 'm--', lw=3,
---------------------------------------------------------------------------
You can change err, errRate input values to get different power output percentage result. Print command should be modified with parentheses to run in python 3.x version. Other programs are included with detail comments easy to follow.
~~~//~~~
References:
----------------------------------------------------------------------------
# of all fuzzy sets from above inputs
im_err_N =
fuzz.interp_membership(x_err, err_N, err)
im_err_Z =
fuzz.interp_membership(x_err, err_Z, err)
im_err_P = fuzz.interp_membership(x_err, err_P, err)
im_errRate_N =
fuzz.interp_membership(x_errRate, errRate_N, errRate)
im_errRate_Z =
fuzz.interp_membership(x_errRate, errRate_Z, errRate)
im_errRate_P =
fuzz.interp_membership(x_errRate, errRate_P, errRate)
# activate Antecedent-input of all
rules
# to get Consequent-output membership
values
R1 = min(im_err_N , im_errRate_N) # --> outPower_C
R2 = min(im_err_Z , im_errRate_N) # --> outPower_H
R3 = min(im_err_P , im_errRate_N) # --> outPower_H
R4 = min(im_err_N , im_errRate_Z) # --> outPower_C
R5 = min(im_err_Z , im_errRate_Z) # --> outPower_NC
R6 = min(im_err_P , im_errRate_Z) # --> outPower_H
R7 = min(im_err_N , im_errRate_P) # --> outPower_C
R8 = min(im_err_Z , im_errRate_P) # --> outPower_C
R9 = min(im_err_P , im_errRate_P) # --> outPower_H
# clipping output fuzzy sets with results from R1 to R9
R1_activate = np.fmin(R1, outPower_C)
R2_activate = np.fmin(R2, outPower_H)
R3_activate = np.fmin(R3, outPower_H)
R4_activate = np.fmin(R4, outPower_C)
R5_activate = np.fmin(R5, outPower_NC)
R6_activate = np.fmin(R6, outPower_H)
R7_activate = np.fmin(R7, outPower_C)
R8_activate = np.fmin(R8, outPower_C)
R9_activate = np.fmin(R9, outPower_H)
# accumulated all clipped areas of each fuzzy
set output
outPower_C = np.fmax(R1_activate, np.fmax(R4_activate,
np.fmax(R7_activate, R8_activate)))
outPower_NC = R5_activate
outPower_H = np.fmax(R2_activate, np.fmax(R3_activate,
np.fmax(R6_activate, R6_activate)))
# aggregated all clipped fuzzy set outputs
clip_aggregated = np.fmax(outPower_C,
np.fmax(outPower_NC, outPower_H))
#Centroid of combined continuous areas - COG
powerLevel = fuzz.defuzz(x_outPower, clip_aggregated,'centroid')
print 'Percent Power
Output: ' , powerLevel
# graphical view of power output -------------------------
# define baseline interval for plotting
# define baseline interval for plotting
x_outPower0 = np.zeros_like(x_outPower)
ax4.plot(x_outPower, clip_aggregated, 'g', lw=2,
label='Clipped Fuzzy
Sets')
# fill centroid area with color
ax4.fill_between(x_outPower,x_outPower0, clip_aggregated,
facecolor='c', alpha=0.6)
ax4.set_xlabel('outPower', fontsize=10)
ax4.set_ylabel('Membership', fontsize=10)
# display inputs, output values in title by formatting
ax4.set_title( "Power output: {:.2f}% , err = {:.1f} , errRate = {:.1f}"
.format(powerLevel, err, errRate) )
ax4.set_title( "Power output: {:.2f}% , err = {:.1f} , errRate = {:.1f}"
# plot vertical dashed line at output value
ax4.plot([powerLevel, powerLevel], [0, 1], 'm--', lw=3,
label='Power Level')
ax4.legend()
plt.show()
---------------------------------------------------------------------------
You can change err, errRate input values to get different power output percentage result. Print command should be modified with parentheses to run in python 3.x version. Other programs are included with detail comments easy to follow.
The result of FuzzyControl.py and
FuzzyCTRL.py should be the same. Comparing with other programs, the power
output will be different due to the formulas and its Fuzzy Inference Systems
implemented. Which one to use depend on actual practice and testing.
The
applications of the fuzzy systems are countless, and in different fields such
as automobile control systems, subway control systems, stocks and funds
prediction.
If some problem can be breakdown to rule base format, then fuzzy
logic can be applied. It can be used in combination with other engineering
tools. Using Fuzzy Inference System to solve temperature control problem is
just one of many.
Future
articles, FuzzyCorner will explore other interesting topics such as uncertainty
and error, lean manufacturing, GR&R, mahalanobis distance and its
application, etc. Nuts-and-bolts engineering will discuss in detail.
~~~//~~~
A Thought
Exercise:
If we have a group of different features (variables).
Is there a way to maximize the net gain effect (useful energy output or maximum
profit) base on their interaction (dependent) or individual feature behaviors
(independent)?
Is the problem solvable or can be formulated
(algorithms) to a certain degree? Surprisingly, come to think of it - people
try to do this in everyday decisions.
This can be applied to DOE, in engineering, lean
methodology, or trading stocks and funds. In all, fuzzy sets cast a shadow over
these domains. May be somebody come up with a universal equation(s) to solve it
all?
References:
Fuzzy Sets paper – Lotfi
A. Zadeh, Nov 16, 1964.
Foundations of fuzzy control:
a practical approach 2nd – Jan Jantzen, 2013.
Fuzzy logic with
engineering applications 4th – Timothy J. Ross, 2017.
Fuzzy Control Systems: The Tipping Problem – from scikit-fuzzy.
The Tipping Problem – The Hard Way – from scikit-fuzzy.
----------------------------------------------------------------------------