This action might not be possible to undo. Are you sure you want to continue?

# B.Eng.

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.

Signed: ...................................................................... Date: ...............................

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

...... 1 2........................................................................................................................................................................................................................................2 Test Case Design................................................... 22 3..............1 What is Fuzzy Modelling? ...............4 Controlling the Linear Model .....................................................................................................5 Controlling the Non-Linear Model ..................... The Inverted Pendulum Test Case ...................................................... 30 iv ............................................................................................ 21 3........................................... 13 2...................................... 2 2.............3 Sugeno Modelling..........................................5 Summary.................................... 16 3.........................................................................................................6 Summary................iv Table of Figures ................................1 Fuzzy Basics ............... 4 2....................................................................3 The Linear Model.....................................................................................................................................................................................................................ii Abstract ....... 5 2.............2................ 18 3.............................................................................. 25 3.................................................Table Of Contents Acknowledgements....................................................................................iii Table Of Contents ............................................................................................................................................. 15 3.............. 14 3...................ii Declaration.................................. Membership Functions and Logical Operators................................2 Fuzzy Logic Models..................................2 Linguistic Variables and Rule Bases .........4 Applying a Fuzzy Logic Controller to the Test Case................................... 7 2.....2.. 26 3..........................................................................................................................1............2....................vi 1............. 15 3......2 Mamdani Modelling.....1 Fuzzy Sets........... 21 3. Fuzzy Logic......................2............................ Introduction...... 3 2....................................................................................................................2...............................1........................1 Why choose the Inverted Pendulum?..........................

.......................................................................................................................................1 Results................................................5 Simple Genetic Algorithm Example...........2................................................................... 41 4............. 50 6...................2..........1 Introduction........................................... Results...............................................................4........................2 Objective and Fitness Functions ...........................................................1 A Quick Overview .......................7 Implications for Test Case ........1........2.........................................................................................................................................................................2..................... Crossover and Mutation ...................................................................... 31 4................................. 47 5........2.......4 The Generation Game ............. 50 6.................2........2...............................................................1 Why Genetic Algorithms? ............................... 35 4........................................................ 39 4.................. 34 4............................................. 55 v ...................2 How Genetic Algorithms work ...3 Reproduction........ 53 Appendix 2 .............................................................................. Conclusion ...2 Implications to Research.................................................................................... 36 4........................................ 46 5..................................................................................................................................... 44 4.............................................................2 Future Research ....................................................................................................................... 48 6.................. Genetic Algorithms.................................................................1 Strength in Numbers ................6 Combining Genetic Algorithms and Fuzzy Logic .......................... 51 Appendix 1.................. 43 4.........................2.................................................... 54 Appendix 3 .......................................................................... 34 4................ 31 4............................8 Summary............ 50 References......................................................................................................... 32 4....................... 47 5....................................................

...........6 Resulting plot showing minimisation of De Jong’s first test function ............................................... 38 Figure 4..............12 Cart-Pole fuzzy control for linearised transfer function ... 26 Figure 3.................................. 43 Figure 5.3 Examples of two-valued and multi-valued logical operations ..........................10 Cart-Pole PID control for linearised transfer function with pre-filter........................................................................ 48 vi ..........7 PID ................................. 8 Figure 2...............5 PID – controlled output plot......... 33 Table 4.......... 12 Figure 2.............................. 10 Figure 2.......................................................................11 PID control for non-linear cart-pole system with pre-filter ........................................................................ 21 Figure 3.......................................................4 Resulting system plot for fuzzy controlled linear model ........................................Table of Figures Figure 2...............................2 PID – controlled non-linear output plot...... 13 Figure 2.........................................................................8 Altered PID ............................................... 26 Figure 3.......13 Visual representation of fuzzy rules .................................................3 Step response of uncontrolled linear model.................. error derivative and overall output ..............6 Mamdani fuzzy control system.......................................................... 11 Figure 2................................... error derivative ...............7 Example of poor membership function overlap...... 27 Figure 3.......11 Linear model output plots showing error........................................ 34 Figure 4...................................................... 47 Figure 5.........................1 Truth tables for the OR and NOT operators .....................................................................4 Example of Binary Mutation ....................................................1 Sample three-dimensional search space for ease of visualisation........................................................................................................ 37 Figure 4........3 Linear model output plots...................................... 41 Figure 4...................9 Cart-Pole control with pre-filter ....................................................controlled output plot (model with pre-filter) ...........6 Cart-Pole PID control with pre-filter .........................5 Mapping of input space to output space ...................................... 13 Figure 3..........................................................7 Diagram showing aggregation and defuzzification........ showing the error..............................................1 PID – controlled system with pre-filter .............. 12 Figure 2.......................2 Matlab code for the cart-pole linearised model .................................................... 9 Figure 2............ 17 Figure 3.................4 Application of rules for lecture attendance example ...1 Cart-pole system.........................................................................................................................................................................9 A comparison of conventional PID and fuzzy control solutions ...................................................................................................................................................................... 28 Figure 3...........1 Comparison of Terminology. 29 Figure 4............. 24 Figure 3................................. 20 Figure 3................................................................. 17 Table 3...................5 Flowchart of Simple Genetic Algorithm ........... 16 Figure 3......................................................... 7 Figure 2................14 Resulting system plot for fuzzy controlled linear model ...... 2 Figure 2....................................... 40 Figure 4............................................. 25 Figure 3............ 9 Figure 2........................2 Sample three-part Gaussian shaped MF .................................. 22 Figure 3.......................................3 Single-point crossover of two chromosomes (k = 4) ........12 PID – controlled non-linear output plot............................... 10 Figure 2.................... 42 Figure 4........8 Implementation of a Sugeno model ....... 48 Figure 5.................................................controlled system output plot...........................................4 Cart-Pole PID control for linearised transfer function......1 Example fuzzy sets ..................2 A sample binary encoded chromosome consisting of a string of 16 bits .......................10 PID – controlled system with pre-filter ......... 28 Figure 3... 47 Figure 5..... 16 Figure 3...........

