kio
TRANSCRIPT
B.Eng. in Electronic Engineering
Final Year Project Report
TITLE
A Genetic Algorithm basedFuzzy Logic Controller
�
�
Malcolm Jeffers April, 2001
ii
Acknowledgements
I would like to thank Jennifer Bruton for her help throughout the term of this project. I amindebted to her, for her thoughtful supervision and clear advice.
My family, friends in EE4, and housemates have also made this an enjoyable task as a resultof both their personal support and the comments that only friends can make.
DeclarationI hereby declare that, except where otherwise indicated, this document is entirely my ownwork and has not been submitted in whole or in part to any other university.
Signed: ...................................................................... Date: ...............................
iii
Abstract
This report investigates a promising method of control engineering, fuzzy logic modelling.It sets out to evaluate the usefulness of genetic algorithms in aiding the control process. Atthe start of the report the ‘Inverted pendulum on a cart’ system is introduced and itscontrollability is studied in detail. The theoretical background to both fuzzy logic andgenetic algorithms is detailed in subsequent chapters and the applications to the test case areunpacked at each stage in the theoretical development. A synopsis of the results obtainedthroughout the report is given in the results section with the intention of guiding futureadvancement of this research.
Though no final genetic algorithm based fuzzy logic controller was implemented, thestrengths of genetic algorithms and fuzzy logic are explained with the express purpose ofproposing how, when combined, a useful and workable method of control may result. Thetesting of each controller in the process of the design has been carefully documentedthroughout the report.
iv
Table Of Contents
Acknowledgements..................................................................................................................ii
Declaration...............................................................................................................................ii
Abstract ...................................................................................................................................iii
Table Of Contents ................................................................................................................... iv
Table of Figures ......................................................................................................................vi
1. Introduction..........................................................................................................................1
2. The Inverted Pendulum Test Case .......................................................................................2
2.1 Why choose the Inverted Pendulum?.........................................................................3
2.2 Test Case Design........................................................................................................4
2.3 The Linear Model.......................................................................................................5
2.4 Controlling the Linear Model ....................................................................................7
2.5 Controlling the Non-Linear Model ..........................................................................13
2.6 Summary..................................................................................................................14
3. Fuzzy Logic........................................................................................................................15
3.1 Fuzzy Basics ............................................................................................................15
3.1.1 Fuzzy Sets, Membership Functions and Logical Operators..............................16
3.1.2 Linguistic Variables and Rule Bases ................................................................18
3.2 Fuzzy Logic Models.................................................................................................21
3.2.1 What is Fuzzy Modelling? ................................................................................21
3.2.2 Mamdani Modelling..........................................................................................22
3.2.3 Sugeno Modelling.............................................................................................25
3.2.4 Applying a Fuzzy Logic Controller to the Test Case........................................26
3.2.5 Summary...........................................................................................................30
v
4. Genetic Algorithms............................................................................................................31
4.1 A Quick Overview ...................................................................................................31
4.1.1 Why Genetic Algorithms? ................................................................................32
4.2 How Genetic Algorithms work................................................................................34
4.2.1 Strength in Numbers .........................................................................................34
4.2.2 Objective and Fitness Functions .......................................................................35
4.2.3 Reproduction, Crossover and Mutation ............................................................36
4.2.4 The Generation Game .......................................................................................39
4.2.5 Simple Genetic Algorithm Example.................................................................41
4.2.6 Combining Genetic Algorithms and Fuzzy Logic ............................................43
4.2.7 Implications for Test Case ................................................................................44
4.2.8 Summary...........................................................................................................46
5. Results................................................................................................................................47
5.1 Results......................................................................................................................47
5.2 Implications to Research..........................................................................................48
6. Conclusion .........................................................................................................................50
6.1 Introduction..............................................................................................................50
6.2 Future Research .......................................................................................................50
References..............................................................................................................................51
Appendix 1.............................................................................................................................53
Appendix 2 ............................................................................................................................54
Appendix 3 ............................................................................................................................55
vi
Table of Figures
Figure 2.1 Cart-pole system.................................................................................................................... 2
Figure 2.2 Matlab code for the cart-pole linearised model ..................................................................... 7
Figure 2.3 Step response of uncontrolled linear model........................................................................... 8
Figure 2.4 Cart-Pole PID control for linearised transfer function........................................................... 9
Figure 2.5 PID – controlled output plot.................................................................................................. 9
Figure 2.6 Cart-Pole PID control with pre-filter ..................................................................................10
Figure 2.7 PID - controlled output plot (model with pre-filter) ............................................................10
Figure 2.8 Altered PID - controlled system output plot ........................................................................11
Figure 2.9 Cart-Pole control with pre-filter ..........................................................................................12
Figure 2.10 PID – controlled system with pre-filter ...............................................................................12
Figure 2.11 PID control for non-linear cart-pole system with pre-filter .................................................13
Figure 2.12 PID – controlled non-linear output plot...............................................................................13
Figure 3.1 Example fuzzy sets ..............................................................................................................17
Table 3.1 Truth tables for the OR and NOT operators ........................................................................16
Figure 3.2 Sample three-part Gaussian shaped MF ..............................................................................16
Figure 3.3 Examples of two-valued and multi-valued logical operations ...........................................17
Figure 3.4 Application of rules for lecture attendance example ...........................................................20
Figure 3.5 Mapping of input space to output space ..............................................................................21
Figure 3.6 Mamdani fuzzy control system............................................................................................22
Figure 3.7 Diagram showing aggregation and defuzzification..............................................................24
Figure 3.8 Implementation of a Sugeno model .....................................................................................25
Figure 3.9 A comparison of conventional PID and fuzzy control solutions .........................................26
Figure 3.10 Cart-Pole PID control for linearised transfer function with pre-filter..................................26
Figure 3.11 Linear model output plots showing error, error derivative and overall output ....................27
Figure 3.12 Cart-Pole fuzzy control for linearised transfer function ......................................................28
Figure 3.13 Visual representation of fuzzy rules ....................................................................................28
Figure 3.14 Resulting system plot for fuzzy controlled linear model .....................................................29
Figure 4.1 Sample three-dimensional search space for ease of visualisation........................................33
Table 4.1 Comparison of Terminology................................................................................................40
Figure 4.2 A sample binary encoded chromosome consisting of a string of 16 bits .............................34
Figure 4.3 Single-point crossover of two chromosomes (k = 4) ...........................................................37
Figure 4.4 Example of Binary Mutation ...............................................................................................38
Figure 4.5 Flowchart of Simple Genetic Algorithm .............................................................................41
Figure 4.6 Resulting plot showing minimisation of De Jong’s first test function .................................42
Figure 4.7 Example of poor membership function overlap...................................................................43
Figure 5.1 PID – controlled system with pre-filter ...............................................................................47
Figure 5.2 PID – controlled non-linear output plot...............................................................................47
Figure 5.3 Linear model output plots, showing the error, error derivative ...........................................48
Figure 5.4 Resulting system plot for fuzzy controlled linear model .....................................................48
1
Chapter 1
1. Introduction
Fuzzy logic has been available as a control methodology for over three decades and its
application to engineering control systems is well proven. In a sense fuzzy logic is a logical
system that is an extension of multi-valued logic although in character it is quite different. It
has become popular due to the fact that human reasoning and thought formation is linked
very strongly with the ways fuzzy logic is implemented. Far – ranging applications exist
including space-rocket control, advanced in-car control systems, and not to mention the
myriad of potential industrial applications. In more recent years the use of fuzzy logic in
combination with neurocomputing and genetic algorithms has become popular in control
system design. The purpose of this amalgamation of methods is to produce systems whose
MIQ (Machine IQ) is considerably higher than those developed using conventional methods.
If this report can in any way advance this branch of control engineering, the work involved
will have been thoroughly worthwhile.
The reader will find that four sections constitute the body of this report. Section one
outlines the cart/pole test case and explains how subsequent research and testing will be
related to it. Sections two and three explain the theoretical background to fuzzy logic and
genetic algorithms respectively and in each of these sections the test case model is
developed to reflect the theory acquired. Section four deals with the more comprehensive
results that stem from the previous research. The conclusion provides a synopsis and some
suggestions for future work in the area are given here also.
2
Chapter 2
2. The Inverted Pendulum Test Case
A common benchmark problem in control theory is that of the inverted pendulum. The
pendulum is a rod of mass m and a hinge connects the rod to a moveable cart. The cart can
move in a horizontal direction only. The outputs of the system are the pendulum angle θ
and the cart position x. The pendulum angle θ is measured from a vertical line and the cart
position x is measured from the centre of the cart track. The movement of the cart is to be
controlled so that that the pendulum remains upright. This implies that at x = 0,
0.
==θθ where.
θ symbolises the time derivative of θ
The system starts with the pendulum standing vertically, i.e. at equilibrium. The challenge
is to implement stabilising control to keep the system at or as close to equilibrium as
possible.
Figure 2.1 Cart-pole system [1]
3
2.1 Why Choose the Inverted Pendulum?
The following reasons help explain why the inverted pendulum on a cart has been selected
as the system on which the findings of this report will be implemented.
1. A progressive model can be built. It is a non-linear system, yet can be approximated
as a linear system if the operating range is small (i.e. slight variations of the angle
from the norm).
2. Intuition plays a large part in the human understanding of the inverted pendulum
model. When the control method is supplemented with a genetic algorithm
optimisation technique that does not benefit from this intuition in Chapter four, the
results will provide an insightful measure of the ability of the method to provide
control.
3. The cart/pole system is a common test case for fuzzy logic so any results can be
compared to previous work in the field.
In order to perform sound criticism of any controllers developed, a reference model must be
designed at the outset of this work. If any testing is worth doing at all it must be planned in
such a way that it has at least a good chance of giving a useful result [2]. A proportional,
integral, derivative (PID) controller will be used as a reference because both the structure of
the controller is simple and the performance is not adversely affected by noise and parameter
variations.
4
2.2 Test Case Design
This section outlines the assumptions, design requirements and physical properties of the
model and its components.
Assumptions:
1. The system starts in a state of equilibrium meaning that the initial conditions
are therefore assumed to be zero.
2. The pendulum does not move more than a few degrees away from the vertical
to satisfy a linear model.
3. Only the angle of the pendulum is to be controlled in this model so the cart’s
position will not be documented.
4. A step input is applied.
Design Requirements:
1. The settling time, ts, is to be less than five seconds. ts < 5secs
2. The system overshoot value is to be at most 22.5%. os < 22.5%
Properties of the Cart and Pendulum:
M – mass of cart 0.5kg
m – mass of pendulum 0.5kg
b – friction or cart 0.1 Nm-1s-1
l – length to pendulum centre of mass 0.3m
I – inertia of the pendulum 0.006 kg m2
F – force applied to cart
x – cart position co-ordinate
θ – pendulum angle from the vertical
5
2.3 The Linear Model
The force analysis shown here yields the linear transfer function for the model.
Summing the horizontal forces on the cart:
FNxbxM =++...
Summing the horizontal forces on the pendulum to get N:
θθθθ sincos 2.....
mlmlxmN −+=
Substituting into above gives the first equation that describes the motion of this system:
θθθθ sincos)( 2......
mlmlxbxmMF −+++=
Summing the forces perpendicular to the pendulum for the second equation of motion:
θθθθθ cossincossin...
xmmlmgNP +=−+ [1]
By summing the moments around the centroid of the pendulum the N and P terms can be
removed:
..
cossin θθθ INlPl =−−
Using the last two equations it can be seen that:
θθθ cossin)(....
2 xmlmglmlI −=++ [2]
The computing package to be used deals well only with linearised models making it
necessary to linearise the model about θ = π . It will be assumed that θ = π + φ where φ
is a small angle from the vertically upward direction. Therefore,
0
sin
1cos
2
=
−=−=
dt
dθ
φθθ
6
After linearisation the two equations of motion ([1], [2]) become (where U is the input):
UmlxbxmM
xmlmglmlI
=−++
=−+.....
....2
)(
sin)(
φ
φθ
To obtain the transfer function of the linearised system it is necessary to first form the
Laplace transform of the equations above:
)()()()()(
)()()()(22
222
sUssmlssbXssXmM
ssmlXsmglssmlI
=Φ−++=Φ−Φ+
Since Φ is to be the output it is helpful to solve for X(s):
)()(
)(2
2
ss
g
ml
mlIsX Φ
−+=
Substituting into the second equation
)()()()(
)()(
)( 22
22
2
2
sUssmlsss
g
ml
mlIbss
s
g
ml
mlImM =Φ−Φ
−++Φ
−++
Rearranging gives:
sv
bmgls
v
mglmMs
v
mlIbs
sv
ml
sU
s
−+−++=Φ
232
4
2
)()()(
)(
Where
Note that there is both a zero and a pole at the origin. Since the discrete time domain is not
being used it is possible to cancel the zero and pole yielding:
v
bmgls
v
mglmMs
v
mlIbs
sv
ml
sU
s
−+−++=Φ
)()()(
)(
22
3
[ ]22 )())(( mlmlImMv −++=
7
2.4 Controlling the Linear Model
The experiment work documented here will take place in the following order:
1. The step response of the linear model given by the transfer function (and constants
specified) above will be plotted.
2. A Proportional-Integral-Derivative (PID) controller will be designed for the linear
model using ITAE and pole placement techniques (explained below) and then this
controller will be modelled in Simulink.
3. The controller will be refined using a pre-filter in order to meet the design criteria.
The Matrix Laboratory package MATLAB has been chosen as the language in which all
programs developed in this report will be written. Matlab is a high-performance language
for technical computing. The choice of Matlab is firstly due to the ease with which
programs can be written as m-files and secondly because of the available links with
Simulink. Simulink is a visual drag-and-drop interface suitable for modelling feedback
control systems.
1. The Matlab code that represents the linearised model is summarised in Figure 2.2. A
full version of the program can be found in Appendix 1.
q = (M+m)*(i+m*l^2)-(m*l)^2;
% used to make equations below more simple
num = [m*l/q 0]den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]
%The constants above yield the following num, denvalues%num = 4.5455 0%den = 1.0000 0.1818 -31.1818 -4.4545
Figure 2.2: Matlab code for the cart-pole linearised model
8
This is the impulse response that results.
Figure 2.3 above shows that within approximately six seconds the system goes very
unstable. The next step of implementing a PID controller will attempt the curb the
instability.
2. To implement a proportional-integral-derivative controller the three numerical values
of P, I and D must be evaluated. Since the root locus of the system does not cross
the jω axis the integral of time multiplied by absolute error (ITAE) method will be
employed.
Using the design conditions of Section 2.2 nω , the undamped natural frequency, is
picked to be equal to 1.85 radians s-1. Following the ITAE design process [3] and
using a step input it is possible to arrive at the following optimum coefficients.
KP = 8.49
KI = 2.37
KD = 0.67
Time (sec.)
Am
plitu
de
Impulse Response
0 1 2 3 4 5 60
2
4
6
8
10
12
14x 1013 From: U(1)
To:
Y(1
)
Figure 2.3 Impulse response of uncontrolled model
9
Using these values it is possible to design the PID controller in Simulink. (Figure 2.4)
The output plot and properties result from this simulation are seen in Figure 2.5 below.
An overshoot of 216% is considerably greater than the design parameter of 22.5%. Even
though the settling time is quite good (5.19s) it is necessary to limit the overshoot.
Figure 2.4 Cart-Pole PID control for linearised transfer function
Figure 2.5 PID – controlled output plot
os = 216.9%
ts = 5.19s
tr = 0.29s
tp = 1.08s
10
To limit this overshoot the ITAE designed PID controller is implemented with an
appropriate pre-filter P(s). The Simulink model for this system is seen in Figure 2.6.
The output plot and properties result from this simulation can be seen in Figure2.5 below.
Figure 2.7 shows that the problem of excess overshoot has been dramatically reduced. This
has been at the expense of the settling time, which is now 12.7s. The way to resolve this
controllability difficulty will be to investigate and use an alternative pole placement
technique.
Figure 2.6 Cart-Pole PID control with pre-filter
0 5 10 15 20 25 300
0.2
0.4
0.6
0.8
1
1.2
1.4Analysis of PID-control for the linear cart-pole model
tout/s
yout/a
mp
os = 0.0085%
ts = 12.7s
tr = 28.4s
tp = 30s
Figure 2.7 PID – controlled output plot (model with pre-filter)
11
The ideal second order transfer function takes the form22
2
2)(
nn
n
sssT
ωζωω
++=
Given that this system has 4318.0=ζ and 85.1=nω rad s-1, the ideal second order transfer
function is:
4225.35976.1
4225.3)(
2
2
++=
sssT with poles at –0.7988 +/- 1.6687j
Choosing a pole whose real part is a factor of six or greater than any existing pole [3], a= -10,
the pole altered second order transfer function of the system now takes the form:
)2)(()(*
22
2
nn
n
ssassT
ωζωω
+++=
So,225.343985.195976.11
4225.3)(*
2 +++=
ssssT
Obtaining optimum coefficients as before yields KP = 11.13, KI = 8.51 and KD = 2.51.
The output plot that results is shown in Figure 2.8.
Although an improvement in overshoot has resulted the design conditions have not yet been
met (os <22.5%). The next section will introduce a pre-filter to aid the design.
os= 88.8%
ts = 5.25s
tr = 0.16s
tp = 0.77s
Figure 2.8 Altered PID – controlled system output plot
12
3. A pre-filter is introduced so that the overall closed loop transfer function will not
have any zeroes. The pre-filter introduced has the form:
IPD
ISTEP
KsKsK
KsP
++=
2)(
51.813.1151.2
51.8)(
2 ++=
sssP STEP
The model and plot that result are given in Figure 2.9 and Figure 2.10 respectively.
The alternative pole-placement technique and additional pre-filter have now enabled both
design criteria to be met for the PID controller. (os < 22.5%, ts <5s)
Figure 2.9 Cart-Pole control with pre-filter
os = 21.6%
ts = 4.64s
tr = 1.32s
tp = 1.99s
Figure 2.10 PID – controlled system with pre-filter
13
2.5 Controlling the Non-Linear Model
In this section an analysis is carried out on how well the designed PID controller copes with
non-linear cart-pole dynamics. The non-linear dynamics represented by a diagram in Figure
2.9 are already packaged as a Simulink function. The underlying model within this block
contains all the cart-pole physical characteristics that have been outlined above. Of the
available output ports only the angle θ is utilised. It is necessary to change the cart/pole
physical properties within this function block to be congruent with the model developed
above. The model (with pre-filter) is shown in Figure 2.11 and the plot that results in Figure
2.12.
Figure 2.11 PID control for non-linear cart-pole system with pre-filter
Figure 2.12 PID – controlled non-linear output plot
14
There is little difference in output characteristic between the model with and the model
without the pre-filter – both fail to control the non-linear system. This inability of the
controller to stabilise the non-linear system was not unexpected. The function of this test
was primarily to show that a more sophisticated method is required.
2.6 Summary
The pole/cart system has been introduced as a suitable non-linear model for study in this
report. The need to provide a reference or benchmark system was outlined and a linearised
model was derived with this objective. A summary of the design steps is given below.
The model’s impulse response was found, confirming its non-linearity.
Optimum PID coefficients were found using the ITAE method.
Due to the fact that the design criteria were not met an alternative pole placement
technique was used find new coefficients to improve the measured overshoot.
Further improvement was gained by designing a pre-filter and a satisfactory model
resulted.
An unsuccessful attempt to control the non-linear system dynamics showed that a
more sophisticated approach is warranted.
15
Chapter 3
3. Fuzzy Logic
3.1 Fuzzy Basics
The primary objective of fuzzy logic is to map an input space to an output space. The way
of controlling this mapping is to use if-then statements known as rules [4]. The order these
rules are carried out in is insignificant since all rules run concurrently. The following
sections will present and develop ideas such as sets, membership functions, logical
operators, linguistic variables and rule bases.
3.1.1 Fuzzy Sets, Membership Functions and Logical Operators
Fuzzy sets are sets without clear or crisp boundaries. The elements they contain may only
have a partial degree of membership. They are therefore not the same as classical sets in the
sense that the sets are not closed. Some examples of vague fuzzy sets and their respective
units include the following.
Loud noises (sound intensity)
High speeds (velocity)
Desirable actions (decision of control space)
Fuzzy sets can be combined through fuzzy rules to represent specific actions/behaviour and
it is this property of fuzzy logic that will be utilised when implementing a fuzzy logic
controller in subsequent chapters.
A membership function (MF) is a curve that defines how each point in the input space is
mapped to the set of all real numbers from 0 to 1. This is really the only stringent condition
brought to bear on a MF.
16
A classical set may be for example written as:
A = {x | x > 3}
Now if X is the universe of discourse with elements x then a fuzzy set A in X is defined as a
set of ordered pairs:
A = {x, µ A (x) | x ∈X}
Note that in the above expression µ A (x) may be called the membership function of x in A
and that each element of X is mapped to a membership value between 0 and 1. Typical
membership function shapes include triangular, trapezoidal and gaussian functions. The
shape is chosen on the basis of how well it describes the set it represents. Below in Figure
3.1 some example fuzzy sets can be observed.
Figure 3.2 Sample three-part Gaussian shaped MF
17
Fuzzy logic reasoning is a superset of standard Boolean logic yet it still needs to use logical
operators such as AND, OR and NOT. Firstly note that fuzzy logic differs from Boolean
yes/no logic in that although TRUE is given a numerical value ‘1’ and FALSE a numerical
value ‘0’, other intermediate values are also allowed. For example the values 0.2 and 0.8
can represent both not-quite-false and not-quite-true respectively. It will be necessary to do
logical operations on these values that lie in the [0,1] set, but two-valued logic operations
like AND, OR and NOT are incapable of doing this. For this functionality, the functions
min, max and additive complement will have to be used. The following examples for the
OR and NOT logical operations shown in Table 3.1 and Figure 3.3 show how this works.
A B A OR B Max(A,B)
0 0 0 0
0 1 1 1
1 0 1 1
1 1 1 1
OR
A NOT A 1 - A
0 1 1
1 0 0
NOT
BTwo-valuedLogic
A OR B
A
A B
Multi-valuedLogic
ORmax(A,B)
A
NOT A
A
NOT A
NOT(1 - A)
Table 3.1 Truth tables for the OR and NOT operators
Figure 3.3 Examples of two-valued and multi-valued logical operations
18
3.1.2 Linguistic Variables and Rule Bases
Linguistic variables are values defined by fuzzy sets. A linguistic variable such as ‘High
Speeds’ for example could consist of numbers that are equal to or between 50km/hr and
80km/hr. The conditional statements that make up the rules that govern fuzzy logic
behaviour use these linguistic variables and have an if-then syntax. These if-then rules are
what make up fuzzy rule bases. A sample if-then rule where A and B represent linguistic
variables could be:
if x is A then y is B
The statement is understood to have both a premise, if ‘x is A’, and a conclusion, then ‘y is
B’. The premise also known as the antecedent returns a single number between 0 and 1
whereas the conclusion also known as the consequent assigns the fuzzy set B to the output
variable y. Another way of writing this rule using the symbols of assignment ‘=’ and
equivalence ‘==’ is:
if x == A then y = B
An if-then rule can contain multiple premises or antecedents. For example,
if velocity is high and road is wet and brakes are poor then…
Similarly, the consequent of a rule may contain multiple parts.
if temperature is very hot then fan is on and throughput is reduced
19
Interpreting these rules involves a number of distinct steps.
1. Firstly, the inputs must be fuzzified. To do this all fuzzy statements in the
premise are resolved to a degree of membership between 0 and 1. This can be
thought of as the degree of support for the rule. At a working level this means
that if the antecedent is true to some degree of membership, then the consequent
is also true to that same degree.
2. Secondly, fuzzy operators are applied for antecedents with multiple parts to
yield a single number between 0 and 1. Again this is the degree of support for
the rule.
3. Thirdly, the result is applied to the consequent. This step is also known as
implication. The degree of support for the entire rule is used to shape the output
fuzzy set. The outputs of fuzzy sets from each rule are aggregated into a single
output fuzzy set. This final set is evaluated (or defuzzified) to yield a single
number.
The following example in Figure 3.4 shows how these steps are applied in practice.
Consider the two rules for a fuzzy model that evaluates lecture attendance:
1. If (LectureQuality) is Good) and (InterestOfMaterial is Interesting) then (Attendance is High)
2. If (LectureQuality is Poor) or (InterestOfMaterial is Boring) then (Attendance is Low)
20
These rules can then be seen visually in Figure 3.4.
The fuzzy AND operator is applied in rule one and since the premise of the rule is true to a
high degree then the consequent is also going to be true to a high degree. In this example
both the fuzzy AND operator and the implication operator use the min function, hence for an
input of 50% for LectureQuality and 80% for InterestOfMaterial, the defuzzified attendance
percentage works out to be 75% (The details of implication and defuzzification are
explained below in Section 3.2.2).
This comes from min ( µ Lecture Quality, µ Interest Of Material)
= min (1.0, 0.75)
= 0.75
Immediate advantages of this approach become apparent. Fuzzy sets can be combined using
fuzzy rules to define system behaviour and thus complex non-linear systems can be
expressed linguistically. In fact, as will be shown later, rule tables can represent fuzzy
controllers. The process of fuzzifying a single crisp input, applying fuzzy operators and then
defuzzifying to produce a single crisp output is known as fuzzy inference. This progression
of modelling is discussed in detail in the next section.
Figure 3.4 Application of rules for lecture attendance example
µ (LectureQuality) = 1.0 µ (Interest Of Material) = 0.75
21
3.2 Fuzzy Logic Models
3.2.1 What is Fuzzy Modelling?
Standard control techniques use numerical data to relate input and output signals. Fuzzy
logic systems can use both numerical information and linguistic information to perform a
mapping of an input to an output space. Consider the following diagram in Figure 3.5.
Many different control mechanisms could reside within the black box but in this case the
mechanism will be confined to a fuzzy logic system. Since the objective is to map inputs to
outputs it becomes possible to model non-linear systems, even complex ones. This is one of
fuzzy logics’ greatest advantages. Put differently, fuzzy logic systems are tolerant of
imprecise data. When considered this suits many real-world applications well because as
real-world systems become increasingly complex often the need for highly precise data
decreases. The rules that govern this mapping can be acquired through two methods. The
first is a method called the direct approach and the second is by using system identification.
The direct approach involves the manual formulation of linguistic rules by a human expert.
These rules are then converted into a formal fuzzy system model. The problem with this
approach is that unless the human expert knows the system well it is very difficult to design
a fuzzy rule base and inference system that is workable, let alone efficient. For complex
systems (non-linear for example) tuning these membership functions would require the
adjustment of many parameters simultaneously. Understandably no human expert could
accomplish this.
BLACKBOX
INPUT SPACE(ALL POSSIBLE I/P'S)
OUTPUT SPACE(ALL POSSIBLE O/P'S)
Figure 3.5 Mapping of input space to output space
22
Fuzzy models that are designed using system identification are based on the use of input-
output data. System identification was introduced to overcome the difficulties involved in
the direct approach of choosing the fuzzy set’s membership functions using a
search/optimisation technique to aid the selection. The system identification method is
covered in Chapter four.
All of the previous elements of fuzzy logic that have been discussed up to this point are put
together to form a fuzzy inference system (FIS). Two main types of fuzzy inference system
exist – the Mamdani and Sugeno type. They are both introduced in the following sections.
3.2.2 Mamdani Modelling
Owing its name to Ebrahim Mamdani the Mamdani model was the first efficient fuzzy logic
controller designed and was introduced in 1975 [6]. The controller consists of a fuzzifier,
fuzzy rule base, an inference engine and a defuzzifier. It is shown in the Figure 3.6 below.
Figure 3.6 Mamdani fuzzy control system [5]
crisp input in R crisp output in Q
fuzzy input set in R fuzzy output set in Q
INFERENCE ENGINE
RULESFUZZIFIER DEFUZZIFIER
23
Conventional control systems require crisp outputs to result from crisp inputs. The above
representation shows how a crisp input in R can be operated on by a fuzzy logic system to
yield a crisp output in Q. This Mamdani controller is realised using the following steps.
A. Fuzzification of Inputs
B. Application of Fuzzy Operators
C. Application of Implication Method
D. Aggregation of all Outputs
E. Defuzzification of Aggregated Output
A. Fuzzification of Inputs
The fuzzifier maps crisp input numbers into fuzzy sets. The value between 0 and 1
each input is given represents the degree of membership that input has within these
output fuzzy sets. Fuzzification can be implemented using lookup tables or as in this
report, using membership functions.
B. Application of Fuzzy Operators
In the case where multiple statements are used in the antecedent of a rule, it is
necessary to apply the correct fuzzy operators as explained in 3.1.1. This allows the
antecedent to be resolved to a single number that represents the strength of that rule.
C. Application of Implication Method
This part of the Mamdani system involves defining the consequence as an output
fuzzy set. This can only be achieved after each rule has been evaluated and is
allowed contribute its ‘weight’ in determining the output fuzzy set.
24
D. Aggregation of all Outputs
The fuzzy outputs of each rule need to be combined in a meaningful way to be of any
use. Aggregation is the method used to perform this by combining each output set
into a single output fuzzy set. The order of rules in the aggregation operation is
unimportant as all rules are considered. The three methods of aggregation available
for use include sum (sum of each rules output set), max (maximum value of each rule
output set) and the probabilistic OR method (the algebraic sum of each rules output
set). An example of the aggregation process using the max operator can be seen in
Figure 3.7 below.
E. Defuzzification of Aggregated Output
The aggregated fuzzy set found in the previous step is the input to the defuzzifier.
As indicated in the model shown in Figure 3.6 this aggregated fuzzy set in Q is
mapped to a crisp output point in Q. This crisp output is a single number that can
usefully be applied in controlling the system. A number of methods of
defuzzification are possible and these include the mean of maximum, largest of
maximum, smallest of maximum and centroid (centre of area) methods. The
centroid method is the most widely used and can be seen in Figure 3.7 below.
Figure 3.7 Diagram showing aggregation and defuzzification
Window showing aggregation =>
(Red line indication Centroid)
25
3.2.3 Sugeno Modelling
The Sugeno fuzzy model, or more fully the Takagi-Sugeno-Kang method of fuzzy inference
was first introduced in 1985 [4]. In many respects it is identical to the Mamdani method
except that the output membership functions for the Sugeno method are always linear or
constant. The output membership functions can be thought of as singleton spikes that
undergo a simple aggregation instead of other aggregation methods such as max, probor or
sum.
Figure 3.8 shows the application of three basic rules for a Sugeno model. All three rules
have been written using the OR connector. For example:
if input1 is x or input2 is y then output1 is z
The method of defuzzification is the weighted average (as marked by the red line in the
bottom right corner of the figure). The output is always a single number and in this case
having the inputs [0.5input1 , 0.8 input2] the output is [6.64output1].
The Sugeno system is computationally efficient and its ability to interpolate multiple linear
models makes it particularly suited to modelling non-linear systems.
Figure 3.8 Implementation of a Sugeno model
26
3.2.4 Applying a Fuzzy Logic Controller to the Test Case
In this section the linear dynamics of the pole/cart system determined in Chapter two will be
used to design a fuzzy logic controller. Figure 3.9 gives a visual review of what has been
discussed up to this point.
Rules for the fuzzy system are to be extracted from the output plot of the linearised, PID
controlled system with pre-filter. The error and error derivative plots are also used. The
model used to produce these plots is shown below in Figure 3.10.
FUZZIFY
REASONING WITH FUZZY
CONTROL RULESDEFUZZIFY
CONTROL SIGNAL
STABILISE AN
INVERTED
PENDULUM
PENDULUM IS
STABILISED
DESIGN PID CONTROLLERLINEARISE
APPLY PID
CONTROLLER
Figure 3.9 A comparison of conventional PID and fuzzy control solutions
Figure 3.10 Cart-Pole PID control for linearised transfer function with pre-filter
27
The subsequent section will outline the design of a fuzzy controller (with the linearised
transfer function model) in Simulink. Design of the fuzzy controlled non-linear Simulink
model will also be attempted.
The direct approach described in Section 3.2.1 (manual writing of fuzzy rules) the approach
utilised in this part of the design. Figure 3.11 above can be analysed for clues about the
relationship between the error, error derivative and output that will aid controllability. A
Using the procedure below a seven-rule fuzzy inference system can be designed.
A. Define rules using linear model output as template
B. Design and label membership functions
C. Decide range of MFs
D. Decide overlap of MFs
E. Design Simulink model and implement controller
F. Redesign/Alter MF properties to improve controllability
Figure 3.11 Linear model output plots showing error, error derivative and overall output
28
The Simulink model created can be seen below in Figure 3.12.
The rules for this fuzzy controller are:
1. If (e is Large) then (output is Small) then (output is Small)
2. If (e is Large) and (edot is Zero1) then (output is Small)
3. If (e is Medium) and (edot is Big) then (output is Average)
4. If (e is Small) and (edot is Small2) then (output is High)
5. If (e is Zero) and (edot is Zero2) then (output is High)
6. If (e is Medium) or (edot is Big) then (output is Average)
7. If (edot is Zero2) then (output is High)
It is possible to view and see the result of the fuzzy operators that connect them in Figure
3.13.
Figure 3.12 Cart-Pole Fuzzy control for linearised transfer function
Figure 3.13 Visual representation of fuzzy rules
29
The resulting system plot of this model is shown in Figure 3.14 below.
This output shows that it has not been possible to stabilise or level off the output. Despite
altering the rules shown above in Figure 3.13 no improvement can be found. In conclusion,
the fuzzy controller has been unsuccessfully implemented. The reason for this could be any
or a combination of the following.
1. The membership function overlap may be too slight leading to poor
controllability.
2. A Sugeno model should have been used to provide outputs that drive the
controller in a better fashion.
3. The direct approach has been used here. System identification may be the
only way that membership functions can be shaped to adequately provide
control that meets the design criteria.
Best practice insists that the fuzzy controller for the linear model be developed extensively
before applying it to the non-linear model. Nevertheless, a Simulink model of the non-linear
system has been proposed in Appendix 2.
0 1 2 3 4 5 6 7 8 9 10-10
-5
0
5x 10
22
Error
0 1 2 3 4 5 6 7 8 9 10-4
-2
0x 10
23
Edot
0 1 2 3 4 5 6 7 8 9 100
5
10x 10
22
Time/secs
O/P
Figure 3.14 Resulting system plot for fuzzy controlled linear model.
30
3.2.5 Summary
The terms and concepts of fuzzy logic have now been introduced. The understanding of
fuzzy sets, membership functions, logical operators, linguistic variables and use of fuzzy
rules has proven necessary in the design of fuzzy control systems. Two fuzzy controllers
have been introduced so far, the Mamdani and Sugeno models. Both models use identical
schemes of fuzzifying and use of fuzzy linear operators, yet differ in the output membership
functions (singleton spikes are used in the Sugeno model) and hence the aggregation method
also. The Mamdani model is preferred when a linguistic description of both the input and
output membership function is desired. The Sugeno model has shown itself to have better
potential in controlling non-linear systems that require more stringent output membership
functions.
The direct approach of rule generation has been unsuccessful in this case indicating that
there is a need for a better method of rule generation. The method of system identification
will now be investigated. Research into genetic algorithms can now be carried out with this
purpose in mind.
31
Chapter 4
4. Genetic Algorithms
4.1 A Quick Overview
In 1960 the first serious investigation into Genetic algorithms (GAs) was undertaken by
John Holland [6]. Genetic algorithms attempt to mimic the properties of natural selection
and natural genetics. They consider a population of potential solutions and then use a
defined ‘survival of the fittest’ technique to produce a new generation of solutions which are
hopefully better approximations to the ideal solution. This process of breeding ‘fitter’
generations comprises the essence of the genetic algorithm approach.
Genetic Algorithms have become popular due to several factors. They have been
successfully applied to self-adaptive control systems and to function optimisation problems.
They are a powerful search technique, yet are computationally simple. The search method
they use is robust since it is not limited like other search methods with regard to
assumptions about the search space. These factors and the relative benefits of GAs will be
investigated in the following sections. The theory and functionality of GAs is explained in
this report while the methods of implementation can be found in the well-outlined Genetic
Algorithm Toolbox [7].
32
4.1.1 Why Genetic Algorithms?
It is helpful to briefly consider the other methods currently known for control optimisation
so as to compare the respective advantages and disadvantages of the GA search method.
Search methods can be defined as being calculus, enumerative or random based.
The calculus-based methods can either be direct or indirect. They are local in scope, that is,
they both seek local optima by solving the derivative of a set of non-linear equations.
Enumerative search methods look at objective function values for every point of the search
space, one at a time. Although this is a simple approach it is highly inefficient for a large
search space. Random search methods tend to explore regions of the search space more
quickly yet tend to perform no more efficiently than enumerative schemes. All of these
search techniques discussed lack a robustness and reliability that GAs can provide. It is
informative to look at the properties of GAs as well as simultaneously comparing how they
differ from traditional search methods [6].
A. GAs use probabilistic transition rules, not deterministic ones.
Random choice is used to search regions of the search space with likely
improvement over the current solution.
B. GAs search from a population of points, not a single point.
Peaks of the search space can be climbed concurrently since the GA works with a
large number of points simultaneously. The problem of only finding local extrema is
resolved.
C. GAs work on an encoding of the parameter set, not the parameters themselves.
By encoding the parameter set GAs can compare solutions and produce new ones in
a very efficient way.
33
D. GAs use objective function information, not derivatives.
Pure GAs may be considered blind to the search space and instead of using
derivative approaches or auxiliary information they work in terms of solution
performance (objective function) and how fit solutions are (fitness function). They
are a derivative free technique, which means that they do not get trapped at local
minima.
Robust Search Algorithms
It is helpful when trying to visualise a search space to think of a three dimensional space in
which a number of maxi and minima (peaks and troughs) exist. It is the goal of any search
algorithm to seek extrema or at least find the closest point possible to it. Current search
methods tend to find local extrema (for example the yellow peaks in Figure 2.1) and not the
global extrema (for example the red and blue peaks in Figure 2.1).
Figure 4.1 Sample three-dimensional search space for ease of visualisation.
34
4.2 How Genetic Algorithms work
4.2.1 Strength in numbers
Populations
GAs operate in cycles called generations, and operate on a population of potential solutions.
A population will have about 30-100 individuals and each individual, or current
approximation is encoded as a string or chromosome.
There are a few ways of encoding these chromosomes such as integer, real-valued and ternary
but one of the most popular ways is binary encoding (bit string), because it is a simpler string
to operate on. To create an initial population at random consisting of NIND individuals whose
chromosomes are LIND bits long the Matlab function crtbp.m will be used. The number of
random numbers that are generated is NIND x LIND and each number will be from the binary set
{0,1}. That is, for binary encoding each chromosome is constructed by stringing binary
representations of vector components end to end (see Figure 4.2 below). The length of each
chromosome depends on the vector dimension and the desired accuracy. [5]
Figure 4.2 A sample binary encoded chromosome consisting of a string of 16 bits
Merely observing the above encoding is not helpful in gaining information about the
problem to be solved. It is only when the chromosome values are mapped to the decision
variable domain that helpful information is obtained. The search process operates on the
chromosome encoding and not the decision variables.
1011010110101110
35
4.2.2 Objective and Fitness Functions
Objective function
The objective function is a measure of how well individuals have performed. It is only
possible to obtain the objective function once the chromosome representation (explained
above) is decoded into the decision variable domain. Once the solution is in a recognisable
format it can be given a grading or scale of usefulness using the objective function.
Fitness function
The fitness function transforms the objective function value into a measure of relative
fitness (i.e. any one solution is positioned relative to all other possible solutions). The
fitness function is a very helpful way of selecting solutions that will continue into the next
generation. As would be expected, solutions that have the highest fitness values also have
the highest probability of being selected as parent solutions for the next generation.
Expressing this mathematically, where g transforms the value of the objective function f to a
non-negative number and F is the relative fitness [7]:
))(()( xfgxF =
To obtain an expression for individual fitness F (xi) the performance of each individual
represented by the objective function f (xi) will be determined in relation to the whole
population NIND:
∑=
=INDN
ii
ii
xf
xfxF
1
)(
)()(
Fitness values can be normalised, scaled, shared or left unchanged. It is often necessary to
offset the objective function before determining fitness to avoid obtaining negative objective
function values if the solution is to be optimised. The following equation can do this, where
a is a positive scaling value, and b is an offset to make fitness values non-negative.
bxafxF += )()(
Further mathematical descriptions of the fitness function are given in Section 4.2.7.
36
4.2.3 Reproduction, Crossover and Mutation
Reproduction
Reproduction is the term given to the process that involves taking parent solutions that have
been selected according to their fitness and combining them to create a new generation of
solutions – children solutions, to extend the analogy.
Before reproduction can take place, this process of selection must be used to decide the
number of times a particular individual will be chosen for reproduction. While these
selection algorithms are detailed elsewhere ([6], [7]) it will suffice to mention here that the
selection algorithm should possess the following properties:
i. Zero bias (an indication of accuracy) – Individuals should be selected entirely on their
position in the population.
ii. Minimum spread (an indication of consistency) – Individuals should be selected a
minimum of et (i) times (floor) and maximum of et (i) times (ceiling).
The stochastic universal sampling selection fulfils these conditions of zero bias and
minimum spread. The Matlab stochastic universal sampling function sus.m will be used in
later examples. Once a string has been selected using the criterion of the fitness function an
exact replica is made using the Matlab replication function rep.m. This is repeated for all
the selected strings and then these copies or replicas undergo crossover operations at
random. The crossover operator is explained in the next section.
37
Crossover
Crossover, also known as recombination, is the operator that produces new chromosomes.
Crossover produces new individuals that have parts of both parents genetic material, akin to
nature. Different types of crossover are possible such as, multi-point, uniform and shuffle,
but the simplest of all is single-point crossover. An example of single-point crossover can
be seen in Figure 4.3. The cross site can be chosen at random and each pair of strings
undergo crossover as follows:
“An integer position k along the string is selected uniformly at random between 1
and the string length less one [1, L-1]. Two new strings are created by swapping all
characters between positions k+1 and L inclusively.” [6]
Figure 4.3 Single-point crossover of two chromosomes (k = 4)
Through this process of reproduction and crossover the GA can exploit the knowledge of the
gene pool by allowing good chromosomes to combine with not so good chromosomes. Note
that the crossover operator is applied with a chosen probability and this means that not all
chromosomes in the population may undergo crossover. Single-point crossover will be
implemented using the Matlab function xovsp.m.
Before crossover After crossover
101010
111011
101011
111010
X Y X’ Y’
k
38
Mutation
It is helpful to consider the phenomenon of mutation in the natural world before seeing how
it is helpful as an operator in GAs.
“In biochemistry a mutation is a change is DNA. To be inherited, the change must
occur in the DNA of a reproductive cell. The simplest mutation occurs when a
single nucleotide (nucleotides are the “building blocks” of DNA) in a creatures’
DNA is switched to a different nucleotide. Alternatively, a single nucleotide can be
added or left out when the DNA is copied during cell division.” [8]
The difference with GAs is that a mutation only ever causes a single bit to change its state.
Therefore a ‘1’ chosen at random will be changed to a ‘0’ and vice versa. Single bits are
neither added nor left out – just switched. An example is given in Figure 4.4.
Mutations are needed in GAs because while reproduction and crossover explore the search
space well, occasionally they lose information that is essential to the solution. It is for this
reason mutation is used to explore the search space as efficiently as possible. Mutation is
applied randomly to an entire population and with low probability (0.001->0.01). It can be
thought of as a secondary background operator. [7]
It may be worth considering how to bias the mutation operator to alter less fit individuals so
that fitter individuals are not made less fit. Another improvement may be to have an
adaptive mutation rate that is a function of population convergence. This induces better
searching even after many generations. Binary mutation will be performed using the Matlab
function mut.m.
Original string: Before mutation
Mutated string: 10100101 After mutation
Mutation point (in bold)
Figure 4.4 Example of Binary Mutation
10110101
39
4.2.4 The Generation Game
Once crossover and mutation have been carried out the strings can once again be decoded
and the cycle of evaluating the objective function, relative fitness, applying crossover and
mutation, and finally decoding can start all over again. The average fitness of the total
population will then increase, and the fittest individuals will approximate acceptable
solutions.
To keep the size of successive populations the same as the original population in the process
outlined above, the new individuals need to be reinserted into the original population. This
leads to the problem of deciding what individuals should be replaced. In this series of tests
the oldest members will be replaced first. In time therefore, as long as there are a sufficient
number of generational cycles each individual in the population will eventually be replaced.
Reinsertion is performed using the Matlab function reins.m.
In determining how many cycles of the GA should take place, any of the following stopping
criteria can be employed.
A. When a particular point in the search space has been found.
For example, the greatest extrema found up to this point in time.
B. When a certain upper limit of generations has been reached.
For example, thirty or one thousand generations have been cycled through.
C. When a mean deviation in the population is obtained.
For example, when any new solutions are very close to the previous generations
solutions, using the mean as a reference.
If after meeting one of these criteria no individuals in the population yield acceptable
solutions to the problem at hand, the GA may simply run again, or be continued with
extended design criteria. The GA allows the user to pick from a number of potential
solutions – it doesn’t just yield one solution. It is good at identifying simultaneously these
other solutions for a problem that by nature has multiple solutions.
40
In Table 4.1 above a summary of the terms commonly used in the field of genetic is given.
The corresponding terms that are more commonly used by biologists are also included.
Genetic Algorithm Natural
String Chromosome
Feature, character,
or detector
Gene
Feature value Allele
String position Locus
Structure Genotype
Parameter set,
Alternative solution,
Decoded structure
Phenotype
Non-linearity Epistasis
41
4.2.5 Simple Genetic Algorithm Example
A flowchart showing the stages of a simple genetic algorithm is given below in Figure 4.5
and a simple genetic algorithm program written in Matlab can be found in Appendix three.
Newindividualsplaced intopopulation
Fitness valuesare assigned
Individuals areselected
Crossovertakes place
Mutationoperator used
Objectivefunction
evaluates newgeneration
Count ofgenerations is
increased
whilegeneration number < max number of generations
initial population is evaluated
population is initialised
matrix to hold chromosomes is built
parameters are specifiednumber of individualsmaximum number of generationsnumber of variablesprecision of variablesgeneration gap
GENERATIONAL LOOP
Figure 4.5 Flowchart of Simple Genetic Algorithm
42
The plot that results from the SGA in Appendix 3 is shown below in Figure 4.6.
With a maximum of 100 generations being controlled by the while loop of the
program, the cycle of events (assignment of fitness functions, selection, crossover,
mutation, evaluation and reinsertion) in the while loop force an improvement in the
solution over time, as evidenced by the plot above.
Figure 4.6 Resulting plot showing minimisation of De
Jong’s first test function
43
4.2.6 Combining Genetic Algorithms and Fuzzy Logic
It was noted in Section 3.2.4 that the difficulty with fuzzy control is in determining suitable
membership functions and rules. GAs have great potential in fulfilling this role. In essence,
the weakest points of fuzzy control can be strengthened by GAs without the need for expert
human knowledge and intuition about the system in question.
The greatest potential problem with this approach is that the membership functions that the
GA may prescribe will have little overlap. This leads to abrupt control and at worst
meaningless membership functions. The challenge is to find ways of constraining elements
of the rule base, so that as the GA informs the rule base, control will remain smooth.
This section describes how genetic algorithms can be used for generating fuzzy if-then rules.
An algorithm that will perform this function will now be outlined followed by a suggested
rule coding technique, an appropriate fitness function definition [9], and a description of the
algorithm development.
Figure 4.7 Example of poor membership function overlap
44
ALGORITHM OUTLINE
Each fuzzy if-then rule will be treated as in individual in the GA and each population
corresponds to a rule-based system. The population is updated by generating new fuzzy if-
then rules from the current population using the genetic operations of selection, mutation
and crossover. The algorithm may be thought of as having the following structure.
1. Generate an initial population of if-then fuzzy rules. This takes place according
to some rule generation procedure but in essence each fuzzy if-then rule is
generated by randomly specifying its antecedent fuzzy sets.
2. Each fuzzy if-then rule in the current population is evaluated.
3. New fuzzy if-then rules are generated using the genetic operations of selection,
mutation and crossover.
4. Some individuals in the current population are replaced with the newly generated
rules according to some replacement strategy (see Section 4.2.4).
5. Unless a stopping condition has been met the algorithm restarts at step two.
CODING OF A FUZZY RULE SET
Each fuzzy if-then rule is coded as a string, and is coded by its antecedent fuzzy sets. A
coding scheme such as what follows could be adopted:
1: small
2: medium small
3: medium
4: medium large
5: large
#: don’t care
A fuzzy if-then rule could then be coded as “3#5#”. In a binary scheme each matrix entry
itself is a vector of 0’s and 1’s. This is the chromosome on which the genetic algorithm
operates.
45
DEFINING THE FITNESS FUNCTION
Consider the current population to be represented by S. Fuzzy if-then rules in S are
evaluated by classifying each training pattern by S. The fitness value of each rule is defined
by the fuzzy rule-based classification system that relates to the current population S. This is
given by:
fitness ( Ri ) = NCP ( Ri )
where NCP ( Ri ) is the number of correctly classified training patterns obtained by the fuzzy
if-then rule in question.
Using standard selection procedures a pair of parent strings from the current population is
used to generate new fuzzy if-then rules (new strings). Crossover and mutation are applied
as before and the worst rules with the lowest fitness values are discarded and replaced with
newly generated rules. The overall population size is kept constant and every time the GA
finds an optimum value, the fuzzy system parameters are saved to a file. The technique for
storing and representing the fuzzy rules is to use a fuzzy associative memory (FAM) matrix.
As mentioned previously to apply the optimisation to the FAM the matrix entries are strung
together into a long single vector (zeroes and ones for a binary scheme).
4.2.7 Implications for Test Case
It is recommended that this step be implemented by writing an s-function (user defined
block and code for Simulink) that would combine the GA with the fuzzy logic controller.
This code would include the formulae that would allow the GA update the membership
functions of the controller in order to optimise the stability of the system. An objective
function (Objpush.m) for the pole/cart problem can be found in the Matlab GA Toolbox [7].
46
4.2.8 Summary
Genetic algorithms show a robustness and reliability that calculus-based methods,
enumerative search methods and random search methods lack and yet are efficient and wide-
ranging in their search performance. They run in cycles called generations and operate on
chromosomes – binary encoded solutions in this case. The process of breeding ‘fitter’
generations of solutions comprises the essence of the genetic algorithm approach. The
concepts of the objective and fitness function have been introduced along with the primary
structures in GAs, reproduction, crossover (recombination), mutation and reinsertion. Also
outlined were the stopping conditions that determine how many cycles of the GA may run.
A suggested rule coding technique, appropriate fitness function definition and description of
the algorithm development have been given and the chromosome on which the genetic
algorithm operates is a vector of 0’s and 1’s for a binary scheme. Standard selection,
crossover and mutation procedures are applied to generate new fuzzy if-then rules (new
strings), and the worst rules with the lowest fitness values are discarded and replaced with
newly generated rules keeping the overall population size constant. Every time the GA finds
an optimum value the fuzzy system parameters are saved to a file. A sample genetic
algorithm program shows how the concepts of GAs are implemented. It is recommended
that an s-function be written to combine a developed GA program with the fuzzy logic
controller.
47
5. Results
5.1 Results
The pole/cart system has been introduced as a suitable non-linear model for study in this
report. This chapter forms a summary of the results obtained and specifically relate to the
degree of controllability of the linear and non-linear model using PID and fuzzy control.
Firstly, the linear model of the system was derived and using some basic Matlab code the
impulse response could be observed confirming the systems’ non-linear behaviour. Next, an
appropriate PID controller with pre-filter was designed to meet the design constraints
(overshoot <22.5%, settling time <5 seconds). This output is shown in Figure 5.1 below.
This same controller was then used to control the non-linear pole/cart dynamics yielding the
uncontrolled output shown in Figure 5.2. The output quickly goes unstable (within a few
seconds). A more sophisticated control technique is needed. This leads to the introduction
of fuzzy logic systems.
os = 21.6%
ts = 4.64s
tr = 1.32s
tp = 1.99s
Figure 5.1 PID – controlled system with pre-filter Figure 5.2 PID – controlled non-linear output plot
48
The rules for the fuzzy controller proposed were extracted from the output of the linear PID
controlled model. It is shown below in Figure 5.3.
After implementing this fuzzy logic controller in the Simulink model of Figure 3.12 the
resulting system plot of this model was Figure 5.4 below.
0 1 2 3 4 5 6 7 8 9 10-10
-5
0
5x 10
22
Error
0 1 2 3 4 5 6 7 8 9 10-4
-2
0x 10
23
Edot
0 1 2 3 4 5 6 7 8 9 100
5
10x 10
22
Time/secs
O/P
Figure 5.4 Resulting system plot for fuzzy controlled linear model.
Figure 5.3 Linear model output plots showing the error,
error derivative and overall output
49
This output shows that it has not been possible to stabilise or level off the output. No
improvement resulted by altering the rules shown in Figure 3.13. In conclusion, the fuzzy
controller was unsuccessfully implemented.
The need for a better method of rule generation than the direct approach was needed. The
method of system identification using genetic algorithms was then investigated. No results
are available for this section due to the blockade encountered when attempting to create the
appropriate GA-fuzzy functionality in Simulink.
5.2 Implications to Research
The design model developed has proven to be sound. The results from the linear model
analysis in Chapter two appropriately directed the flow of research into fuzzy logic in
Chapter three. The research in Chapter three made clear that the direct approach of selecting
the fuzzy if-then rules for this system is not successful. This implies that the research into
the system identification method investigated in Chapter four was worthwhile.
This research may have prospered more greatly if a Sugeno model had been used. Better
input membership functions and well chosen output singleton spikes would have given the
Sugeno model greater success than the Mamdani model in controlling the linear system in a
manner similar to the benchmark PID controller. Further suggestions for future research
have been outlined in Section 6.2 of the next chapter.
50
6. Conclusion
6.1 Introduction
The ‘Inverted Pendulum on a Cart’ test case chosen to aid research into this area has been
satisfactory. The behaviour exhibited in response to different controllers has given insight
into how improved techniques will have to be able to cope with the phenomenon of non-
linear control.
Fuzzy logic has been shown to provide an intuitive method of simplifying the control of
complex non-linear systems and lets the language and comments that are clearest also make
sense to the systems they are controlling. It has been observed how genetic algorithms
exhibit robustness and are both an efficient and wide-ranging optimisation technique.
Genetic algorithms have been proposed as a substitute for the intuition that complex systems
often need and although not tested in this period of investigation show few signs that would
render them unhelpful. The main note of caution in combining these techniques is to be
careful to define how the genetic algorithm implements the membership functions and rules
fuzzy controller needs to enable smooth control.
6.2 Future Work
As suggested in Section 5.2 this research may have benefited further if a Sugeno model had
been used. Better input membership functions and well chosen output singleton spikes
would have given the Sugeno model greater success than the Mamdani model in controlling
the linear model in a manner similar to the benchmark PID controller. Yet it is possible that
even with this alternative model the output response of the linear cart/pole model may
remain uncontrolled. The primary aim has been to control the non-linear model, which
means the research into how the genetic algorithms described in Chapter four may be linked
with the fuzzy controller is still a worthwhile pursuit.
51
No genetic algorithm program dealing with the cart/pole model has been given in this report
but note that an objective function (Objpush.m) for the system can be found in the Matlab
GA Toolbox [7]. Finally, a better understanding of how s-functions (user defined code for
Simulink) could be written to link the fuzzy inference system and genetic algorithm that
informs it would be a very positive development. This is essentially the next logical step in
the development of controlling the cart/pole system.
52
References
[1] Figure 2.1, www.engin.umich.edu/group/ctm/examples/pend/invpen.html.
[2] Beveridge, W.I.B, “The Art of Scientific Investigation”, Ch 2 Experimentation,
pp15, 1964.
[3] Bruton, J., EE4 Control notes, School of Electronic Engineering, D.C.U., 1999-2000
[4] Mathworks, “Matlab Fuzzy Logic Toolbox manual”.
[5] Dadios, Elmer P.; Williams, David J., “A Fuzzy-Genetic Controller for the FlexiblePole-Cart Balancing Problem”, Proceedings of IEEE International Conference onEvolutionary Computation, pp 223, ISBN: 0 7803 2902 3, Nagoya, Japan, 1996.
[6] Goldberg, David E., “Genetic Algorithms in Search, Optimization and Machine
Learning”, Mass: Addison-Wesley Pub. Co, 1989, Ch1, pp7.
[7] Chipperfield, Andrew; Fleming, Peter; Pohlheim, Hartmut; Fonseca, Carlos;
“Genetic Algorithm Toolbox”, Version 1.2, University of Sheffield.
[8] Behe, Michael J., “Darwin’s Black Box: The Biochemical Challenge to Evolution”,
Touchstone Simon & Schuster, 1996, Ch2, pp40.
[9] Leondes, Cornelius, T., “Fuzzy Theory Systems”, Academic Press, pp 1098, 1999.
[10] Ruspini, Enrique, “Introduction to fuzzy set theory and fuzzy logic: basic concepts
and structures”, 1992.
[11] Mamdani, E.H., Assilian, S., “An experiment in linguistic synthesis with a fuzzy
logic controller”, Int. J. Man Mach. Studies, Vol. 7, No. 1, pp 1-13, 1975.
53
Appendix 1
% Title: Cart-pole linearised model% Author: Malcolm Jeffers% Version: 1.0% Date: 18-03-01
M = 0.5;m = 0.2;b = 0.1;i = 0.006;g = 9.8;l = 0.3;
q = (M+m)*(i+m*l^2)-(m*l)^2;% used to make equations below more simple
num = [m*l/q 0]den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]
t=0:0.01:6;impulse(num,den,t)axis([0 1 0 60])% Output figure shows the response to an impulse input
%The constants above yield the following num, den values%num = 4.5455 0%den = 1.0000 0.1818 -31.1818 -4.4545
Appendix 1: Matlab code for the cart-pole linearised model
54
Appendix 2
A proposed Simulink model including a fuzzy logic controller to control the non-linear
system dynamics of the cart and pole
55
Appendix 3% ---------------------------------------------------------------------------% Title: Simple Genetic Algorithm Example% Author: Malcolm Jeffers% Purpose: To show the practical implementation of the ideas discussed% in the report so far and also outline the structure and% principles of writing a genetic algorithm to solve an% optimisation problem.% ---------------------------------------------------------------------------
MAXGEN = 100; % maximum Number of generationsNVAR = 20; % Generation gap, how many new individuals are createdGGAP = .8; % Generation gap, how many new individuals are createdPRECI = 20; % Binary representation precisionNIND = 30; % No. of individuals per subpopulations
% First, a field descriptor is set upFieldD = [rep([PRECI],[1, NVAR]); rep([-512;512],[1, NVAR]);...
rep([1; 0; 1 ;1], [1, NVAR])];
% The population is then initialisedChrom = crtbp(NIND, NVAR*PRECI);
% Reset countersBest = NaN*ones(MAXGEN,1); % Defines 'best' in current populationgen = 0; % generational counter
% Initial population is evaluatedObjV = objfun1(bs2rv(Chrom,FieldD));
% Best individual is tracked and plottedBest(gen+1) = min(ObjV);plot(log10(Best),'ro');xlabel('Generations'); ylabel('log10(f(x))');text(0.5,0.95,['Best = ', num2str(Best(gen+1))],'Units','normalized');drawnow;
% Loop to control number of generationswhile gen < MAXGEN,
FitnV = ranking(ObjV); % Fitness-values are assigned to entire pop.
SelCh = select('sus', Chrom, FitnV, GGAP); % Select individuals
SelCh = recombin('xovsp',SelCh,0.7); % Recombine selected individuals
SelCh = mut(SelCh); % Perform mutation on offspring
ObjVSel = objfun1(bs2rv(SelCh,FieldD)); % Evaluate offspring% the using objective funct.
[Chrom ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel); % Reinsert offspring% into current pop.
gen = gen+1; % Increment generational counter
Best(gen+1) = min(ObjV);plot(log10(Best),'bo'); xlabel('generation'); ylabel('log10(f(x))');title('Simple GA example - to minimise De Jongs 1st test function');text(0.5,0.95,['Best = ', num2str(Best(gen+1))],'Units','normalized');drawnow;% Update display and record current best individual
end% ---------------------------------------------------------------------------