Pid Ga Keren Itae

Embed Size (px)

Citation preview

  • 8/3/2019 Pid Ga Keren Itae

    1/71

    i

    DUBLIN CITY UNIVERSITY

    SCHOOL OF ELECTRONIC ENGINEERING

    Real-time adaptive PID control

    of a non-linear process

    based on genetic

    optimisation

    Kieran O Donoghue M.Eng

    August 2002

    MASTER OF ENGINEERINGIN

    Telecommunications

    Supervised by Miss Jennifer Bruton

  • 8/3/2019 Pid Ga Keren Itae

    2/71

    ii

    Acknowledgements

    I would like to thank my supervisor Miss Jennifer Bruton for her guidance, enthusiasm and

    commitment to this project. I would also like to thank my family and friends for theirsupport throughout the duration of my education.

    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: ...............................

  • 8/3/2019 Pid Ga Keren Itae

    3/71

    iii

    Abstract

    Proportional Integral Derivative (PID) controllers are widely used as a means of controlling

    system outputs. Many techniques have been developed to obtain the optimum PID

    parameters for a particular system. In this report genetic algorithms are proposed as a

    method for PID optimisation of non-linear systems and compared with those of traditional

    optimisation methods. A simple genetic algorithm is designed to solve this non-linear

    problem based on genetic algorithm theory. An example is presented in which the water

    level of a non-linear process is regulated using a PID controller whose parameters are

    optimised using the genetic algorithm proposed.

  • 8/3/2019 Pid Ga Keren Itae

    4/71

    iv

    Table Of Contents

    Acknowledgements ................................................................................................................ii

    Declaration .............................................................................................................................ii

    Abstract................................................................................................................................. iii

    TableOfContents.................................................................................................................iv

    Chapter 1 ................................................................................................................................6

    1.1 Introduction..........................................................................................................6

    1.2 Background..........................................................................................................6

    1.3 Summary..............................................................................................................8

    Chapter 2 ................................................................................................................................9

    2.1 Genetic algorithms in more detail........................................................................9

    2.2 Genetic algorithms outline....................................................................................9

    2.3 Minimisation of a non-linear function ................................................................11

    2.4 Results of minimisation of function....................................................................14

    2.5 Conclusion ..........................................................................................................15

    Chapter 3 ..............................................................................................................................16

    3.1 Introduction..........................................................................................................16

    3.2 Equipment used....................................................................................................16

    3.3 Obtaining second order linear approximation of system .....................................18

    3.3 Designing PID parameters using ITAE minimisation .........................................23

    3.4 Results using PID parameters obtained on model of system...............................29

    3.5 Deriving voltage water level relationship ............................................................30

    3.6 Testing PID values obtained on coupled tank apparatus .....................................31

    3.7 Conclusion ...........................................................................................................34

    Chapter 4 ..............................................................................................................................36

    4.1 Introduction..........................................................................................................36

    4.2 Off-line tuning of the approximated model ..........................................................36

    4.3 Results of off-line tuning the approximated model ..............................................39

    4.4 Off-line tuning the actual rig................................................................................44

    4.5 Testing parameters obtained from off-line tuning the model of actual system ...47

    4.6 Conclusion ............................................................................................................48

  • 8/3/2019 Pid Ga Keren Itae

    5/71

    v

    Chapter 5 ..............................................................................................................................50

    5.1 On-line tuning of approximated model................................................................50

    5.2 Results for on-line tuning ....................................................................................55

    5.3 Conclusion ...........................................................................................................58

    5.4 Overall conclusion ...............................................................................................59

    References .............................................................................................................................60

    Appendices............................................................................................................................61

    Mfilesused ............................................................................................................................61

    Genetic_algo.m..........................................................................................................61

    GA.m..........................................................................................................................61

    initialise.m..................................................................................................................64

    min_evaluation_function.m.......................................................................................65

    off_line_evaluation_function.....................................................................................66

    on_line_evaluation_function......................................................................................66

    Crossover.m ...............................................................................................................68

    Selection.m ................................................................................................................68

    Mutation.m.................................................................................................................69

    Parse.m.......................................................................................................................69

    Delta.m.......................................................................................................................70

    calculation_model.m..................................................................................................70

    calculation.m . 70

  • 8/3/2019 Pid Ga Keren Itae

    6/71

    6

    Chapter 1

    1.1Introduction

    Over the past few years many different techniques have been developed for obtaining

    the optimum proportional, integral and derivative control parameters for PID

    controllers. This paper describes the application of genetic algorithms to the optimal

    tuning of the three terms for the classical PID controllers being used to regulate non-

    linear processes. The use of genetic algorithms in this field of PID optimisation is

    expected to overcome weaknesses of other conventional approaches in non-linear

    situations. Genetic algorithms are very effective at finding optimal solutions to a varietyof problems. This innovative technique performs well when solving complex problems

    because it does not impose many limitations of traditional techniques. Genetic

    algorithm will search for solutions without regard to the inner workings of the problem

    in question.

    1.2 Background

    Genetic algorithms are stochastic global search methods that are based on natural

    biological evolution. Genetic algorithms operate on an initial population of search

    solutions applying the principle of survival of the fittest to produce better solutions. At

    each generation a new set of solutions is created which will ideally have greater fitness

    values than the previous solutions. Fitness refers to how well a solution acts in the

    problem domain. This process leads to the evolution of populations of individuals that

    are better suited to the environment they are in. Genetic algorithms will be discussed in

    more detail later on.

    In a regulated system an input, which is called a set point, indicates the desired output

    response that one would like to obtain from the system output.

  • 8/3/2019 Pid Ga Keren Itae

    7/71

    7

    In a lot of real processes it is not possible to obtain the correct output from a specified

    input, as the output may drift away from the set point for a variety of reasons or may

    respond too slowly to changes. A controller is therefore used, which compares the

    system output to the desired set point. It then uses this information to find a new control

    input to the system so the system output can be adjusted correctly. This closed loopcontrol is illustrated in diagram 1 below.

    Diagram 1

    A common strategy that is used for controlling the system output is the PID controller.

    The PID controller has three parameters that it uses for controlling the system output.

    1. (P) Proportional error correction. The output from the system and the

    set point are compared and when this is done the difference is amplified

    by multiplying it by a gain and is feed back to provide a correction

    signal.

    2. (I) Integral error control. Over time differences between the set point

    and the system output accumulate over time until they become large

    enough to drive the system output back towards the set point.

    3. (D) Derivative error control. This correction signal opposes rapid

    deviations in the system output hence reducing the response of the

    system output to disturbances.

    Controller System

    Set PointSystem output

  • 8/3/2019 Pid Ga Keren Itae

    8/71

    8

    1.3 Summary

    Genetic algorithm will be applied to the area of PID optimisation in both an off-line and

    on-line tuning environment. Tuning a system off-line means that the PID parameters of

    the controller are updated when the system has been taken off-line. The PID values are

    updated using the systems input and output data after the system has been placed off-

    line. These updated PID values are used in place of the old PID values and the system

    is brought back on-line. This process continues until the optimum PID coefficients of

    the system in question have been obtained. Tuning a system on-line means that the PID

    parameters of a controller are updated while the system is on-line. The systems input

    and output data are collected at regular intervals and used to update the PID values. The

    new PID values are then used in place of the old ones until a newer updated set of PID

    values have been deduced at the next time interval.

    The optimisation of the controllers PID parameters using genetic algorithms

    will be compared to that of a standard method for designing PID controllers. This

    method is PID design using the ITAE index, which will be discussed later. The genetic

    algorithm is going to be used to find the optimum PID values for the CE105MV

    coupled tank apparatus, which is a non-linear process. The non-linear CE105MV

    coupled tank apparatus will be discussed in more detail in chapter three.

  • 8/3/2019 Pid Ga Keren Itae

    9/71

    9

    Chapter 2

    2.1 Genetic algorithms in more detail

    The searching process used by genetic algorithms is similar to that in nature where

    successive generations of organisms are reproduced and raised until they themselves

    can reproduce. To use a genetic algorithm you must initialise the genetic algorithm with

    a set of solutions represented by chromosomes called a population. Each solution can

    be represented as either real valued numbers or a binary string of ones and zeros. These

    solutions are known as individuals. In these algorithms the fittest among a group of

    individuals survive and are used to form new generations of individuals with improved

    fitness vales. The fitness of an individual is a measure of how well the individual hasperformed in the problem domain. By using the innovative flair of human search

    genetic algorithms can be very useful.

    2.2 Genetic algorithms outline

    Diagram 2

    . Initialise Population

    Measure fitness

    Mutation

    Selection

    Crossover

    Optimum solutions

    Not Optimum Solutions

  • 8/3/2019 Pid Ga Keren Itae

    10/71

    10

    Diagram 2 above shows the basic outlay of a genetic algorithm. Firstly the algorithm is

    initiated with a set of solutions called a population. Ideally an initial population should

    consist of between 30 and 100 individuals. When the population has been initialised afitness evaluation function then measures the fitness of each individual based on how it

    acts in the problem domain. Different problems will have different evaluation

    functions.

    When the individuals have been evaluated the genetic algorithm then selects the

    fittest individuals from the population.Selection is the process of determining the

    number of trials or times a particular individual is chosen for reproduction and, thus,

    the number of offspring that an individual will produce. Selection of individuals can be

    divided into two separate steps:

    1. Determining the number of trials an individual will receive.

    2. Conversion of expected number of trials into discrete number of offspring.

    Part one is concerned with conversion of raw fitness values into real-valued

    expectations of the probability that the individual will reproduce

    Part two is a probabilistic selection of individuals for reproduction based on the fitness

    of individuals relative to each other.

    The fittest individuals from the initial population have now been chosen so the

    genetic algorithm now mutates each of the selected individuals.The purpose of this

    function is to change one of the parameters of the parent based on a non-uniform

    probability distribution. This mutation guarantees that the probability of searching an

    area will never be zero. In genetic algorithms mutation is randomly applied with low

    probability, typically between 0.001 and 0.01 and this modifies elements in the

    chromosome. The probability of mutation must be kept low otherwise it could cause the

    loss of too many good individuals. It acts as a safety net to recover good genetic

    material that may have been lost during selection and crossover. The mutation helps in

    randomly searching other areas of the search space that may have been unexplored and

    might contain local or global maxima.

  • 8/3/2019 Pid Ga Keren Itae

    11/71

    11

    When the selected individuals have been mutated the process of crossover commences.

    What crossover basically means is taking two of the mutated individuals and creating a

    new individual from both these individuals. These individuals will ideally contain the

    best parts of each parents genetic material. This process of fitness evaluation,selection, mutation and crossover continues until the optimum solutions to the problem

    in question are obtained.

    2.3 Minimisation of a non-linear function

    Before the genetic algorithm is used to tune the PID values of the coupled tank

    apparatus it will be used to minimise a non-linear function to see how effective genetic

    algorithms can be at problem solving. The non-linear transfer function that is chosen is

    f(x)=x+10sin(5x)+7cos(4x)(Equation 1)

    This function was chosen as it displays multiple local minima over the input range 0 to

    9. Figure 1 below shows a plot of this function over the range 0 to 9 and also shows

    how the function displays local minima.

    Figure 1

  • 8/3/2019 Pid Ga Keren Itae

    12/71

    12

    A population of 50 is initialised over this 0 to 9 range. The function used to

    initialise the population is initialise.m (refer to appendix). This function basically

    creates a matrix of random numbers which are placed at various points on the function

    f(x) over the range o to 9. This matrix of random numbers is the initial population and

    this population is returned by the function to the main body of the genetic algorithm.Figure 2 below shows a plot of the initial population.

    Figure 2

    The evaluation function used to measure the fitness of the initial population is

    min_evaluation_func.m (refer to appendix). This function takes in the values from the

    population and substitutes them into the function itself. This fitness value is returned

    along with the individual itself to the main body of the genetic algorithm to allow for

    further evolution. The closer this fitness value is to the minimum of the function the

    more likely it will be selected and used in the crossover process. The following piece of

    code demonstrates how the fitness of each individual is evaluated. In the code below

    sol(1) corresponds to the value of the individuals in the population. The individual itself

    and its fitness value are both returned by the function to the main body of the genetic

    algorithm, which are the files Genetic_algo.m and GA.m (refer to appendix).

  • 8/3/2019 Pid Ga Keren Itae

    13/71

    13

    x=sol(1);

    val = opt*(x + 10*sin(5*x)+7*cos(4*x));

    Now that the fitness of each individual has been evaluated the fittest

    individuals are chosen using the selection functionselection.m (refer to appendix).

    The selection function used is a ranking function based on normalised geometric

    distribution.

    function[newPop] = select(oldPop,options)

    This function takes in the old population oldPop,performs its selection process and

    then returns a new population newPop, which consists of the fittest individuals in the

    population. The options value used is set equal to 0.08 and this value is the probability

    that the best individual will be selected.

    The individuals are now mutated using the function mutation.m (refer to

    appendix). This function mutates the individual slightly to try and recover any useful

    genetic material that may have been lost.

    When the individuals have been mutated they are then crossed over with each

    other to form new fitter individuals using the function crossover.m (refer to appendix).

    This function takes in two parent individualsP1 andP2. It then sets a variable a equal

    to a random umber between 0 and 1. The following code is then use to create two new

    individuals C1 and C2.

    C1 = P1*a + P2*(1-a);

    C2 = P1*(1-a) + P2*a;

    This function then returns C1 and C2 to the main body of the genetic algorithm. This

    process of fitness evaluation, selection, mutation and crossover continues until the

    minimum of the transfer function has been obtained.

  • 8/3/2019 Pid Ga Keren Itae

    14/71

    14

    2.4 Results of minimisation of function

    The genetic algorithm was allowed run for 100 generations using an initial population

    of 50. Figure 3 below shows how the average fitness values of the population change as

    the number of generations increase.

    Figure 3

    From the figure above it can be seen that as the number of generations increase the

    average and best fitness values of the population tend towards the minimum of the non-

    linear function. It can be concluded from the plot above that the minimum of the

    function is at a value around -15.

    Figure 5 below shows a plot of the final population along with a plot of the

    function itself. From the figure below it can be seen that the genetic algorithm has

    successfully minimised the non-linear function f(x) and the minimum of this function is

    found to be a value of 15.16.

  • 8/3/2019 Pid Ga Keren Itae

    15/71

    15

    Figure 4

    2.5 Conclusion

    It can be concluded that the genetic algorithm minimises the non-linear function

    correctly after 100 generations. The number of generations it takes to minimise the non-

    linear function depends on a number of factors such as the population size, the mutationfactor and the selection process. When a large population is used there is increased

    computational overhead. However the large population size means that there is a larger

    search area examined thus increasing the chances of finding the minimum of the non-

    linear function faster. Genetic algorithms do not seem to get caught at local minima

    when the correct population size is chosen, which makes them very useful in the non-

    linear field.

  • 8/3/2019 Pid Ga Keren Itae

    16/71

    16

    Chapter 3

    3.1 IntroductionThis chapter will deal with the design of a set of PID parameters for the non-linear

    CE105MV coupled tank apparatus in figure 5 below using PID control design using

    ITAE, which will be discussed later.

    3.2 Equipment used

    1. Figure 5 below shows a picture of the CE105MV coupled tank apparatus. This

    apparatus basically consists of two tanks, tank1 and tank2. Water flows into tank 1

    from the source. Water can flow from tank 1 to tank 2 depending on the setting of

    valve B. The value that valve C is set to will determine how much water can flow

    from tank 1 to tank 2. There are also valves A and C and these valves determine the

    output flow of water from tank 1 and tank 2 back to the water reservoir. The valves

    can be set between 0 and 5, where 0 corresponds to the valve being fully shut and 5

    corresponds to the valve being fully open.

    Figure 5

  • 8/3/2019 Pid Ga Keren Itae

    17/71

    17

    2. A PC containing Matlab and simulink software packages including the matlab real

    time toolbox.

    3. A data acquisition card, which sends an input voltage to the rig and which receives

    information regarding the level of water in the second tank

    4. A multi-meter was used for checking voltage inputs and outputs from the dataacquisition card. Diagram 3 below shows the outlay of the equipment used when it

    is connected up.

    Diagram 3

    Data acquistion cardPC containing

    Matlab, Simulink and

    Matlab Real time

    toolbox

    Voltage to RigOuput from Tank 2

    H1

    H2

    Valve AValve C

    Valve B

    Qin

    Tank apparatus

  • 8/3/2019 Pid Ga Keren Itae

    18/71

    18

    3.3 Obtaining second order linear

    approximation of system

    A second order linear approximation of the system needs to be obtained. This

    approximation can be obtained from the open loop step response of the system so this

    open loop response needs to be obtained for the coupled tank apparatus. A program was

    written in simulink using the matlab real time toolbox to perform the open loop step

    response on the system. Valve A was set to zero, valve B was set to 5 and valve C was

    set to 3.

    This program consists of an input step with an amplitude of 0.6 which

    corresponds to a step input of 6 volts, which is recommended by the accompanyingmanual when performing an open loop step response. This step input is then fed into

    the RToutblock, which collects the output data from the actual coupled tank apparatus

    about the water level in tank2. This information is then collected in workspace using

    the RTinblock so this information can be plotted and analysed. The basic outlay of this

    program is in diagram 4 below.

    Diagram 4

    The simulation is allowed run for 1200 seconds until the water level in tank 2remains constant. Figure 6 below displays the open loop step response of the system to

    a step input of 6 volts.

    Step

    Input RTout

    RTin Workspace

    Matlab real time

    tool box icons

  • 8/3/2019 Pid Ga Keren Itae

    19/71

    19

    Figure 6

    It is necessary to linearise the system equations by considering small variations qi in Qi,

    qc in Qc, h1 in H1 and h2 in H2. Linearisation of the double tank model gives a second

    order transfer function of the following form in equation 2.,

    )1)()(1)(()(

    )(

    21

    2

    ++

    =

    stst

    G

    sq

    sh

    i

    (Equation 2)

    G is the gain of the system and t1 and t2 are the time constants of this system.

    G, t1 and t2 can be approximated from the open loop response previously obtained. The

    standard transfer function for a second order system is also used in the calculation of G,

    t1 and t2.

    22

    2

    2 nn

    n

    WsWs

    W

    ++

    (Equation 3)

  • 8/3/2019 Pid Ga Keren Itae

    20/71

    20

    The gain of the second order linear approximation G is equal to the value that the open

    loop step response begins to level off and this can be calculated in matlab using the

    following command.

    G=mean(output(1100:1200))

    The gain value obtained from the open loop step response was 0.5361. Equation 4 is

    obtained by multiplying out equation 2.

    1)()( 112

    21 +++ sttstt

    G

    (Equation 4)

    The by dividing across by ( t1 x t2)equation 5 is obtained.

    )(

    1

    )(

    )(

    )(

    2121

    212

    21

    tts

    tt

    tts

    tt

    G

    +

    ++

    (Equation 5)

    The settling time of the open loop step response can be calculated in matlab using the

    following commands and is found to be 1015 seconds.

    ts_hi=max(find(ouput>1.02*Yss));

    ts_lo=max(find(output

  • 8/3/2019 Pid Ga Keren Itae

    21/71

    21

    Yss=mean(output(1100:1200));

    time_constant=min(find(output>0.63*Yss));

    It is known that the overall time constant of the system is

    2

    1

    n

    cW

    T =

    (Equation 6)

    nW can be calculated using equation 6and is found to be 0.0623 rad/sec. From the open

    loop response in figure 6 it can be seen that the system is over damped as the system

    has two real different poles. It can therefore be assumed that 0> . A value of 4 is

    assumed for .

    00388.01 2

    21

    ==

    nW

    tt

    (Equation 7)

    By equating equations 3 and equation 5, equation 8 can be obtained.

    4984.0221

    21==

    +Wn

    tt

    tt .

    (Equation 8)

    Now by solving equation 7 and 8 simultaneously t1 and t2 can be found. From equation

    7 and 8 equation 9 can be obtained.

    2

    100388.0

    1

    tt =

    (Equation 9)

  • 8/3/2019 Pid Ga Keren Itae

    22/71

    22

    Substituting equation 9 into equation 7 values for 1t and 2t can be obtained from the

    following quadratic equation in equation 10.

    01623.000388.0 12

    1 =+ tt

    (Equation 10)

    The roots of this quadratic equation are found using the roots command in matlab and a

    value for 1t is obtained. 2t can now be obtained using equation 7. The values obtained

    for 1t and 2t are 2.03 and 126.41 respectively. Now as all the variables needed by

    equation 2.1 are known, the second order linear approximation of the system can beobtained and is found to be equal to equation 11 below.

    00388.0623.0

    00208.0

    )16.16075.257(

    5361.022

    ++

    =

    ++

    =

    ssssModel

    (Equation 11)

    The above calculations were made using the mfile calculation_model.m (refer to

    appendix). Figure 7 displays the open loop response of the second order linear

    approximation.

    Figure 7

  • 8/3/2019 Pid Ga Keren Itae

    23/71

    23

    The open loop step responses of the actual coupled tank apparatus and the second order

    approximation are now compared to see if the approximated model is accurate. Figure 8

    below displays both responses.

    Figure 8

    From the figure above it can be seen that the approximated model seems accurate

    although there is a slight error. The mean square error between the two responses is

    calculated and found to be 0.0389.

    3.3 Designing PID parameters using ITAE

    minimisation

    The design method used here was PID controller design using ITAE. The design of the

    PID controller involves the minimisation of the ITAE index.

  • 8/3/2019 Pid Ga Keren Itae

    24/71

    24

    The closed loop response of the system needs to be obtained and this is done in

    simulink using the following design in diagram 5. This simulink program was allowed

    run for 500 seconds.

    Diagram 5

    The output data was collected in workspace and then plotted giving the closed loop

    response of the approximated second order model. Figure 9 below displays this

    closed loop response.

    Figure 9

    Using matlab Wn was calculated using the following commands and was found to be

    equal to 0.707 rad/sec.

    Step

    InputPIDController

    C(s)

    System

    Model

    G(s)

    Feedback

    System

    Output

  • 8/3/2019 Pid Ga Keren Itae

    25/71

    25

    Yss=mean(ouput(400:500));

    time_constant=min(find(output>0.63*Yss));

    Wn=sqrt(1/time_constant);

    The overall closed loop transfer function of diagram 5 needs to be obtained. The PID

    controller C(s) is equal to equation 12 below.

    s

    KsKsKsC

    IpD ++

    =

    2

    )(

    (Equation 12)

    G(s) is equal to equation 11. Therefore the overall closed loop transfer function )(sGCL

    is found to be equal to equation 13 below.

    )00388.0623.0

    00208.0)(

    )((1

    )00388.0623.0

    00208.0)(

    )((

    )(

    2

    2

    2

    2

    ++

    ++

    +

    ++

    ++

    =

    sss

    KsKsK

    sss

    KsKsK

    sGIpD

    IpD

    CL

    (Equation 13)

    Equation 12 can be manipulated to obtain equation 13.

    IpD

    IpD

    CLKsKsKs

    KsKsKsG

    00208.0)00208.000388.0()00208.06232.0(

    )(00208.0)(

    23

    2

    +++++

    ++

    =

    (Equation 13)

    Having obtained )(sGCL the ITAE coefficient table for a step input was then used to

    search for the appropriate T(s), which was equal to equation 14 below.

    3223

    3

    15.275.1)(

    nnn

    n

    WsWsWs

    WsT

    +++

    =

    (Equation 14)

  • 8/3/2019 Pid Ga Keren Itae

    26/71

    26

    Wn has been already been calculated and found to be 0.707 rad/sec. The denominator

    coefficients of equations 13 and equation 14 can be equated as in equation 15 below.

    IpD

    nnn

    KsKsKs

    WsWsWs

    00208.0)00208.000388.0()00208.06232.0(

    15.275.1

    23

    3223

    +++++

    =+++

    (Equation 14)

    By substituting the value for Wn into equation 15 values for KiKp, and DK be

    calculated and are found to be equal to the values in the table 1 below. Table contains a

    set of PID parameters for the approximated second order model using ITAE.

    Table 1

    Kp KI K d

    2.1465 0.11625 -245

    Using the above parameters and the PID controller block in simulink a PID

    controller was then set up to control the step input of 1. Figure 10 below displays this

    response.

    Figure 10

    It is better if )()( sTsGCL=

    hence a pre filter )(sF is needed such that

  • 8/3/2019 Pid Ga Keren Itae

    27/71

    27

    )()()( sFsGsT CL =

    (Equation 15)

    This is equal to equation 17 below.

    ))()(0002418.0.000834.01136.0

    )11625.01465.2245(00208.0(

    15.275.1

    23

    2

    3223

    3

    sFsss

    ss

    WsWsWs

    W

    nnn

    n

    +++

    ++

    =

    +++

    )000242.0004464.05096.0

    000241.0)(0002418.000834.01136.0

    )000242.0004464.05096.0((

    15.275.1

    223

    2

    3223

    3

    +++++

    ++

    =

    +++

    sssssss

    WsWsWs

    W

    nnn

    n

    (Equation 17)

    From equation 17 a pre filter with a transfer function )(sF is used equal to equation 18

    can be obtained.

    )000241.0004464.0501.0

    000241.0()(

    2++

    =

    sssF

    (Equation 18)

    Figure 11 shows the response of the controller using the new pre filter.

    Figure 11

  • 8/3/2019 Pid Ga Keren Itae

    28/71

    28

    The graph above shows that the pre filter introduces instability to the system. This is

    caused by the negative derivative value of 245. As this instability is undesirable a

    positive derivative value is chosen to try and overcome this problem. The best controllerresponse was obtained using a derivative value of 10 and changing the pre filter

    accordingly to compensate for this. The new filter used is that in equation 19 below.

    )000241.0004464.00208.0

    000241.0()(

    2++

    =

    sssF

    (Equation 19)

    Table 2 shows the PID parameters used.

    Table 2

    Kp KI K d

    2.1465 0.11625 -245

    Using this new filter and positive derivative value the following control response is obtained

    for a step input of 1 and this response is illustrated in figure 12.

    Figure 12

  • 8/3/2019 Pid Ga Keren Itae

    29/71

    29

    3.4 Results using PID parameters obtained

    on model of system

    The following calculations were carried out to see how efficient the PID controllerparameters designed are, when their controlling ability was tested on the second order linear

    approximation of the system.

    1. Calculation of the percentage overshoot.

    2. Calculation of the settling time.

    3. Calculation rise time.

    4. Calculation of the peak time.

    Using the mfile characteristics.m the percentage overshoot, risetime and settling time are

    calculated using the PID parameters in table 1 for a step input of 1 and 0.5. Table 3 below

    displays the results.

    Table 3

    Step input %overshoot

    Risetime(s)

    Settling Time(s)

    Peak time(s)

    1 82.73% 37 seconds 177 seconds 56 seconds

    0.5 82.73% 37 seconds 177 seconds 56 seconds

    Using the mfile characteristics.m the percentage overshoot, risetime and settling

    time are calculated using the PID parameters in table 2 for a step input of 1 and 0.5.

    Table 4 below displays the results. The pre filter in equation 19 is also used because

    instability will not result as the negative derivative of 245 is not being used.

    Table 4

    Step input % overshoot Risetime Settling Time Peak time

    1 35.38% 106 seconds 589 seconds 173 seconds0.5 35.38% 106 seconds 589 seconds 173 seconds

  • 8/3/2019 Pid Ga Keren Itae

    30/71

    30

    Using the PID parameters in table 1 the PID controller gives rise to a very good

    risetime of 37 seconds and a good settling time of 177seconds. There is a large

    overshoot of 82.73%, which is not very desirable. Another drawback about thiscontroller, which can be seen in figure 10 is the fact that it exhibits undershoot. From a

    practical point of view the system being dealt with will not exhibit undershoot and

    therefore problems may be encountered when this PID controller is used on the actual

    coupled tank apparatus itself. Another drawback with this controller is that when a pre

    filter is added to improve the performance it goes very unstable as shown in figure 11.

    This lack of stability is due to the negative derivative value of -245.

    The PID controller using the PID parameters in table 2 has a much longer rise

    time of 106 seconds and a much larger settling time of 589 seconds. There is however

    a big reduction in the overshoot as it is now only 35.38%. It seems that perhaps the

    PID controller using the PID values in table 4 may be more suited to the actual

    process.

    Changing the set point from 1 to 0.5 has no effect on the overshoot, risetime

    and settling time in either of the above cases. This is due to the linear nature of the

    second order approximated transfer function that is being dealt with but this may not

    be the case with the actual coupled tank apparatus itself as it is a non-linear process.

    3.5 Deriving voltage water level relationship

    A relationship between voltage and water level height in tank2 needs to be established

    so that when an input voltage is applied the corresponding height value in relation to

    the input voltage is known. Table 8 below displays the relationship between the input

    voltage and the water level in tank 2.

  • 8/3/2019 Pid Ga Keren Itae

    31/71

    31

    Table 5

    Table of Input Voltage corresponding

    to water level in tank 2

    Voltage (volts) Water level (cm/cubed)

    4 V 55

    5 V 68

    6 V 82

    7 V 95

    When the data in table 5 is plotted it can be seen that there is a linear relationship

    between input voltage and water level in tank 2 as displayed in figure 13. It can

    therefore be deduced that I volt corresponds to 13.7 3cm .

    Figure 13

    3.6 Testing PID values obtained on coupled

    tank apparatus

    The PID values from tables 1 and 2 above will be tested on the actual coupled tank

    apparatus to test their controlling ability on this non-linear process. A simulink program

    P lot o f vo ltag e a ga inst w ater leve l

    0

    2 0

    4 0

    6 0

    8 0

    10 0

    3 4 5 6 7 8vo ltage (vol ts)

    waterlevel

    (cm\cubed)

  • 8/3/2019 Pid Ga Keren Itae

    32/71

    32

    was designed using simunlink and matlab real time toolbox to test the efficiency of the

    PID controllers designed. Diagram 6 below shows the outlay of this simulink program.

    Diagram 6

    The first PID controller tested that using the control parameters in table 1 where no pre

    filter is used. The step input value was set to 6 volts (82 3cm ). The simulink program

    was run for 350 seconds and the results were recorded.

    Note: As explained earlier good controlling ability was not expected for this

    controller due to the negative derivative value. Figure 14 below shows the response of

    the coupled tank apparatus to the PID controller. Valve B was set fully open, valve A

    was set fully closed and valve C was set to 3.

    Figure 14

    The above plot shows that this PID controller does not control the coupled tank

    apparatus efficiently at all as it is not even close to settling at the desired set point after

    350 seconds.

    Input

    Voltage RTout

    RTin Workspace

  • 8/3/2019 Pid Ga Keren Itae

    33/71

    33

    The second PID controller tested was that using the control parameters in table 2

    where the pre filter in equation 19 is used. The step input values chosen were 4, 5 and 6 volts

    (55,68 and 82 3cm ). The simulink program in diagram 6 was run for 400 seconds and the

    results were recorded. Figure 14 below shows the response of the controller on the coupledtank apparatus to the different set points.

    Figure 15

    Table 6

    Water level(cm/cubed)

    %overshoot

    Risetime(s)

    SettlingTime (s)

    Peak time(s)

    55 40.27% 57 392 79

    68 52.18% 62 379 8882 57.72% 68 327 102

    From table 6 above it can be seen that as the set point is changed overshoot, risetime,

    peak time and settling time also change. It seems that when the desired water level in

    tank 2 increases the overshoot and rise time increase but the settling time does not

    increase. This is due to the fact the second order approximation is linear but the actual

    coupled tank apparatus itself is a non linear process.

  • 8/3/2019 Pid Ga Keren Itae

    34/71

    34

    Value B was then adjusted to see the affect that it would have on the controlling

    ability of the controller designed. This time value B was set to approximately 2.5

    allowing a greater output flow from tank 2 to the reservoir. Again the PID controllers

    ability was tested for an input of 5 and 7 volts (68 and 95 3cm ). Figure 16 below

    displays the results obtained.

    Figure 16

    Table 7

    Water level(cm/cubed)

    %overshoot

    Risetime(s)

    Settling Time(s)

    Peak time (s)

    68 43.64% 58 360 75

    96 40.44% 48 401 68

    When table 6 and table 7 are compared it can be seen that changing valve C from position 3

    to 2 has an effect on overshot, risetime, settling time and peak time. Again these results are

    ude to the non linear nature of the process.

    3.7 Conclusion

  • 8/3/2019 Pid Ga Keren Itae

    35/71

    35

    It can be successfully concluded that using the ITAE PID design method, a PID

    controller that controls the approximated model efficiently has been obtained. When an

    adjustment was made to the derivative value a PID controller was obtained that

    successfully controlled the non-linear coupled tank apparatus also. The PID controllers

    designed using the ITAE standard will be compared to those that are obtained using thegenetic algorithm in the next chapters. The non-linearity of the coupled tank apparatus

    has also been demonstrated in the above chapter.

  • 8/3/2019 Pid Ga Keren Itae

    36/71

    36

    Chapter 4

    4.1 Introduction

    For many different years a variety of different methods have been used to determine

    optimal PID parameters. Genetic algorithms are proposed as a new method for

    designing robust PID parameters for a system. Genetic algorithms will search for the

    optimal solutions without concerning itself with the internal workings of the problem.

    For a genetic algorithm to improve the solution is must reject poor solutions and only

    allow reproduction by the fittest individuals. To allow the fittest individuals to survive

    an fitness evaluation function is needed, which measures the degree of fitness of the

    individual in relation to problem requirements. The population evolves over time

    through survival of the fittest, recombination and mutation to produce a new population

    better than the old population. The genetic algorithm used has a population of solutions

    scattered across the static fitness landscape whose solutions gradually converge to give

    a fitness maximum.

    4.2 Off-line tuning of the approximated

    model

    Initially the genetic algorithm is initialised with a population of PID sets. The ranges

    chosen for the initial population for the PID values was approximately + and -

    approximately 100% of the PID values derived in chapter two using the ITAE method.

    The size of the initial populations for each of the proportional, integral and derivative

    parameters was 50. The code used in chapter one was changed slightly to initialise the

    PID values. The only other code that was changed was a new fitness evaluation

    function was written called off_line_evaluation.m (refer to appendices).

  • 8/3/2019 Pid Ga Keren Itae

    37/71

    37

    initPop=initialise(50,[-5 5;-0.3 0.3;-20 20],

    'fitness_evaluation', opt);

    The overall closed loop response for each set of PID values needs to be

    calculated. The overall closed loop equation is given earlier in equation 12. A fitness

    evaluation function needs to be written which will calculate the overall closed loop

    response for each of the sets of PID values and from this response generate a fitness

    value for each set of individuals.

    This fitness evaluation function off_line_evaluation.m simulates the closed loopresponse for each set of PID values using the simulink program in diagram 5. This

    simulink program records the system output at various sample points and also calculates

    the difference between the set point and the actual system output at these sample points.

    Now that the PID values have been initialised and the closed loop response can

    be obtained for each set of PID values and the efficiency of each PID controller can be

    measured. This is done by calculating the integral of the time multiplied by the absolute

    error value for the unit step response during the time [0 , t] and is shown in equation 20

    where T is equal to 1.

    dtkerrorkTerrorweightedt

    k

    =

    =

    1

    )(*)(_

    Equation 20

    The goal here is to find a set of PID parameters that will give a minimum

    weighted error over the time period [0 t]. Each set of PID parameters are passed to the

    fitness evaluation function assol(1),sol(2) andsol(3). The fitness function initiallyobtains the closed loop response for each set of solutions. From the closed loop response

    it then calculates the weighted error using equation 20. The following piece of code

    demonstrates how the weighted error is calculated.

    weighted_error=abs(error)' * (1:length(error))';

  • 8/3/2019 Pid Ga Keren Itae

    38/71

    38

    As the purpose of the genetic algorithm is to minimise the weighted error the

    genetic algorithm will therefore try to maximize the fitness value, which is the inverse

    of the weighted error given in equation 21. The fitness value for each set of PID

    parameters will then be returned along with the set of PID values themselves to allowevolution to continue.

    errorweightedvalueFitness

    _

    1_ =

    (Equation 21)

    Once the PID individuals have been returned the selection process then occurs

    based on the fitness levels of each PID set. The mutation process follows and then the

    crossover process as explained earlier in the genetic algorithm outline. When this cycle

    is complete new sets of PID values are created which ideally will have greater fitness

    levels than the initial population of PID values. These new fitter sets of PID values are

    then passed to the fitness evaluation function again where the fitness values of this new

    population are evaluated and hence the cycle continues.

    To take into account stability the poles of the overall transfer function in

    equation 12 must be taken into account. For the system to be stable its poles (roots of

    the characteristic equation) must lie in the left hand side of the s plane. So for each

    set of PID values that are used by the evaluation function the poles for the overall

    closed loop equation must be calculated and this is done using the following piece of

    code.

    Kp=sol(1);

    Kd=sol(3);

    Ki=sol(2);

    sys=tf(0.00208,[1 0.623 0.00388]);

    cont_sys=tf([Kd Kp Ki],[1 0]);

    cas_sys=series(sys,cont_sys);

    overall_tf=feedback(cas_sys,1);

    poles=pole(overall_tf);

  • 8/3/2019 Pid Ga Keren Itae

    39/71

    39

    The poles of the overall closed loop equation in equation 12 can be calculated by the

    evaluation function. The evaluation function uses equation 12 to check if the poles of

    the overall closed loop equation lie in the left hand side of the s plane. The poles are the

    roots of the denominator of the overall closed loop equation. If the fitness evaluationfunction finds a set of PID values with poles on the right hand side of the s plane it

    knows that the overall closed loop system is unstable. The evaluation function sets the

    fitness values for this set of PID parameters to be high so that they will not be selected.

    This is done using the following piece of code.

    poles=pole(overall_tf);

    if poles(1)>0;

    weighted_error=1*exp(20);

    end

    if poles(2)>0;

    weighted_error=1*exp(20);

    end

    if poles(3)>0;

    weighted_error=1*exp(20);

    end

    From the above piece of code the evaluation function determines if the poles

    are in the left hand side of the s plane or not. If any of the poles are in the right hand

    side of the s plane the weighted error value is set to be very large ( 20101 ) for that

    specific set of PID values and therefore they will not be selected in the evolution cycle.

    4.3 Results of off-line tuning the

    approximated model

    Figure 17 below shows how the fitness level of the individuals change over 150

    generations with populations of 50 and 100 respectively. Table 8 gives the optimal PID

    parameters when a population of 50 and population of 100 is used. The step input value

    used was one.

  • 8/3/2019 Pid Ga Keren Itae

    40/71

    40

    Figure 17

    Table 8

    population size Kp KI Kd

    50 4.99 0.0349 0.04

    100 4.99 0.0346 0.04

    From figure 17 it can be seen that fitness value converges to a value of approximately

    4107.6 . The fitness convergence is much faster when there is a greater population as

    a much greater search area is being explored using an initialpopulation of 100. The

    PID values obtained using the different populations eventually converge to

    approximately the same values as shown in table 8 but optimal PIDvalue convergence

    is much faster with the larger population.

    Now that the optimal PID coefficients for the approximated model have been

    obtained their controlling ability will now be tested on the second order approximated

    itself model. Figure 18 below shows the closed loop step response for using the PID

    parameters in table 9.

  • 8/3/2019 Pid Ga Keren Itae

    41/71

    41

    Figure 18

    The controller response is the same for both sets of PID values obtained using a

    population of 50 and 100 respectively. Optimal results will be achieved faster using a

    large population though. There is a trade off however a trade off between

    computational complexity and optimal value convergence. To test the efficiency of the

    controller the overshoot, rise time, settling time and steady state error were calculated.

    Table 9 displays these results.

    Table 9

    %Overshoot

    Rise time(s)

    Settling time(s)

    steady stateerror

    0% 114 199 0

  • 8/3/2019 Pid Ga Keren Itae

    42/71

    42

    From table 9 above it can be seen that an efficient controller is obtained using the

    genetic algorithm. This controller is much more accurate than that obtained using the

    ITAE standard to design a PID controller as the overshoot, rise time and settling timehave been greatly reduced when table 9 is compared to table 3 and 4.The step input was

    changed from 1 to 0.5 to see the effect that this will have on the PID values obtained

    from the genetic algorithm. The algorithm was run for 150 generations and a population

    of 100. Figure 20 below displays how the average fitness levels of the individuals vary

    over the 150 generations.

    Figure 19

    Table 10

    PID parameters after 150 generations

    Kp KI Kd

    4.99 0.0345 0.04

    Changing the step input to 0.5 seems to have a negligible effect on the fitness

    level as the average fitness value of the population is 3107.6 which is approximately

    the same fitness value that was obtained when the step input was 1. The values that the

    PID parameters converge to, is also approximately the same as those, which are

  • 8/3/2019 Pid Ga Keren Itae

    43/71

    43

    obtained when the step input was 1. Figure 20 displays the closed loop step response of

    the second order approximation when the step input was 0.5 and the PID parameters in

    table 10 and table were used.

    Figure 20

    The overshoot, settling time, rise time and steady state error are then calculated and the

    results are displayed in table 3.5 below.

    Table 11

    Overshoot Rise time Settling time steady sate error

    15.19% 114 199 0

    From the above table it can be seen that changing the set point has no effect on

    overshoot, rise time, settling time and steady state error. This is to the linearity of the

    approximated model of the coupled tank apparatus being used.

  • 8/3/2019 Pid Ga Keren Itae

    44/71

    44

    4.4 Offline tuning the actual rig

    The rig was offline tuned for 5 generations to see how the fitness value changes over

    this number of generations. The ranges for the initial PID population and the initial

    populations size are given below in table 3.6.

    Table 12

    PID parameter Kp KI Kd

    Range of values [ -5 5] [-0.3 0.3] [-20 20]

    Population size 6 6 6

    Each of the initial six sets of PID values is used to control the actual rig using

    the simulink design in diagram 6. After the closed loop step response of a set of PID

    values has been obtained the water levels in tank 1 and 2 are set to zero so the next step

    response can be obtained for the next set of PID values.

    Using equation 20 the weighted error is then calculated for each set of

    parameters. The fitness value returned to the genetic algorithm is the inverse of this

    weighted error. As the genetic algorithm is trying to maximise this fitness value it will

    reject PID values with the lowest fitness values. Six of these fitness values are returned

    to the genetic algorithm along with the individuals themselves. As explained earlier the

    evolution cycle proceeds and six new PID values will be obtained and used on the rig

    which ideally will have greater fitness values than the previous ones. This cycle

    continues for the six generations.

    Figure 21 shows how the average and best fitness values change from the 2nd

    generation of PID values to the 5th

    generation of PID values. From the figure below it

    can be seen that the average fitness value is increasing as the number of generations is

    increased because the PID values are becoming fitter and the weighted error is

    decreasing.

  • 8/3/2019 Pid Ga Keren Itae

    45/71

    45

    Figure 21

    It can be seen from the above plot that the population of PID parameters is

    getting fitter but to see how the they actually control the non-linear coupled tank

    apparatus the closed step response is obtained for the mean of the initial set of PID

    parameters and the final set of PID parameters obtained after the 5th

    generation. Table

    13 displays these PID parameters for the 2

    st

    and 5

    th

    generation.

    Table 3.7

    No. Generations Kp Ki Kd

    2 3.17 0.1023 -7.16

    5 2.73 0.0797 6.866

    The closed loop step response of the actual rig is obtained for both sets of PIDparameters and is displayed in figure 22 below for a step input of 6 volts (96 3cm ).

    Valve C was set to 3. The initial PID values used are within a good range for finding

    the optimal PID values quite quickly as can be seen from the graph below. If a much

    broader range of PID values were chosen a lot more generations would be needed

    before an efficient controller would be obtained.

  • 8/3/2019 Pid Ga Keren Itae

    46/71

    46

    Figure 22

    From figure 22 above it can be seen that the average PID values from the 5th

    generation of values control the coupled tank apparatus more efficiently than those

    obtained from the second generation. This is evident in table 14 below because

    overshoot and settling time have been significantly reduced.

    Table 14

    No generations %Overshoot

    Rise time(s)

    Settling time(s)

    Steady state error

    2 67.06 62 276 0

    5 49.89 61 161 0

    If enough new generations of PID values were created using the genetic

    algorithm it can be assumed that the optimal PID values of the actual system would be

    obtained. However this would be very time consuming for large populations but very

    accurate PID parameters for the actual system and not an approximated system can be

    obtained.

  • 8/3/2019 Pid Ga Keren Itae

    47/71

    47

    4.5 Testing parameters obtained from offline

    tuning the model of actual system

    The PID parameters obtained using the offline tuning of the second order

    approximation were tested on the actual rig to see how efficiently they would control it.

    Using these parameters and running the closed loop control simulink in diagram 5 for

    300 seconds the controller response was obtained for two different desired water levels

    in tank2. The approximated model is not the actual model of the system so optimal

    parameters achieved for the second order approximation do not necessarily correspond

    to the optimal parameters for theactual coupled tank apparatus.

    For desired water levels of 963cm and 68

    3cm the following closed loop

    responses over a period of 300 seconds are obtained and are displayed in figure 23.

    Table 15 below displays the overshoot, rise time, settling time and steady state error in

    each case.

    Figure 23

  • 8/3/2019 Pid Ga Keren Itae

    48/71

    48

    Table 15

    Water level

    (cm/cubed)

    %

    Overshoot

    Rise time

    (s)

    Settling

    time (s)

    Steady state

    error68 10.79 52 102 0.04

    96 5.1 40 295 0.41

    From the table above it can be seen that the optimal set of PID coefficients

    obtained from the genetic algorithm using the approximated second order model control

    the coupled tank apparatus at a desired water level of 96 3cm but not as efficiently at a

    desired water level of 68 3cm . It can be concluded from this that even though a set of

    PID values can control a non-linear system at one desired set point, it does not

    necessarily mean that they will control the system at a different desired set point.

    4.6 Conclusion

    From the results in this chapter it can be concluded that off-line tuning an approximated

    model for a non-linear system using the genetic algorithm gives rise to PID parameters

    that control the approximated model very efficiently. The PID parameters obtained

    seem to control the actual rig itself but their controlling ability can vary as the desired

    set point changes

    The optimal PID controller obtained using the genetic algorithm is far more

    efficient than that of deriving the parameters using the ITAE index as overshoot, rise

    time and settling time are greatly reduced.

    Changing the input set point does not affect the resulting PID parameters

    obtained from the genetic algorithm when tuning the approximated second order model

    off-line. The controllers response for the second order approximated model shows no

    change in the overshoot, rise time and settling when the set point is changed. This is

    due to the non-linearity of the approximated model.

    However judging from the controller responses obtained using the actual

    coupled tank apparatus itself it can be seen that the overshoot, risetime and settling time

    are significantly affected by a change in the set point. This is caused by the non-

    linearity of the system. If the actual process was to be tuned off-line different sets of

  • 8/3/2019 Pid Ga Keren Itae

    49/71

    49

    parameters would be expected to be obtained for different set points because of the

    non-linearity of the rig. This is not the case using the second order linear approximation

    however.Off-line tuning of the actual system can give very efficient PID parameters but

    the disadvantage of this is that it can be very time consuming. If an accurate system

    model can be obtained then tuning the approximated model can be a lot more efficient

    but the PID values will not just be as accurate as those that would have been obtained

    from off-line tuning of the actual system.

  • 8/3/2019 Pid Ga Keren Itae

    50/71

    50

    Chapter 5

    5.1 On-line tuning of approximated model

    In last chapter tuning the parameters of the system off-line was dealt with. It is now

    proposed that the genetic algorithm is used to tune the approximated second order

    linear model of the coupled tank apparatus while the system is online. This process

    of on-line tuning involves collecting data at regular time intervals while the process

    is running and using this data to tune the PID parameters of the system. Todetermine the amount of time between time intervals the sample time of the second

    order linear approximation in equation 11 needs to be found. The sampling time of

    the system was estimated by calculating the rise time from the step response of the

    system in figure 24 below. The rise time is divided by 10 to obtain a sampling time

    for the system.

    Figure 24

    As the output value from the system needs to be calculated at each time interval

    (sampling time), equation 11 needs to be converted from the continuous time domain to

    the discrete time domain. This is done using the following command in matlab.

  • 8/3/2019 Pid Ga Keren Itae

    51/71

    51

    system_gz=c2d(system,37,'zoh')

    Converting from the continuous domain to the discrete domain will result in a

    loss of information, since the sampled signal is not known at all instants of time. In an

    effort to reduce this loss of information, a data reconstruction device, called a data hold,

    is inserted into the system directly following the sampling operation so the signal can

    be reconstructed in a form that will closely resemble the signal before sampling.

    The resulting transfer function in the Z domain is displayed in equation 22 below. The

    sample time is 37 seconds and a data reconstruction device called a zero order hold

    (ZOH) is used.

    )10752.97923.0

    004378.0107.0()(

    112 ++

    +=

    zz

    zzG

    (Equation 22)

    Figure 25 below displays the step response of equation 22.

    Figure 25

    A new evaluation function needs to be written called on_line_evaluation.m. To

    calculate the efficiency of the PID parameters that are being used when the genetic

    algorithm is running the overall closed loop transfer function (refer to equation 12) is

    calculated and converted to the Z domain so that the actual controlled signal output can

  • 8/3/2019 Pid Ga Keren Itae

    52/71

    52

    be calculated at each interval. The following piece of code demonstrates how the

    overall closed loop transfer function is converted to the Z domain.

    Kp=sol(1);

    Ki=sol(2);

    Kd=sol(3);

    sys=tf(0.00208,[1 0.623 0.00388]);

    cont_sys=tf([Kd Kp Ki],[1 0]);

    cas_sys=series(sys,cont_sys);

    overall_tf=feedback(cas_sys,1);

    overall_tf_gz=c2d(overall_tf,37,'zoh');

    The genetic algorithm takes each set of PID parameters and calculates a specific closed

    loop transfer function in the Z domain. The following piece of code shows how the

    weighted error is then calculated from the overall closed loop transfer function of the

    system and controller in the Z domain. The coefficient of each over all closed loop

    transfer function are extracted and then used to calculate the output at each time

    interval.

    The evaluation function can now compare the output signal from the system for a

    specific set of PID parameters to the desired set point at the specified time intervals.

    The fitter the set of PID parameters are the smaller the weighted error will be. Fitness is

    calculated using equation 23

    =

    +

    =m

    k

    i ke

    Fitness

    1

    2)(1

    1

    (Equation 23)

    The error for each set of PID parameters is calculated at time intervals of 37 seconds

    and the genetic algorithm uses this information to select the fittest PID values at a

    specific interval and through the evolutionary process new and improved PID values

    can be obtained.

  • 8/3/2019 Pid Ga Keren Itae

    53/71

    53

    The evaluation function had to be modified to cope with on-line tuning. A loop

    was set up in Genetic_algo.m so that for each time interval the corresponding output

    from the system can be calculated. The following piece of code demonstrates how theoutput is calculated for each set of PID values at each time interval. It then shows how

    the weighted error is calculated and how the fitness value to be maximised is

    calculated.

    y(i)=(num(2)+num(3) +num(4))*setpoint(i)- (den(2)

    *y(i-1) +den(3)*y(i-2) +den(4)*y(i-3));

    weighted_error(i)=abs(setpoint(i)-y(i))^2;weighed_error(i)=sum_weighted_error(4:i);

    val = 1/weighted_error;

    Initially there is a population of 50 sets of PID parameters over the following

    ranges in table 16 below.

    Table 16

    PID parameter Kp KI Kd

    Range of values [ -5 5] [-0.3 0.3] [0 20]

    Population size 50 50 50

    At each time interval 10 generations of new PID values are allowed evolve so that the

    weighed error can be reduced significantly at each time interval without huge

    computational overhead. The system can be viewed as having two loops. There is an

    inner loop, which calculates the weighted error for specific sets of PID values atspecific time intervals and an outer loop, which is the genetic algorithm, which adjusts

    the PID controller parameters. Figure 25 below illustrates this.

  • 8/3/2019 Pid Ga Keren Itae

    54/71

    54

    Figure 25

    Stability needs to be taken into account. When dealing with continuous systems

    the poles have to be in the left hand side of the s plane. This is not the case however

    when dealing with discrete systems. For a discrete system the poles of the overall

    closed loop equation must lie with the unit circle. Any set of PID parameters that gave

    poles that were outside the unit circle had there weighted error value set really high as

    explained in chapter 3 so that they would not be chosen for selection in the evolutionary

    process. The following piece of code takes this into account.

    if abs(poles(1))>1,

    weighted_error=1*exp(20);

    end

    if abs(poles(2))>1,

    weighted_error=1*exp(20);

    end

    if abs(poles(3))>1,

    weighted_error=1*exp(20);

    end

    As the poles obtained for the overall closed loop transfer function for the different sets

    of PID parameters can be real and imaginary the absolute value of each pole is taken. If

    PID controller Plant

    Genetic

    Algorithm

    Input

    Error OutputUpdated PID values

  • 8/3/2019 Pid Ga Keren Itae

    55/71

    55

    this absolute value is greater than one then the pole is outside the unit circle hence the

    overall system is unstable. When the system is unstable for a set of PID values the

    fitness evaluation gives this set of PID parameters a low fitness value.

    5.2 Results for on-line tuning

    The efficiency of the on-line tuning using the genetic algorithm was tested using a

    number of different set points. The graph below shows the response of the controller

    using the on-line tuning for an initial population of 50. The genetic algorithm is allowed

    tune over 30 time intervals with 10 new generations of PID values being allowed

    evolve at each time interval. Figure 26 below displays the results.

    Figure 26

    The graph above shows that the set point is reached and maintained after approximately

    13 intervals. The overshoot, rise time, settling time and steady state error were

    calculated in matlab and are displayed in table 4.2 below. The sample time was 37

    seconds so each interval corresponds to a time of 37 seconds.

  • 8/3/2019 Pid Ga Keren Itae

    56/71

    56

    Table 17

    Overshoot(%) Rise time(sec) Settling time(sec) Steady state error

    48.81 37 370 0

    To see the effect that the population and number of generations has on the on-line

    tuning process the number of generations was increased from 10 to 20 and the

    population size was increased from 50 to 100. Figure 27 below displays the results.

    Figure 27

    Table 18 below displays the overshoot, rise time, settling time and steady state error

    when the population size is set to 100 and the number of generations per interval is 20

    generations.

    Table 18

    Overshoot(%) Rise time(sec) Settling time(sec) Steady state error

    41.54 37 370 0

  • 8/3/2019 Pid Ga Keren Itae

    57/71

    57

    From the results above it can be seen that increasing the computational complexity

    significantly does not gave a huge efficiency increase in the controllers ability as the

    overshoot is reduced by about 8 %. The rise time and settling time are approximatelythe same.

    The set point was changed from 2 to 6 to see the effect that this would have on

    the on-line tuning process. Figure 28 shows how the on-line tuning process copes with

    this new set point.

    Figure 28

    Table 19 below displays the overshoot , rise time, settling time and steady state error.

    Table 19

    Overshoot(%) Rise time(sec) Settling time(sec) Steady state error

    54.79 37 592 0

    From the table above it can be seen that changing the set point increases the settling

    time from 370 seconds to 592 seconds and the overshoot has increased from 48.81% to

    54.79%.

  • 8/3/2019 Pid Ga Keren Itae

    58/71

    58

    The following graph shows how the on-line tuning process using genetic

    algorithms reacts to changes in the set point as the system is running. Initially the set

    point is set to 2. After 30 time intervals it is then changed to 6 and after 60 intervals it isthen changed back to 4. Figure 29 below shows how the on-line tuning reacted to this.

    Figure 29

    The above graph shows how the on-line tuning reacts well to changes in the set point

    while the genetic algorithm is running. It adjusts its PID values quite efficiently to settle

    on the new set point.

    5.3 Conclusion

    From the results above it seems that genetic algorithms can be used to successfully on-

    line tune systems when an accurate approximation of the system is known. However

    although the desired set point is being reached the PID values are not converging to

    specific values. This could be due to the fact that there can be various combinations of

    PID values that will control a system to give the desired output.

  • 8/3/2019 Pid Ga Keren Itae

    59/71

    59

    5.4 Overall conclusion

    This overall project has suggested a novel fine tuning technique for classical PID

    controllers based on genetic algorithms. Moreover it was confirmed that the method

    proposed was effective in a non-linear simulation example. From the results achieved

    throughout the course of the project it can be assumed that genetic algorithms are an

    efficient way of finding optimal PID parameters for non-linear systems and genetic

    algorithms have a lot of advantages over standard design methods such as ITAE.Genetic algorithms do not get caught at local minima if a correct population is used and

    this characteristic is a very desirable feature of genetic algorithms.

    Unlike other PID design methods such as ITAE the exact optimal set of PID

    parameters can be obtained for a system because genetic algorithms can tune the system

    itself and not just an approximated model of the system. This can be time consuming

    but where PID parameter accuracy is critical this is an excellent method for achieving

    PID parameters. If an accurate approximation of the system is obtained optimal PID

    parameters can be obtained using the genetic algorithm very quickly. The accuracy of

    the PID parameters would not be as good as tuning the actual system using the genetic

    algorithm. There is a trade off between time and computational complexity and PID

    optimisation accuracy.

    Another useful characteristic about genetic algorithms in respect to tuning PID

    controllers is that they can be used to obtain PID parameters for controllers with

    specific specification. Objective functions can be used to specify specific overshoots,

    rise time and settling times. For example there could be multi objective function. There

    could be an evaluation function for calculating fitness and another evaluation function

    to make sure that the PID values conform to a specific standard. An example of this

    would be to reject PID values that cause an overshoot greater than that desired. This is

    an area that would be worth investigating.

  • 8/3/2019 Pid Ga Keren Itae

    60/71

    60

    References

    [1] David E. Goldberg, Genetic algorithms in search, optimisation and

    machine learning.

    [2] Genetic algorithm Gaotv5 software toolbox.

    [3] D.P Kwok and P. Wang, Fine-Tuning of classical PID controllers

    based on genetic algorithms.

    [4] Charles L. Philips and Royce D. Harbor, Feedback control systems.

    [5] Jennifer Bruton, EE109 control systems lecture notes.

    [6] M. Ahmad, L. Zhang and J. C. Readle, On-line genetic algorithm

    tuning of a PI controller for a heating system.

    [7] Mehrdad Salami and Greg Cain, An adaptive PID controller based on

    a genetic algorithm processor.

    [8] Jin-Hyun park and Young-Kiu Choi, An on-line PID control schemefor unknown non-linear dynamic systems using evolutionary strategy.

    [9] Y. Mitsukura, T. Yamamoto and M. Kaneda, A genetic tuningalgorithm of PID parameters.

    [10] Dr a H Jones and Mr P B De Moura oliveira, Genetic auto-tuning of PID

    controllers.

    [11] CE105MV coupled tank apparatus accompanying manual

    [12] www.mathworks.com

  • 8/3/2019 Pid Ga Keren Itae

    61/71

    61

    Appendices

    Mfiles used

    Genetic_algo.m

    global Kp

    global Ki

    global Kdglobal i

    global num

    global den

    global y

    global setpoint

    for i=4:40,

    opt = 1; %Set to -1 for minimization and one for maxoimisation

    % Let's create a random starting popluation of size 10.

    initPop=initialise(20,[-5 5;-0.3 0.3;0

    20],'demo1_eval_func',opt);%changed

    % Now let's run the ga for 25 generations

    % x - the best solution found during the course of the

    run

    % endPop - the final population

    % bPop - a trace of the best population

    % traceInfo - a matrix of best and means of the ga for each

    generation

    [x endPop bpop trace] = GA([-5 5;-0.3 0.3;0

    20],'evaluation_function',opt,initPop,[1e-6 1 0],'maxgen',5,...

    'selection',[0.08],['crossover'],[2],'muation',[2 1000 3]);%changed

    end

    %Lets take a look at the performance of the ga during the run

    %average fitnessfigure;

    plot(trace(:,1),opt*trace(:,3),'b-')%changed

    hold on

    %best fitness

    plot(trace(:,1),opt*trace(:,2),'r-')%changed

    xlabel('Generation'); ylabel('Fittness');

    hold off

    GA.m

  • 8/3/2019 Pid Ga Keren Itae

    62/71

    62

    function [x,endPop,bPop,traceInfo] =

    ga_demo1(bounds,evalFN,evalOps,startPop,opts, ...

    termFN,termOps,selectFN,selectOps,xOverFNs,xOverOps,mutFNs,mutOps)

    if any(evalFN3); %Should we collect info every gen

    floatGA = opts(2)==1; %Probabilistic application of

    ops

    display = opts(3); %Display progress

    while(~done)

    %Elitist Model[bval,bindx] = max(startPop(:,xZomeLength)); %Best of current pop

    best = startPop(bindx,:);

    if collectTrace

    traceInfo(gen,1)=gen; %current generation

    traceInfo(gen,2)=startPop(bindx,xZomeLength); %Best fittness

    traceInfo(gen,3)=mean(startPop(:,xZomeLength)); %Avg fittness

    traceInfo(gen,4)=std(startPop(:,xZomeLength));

    end

    if ( (abs(bval - oval)>epsilon) | (gen==1)) %If we have a new best

    sol

    if floatGA

  • 8/3/2019 Pid Ga Keren Itae

    63/71

    63

    bPop(bFoundIn,:)=[gen startPop(bindx,:)]; %Update bPop Matrix

    else

    bPop(bFoundIn,:)=[gen b2f(startPop(bindx,1:numVar),bounds,bits)...

    startPop(bindx,xZomeLength)];

    endbFoundIn=bFoundIn+1; %Update number of

    changes

    oval=bval; %Update the best val

    else

    end

    endPop = feval(selectFN,startPop,[gen selectOps]); %Select

    if floatGA %Running with the model where the parameters are numbers

    of ops

    for i=1:numXOvers,

    for j=1:xOverOps(i,1),

    a = round(rand*(popSize-1)+1); %Pick a parentb = round(rand*(popSize-1)+1); %Pick another parent

    xN=deblank(xOverFNs(i,:)); %Get the name of crossover function

    [c1 c2] = feval(xN,endPop(a,:),endPop(b,:),bounds,[gen

    xOverOps(i,:)]);

    endPop(a,:)=c1;

    endPop(b,:)=c2;

    end

    end

    for i=1:numMuts,

    for j=1:mutOps(i,1),

    a = round(rand*(popSize-1)+1);

    c1 = feval(deblank(mutFNs(i,:)),endPop(a,:),bounds,[genmutOps(i,:)]);

    if c1(1:numVar)==endPop(a,(1:numVar))

    c1(xZomeLength)=endPop(a,xZomeLength);

    else

    %[c1(xZomeLength) c1] = feval(evalFN,c1,[gen evalOps]);

    eval(e1str);

    end

    endPop(a,:)=c1;

    end

    end

    else %We are running a probabilistic model of genetic operators

    for i=1:numXOvers,xN=deblank(xOverFNs(i,:)); %Get the name of crossover

    function

    cp=find(rand(popSize,1)

  • 8/3/2019 Pid Ga Keren Itae

    64/71

    64

    for j=1:popSize

    endPop(j,:) = feval(mN,endPop(j,:),bounds,[gen mutOps(i,:)]);

    eval(e1str);

    end

    end

    end

    gen=gen+1;

    done=feval(termFN,[gen termOps],bPop,endPop); %See if the ga is done

    startPop=endPop; %Swap the populations

    [bval,bindx] = min(startPop(:,xZomeLength)); %Keep the worst

    solution

    startPop(bindx,:) = best; %replace it with the best

    end

    initialise.m

    %creates matrix of random numbers (rows-pop size) (cols no cols in

    bound tf plus 1)

    function [pop] = init_demo1(num, bounds, evalFN,evalOps,options)

    % function [pop]=initializega(populationSize, variableBounds,evalFN,

    % evalOps,options)

    % initializega creates a matrix of random numbers with

    % a number of rows equal to the populationSize and a number

    % columns equal to the number of rows in bounds plus 1 for

    % the f(x) value which is found by applying the evalFN.

    % This is used by the ga to create the population if it

    % is not supplied.

    %% pop - the initial, evaluated, random population

    % populatoinSize - the size of the population, i.e. the number to

    create

    % variableBounds - a matrix which contains the bounds of each

    variable, i.e.

    % [var1_high var1_low; var2_high var2_low; ....]

    % evalFN - the evaluation fn, usually the name of the .m file

    for

    % evaluation

    % evalOps - any options to be passed to the eval function

    defaults []

    % options - options to the initialize function, ie.

    % [type prec] where eps is the epsilon value

    % and the second option is 1 for float and 0 forbinary,

    % prec is the precision of the variables defaults

    [1e-6 1]

    if nargin

  • 8/3/2019 Pid Ga Keren Itae

    65/71

    65

    estr=['x=pop(i,1); pop(i,xZomeLength)=', evalFN ';'];

    else %Binary GA

    estr=['x=b2f(pop(i,:),bounds,bits); pop(i,xZomeLength)=', evalFN ';'];

    end

    else %A .m fileif options(2)==1 %Float GA

    estr=['[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0

    evalOps]);'];

    else %Binary GA

    estr=['x=b2f(pop(i,:),bounds,bits);[x v]=' evalFN ...

    '(x,[0 evalOps]); pop(i,:)=[f2b(x,bounds,bits) v];'];

    end

    end

    numVars = size(bounds,1); %Number of variables

    rng = (bounds(:,2)-bounds(:,1))'; %The variable ranges'

    if options(2)==1 %Float GA

    xZomeLength = numVars+1; %Length of string is numVar + fitpop = zeros(num,xZomeLength); %Allocate the new population

    pop(:,1:numVars)=(ones(num,1)*rng).*(rand(num,numVars))+ ...

    (ones(num,1)*bounds(:,1)');

    else %Binary GA

    bits=calcbits(bounds,options(1));

    xZomeLength = sum(bits)+1; %Length of string is numVar + fit

    pop = round(rand(num,sum(bits)+1));

    end

    for i=1:num

    eval(estr);

    end

    min_evaluation_function.m

    function [sol, val] = demo1_eval_func(sol,options)

    % val - the fittness of this individual

    % sol - the individual, returned to allow for Lamarckian evolution

    % options - [current_generation]

    %opt indicates if we are maximising or minimising the function.

    % Set to 1 for maximisation (default) or -1 for minimisation.

    %NARGIN returns the number of input arguments that were used to callthe

    % function.

    if nargin

  • 8/3/2019 Pid Ga Keren Itae

    66/71

    66

    off_line_evaluation_function

    global poles

    %value for KpKp=sol(1);

    %value for Ki

    Ki=sol(2);

    %value for Kd

    Kd=sol(3);

    %transfer function for second order approximation

    sys=tf(0.00208,[1 0.623 0.00388]);

    %transfer function for control system

    cont_sys=tf([Kd Kp Ki],[1 0]);

    cas_sys=series(sys,cont_sys);

    %overall closed loop equationoverall_tf=feedback(cas_sys,1);

    %calculation of poles for overall_closed loop transfer function

    poles=pole(overall_tf);

    %run simulation

    sim('closed_loop_reponse');

    %weighted error value

    weighted_error=abs(err)' * (1:length(err))';

    %if poles d'ont lie in left half of the s plane

    if poles(1)>0;

    weighted_error=1*exp(20);

    end

    if poles(2)>0;

    weighted_error=1*exp(20);end

    if poles(3)>0;

    weighted_error=1*exp(20);

    end

    val = 1/weighted_error;

    on_line_evaluation_function

    function [sol, val] = demo1_eval_func(sol,options)

    % val - the fittness of this individual

    % sol - the individual, returned to allow for Lamarckian evolution

    % options - [current_generation]

    %opt indicates if we are maximising or minimising the function.

    % Set to 1 for maximisation (default) or -1 for minimisation.

    %NARGIN returns the number of input arguments that were used to call

    the

    % function.

    if nargin

  • 8/3/2019 Pid Ga Keren Itae

    67/71

    67

    if abs(opt) ~= 1

    opt = -1;

    end

    global Kp

    global Ki

    global Kdglobal num

    global i

    global y

    global den

    global setpoint

    %value for Kp

    Kp(i)=sol(1);

    %value for Ki

    Ki(i)=sol(2);

    %value for Kd

    Kd(i)=sol(3);

    %transfer function for second order approximation

    sys=tf(0.00208,[1 0.623 0.00388]);%transfer function for control system

    cont_sys=tf([Kd(i) Kp(i) Ki(i)],[1 0]);

    cas_sys=series(sys,cont_sys);

    %overall closed loop equation

    overall_tf=feedback(cas_sys,1);

    overall_tf_gz=c2d(overall_tf,37,'zoh');

    %calculation of poles for overall_closed loop transfer function

    poles=pole(overall_tf_gz);

    [num,den] = tfdata(overall_tf_gz,'v');

    %run simulation

    setpoint(i)=6;

    %if i>30,

    % setpoint(i)=6;

    %end%if i>60,

    % setpoint(i)=4;

    %end

    y(1)=0;

    y(2)=0;

    y(3)=0;

    y(i)=(num(2)+num(3) +num(4))*setpoint(i)- (den(2)*y(i-1) +den(3)*y(i-

    2) +den(4)*y(i-3));

    weighted_error(i)=abs((setpoint(i)-y(i))^2);

    weighted_error_sum=1+sum(weighted_error(4:i));

    if y(i)>1.3*setpoint(i),

    weighted_error_sum=1*exp(20);

    endif abs(poles(1))>1,

    weighted_error_sum=1*exp(20);

    end

    if abs(poles(2))>1,

    weighted_error_sum=1*exp(20);

    end

    if abs(poles(3))>1,

    weighted_error_sum=1*exp(20);

    end

    val = 1/weighted_error_sum;

  • 8/3/2019 Pid Ga Keren Itae

    68/71

    68

    Crossover.m

    function [c1,c2] = Xover_demo1(p1,p2,bounds,Ops)

    % Xover_demo1 takes two parents P1,P2 and performs an interpolation

    % along the line formed by the two parents.

    %% function [c1,c2] = Xover_demo1(p1,p2,bounds,Ops)

    % p1 - the first parent ( [solution string function value] )

    % p2 - the second parent ( [solution string function value] )

    % bounds - the bounds matrix for the solution space

    % Ops - Options matrix for arith crossover [gen #ArithXovers]

    % Pick a random mix amount

    a = rand;

    % Create the children

    c1 = p1*a + p2*(1-a);

    c2 = p1*(1-a) + p2*a;

    Selection.m

    function[newPop] = select(oldPop,options)

    % NormGeomSelect is a ranking selection function based on the

    normalized

    % geometric distribution.

    %

    % function[newPop] = normGeomSelect(oldPop,options)

    % newPop - the new population selected from the oldPop

    % oldPop - the current population

    % options - options to normGeomSelect [genprobability_of_selecting_best]

    q=options(2); % Probability of selecting the best

    e = size(oldPop,2); % Length of xZome, i.e. numvars+fit

    n = size(oldPop,1); % Number of individuals in pop

    newPop = zeros(n,e); % Allocate space for return pop

    fit = zeros(n,1); % Allocates space for prob of select

    x=zeros(n,2); % Sorted list of rank and id

    x(:,1) =[n:-1:1]'; % To know what element it was

    [y x(:,2)] = sort(oldPop(:,e)); % Get the index after a sort

    r = q/(1-(1-q)^n); % Normalize the distribution, q prime

    fit(x(:,2))=r*(1-q).^(x(:,1)-1); % Generates Prob of selection

    fit = cumsum(fit); % Calculate the cumulative prob. func

    rNums=sort(rand(n,1)); % Generate n sorted random numbers

    fitIn=1; newIn=1; % Initialize loop control

    while newIn

  • 8/3/2019 Pid Ga Keren Itae

    69/71

    69

    Mutation.m

    function [parent] = nonUnifMutate(parent,bounds,Ops)

    % Non uniform mutation changes one of the parameters of the parent

    % based on a non-uniform probability distribution. This Gaussian

    % distribution starts wide, and narrows to a point distribution as the

    % current generation approaches the maximum generation.%

    % function [newSol] = multiNonUnifMutate(parent,bounds,Ops)

    % parent - the first parent ( [solution string function value] )

    % bo