Introduction Fuzzy logic has been available as a control methodology for over three decades and its application to engineering control systems is well proven. and not to mention the myriad of potential industrial applications. The purpose of this amalgamation of methods is to produce systems whose MIQ (Machine IQ) is considerably higher than those developed using conventional methods. Far – ranging applications exist including space-rocket control. 1 . The conclusion provides a synopsis and some suggestions for future work in the area are given here also. In more recent years the use of fuzzy logic in combination with neurocomputing and genetic algorithms has become popular in control system design. 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. Section four deals with the more comprehensive results that stem from the previous research. In a sense fuzzy logic is a logical system that is an extension of multi-valued logic although in character it is quite different. the work involved will have been thoroughly worthwhile. Section one outlines the cart/pole test case and explains how subsequent research and testing will be related to it. The reader will find that four sections constitute the body of this report. 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. If this report can in any way advance this branch of control engineering. advanced in-car control systems.Chapter 1 1.

. The pendulum angle θ is measured from a vertical line and the cart position x is measured from the centre of the cart track.Chapter 2 2. The outputs of the system are the pendulum angle θ and the cart position x. The pendulum is a rod of mass m and a hinge connects the rod to a moveable cart. The Inverted Pendulum Test Case Figure 2. The cart can move in a horizontal direction only. The movement of the cart is to be controlled so that that the pendulum remains upright. The challenge is to implement stabilising control to keep the system at or as close to equilibrium as possible. θ = θ = 0 where θ symbolises the time derivative of θ The system starts with the pendulum standing vertically. at equilibrium.e. This implies that at x = 0.1 Cart-pole system [1] A common benchmark problem in control theory is that of the inverted pendulum. . 2 . i.

integral. 1. 3.2. a reference model must be designed at the outset of this work. the results will provide an insightful measure of the ability of the method to provide control. When the control method is supplemented with a genetic algorithm optimisation technique that does not benefit from this intuition in Chapter four. yet can be approximated as a linear system if the operating range is small (i. A progressive model can be built. 2. Intuition plays a large part in the human understanding of the inverted pendulum model. 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. It is a non-linear system.e. slight variations of the angle from the norm). 3 .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. 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]. 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 proportional.

006 kg m2 F – force applied to cart x – cart position co-ordinate θ – pendulum angle from the vertical 4 .2 Test Case Design This section outlines the assumptions. The settling time. Assumptions: 1. The system starts in a state of equilibrium meaning that the initial conditions are therefore assumed to be zero. ts.1 Nm-1s-1 l – length to pendulum centre of mass 0.5kg b – friction or cart 0. 2.5kg m – mass of pendulum 0. is to be less than five seconds.3m I – inertia of the pendulum 0. Design Requirements: 1. 4. The system overshoot value is to be at most 22. 3. Only the angle of the pendulum is to be controlled in this model so the cart’s position will not be documented. The pendulum does not move more than a few degrees away from the vertical to satisfy a linear model.5%.2. 2. design requirements and physical properties of the model and its components. ts < 5secs os < 22. A step input is applied.5% Properties of the Cart and Pendulum: M – mass of cart 0.

.. . Summing the forces perpendicular to the pendulum for the second equation of motion: P sin θ + N cos θ − mg sin θ = ml θ + m x cos θ [1] . Summing the horizontal forces on the pendulum to get N: N = m x + ml θ cos θ − mlθ 2 sin θ . . Summing the horizontal forces on the cart: M x+ b x + N = F . It will be assumed that θ = π + φ where φ is a small angle from the vertically upward direction.. The computing package to be used deals well only with linearised models making it necessary to linearise the model about θ = π . 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 θ . . cos θ = −1 sin θ = −φ dθ =0 dt 2 5 ... 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] . ... .2..3 The Linear Model The force analysis shown here yields the linear transfer function for the model.. Therefore. .. . .

