Академический Документы
Профессиональный Документы
Культура Документы
in Electronic Engineering Final Year Project Report TITLE A Genetic Algorithm based Fuzzy Logic Controller Malcolm Jeffers April, 2001
Acknowledgements
I would like to thank Jennifer Bruton for her help throughout the term of this project. I am indebted 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 result of both their personal support and the comments that only friends can make.
Declaration
I hereby declare that, except where otherwise indicated, this document is entirely my own work and has not been submitted in whole or in part to any other university.
ii
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. At the start of the report the Inverted pendulum on a cart system is introduced and its controllability is studied in detail. The theoretical background to both fuzzy logic and genetic algorithms is detailed in subsequent chapters and the applications to the test case are unpacked at each stage in the theoretical development. A synopsis of the results obtained throughout the report is given in the results section with the intention of guiding future advancement of this research. Though no final genetic algorithm based fuzzy logic controller was implemented, the strengths of genetic algorithms and fuzzy logic are explained with the express purpose of proposing how, when combined, a useful and workable method of control may result. The testing of each controller in the process of the design has been carefully documented throughout the report.
iii
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
iv
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
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 Jongs 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
vi
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.
Figure 2.1 Cart-pole system [1] 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,
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.
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 carts 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. 2. The system overshoot value is to be at most 22.5%. ts < 5secs 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
M x+ b x + N = F
..
Substituting into above gives the first equation that describes the motion of this system:
F = ( M + m ) x + b x + ml cos ml 2 sin
.. . .. .
Summing the forces perpendicular to the pendulum for the second equation of motion:
..
By summing the moments around the centroid of the pendulum the N and P terms can be removed: Pl sin Nl cos = I
Using the last two equations it can be seen that:
( I + ml 2 ) + mgl sin = ml x cos [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, cos = 1 sin =
d =0 dt
2
After linearisation the two equations of motion ([1], [2]) become (where U is the input):
( I + ml 2 ) mgl sin = ml x ( M + m) x + b x ml = U
.. . ..
..
..
To obtain the transfer function of the linearised system it is necessary to first form the Laplace transform of the equations above:
Rearranging gives:
ml 2 s v b( I + ml 2 ) 3 ( M + m)mgl 2 bmgl s4 + s s s v v v
v = ( M + m)( I + ml 2 ) (ml ) 2
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:
(s) = U (s)
ml s v b( I + ml 2 ) 2 ( M + m)mgl bmgl s3 + s s v v v
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, den values %num = 4.5455 0 %den = 1.0000 0.1818 -31.1818 -4.4545 Figure 2.2: Matlab code for the cart-pole linearised model
12
10
Amplitude
To: Y(1)
0 0 1 2 3 4 5 6
Time (sec.)
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.
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.
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.
Analysis of PID-control for the linear cart-pole model 1.4
1.2
os = 0.0085%
p m a / t u o y 0.8
0.6
0.4
0.2
0 0 5 10 15 tout/s 20 25 30
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.
10
n2
s 2 + 2 n s + n
2
Given that this system has = 0.4318 and n = 1.85 rad s-1, the ideal second order transfer
function is:
T (s) = 3.4225 2 s 2 + 1.5976 s + 3.4225
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:
T * (s) =
n2 2 ( s + a )( s 2 + 2 n s + n )
So,
T * (s) =
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.
11
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:
P( s) STEP = P ( s ) STEP =
KI KDs + KPs + KI
2
2
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)
12
Figure 2.11 PID control for non-linear cart-pole system with pre-filter
13
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 models 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.
14
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.
15
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.
16
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 0 0 1 1 B 0 1 0 1 A OR B 0 1 1 1 OR
A
Max(A,B) 0 1 1 1
A 0 1
NOT A 1 0
1 - A 1 0
NOT
Twovalued Logic
A OR B NOT A
B A
Multivalued Logic
NOT A
OR max(A,B)
NOT (1 - A)
17
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
if velocity is high and road is wet and brakes are poor then
18
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)
19
(LectureQuality) = 1.0
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% explained below in Section 3.2.2). min ( Lecture Quality, Interest Of Material) = min (1.0, 0.75) = 0.75 (The details of implication and defuzzification are
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.
20
BLACK BOX
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.
21
Fuzzy models that are designed using system identification are based on the use of inputoutput data. System identification was introduced to overcome the difficulties involved in the direct approach of choosing the fuzzy sets 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.
crisp input in R
crisp output in Q
FUZZIFIER
RULES
DEFUZZIFIER
INFERENCE ENGINE
fuzzy input set in R fuzzy output set in Q
22
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.
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.
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.
23
D.
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.
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. centroid method is the most widely used and can be seen in Figure 3.7 below. The
24
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:
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.
25
PENDULUM STABILISED
IS
CONTROLLER
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.
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
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. B. C. D. E. F.
Define rules using linear model output as template Design and label membership functions Decide range of MFs Decide overlap of MFs Design Simulink model and implement controller Redesign/Alter MF properties to improve controllability
27
It is possible to view and see the result of the fuzzy operators that connect them in Figure 3.13.
28
The resulting system plot of this model is shown in Figure 3.14 below.
5 0 r o r r E -5 -10 0 0 23 1 x 10 2 3 4 5 6 7 8 9 10 x 10
22
t o d E
-2
-4 10
22 1 0 x 10
10
P / O
5 6 Time/secs
10
Figure 3.14 Resulting system plot for fuzzy controlled linear model.
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.
29
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.
30
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].
31
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].
Random choice is used to search regions of the search space with likely improvement over the current solution.
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.
32
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.
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).
33
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] 1011010110101110
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.
34
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]:
F ( x) = g ( f ( x))
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:
F ( xi ) = f ( xi )
N IND i =1
f (x )
i
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. F ( x) = af ( x) + b Further mathematical descriptions of the fitness function are given in Section 4.2.7.
35
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.
36
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] Before crossover After crossover
1 0 1 0 1 0
1 1 1 0 1 1
1 0 1 0 1 1
1 1 1 0 1 0
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.
37
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]
Original string:
10110101 10100101
Before mutation
Mutated string:
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.
38
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.
B.
For example, thirty or one thousand generations have been cycled through.
C.
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 doesnt just yield one solution. It is good at identifying simultaneously these other solutions for a problem that by nature has multiple solutions. 39
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 Feature, character, or detector Feature value String position Structure Parameter set, Alternative solution, Decoded structure Non-linearity
Chromosome Gene
Epistasis
40
population is initialised
GENERATIONAL LOOP
41
The plot that results from the SGA in Appendix 3 is shown below in Figure 4.6.
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.
42
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.
43
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 ifthen 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.
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:
A fuzzy if-then rule could then be coded as 3#5#. In a binary scheme each matrix entry itself is a vector of 0s and 1s. This is the chromosome on which the genetic algorithm operates.
44
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).
45
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 wideranging 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 0s and 1s 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.
46
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.
Figure 5.1 PID controlled system with pre-filter Figure 5.2 PID controlled non-linear output plot
47
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.
5 0 r o r r E -5 -10 0
x 10
22
0 23 1 x 10
10
t o d E
-2
-4 10
22 1 0 x 10
10
P / O
5 6 Time/secs
10
Figure 5.4 Resulting system plot for fuzzy controlled linear model.
48
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.
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.
49
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 nonlinear 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.
50
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.
51
References
[1] [2] Figure 2.1, www.engin.umich.edu/group/ctm/examples/pend/invpen.html. Beveridge, W.I.B, The Art of Scientific Investigation, Ch 2 Experimentation, pp15, 1964. [3] [4] [5] Bruton, J., EE4 Control notes, School of Electronic Engineering, D.C.U., 1999-2000 Mathworks, Matlab Fuzzy Logic Toolbox manual. Dadios, Elmer P.; Williams, David J., A Fuzzy-Genetic Controller for the Flexible Pole-Cart Balancing Problem, Proceedings of IEEE International Conference on Evolutionary Computation, pp 223, ISBN: 0 7803 2902 3, Nagoya, Japan, 1996. 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., Darwins Black Box: The Biochemical Challenge to Evolution, Touchstone Simon & Schuster, 1996, Ch2, pp40. [9] [10] Leondes, Cornelius, T., Fuzzy Theory Systems, Academic Press, pp 1098, 1999. 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.
[6]
52
Appendix 1
% % % % M m b i g l Title: Author: Version: Date: = = = = = = 0.5; 0.2; 0.1; 0.006; 9.8; 0.3; Cart-pole linearised model Malcolm Jeffers 1.0 18-03-01
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
53
Appendix 2
A proposed Simulink model including a fuzzy logic controller to control the non-linear system dynamics of the cart and pole
54
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. --------------------------------------------------------------------------% % % % % maximum Number of generations Generation gap, how many new individuals are created Generation gap, how many new individuals are created Binary representation precision No. of individuals per subpopulations
MAXGEN = 100; NVAR = 20; GGAP = .8; PRECI = 20; NIND = 30;
% First, a field descriptor is set up FieldD = [rep([PRECI],[1, NVAR]); rep([-512;512],[1, NVAR]);... rep([1; 0; 1 ;1], [1, NVAR])]; % The population is then initialised Chrom = crtbp(NIND, NVAR*PRECI); % Reset counters Best = NaN*ones(MAXGEN,1); gen = 0; % Defines 'best' in current population % generational counter
% Initial population is evaluated ObjV = objfun1(bs2rv(Chrom,FieldD)); % Best individual is tracked and plotted Best(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 generations while gen < MAXGEN, FitnV = ranking(ObjV); % Fitness-values are assigned to entire pop. % Select individuals
SelCh = recombin('xovsp',SelCh,0.7); % Recombine selected individuals SelCh = mut(SelCh); % Perform mutation on offspring % Evaluate offspring % the using objective funct.
ObjVSel = objfun1(bs2rv(SelCh,FieldD));
[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 % ---------------------------------------------------------------------------
55