.. [2]) become (where U is the input): ( I + ml 2 )θ − mgl sin φ = ml x ( M + m) x + b x − ml φ = U . .... 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 6 . To obtain the transfer function of the linearised system it is necessary to first form the Laplace transform of the equations above: ( I + ml 2 )Φ ( s) s 2 − mglΦ ( s ) = mlX ( s ) s 2 ( M + m) X ( s) s 2 + bX ( s ) s − mlΦ ( s) s 2 = U ( s ) Since Φ is to be the output it is helpful to solve for X(s): ( I + ml 2 ) g X (s) = − 2 Φ(s) s ml Substituting into the second equation ( I + ml 2 ) g ( I + ml 2 ) g ( M + m) − 2 Φ ( s) s 2 + b − 2 Φ ( s ) s − mlΦ ( s ) s 2 = U ( s ) s s ml ml Rearranging gives: Φ(s) = U (s) Where 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. .After linearisation the two equations of motion ([1]. .

% 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.0000 0. den values %num = 4. The controller will be refined using a pre-filter in order to meet the design criteria. 3.1818 -31. 1. Matlab is a high-performance language for technical computing. The Matrix Laboratory package MATLAB has been chosen as the language in which all programs developed in this report will be written. The Matlab code that represents the linearised model is summarised in Figure 2.4545 Figure 2.2: Matlab code for the cart-pole linearised model 7 .4 Controlling the Linear Model The experiment work documented here will take place in the following order: 1. q = (M+m)*(i+m*l^2)-(m*l)^2.2. 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.1818 -4.5455 0 %den = 1. Simulink is a visual drag-and-drop interface suitable for modelling feedback control systems. 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.2. 2. A full version of the program can be found in Appendix 1. The step response of the linear model given by the transfer function (and constants specified) above will be plotted.

Using the design conditions of Section 2.37 KD = 0.49 KI = 2. To implement a proportional-integral-derivative controller the three numerical values of P.67 8 .3 Impulse response of uncontrolled model Figure 2. KP = 8.3 above shows that within approximately six seconds the system goes very unstable. is picked to be equal to 1. the undamped natural frequency.85 radians s-1. Impulse Response 14 x 1013 From: U(1) 12 10 Amplitude To: Y(1) 8 6 4 2 0 0 1 2 3 4 5 6 Time (sec. The next step of implementing a PID controller will attempt the curb the instability.) Figure 2. Following the ITAE design process [3] and using a step input it is possible to arrive at the following optimum coefficients.2 ω n .This is the impulse response that results. I and D must be evaluated. 2. 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.

19s) it is necessary to limit the overshoot. Even though the settling time is quite good (5. (Figure 2.4) Figure 2.19s tr = 0.5 PID – controlled output plot An overshoot of 216% is considerably greater than the design parameter of 22.9% ts = 5. 9 .08s Figure 2.5%.Using these values it is possible to design the PID controller in Simulink.29s tp = 1.5 below.4 Cart-Pole PID control for linearised transfer function The output plot and properties result from this simulation are seen in Figure 2. os = 216.

4s tp = 30s 0.6 0.7s. Analysis of PID-control for the linear cart-pole model 1.0085% p m a / t u o y 0.4 1.2 0 0 5 10 15 tout/s 20 25 30 Figure 2. This has been at the expense of the settling time.6. The Simulink model for this system is seen in Figure 2.7 shows that the problem of excess overshoot has been dramatically reduced.5 below. The way to resolve this controllability difficulty will be to investigate and use an alternative pole placement technique.7s tr = 28. which is now 12. 10 .2 1 os = 0.6 Cart-Pole PID control with pre-filter The output plot and properties result from this simulation can be seen in Figure2.4 0.To limit this overshoot the ITAE designed PID controller is implemented with an appropriate pre-filter P(s). Figure 2.8 ts = 12.7 PID – controlled output plot (model with pre-filter) Figure 2.

8. a= -10.4225 with poles at –0. T * (s) = 3.8% ts = 5.4225 s + 11.4318 and ω n = 1.5976 s + 19. The next section will introduce a pre-filter to aid the design.4225 2 s 2 + 1.5976 s + 3.1. KI = 8. 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.51 and KD = 2.7988 +/.6687j Choosing a pole whose real part is a factor of six or greater than any existing pole [3].25s tr = 0. the ideal second order transfer function is: T (s) = 3. os= 88.8 Altered PID – controlled system output plot Although an improvement in overshoot has resulted the design conditions have not yet been met (os <22.51.3985s + 34.16s tp = 0. The output plot that results is shown in Figure 2.225 2 Obtaining optimum coefficients as before yields KP = 11.13.5%).85 rad s-1. 11 .The ideal second order transfer function takes the form T ( s ) = ωn2 s 2 + 2ζω n s + ω n 2 Given that this system has ζ = 0.77s Figure 2.

ts <5s) 12 .13s + 8. (os < 22.99s Figure 2.3.51s + 11.5%.51 2.51 The model and plot that result are given in Figure 2.32s tp = 1.10 PID – controlled system with pre-filter The alternative pole-placement technique and additional pre-filter have now enabled both design criteria to be met for the PID controller. A pre-filter is introduced so that the overall closed loop transfer function will not have any zeroes.64s tr = 1.9 Cart-Pole control with pre-filter os = 21. The pre-filter introduced has the form: P( s) STEP = P ( s ) STEP = KI KDs + KPs + KI 2 2 8.10 respectively. Figure 2.6% ts = 4.9 and Figure 2.

11 PID control for non-linear cart-pole system with pre-filter Figure 2. It is necessary to change the cart/pole physical properties within this function block to be congruent with the model developed above.11 and the plot that results in Figure 2.12 PID – controlled non-linear output plot 13 .9 are already packaged as a Simulink function.2. The non-linear dynamics represented by a diagram in Figure 2.12. 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. The model (with pre-filter) is shown in Figure 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. Figure 2.

The function of this test was primarily to show that a more sophisticated method is required. This inability of the controller to stabilise the non-linear system was not unexpected. 14 . The need to provide a reference or benchmark system was outlined and a linearised model was derived with this objective. An unsuccessful attempt to control the non-linear system dynamics showed that a more sophisticated approach is warranted. The model’s impulse response was found. Optimum PID coefficients were found using the ITAE method.6 Summary The pole/cart system has been introduced as a suitable non-linear model for study in this report.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. confirming its non-linearity. 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. A summary of the design steps is given below. Further improvement was gained by designing a pre-filter and a satisfactory model resulted. 2.

The elements they contain may only have a partial degree of membership. Membership Functions and Logical Operators Fuzzy sets are sets without clear or crisp boundaries.1 Fuzzy Sets. logical operators. 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. The following sections will present and develop ideas such as sets. 15 . They are therefore not the same as classical sets in the sense that the sets are not closed. Fuzzy Logic 3. 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. 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.Chapter 3 3. membership functions.1 Fuzzy Basics The primary objective of fuzzy logic is to map an input space to an output space. Some examples of vague fuzzy sets and their respective units include the following. 3. This is really the only stringent condition brought to bear on a MF. linguistic variables and rule bases.1.

Below in Figure 3.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. Figure 3. µ 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.1 some example fuzzy sets can be observed. trapezoidal and gaussian functions.2 Sample three-part Gaussian shaped MF 16 . The shape is chosen on the basis of how well it describes the set it represents.

1 Truth tables for the OR and NOT operators A B Twovalued Logic A OR B NOT A A B A Multivalued Logic NOT A OR max(A.1] set.A 1 0 NOT Table 3.Fuzzy logic reasoning is a superset of standard Boolean logic yet it still needs to use logical operators such as AND.8 can represent both not-quite-false and not-quite-true respectively.3 Examples of two-valued and multi-valued logical operations 17 .A) Figure 3. other intermediate values are also allowed.B) NOT (1 . OR and NOT.2 and 0.B) 0 1 1 1 A 0 1 NOT A 1 0 1 . but two-valued logic operations like AND. 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’.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. It will be necessary to do logical operations on these values that lie in the [0. max and additive complement will have to be used. the functions min. The following examples for the OR and NOT logical operations shown in Table 3. For example the values 0. OR and NOT are incapable of doing this.1 and Figure 3. For this functionality.

then ‘y is B’. if ‘x is A’. 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.3. if temperature is very hot then fan is on and throughput is reduced 18 . The conditional statements that make up the rules that govern fuzzy logic behaviour use these linguistic variables and have an if-then syntax. 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. For example.1. 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. A linguistic variable such as ‘High Speeds’ for example could consist of numbers that are equal to or between 50km/hr and 80km/hr.2 Linguistic Variables and Rule Bases Linguistic variables are values defined by fuzzy sets. the consequent of a rule may contain multiple parts. and a conclusion. if velocity is high and road is wet and brakes are poor then… Similarly. These if-then rules are what make up fuzzy rule bases.

The outputs of fuzzy sets from each rule are aggregated into a single output fuzzy set. The following example in Figure 3. Thirdly.Interpreting these rules involves a number of distinct steps. the inputs must be fuzzified. fuzzy operators are applied for antecedents with multiple parts to yield a single number between 0 and 1. 1. The degree of support for the entire rule is used to shape the output fuzzy set. the result is applied to the consequent. If (LectureQuality is Poor) or (InterestOfMaterial is Boring) then (Attendance is Low) 19 . To do this all fuzzy statements in the premise are resolved to a degree of membership between 0 and 1. Secondly.4 shows how these steps are applied in practice. This step is also known as implication. Firstly. This final set is evaluated (or defuzzified) to yield a single number. Again this is the degree of support for the rule. Consider the two rules for a fuzzy model that evaluates lecture attendance: 1. 2. This can be thought of as the degree of support for the rule. If (LectureQuality) is Good) and (InterestOfMaterial is Interesting) then (Attendance is High) 2. 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. 3.

min ( µ Lecture Quality. 20 .0 µ (Interest Of Material) = 0.2). applying fuzzy operators and then defuzzifying to produce a single crisp output is known as fuzzy inference. as will be shown later. Fuzzy sets can be combined using fuzzy rules to define system behaviour and thus complex non-linear systems can be expressed linguistically.4. In fact.4 Application of rules for lecture attendance example 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.These rules can then be seen visually in Figure 3.75 Figure 3. hence for an input of 50% for LectureQuality and 80% for InterestOfMaterial. This progression of modelling is discussed in detail in the next section. The process of fuzzifying a single crisp input. rule tables can represent fuzzy controllers. 0. the defuzzified attendance percentage works out to be 75% explained below in Section 3.2. µ Interest Of Material) = min (1. In this example both the fuzzy AND operator and the implication operator use the min function. µ (LectureQuality) = 1.75) = 0.75 (The details of implication and defuzzification are This comes from Immediate advantages of this approach become apparent.0.

INPUT SPACE (ALL POSSIBLE I/P'S) OUTPUT SPACE (ALL POSSIBLE O/P'S) BLACK BOX Figure 3. Since the objective is to map inputs to outputs it becomes possible to model non-linear systems. let alone efficient. The first is a method called the direct approach and the second is by using system identification. fuzzy logic systems are tolerant of imprecise data. Consider the following diagram in Figure 3. For complex systems (non-linear for example) tuning these membership functions would require the adjustment of many parameters simultaneously. Fuzzy logic systems can use both numerical information and linguistic information to perform a mapping of an input to an output space.3. This is one of fuzzy logics’ greatest advantages. 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.2. 21 .5.2 Fuzzy Logic Models 3.1 What is Fuzzy Modelling? Standard control techniques use numerical data to relate input and output signals. 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. Understandably no human expert could accomplish this. 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. The rules that govern this mapping can be acquired through two methods. Put differently.5 Mapping of input space to output space Many different control mechanisms could reside within the black box but in this case the mechanism will be confined to a fuzzy logic system. even complex ones.

It is shown in the Figure 3.6 below. fuzzy rule base. crisp input in R crisp output in Q FUZZIFIER RULES DEFUZZIFIER INFERENCE ENGINE fuzzy input set in R fuzzy output set in Q Figure 3. The system identification method is covered in Chapter four. 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 controller consists of a fuzzifier.2.6 Mamdani fuzzy control system [5] 22 . 3.Fuzzy models that are designed using system identification are based on the use of inputoutput data. 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.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]. an inference engine and a defuzzifier. They are both introduced in the following sections.

This can only be achieved after each rule has been evaluated and is allowed contribute its ‘weight’ in determining the output fuzzy set. Application of Fuzzy Operators C. 23 . C. Application of Fuzzy Operators In the case where multiple statements are used in the antecedent of a rule. Application of Implication Method This part of the Mamdani system involves defining the consequence as an output fuzzy set.1. Fuzzification can be implemented using lookup tables or as in this report. using membership functions. 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. Fuzzification of Inputs B. Defuzzification of Aggregated Output A.1. Application of Implication Method D. This Mamdani controller is realised using the following steps. 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. B. Aggregation of all Outputs E. A. it is necessary to apply the correct fuzzy operators as explained in 3.Conventional control systems require crisp outputs to result from crisp inputs. This allows the antecedent to be resolved to a single number that represents the strength of that rule.

The order of rules in the aggregation operation is unimportant as all rules are considered. max (maximum value of each rule output set) and the probabilistic OR method (the algebraic sum of each rules output set). Aggregation of all Outputs The fuzzy outputs of each rule need to be combined in a meaningful way to be of any use. The Window showing aggregation => (Red line indication Centroid) 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. An example of the aggregation process using the max operator can be seen in Figure 3. centroid method is the most widely used and can be seen in Figure 3. The three methods of aggregation available for use include sum (sum of each rules output set). Defuzzification of Aggregated Output The aggregated fuzzy set found in the previous step is the input to the defuzzifier. 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. As indicated in the model shown in Figure 3.7 Diagram showing aggregation and defuzzification 24 .7 below. E. Aggregation is the method used to perform this by combining each output set into a single output fuzzy set.D.7 below.

5input1 .2.3 Sugeno Modelling The Sugeno fuzzy model. The output is always a single number and in this case having the inputs [0. The output membership functions can be thought of as singleton spikes that undergo a simple aggregation instead of other aggregation methods such as max. or more fully the Takagi-Sugeno-Kang method of fuzzy inference was first introduced in 1985 [4]. All three rules have been written using the OR connector. 0. Figure 3. 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 Sugeno system is computationally efficient and its ability to interpolate multiple linear models makes it particularly suited to modelling non-linear systems. 25 .3. 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). probor or sum.64output1].8 input2] the output is [6.8 shows the application of three basic rules for a Sugeno model.8 Implementation of a Sugeno model Figure 3.

The model used to produce these plots is shown below in Figure 3. The error and error derivative plots are also used. Figure 3.9 gives a visual review of what has been discussed up to this point.9 A comparison of conventional PID and fuzzy control solutions PENDULUM STABILISED IS CONTROLLER Rules for the fuzzy system are to be extracted from the output plot of the linearised. Figure 3. PID controlled system with pre-filter. REASONING WITH FUZZY FUZZIFY CONTROL RULES DEFUZZIFY CONTROL SIGNAL STABILISE AN INVERTED PENDULUM APPLY PID LINEARISE DESIGN PID CONTROLLER Figure 3.3.2.10 Cart-Pole PID control for linearised transfer function with pre-filter 26 .10.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.

B.11 above can be analysed for clues about the relationship between the error. Figure 3. A. The direct approach described in Section 3. 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 .11 Linear model output plots showing error. C.Figure 3. D.1 (manual writing of fuzzy rules) the approach utilised in this part of the design. error derivative and output that will aid controllability. F.2. Design of the fuzzy controlled non-linear Simulink model will also be attempted. E. A Using the procedure below a seven-rule fuzzy inference system can be designed. error derivative and overall output The subsequent section will outline the design of a fuzzy controller (with the linearised transfer function model) in Simulink.

13 Visual representation of fuzzy rules 28 . If (e is Small) and (edot is Small2) then (output is High) 5.13.The Simulink model created can be seen below in Figure 3. Figure 3. If (e is Zero) and (edot is Zero2) then (output is High) 6.12 Cart-Pole Fuzzy control for linearised transfer function The rules for this fuzzy controller are: 1. If (e is Medium) and (edot is Big) then (output is Average) 4. 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. Figure 3.12. 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) or (edot is Big) then (output is Average) 7.

Nevertheless. A Sugeno model should have been used to provide outputs that drive the controller in a better fashion. the fuzzy controller has been unsuccessfully implemented. 29 .14 below. 1.13 no improvement can be found. 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 2 3 4 5 6 7 8 9 10 P / O 5 0 0 1 2 3 4 5 6 Time/secs 7 8 9 10 Figure 3. Despite altering the rules shown above in Figure 3. a Simulink model of the non-linear system has been proposed in Appendix 2. 2. The membership function overlap may be too slight leading to poor controllability. The reason for this could be any or a combination of the following.The resulting system plot of this model is shown in Figure 3. In conclusion. Best practice insists that the fuzzy controller for the linear model be developed extensively before applying it to the non-linear model. The direct approach has been used here. 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. System identification may be the only way that membership functions can be shaped to adequately provide control that meets the design criteria.

2. 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 Mamdani model is preferred when a linguistic description of both the input and output membership function is desired. Two fuzzy controllers have been introduced so far. 30 . yet differ in the output membership functions (singleton spikes are used in the Sugeno model) and hence the aggregation method also. logical operators. The method of system identification will now be investigated.3. Both models use identical schemes of fuzzifying and use of fuzzy linear operators. linguistic variables and use of fuzzy rules has proven necessary in the design of fuzzy control systems. The understanding of fuzzy sets. membership functions.5 Summary The terms and concepts of fuzzy logic have now been introduced. the Mamdani and Sugeno models. The Sugeno model has shown itself to have better potential in controlling non-linear systems that require more stringent output membership functions. Research into genetic algorithms can now be carried out with this purpose in mind.

They are a powerful search technique. The search method they use is robust since it is not limited like other search methods with regard to assumptions about the search space. Genetic Algorithms 4. They have been successfully applied to self-adaptive control systems and to function optimisation problems. These factors and the relative benefits of GAs will be investigated in the following sections. This process of breeding ‘fitter’ generations comprises the essence of the genetic algorithm approach. Genetic Algorithms have become popular due to several factors. 31 . 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]. yet are computationally simple. Genetic algorithms attempt to mimic the properties of natural selection and natural genetics.1 A Quick Overview In 1960 the first serious investigation into Genetic algorithms (GAs) was undertaken by John Holland [6]. 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.Chapter 4 4.

not a single point. The calculus-based methods can either be direct or indirect. Although this is a simple approach it is highly inefficient for a large search space. that is. By encoding the parameter set GAs can compare solutions and produce new ones in a very efficient way.4. they both seek local optima by solving the derivative of a set of non-linear equations. Random search methods tend to explore regions of the search space more quickly yet tend to perform no more efficiently than enumerative schemes. GAs search from a population of points. It is informative to look at the properties of GAs as well as simultaneously comparing how they differ from traditional search methods [6]. GAs use probabilistic transition rules. The problem of only finding local extrema is resolved. Enumerative search methods look at objective function values for every point of the search space. Random choice is used to search regions of the search space with likely improvement over the current solution. A.1. They are local in scope. B. 32 . not the parameters themselves. one at a time. GAs work on an encoding of the parameter set. All of these search techniques discussed lack a robustness and reliability that GAs can provide.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. Peaks of the search space can be climbed concurrently since the GA works with a large number of points simultaneously. enumerative or random based. C. not deterministic ones.

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.

33

**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] 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

**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]:

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

4. The crossover operator is explained in the next section.3 Reproduction. to extend the analogy. [7]) it will suffice to mention here that the selection algorithm should possess the following properties: i. 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.2. The stochastic universal sampling selection fulfils these conditions of zero bias and minimum spread. The Matlab stochastic universal sampling function sus. Minimum spread (an indication of consistency) – Individuals should be selected a minimum of et (i) times (floor) and maximum of et (i) times (ceiling).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. While these selection algorithms are detailed elsewhere ([6]. ii. 36 . Zero bias (an indication of accuracy) – Individuals should be selected entirely on their position in the population. this process of selection must be used to decide the number of times a particular individual will be chosen for reproduction. Before reproduction can take place. This is repeated for all the selected strings and then these copies or replicas undergo crossover operations at random.

Different types of crossover are possible such as.3.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. Crossover produces new individuals that have parts of both parents genetic material. multi-point. is the operator that produces new chromosomes.Crossover Crossover.m. Note that the crossover operator is applied with a chosen probability and this means that not all chromosomes in the population may undergo crossover. 37 . An example of single-point crossover can be seen in Figure 4.” [6] Before crossover After crossover 1 0 1 0 1 0 k 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 0 X Y X’ Y’ Figure 4. uniform and shuffle. but the simplest of all is single-point crossover. L-1]. 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. akin to nature. Two new strings are created by swapping all characters between positions k+1 and L inclusively. Single-point crossover will be implemented using the Matlab function xovsp. also known as recombination.

Another improvement may be to have an adaptive mutation rate that is a function of population convergence.m. An example is given in Figure 4.” [8] The difference with GAs is that a mutation only ever causes a single bit to change its state. 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.4. It is for this reason mutation is used to explore the search space as efficiently as possible. Binary mutation will be performed using the Matlab function mut. Therefore a ‘1’ chosen at random will be changed to a ‘0’ and vice versa. It can be thought of as a secondary background operator. “In biochemistry a mutation is a change is DNA. Mutations are needed in GAs because while reproduction and crossover explore the search space well. Single bits are neither added nor left out – just switched. 38 . To be inherited.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. occasionally they lose information that is essential to the solution. the change must occur in the DNA of a reproductive cell.4 Example of Binary Mutation 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. [7] Original string: 10110101 10100101 Before mutation Mutated string: After mutation Mutation point (in bold) Figure 4.01).001->0. This induces better searching even after many generations. Mutation is applied randomly to an entire population and with low probability (0. a single nucleotide can be added or left out when the DNA is copied during cell division.

This leads to the problem of deciding what individuals should be replaced. Reinsertion is performed using the Matlab function reins. A. when any new solutions are very close to the previous generations solutions.m. C. and finally decoding can start all over again. In determining how many cycles of the GA should take place. For example. In time therefore. B.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. For example. applying crossover and mutation. and the fittest individuals will approximate acceptable solutions. or be continued with extended design criteria. The average fitness of the total population will then increase. thirty or one thousand generations have been cycled through. When a mean deviation in the population is obtained. When a particular point in the search space has been found. as long as there are a sufficient number of generational cycles each individual in the population will eventually be replaced.4. In this series of tests the oldest members will be replaced first. It is good at identifying simultaneously these other solutions for a problem that by nature has multiple solutions.2. The GA allows the user to pick from a number of potential solutions – it doesn’t just yield one solution. For example. the greatest extrema found up to this point in time. When a certain upper limit of generations has been reached. If after meeting one of these criteria no individuals in the population yield acceptable solutions to the problem at hand. the new individuals need to be reinserted into the original population. the GA may simply run again. any of the following stopping criteria can be employed. 39 . relative fitness. To keep the size of successive populations the same as the original population in the process outlined above. using the mean as a reference.

Genetic Algorithm Natural String Feature. The corresponding terms that are more commonly used by biologists are also included. or detector Feature value String position Structure Parameter set. character.In Table 4. Alternative solution.1 above a summary of the terms commonly used in the field of genetic is given. Decoded structure Non-linearity Chromosome Gene Allele Locus Genotype Phenotype Epistasis 40 .

5 Flowchart of Simple Genetic Algorithm 41 .2. parameters are specified number of individuals maximum number of generations number of variables precision of variables generation gap matrix to hold chromosomes is built population is initialised initial population is evaluated while generation number < max number of generations Fitness values are assigned Individuals are selected Count of generations is increased New individuals placed into population GENERATIONAL LOOP Crossover takes place Objective function evaluates new generation Mutation operator used Figure 4.5 and a simple genetic algorithm program written in Matlab can be found in Appendix three.5 Simple Genetic Algorithm Example A flowchart showing the stages of a simple genetic algorithm is given below in Figure 4.4.

42 .6 Resulting plot showing minimisation of De Jong’s first test function With a maximum of 100 generations being controlled by the while loop of the program. crossover. the cycle of events (assignment of fitness functions.6. mutation.The plot that results from the SGA in Appendix 3 is shown below in Figure 4. as evidenced by the plot above. Figure 4. selection. evaluation and reinsertion) in the while loop force an improvement in the solution over time.

The challenge is to find ways of constraining elements of the rule base. An algorithm that will perform this function will now be outlined followed by a suggested rule coding technique. 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.2. 43 . control will remain smooth. so that as the GA informs the rule base. an appropriate fitness function definition [9]. In essence. This leads to abrupt control and at worst meaningless membership functions.6 Combining Genetic Algorithms and Fuzzy Logic It was noted in Section 3. Figure 4. The greatest potential problem with this approach is that the membership functions that the GA may prescribe will have little overlap. GAs have great potential in fulfilling this role. and a description of the algorithm development.7 Example of poor membership function overlap This section describes how genetic algorithms can be used for generating fuzzy if-then rules.4.4 that the difficulty with fuzzy control is in determining suitable membership functions and rules.2.

Unless a stopping condition has been met the algorithm restarts at step two. Each fuzzy if-then rule in the current population is evaluated. Generate an initial population of if-then fuzzy rules. mutation and crossover.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. 3. 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. 44 . Some individuals in the current population are replaced with the newly generated rules according to some replacement strategy (see Section 4. 5. In a binary scheme each matrix entry itself is a vector of 0’s and 1’s. 1. CODING OF A FUZZY RULE SET Each fuzzy if-then rule is coded as a string. The algorithm may be thought of as having the following structure. and is coded by its antecedent fuzzy sets. The population is updated by generating new fuzzy ifthen rules from the current population using the genetic operations of selection. mutation and crossover. 2.2. This is the chromosome on which the genetic algorithm operates. 4.4). 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#”. New fuzzy if-then rules are generated using the genetic operations of selection.

2. 4.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 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. An objective function (Objpush. Using standard selection procedures a pair of parent strings from the current population is used to generate new fuzzy if-then rules (new strings). The overall population size is kept constant and every time the GA finds an optimum value.m) for the pole/cart problem can be found in the Matlab GA Toolbox [7]. The fitness value of each rule is defined by the fuzzy rule-based classification system that relates to the current population S. 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. 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 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. 45 . Fuzzy if-then rules in S are evaluated by classifying each training pattern by S. 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).DEFINING THE FITNESS FUNCTION Consider the current population to be represented by S.

and the worst rules with the lowest fitness values are discarded and replaced with newly generated rules keeping the overall population size constant. enumerative search methods and random search methods lack and yet are efficient and wideranging in their search performance. It is recommended that an s-function be written to combine a developed GA program with the fuzzy logic controller. crossover and mutation procedures are applied to generate new fuzzy if-then rules (new strings). reproduction. A suggested rule coding technique. Standard selection. 46 . Every time the GA finds an optimum value the fuzzy system parameters are saved to a file.2.4.8 Summary Genetic algorithms show a robustness and reliability that calculus-based methods. The process of breeding ‘fitter’ generations of solutions comprises the essence of the genetic algorithm approach. mutation and reinsertion. crossover (recombination). A sample genetic algorithm program shows how the concepts of GAs are implemented. They run in cycles called generations and operate on chromosomes – binary encoded solutions in this case. Also outlined were the stopping conditions that determine how many cycles of the GA may run. The concepts of the objective and fitness function have been introduced along with the primary structures in GAs. 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.

Next. 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. Firstly.32s tp = 1. an appropriate PID controller with pre-filter was designed to meet the design constraints (overshoot <22. This output is shown in Figure 5.1 Results The pole/cart system has been introduced as a suitable non-linear model for study in this report.64s tr = 1. Results 5.6% ts = 4. A more sophisticated control technique is needed. 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.1 below.1 PID – controlled system with pre-filter Figure 5.2 PID – controlled non-linear output plot 47 . This leads to the introduction of fuzzy logic systems. os = 21.99s Figure 5. settling time <5 seconds).5%. The output quickly goes unstable (within a few seconds). This same controller was then used to control the non-linear pole/cart dynamics yielding the uncontrolled output shown in Figure 5.5.2.

12 the resulting system plot of this model was Figure 5. error derivative and overall output After implementing this fuzzy logic controller in the Simulink model of Figure 3.4 Resulting system plot for fuzzy controlled linear model. 5 0 r o r r E -5 -10 0 x 10 22 0 23 1 x 10 2 3 4 5 6 7 8 9 10 t o d E -2 -4 10 22 1 0 x 10 2 3 4 5 6 7 8 9 10 P / O 5 0 0 1 2 3 4 5 6 Time/secs 7 8 9 10 Figure 5. Figure 5. 48 .3 Linear model output plots showing the error. It is shown below in Figure 5.3.4 below.The rules for the fuzzy controller proposed were extracted from the output of the linear PID controlled model.

The results from the linear model analysis in Chapter two appropriately directed the flow of research into fuzzy logic in Chapter three.2 Implications to Research The design model developed has proven to be sound.13. In conclusion. the fuzzy controller was unsuccessfully implemented. 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. No improvement resulted by altering the rules shown in Figure 3. The method of system identification using genetic algorithms was then investigated. Further suggestions for future research have been outlined in Section 6.2 of the next chapter. This research may have prospered more greatly if a Sugeno model had been used. The need for a better method of rule generation than the direct approach was needed. 5. The research in Chapter three made clear that the direct approach of selecting the fuzzy if-then rules for this system is not successful. No results are available for this section due to the blockade encountered when attempting to create the appropriate GA-fuzzy functionality in Simulink.This output shows that it has not been possible to stabilise or level off the output. 49 . This implies that the research into the system identification method investigated in Chapter four was worthwhile.

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. 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. 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.6. Conclusion 6. Yet it is possible that even with this alternative model the output response of the linear cart/pole model may remain uncontrolled. 6.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.2 Future Work As suggested in Section 5. 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. It has been observed how genetic algorithms exhibit robustness and are both an efficient and wide-ranging optimisation technique.1 Introduction The ‘Inverted Pendulum on a Cart’ test case chosen to aid research into this area has been satisfactory. 50 . 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. The primary aim has been to control the non-linear model.

m) for the system can be found in the Matlab GA Toolbox [7].No genetic algorithm program dealing with the cart/pole model has been given in this report but note that an objective function (Objpush. 51 . This is essentially the next logical step in the development of controlling the cart/pole system. 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.

Ruspini. “Genetic Algorithm Toolbox”. J. Goldberg. “Darwin’s Black Box: The Biochemical Challenge to Evolution”. David J. Fonseca. Int.engin. W. www. “Matlab Fuzzy Logic Toolbox manual”.I. Williams. No. Studies. Mass: Addison-Wesley Pub. Fleming. [9] [10] Leondes. EE4 Control notes. “An experiment in linguistic synthesis with a fuzzy logic controller”.html. 1964. J. 1996. Elmer P.2.References [1] [2] Figure 2. “A Fuzzy-Genetic Controller for the Flexible Pole-Cart Balancing Problem”.. University of Sheffield.U.B. Ch 2 Experimentation.. 1992. 1. “Fuzzy Theory Systems”. pp15. Version 1.. pp 1098. Nagoya. 1989.edu/group/ctm/examples/pend/invpen. 1975. Academic Press.umich. “Introduction to fuzzy set theory and fuzzy logic: basic concepts and structures”. Proceedings of IEEE International Conference on Evolutionary Computation. 1999-2000 Mathworks. Carlos. Dadios.. [7] Chipperfield. Optimization and Machine Learning”.C. Andrew. 7. [6] 52 .1. Ch1. Pohlheim. Peter. Man Mach. Michael J. D. [11] Mamdani. School of Electronic Engineering. “Genetic Algorithms in Search. Japan. T. Touchstone Simon & Schuster. 1999... “The Art of Scientific Investigation”. pp 223. Enrique. pp 1-13.. Assilian. Co. [8] Behe. 1996. Cornelius. E. pp40. ISBN: 0 7803 2902 3. pp7. Beveridge. Vol.. David E. [3] [4] [5] Bruton. S. Ch2. Hartmut..H.

0.3.8.den.t) axis([0 1 0 60]) % Output figure shows the response to an impulse input %The constants above yield the following num.1.5.4545 Appendix 1: Matlab code for the cart-pole linearised model 53 . 9.01:6. 0.0000 0.1818 -4.1818 -31.006.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.2. Cart-pole linearised model Malcolm Jeffers 1. 0.5455 0 %den = 1. impulse(num.Appendix 1 % % % % M m b i g l Title: Author: Version: Date: = = = = = = 0. 0. den values %num = 4.

Appendix 2 A proposed Simulink model including a fuzzy logic controller to control the non-linear system dynamics of the cart and pole 54 .

num2str(Best(gen+1))]. ylabel('log10(f(x))'). rep([1.['Best = '. PRECI = 20. num2str(Best(gen+1))]. % Reinsert offspring % into current pop. GGAP).7).'bo'). % Recombine selected individuals SelCh = mut(SelCh). NVAR])].'normalized'). SelCh = recombin('xovsp'.FieldD)).5. plot(log10(Best). % Best individual is tracked and plotted Best(gen+1) = min(ObjV).'Units'.1). NVAR*PRECI). % Fitness-values are assigned to entire pop. ObjVSel = objfun1(bs2rv(SelCh. NIND = 30.1].512]. % Select individuals SelCh = select('sus'. text(0.['Best = '. drawnow.SelCh. how many new individuals are created Binary representation precision No.'ro').[1. how many new individuals are created Generation gap. plot(log10(Best).[1. rep([-512. % Loop to control number of generations while gen < MAXGEN. of individuals per subpopulations MAXGEN = 100. NVAR]). % The population is then initialised Chrom = crtbp(NIND.95. gen = 0. FitnV. 0.1. % Update display and record current best individual end % --------------------------------------------------------------------------- 55 . title('Simple GA example .. [1.5. GGAP = .to minimise De Jongs 1st test function'). NVAR = 20.0.1. FitnV = ranking(ObjV).ObjVSel). gen = gen+1.SelCh. NVAR]). text(0.. % Defines 'best' in current population % generational counter % Initial population is evaluated ObjV = objfun1(bs2rv(Chrom. xlabel('generation'). 1 . --------------------------------------------------------------------------% % % % % maximum Number of generations Generation gap. % Increment generational counter Best(gen+1) = min(ObjV).'Units'.8. % Reset counters Best = NaN*ones(MAXGEN..0. [Chrom ObjV]=reins(Chrom.95.xlabel('Generations'). drawnow.'normalized').0. % First.ObjV.FieldD)).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. Chrom. a field descriptor is set up FieldD = [rep([PRECI]. % Perform mutation on offspring % Evaluate offspring % the using objective funct. ylabel('log10(f(x))').