naresh final

34
1 1.Introduction The aim of this tutorial is to explain genetic algorithms sufficiently for you to be able to use them in your own projects. This is a stripped-down to-the-bare- essentials type of tutorial. I'm not going to go into a great deal of depth and I'm not going to scare those of you with math anxiety by throwing evil equations at you every few sentences. In fact, I'm not going to throw any nasty equations at you at all! Not in this particular tutorial anyway... 2.What Is the Genetic Algorithm? The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, no differentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued. The genetic algorithm uses three main types of rules at each step to create the next generation from the current population: Selection rules select the individuals, called parents that contribute to the population at the next generation. Crossover rules combine two parents to form children for the next generation. Mutation rules apply random changes to individual parents to form children.

Upload: guvvala-nagaraju

Post on 13-Sep-2015

244 views

Category:

Documents


2 download

DESCRIPTION

final report on neural controls

TRANSCRIPT

  • 11.Introduction

    The aim of this tutorial is to explain genetic algorithms sufficiently for you to be able to use them in your own projects. This is a stripped-down to-the-bare-essentials type of tutorial. I'm not going to go into a great deal of depth and I'm not going to scare those of you with math anxiety by throwing evil equations at you every few sentences. In fact, I'm not going to throw any nasty equations at you at all! Not in this particular tutorial anyway...

    2.What Is the Genetic Algorithm?

    The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, no differentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

    The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

    Selection rules select the individuals, called parents that contribute to the population at the next generation.

    Crossover rules combine two parents to form children for the next generation.

    Mutation rules apply random changes to individual parents to form children.

  • 2The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.

    Classical Algorithm Genetic Algorithm

    Generates a single point at each iteration. The sequence of points approaches an optimal solution.

    Generates a population of points at each iteration. The best point in the population approaches an optimal solution.

    Selects the next point in the sequence by a deterministic computation.

    Selects the next population by computation which uses random number generators.

    3.Genetic Algorithm Terminology

    3.1 Fitness Functions

    The fitness function is the function you want to optimize. For standard optimization algorithms, this is known as the objective function. The toolbox software tries to find the minimum of the fitness function.

    Write the fitness function as a file or anonymous function, and pass it as a function handle input argument to the main genetic algorithm function.

    3.2 Individuals

    An individual is any point to which you can apply the fitness function. The value of the fitness function for an individual is its score. For example, if the fitness function is

    f(x1,x2,x3)=(2x1+1)2+(3x2+4)2+(x32)2,

    the vector (2, -3, 1), whose length is the number of variables in the problem, is an individual. The score of the individual (2, 3, 1) is f(2, 3, 1) = 51.

  • 3An individual is sometimes referred to as a genome and the vector entries of an individual as genes.

    3.3 Populations and Generations

    A population is an array of individuals. For example, if the size of the population is 100 and the number of variables in the fitness function is 3, you represent the population by a 100-by-3 matrix. The same individual can appear more than once in the population. For example, the individual (2, -3, 1) can appear in more than one row of the array.

    At each iteration, the genetic algorithm performs a series of computations on the current population to produce a new population. Each successive population is called a new generation.

    3.4 Diversity

    Diversity refers to the average distance between individuals in a population. A population has high diversity if the average distance is large; otherwise it has low diversity. In the following figure, the population on the left has high diversity, while the population on the right has low diversity.

    Diversity is essential to the genetic algorithm because it enables the algorithm to search a larger region of the space.

  • 43.5 Fitness Values and Best Fitness Values

    The fitness value of an individual is the value of the fitness function for that individual. Because the toolbox software finds the minimum of the fitness function, the best fitness value for a population is the smallest fitness value for any individual in the population.

    3.6 Parents and Children

    To create the next generation, the genetic algorithm selects certain individuals in the current population, called parents, and uses them to create individuals in the next generation, called children. Typically, the algorithm is more likely to select parents that have better fitness values.

    4 . How the Genetic Algorithm Works

    4.1 Outline of the Algorithm

    The following outline summarizes how the genetic algorithm works:

    1. The algorithm begins by creating a random initial population.

    2. The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:

    a. Scores each member of the current population by computing its fitness value.

    b. Scales the raw fitness scores to convert them into a more usable range of values.

    c. Selects members, called parents, based on their fitness.

    d. Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.

  • 5e. Produces children from the parents. Children are produced either by making random changes to a single parentmutationor by combining the vector entries of a pair of parentscrossover.

    f. Replaces the current population with the children to form the next generation.

    The algorithm stops when one of the stopping criteria is met.

    4.2 Initial Population

    The algorithm begins by creating a random initial population, as shown in the following figure.

    In this example, the initial population contains 20 individuals, which is the default value of Population size in thePopulation options. Note that all the individuals in the initial population lie in the upper-right quadrant of the picture, that is, their coordinates lie between 0 and 1, because the default value of Initial range in the Population options is [0;1].

    If you know approximately where the minimal point for a function lies, you should set Initial range so that the point lies near the middle of that range. For example, if you believe that the minimal point for Rastrigin's function is near the point [0 0], you could set Initial range to be [-1;1]. However, as this example shows, the

  • 6genetic algorithm can find the minimum even with a less than optimal choice for Initial range.

    4.3 Creating the Next Generation

    At each step, the genetic algorithm uses the current population to create the children that make up the next generation. The algorithm selects a group of individuals in the current population, called parents, who contribute their genesthe entries of their vectorsto their children. The algorithm usually selects individuals that have better fitness values as parents. You can specify the function that the algorithm uses to select the parents in the Selection function field in theSelection options.

    The genetic algorithm creates three types of children for the next generation:

    Elite children are the individuals in the current generation with the best fitness values. These individuals automatically survive to the next generation.

    Crossover children are created by combining the vectors of a pair of parents.

    Mutation children are created by introducing random changes, or mutations, to a single parent.

    The following schematic diagram illustrates the three types of children.

  • 7Mutation and Crossover explains how to specify the number of children of each type that the algorithm generates and the functions it uses to perform crossover and mutation.

    The following sections explain how the algorithm creates crossover and mutation children.

    4.4 Crossover Children

    The algorithm creates crossover children by combining pairs of parents in the current population. At each coordinate of the child vector, the default crossover function randomly selects an entry, or gene, at the same coordinate from one of the two parents and assigns it to the child. For problems with linear constraints, the default crossover function creates the child as a random weighted average of the parents.

    4.5 Mutation Children

    The algorithm creates mutation children by randomly changing the genes of individual parents. By default, for unconstrained problems the algorithm adds a random vector from a Gaussian distribution to the parent. For bounded or linearly constrained problems, the child remains feasible.

    The following figure shows the children of the initial population, that is, the population at the second generation, and indicates whether they are elite, crossover, or mutation children.

  • 84.6 Plots of Later Generations

    The following figure shows the populations at iterations 60, 80, 95, and 100.

  • 9As the number of generations increases, the individuals in the population get closer together and approach the minimum point [0 0].

    5 Stopping Conditions for the Algorithm

    The genetic algorithm uses the following conditions to determine when to stop:

    Generations The algorithm stops when the number of generations reaches the value of Generations.

    Time limit The algorithm stops after running for an amount of time in seconds equal to Time limit.

    Fitness limit The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to Fitness limit.

    Stall generations The algorithm stops when the average relative change in the fitness function value over Stall generations is less than Function tolerance.

    Stall time limit The algorithm stops if there is no improvement in the objective function during an interval of time in seconds equal to Stall time limit.

  • 10

    Stall test The stall condition is either average change or geometric weighted. For geometric weighted, the weighting function is 1/2n, where n is the number of generations prior to the current. Both stall conditions apply to the relative change in the fitness function over Stall generations.

    Function Tolerance The algorithm runs until the average relative change in the fitness function value over Stall generations is less than Function tolerance.

    Nonlinear constraint tolerance The Nonlinear constraint tolerance is not used as stopping criterion. It is used to determine the feasibility with respect to nonlinear constraints. Also, a point is feasible with respect to linear constraints when the constraint violation is below the square root of Nonlinear constraint tolerance.

    The algorithm stops as soon as any one of these conditions is met. You can specify the values of these criteria in theStopping criteria pane in the Optimization app. The default values are shown in the pane.

  • When you run the genetic algorithm, thedisplays the criterion that caused the algorithm to stop.

    The options Stall time limit andlong. If the algorithm stops due to one of these conditions, you might improve your results by increasing the values of

    5.1 Selection

    The selection function chooses parents for the next generation based on their scaled values from the fitness scaling function. An individual can be selected more than once as a parent, in which case it contributes its genes to more than one child. The default selection option,each parent corresponds to a section of the line of length proportional to its scaled value. The algorithm moves along the line in steps of equal size. At each step, the algorithm allocates a parent

    When you run the genetic algorithm, the Run solver and view resultsdisplays the criterion that caused the algorithm to stop.

    and Time limit prevent the algorithm from running too long. If the algorithm stops due to one of these conditions, you might improve your results by increasing the values of Stall time limit and Time limit.

    ion function chooses parents for the next generation based on their scaled values from the fitness scaling function. An individual can be selected more than once as a parent, in which case it contributes its genes to more than one

    ion option, Stochastic uniform, lays out a line in which each parent corresponds to a section of the line of length proportional to its scaled value. The algorithm moves along the line in steps of equal size. At each step, the algorithm allocates a parent from the section it lands on.

    11

    Run solver and view results panel

    prevent the algorithm from running too long. If the algorithm stops due to one of these conditions, you might improve

    Time limit.

    ion function chooses parents for the next generation based on their scaled values from the fitness scaling function. An individual can be selected more than once as a parent, in which case it contributes its genes to more than one

    Stochastic uniform, lays out a line in which each parent corresponds to a section of the line of length proportional to its scaled value. The algorithm moves along the line in steps of equal size. At each

  • 12

    A more deterministic selection option is Remainder, which performs two steps:

    In the first step, the function selects parents deterministically according to the integer part of the scaled value for each individual. For example, if an individual's scaled value is 2.3, the function selects that individual twice as a parent.

    In the second step, the selection function selects additional parents using the fractional parts of the scaled values, as in stochastic uniform selection. The function lays out a line in sections, whose lengths are proportional to the fractional part of the scaled value of the individuals, and moves along the line in equal steps to select the parents.

    Note that if the fractional parts of the scaled values all equal 0, as can occur using top scaling, the selection is entirely deterministic.

    5.2 Reproduction Options

    Reproduction options control how the genetic algorithm creates the next generation. The options are

    Elite count the number of individuals with the best fitness values in the current generation that are guaranteed to survive to the next generation. These individuals are called elite children. The default value of Elite count is 2.

    When Elite count is at least 1, the best fitness value can only decrease from one generation to the next. This is what you want to happen, since the genetic algorithm minimizes the fitness function. Setting Elite count to a high value causes the fittest individuals to dominate the population, which can make the search less effective.

    Crossover fraction The fraction of individuals in the next generation, other than elite children, that are created by crossover. Setting the Crossover Fraction describes how the value of Crossover fraction affects the performance of the genetic algorithm.

  • 13

    5.3 Mutation and Crossover

    The genetic algorithm uses the individuals in the current generation to create the children that make up the next generation. Besides elite children, which correspond to the individuals in the current generation with the best fitness values, the algorithm creates

    Crossover children by selecting vector entries, or genes, from a pair of individuals in the current generation and combines them to form a child

    Mutation children by applying random changes to a single individual in the current generation to create a child

    Both processes are essential to the genetic algorithm. Crossover enables the algorithm to extract the best genes from different individuals and recombine them into potentially superior children. Mutation adds to the diversity of a population and thereby increases the likelihood that the algorithm will generate individuals with better fitness values.

    You can specify how many of each type of children the algorithm creates as follows:

    Elite count, in Reproduction options, specifies the number of elite children.

    Crossover fraction, in Reproduction options, specifies the fraction of the population, other than elite children, that are crossover children.

    For example, if the Population size is 20, the Elite count is 2, and the Crossover fraction is 0.8, the numbers of each type of children in the next generation are as follows:

    There are two elite children.

    There are 18 individuals other than elite children, so the algorithm rounds 0.8*18 = 14.4 to 14 to get the number of crossover children.

    The remaining four individuals, other than elite children, are mutation children.

  • 14

    6. Biology Lesson

    Every organism has a set of rules, a blueprint so to speak, describing how that organism is built up from the tiny building blocks of life. These rules are encoded in the genes of an organism, which in turn are connected together into long strings called chromosomes. Each gene represents a specific trait of the organism, like eye colour or hair colour, and has several different settings. For example, the settings for a hair colour gene may be blonde, black or auburn. These genes and their settings are usually referred to as an organism's genotype. The physical expression of the genotype - the organism itself - is called the phenotype.

    When two organisms mate they share their genes. The resultant offspring may end up having half the genes from one parent and half from the other. This process is called recombination. Very occasionally a gene may be mutated. Normally this mutated gene will not affect the development of the phenotype but very occasionally it will be expressed in the organism as a completely new trait.

    Life on earth has evolved to be as it is through the processes of natural selection, recombination and mutation. To illustrate how these processes work together to produce the diverse range of flora and fauna we share our planet with let me tell you a little story....

    Once upon a time there lived a species of creatures called Hooters. Hooters had evolved entirely within the darkened confines of a vast cave system hidden deep in the bowels of a mountain range. They'd had an easy life, feeling and smelling around the damp cave walls for the algae they so loved to eat, oozing between rocks and, at mating time, listening intently for the hoots of other Hooters. There were no predators in the caves, it was just the Hooters, the algae and the occasional friendly slug, so the Hooters never had anything to fear (except for maybe the occasional bad tempered Hooter). An underground river flowed

  • 15

    through the cave system and water continuously dripped down through the water table bringing with it the fresh nutrients the algae thrived on so there was always plenty to eat and drink. However, although Hooters could feel and hear well they never had any need for eyes in the pitch blackness of the caves and as a result were totally blind. This never seemed to concern any of the Hooters though and they all had a whale of a time munching away and hooting in the darkness.

    Then one day an earthquake caused part of the cave system to collapse and for the first time in many millennia the Hooters felt the warmth of sunlight upon their skin and the soft springiness of moss beneath their feet. A few daring Hooters tasted the moss and found that it was even better eating than the cave algae. "Ooooooooooh!" they hooted between mouthfuls of moss and promptly got gobbled up by the marauding eagles who had flown in to see what all the commotion was about.

    For a while it looked as though the Hooters may be hunted to extinction, for although they liked to eat the moss they could never tell if an eagle was flying above. Not only that, they couldn't even tell if they were concealed beneath a rock or not unless it was low enough to reach for with their feelers. Every day many Hooters would stumble out from the caves with the sweet smell of moss in their nostrils only to be swiftly carried away and eaten by an eagle. Their situation seemed grim indeed.

    Fortunately, over the years, the population of Hooters had grown to be enormous in the safety of the caves and enough of them were surviving to mate - after all, an eagle can only eat so much. One day, a brood of Hooters was born that shared a mutated skin cell gene. This particular gene was responsible for the development of the skin cells on their foreheads. During the development of the baby Hooters, when their skin cells grew from the mutated gene instructions they were slightly light sensitive. Each new baby Hooter could sense if something was blocking the

  • 16

    light to its forehead or not. When these little baby Hooters grew up into bigger Hooters and ventured into the light to eat the moss they could tell if something was swooping overhead or not. So these Hooters grew up to have a slightly better chance of survival than their totally blind cousins. And because they had a better chance of survival, they reproduced much more, therefore passing the new light sensitive skin cell gene to their offspring. After a very short while the population became dominated by the Hooters with this slight advantage.

    Now let's zip a few thousand generations into the future. If you extrapolate this process over very many years and involving lots of tiny mutations occurring in the skin cell genes it's easy to imagine a process where one light sensitive cell may become a clump of light sensitive cells, and then how the interior cells of the clump may mutate to harden into a tiny lens shaped area, which would help to gather the light and focus it into one place. It's not too difficult to envision a mutation that gives rise to two of these light gathering areas thereby bestowing binocular vision upon the Hooters. This would be a huge advantage over their Cyclopsian cousins as the Hooters would now be able to judge distances accurately and have a greater field of view.

    As you can see the processes of natural selection - survival of the fittest - and gene mutation have very powerful roles to play in the evolution of an organism. But how does recombination fit into the scheme of things? Well to show you that I need to tell about some other Hooters...

    At around the same time the Hooters with the light sensitive cells were frolicking around in the moss and teasing the eagles, another brood of Hooters had been born who shared a mutated gene that affected their hooter. This mutation gave rise to a slightly bigger hooter than their cousins, and because it was bigger they could now hoot over longer distances. This turned out to be useful in the rapidly diminishing population because the Hooters with the bigger hooters could call out

  • 17

    to potential mates situated far away. Not only that but the female Hooters began to show a slight preference to males with larger hooters . The upshot of this of course was that the better endowed Hooters stood a much better chance of mating than any not so well off Hooters. Over a period of time, large hooters became prevalent in the population.

    Then one fine day a female Hooter with the gene for light sensitive skin cells met a male Hooter with the gene for producing huge hooters. They fell in love, and shortly afterwards produced a brood of lovely baby Hooters. Now, because the babies chromosomes were a recombination of both parents chromosomes, some of the babies shared both the special genes and grew up not only to have light sensitive skin cells, but huge hooters too! These new offspring were extremely good at avoiding the eagles and reproducing so the process of evolution began to favour them and once again this new improved type of Hooter became dominant in the population.

    And so on. And so on...

    Genetic Algorithms are a way of solving problems by mimicking the same processes mother nature uses. They use the same combination of selection, recombination and mutation to evolve a solution to a problem. Neat huh? Turn the page to find out exactly how it's done.

    6.1 The Genetic Algorithm - a brief overview

    Before you can use a genetic algorithm to solve a problem, a way must be found of encoding any potential solution to the problem. This could be as a string of real numbers or, as is more typically the case, a binary bit string. I will refer to this bit string from now on as the chromosome. A typical chromosome may look like this:

  • 18

    10010101110101001010011101101110111111101

    (Don't worry if none of this is making sense to you at the moment, it will all start to become clear shortly. For now, just relax and go with the flow.)

    At the beginning of a run of a genetic algorithm a large population of random chromosomes is created. Each one, when decoded will represent a different solution to the problem at hand. Let's say there are N chromosomes in the initial population. Then, the following steps are repeated until a solution is found

    Test each chromosome to see how good it is at solving the problem at hand and assign a fitness score accordingly. The fitness score is a measure of how good that chromosome is at solving the problem to hand.

    Select two members from the current population. The chance of being selected is proportional to the chromosomes fitness. Roulette wheel selection is a commonly used method.

    Dependent on the crossover rate crossover the bits from each chosen chromosome at a randomly chosen point.

    Step through the chosen chromosomes bits and flip dependent on the mutation rate.

    Repeat step 2, 3, 4 until a new population of N members has been created.

    6.2 Tell me about Roulette Wheel selection

    This is a way of choosing members from the population of chromosomes in a way that is proportional to their fitness. It does not guarantee that the fittest member

  • 19

    goes through to the next generation merely that it has a very good chance of doing so. It works like this:

    Imagine that the populations total fitness score is represented by a pie chart, or roulette wheel. Now you assign a slice of the wheel to each member of the population. The size of the slice is proportional to that chromosomes fitness score. i.e. the fitter a member is the bigger the slice of pie it gets. Now, to choose a chromosome all you have to do is spin the ball and grab the chromosome at the point it stops.

    6.3 What's the Crossover Rate?

    This is simply the chance that two chromosomes will swap their bits. A good value for this is around 0.7. Crossover is performed by selecting a random gene along the length of the chromosomes and swapping all the genes after that point.

    e.g. Given two chromosomes

    10001001110010010

    01010001001000011

    Choose a random bit along the length, say at position 9, and swap all the bits after that point

    so the above become:

  • 20

    10001001101000011

    01010001010010010

    6.4 What's the Mutation Rate?

    This is the chance that a bit within a chromosome will be flipped (0 becomes 1, 1 becomes 0). This is usually a very low value for binary encoded genes, say 0.001

    So whenever chromosomes are chosen from the population the algorithm first checks to see if crossover should be applied and then the algorithm iterates down the length of each chromosome mutating the bits if applicable.

    6.5 From Theory to Practice

    To hammer home the theory you've just learnt let's look at a simple problem:

    Given the digits 0 through 9 and the operators +, -, * and /, find a sequence that will represent a given target number. The operators will be applied sequentially from left to right as you read.

    So, given the target number 23, the sequence 6+5*4/2+1 would be one possible solution.

  • 21

    If 75.5 is the chosen number then 5/2+9*7-5 would be a possible solution.

    Please make sure you understand the problem before moving on. I know it's a little contrived but I've used it because it's very simple.

    Stage 1: Encoding

    First we need to encode a possible solution as a string of bits a chromosome. So how do we do this? Well, first we need to represent all the different characters available to the solution... that is 0 through 9 and +, -, * and /. This will represent a gene. Each chromosome will be made up of several genes.

    Four bits are required to represent the range of characters used:

    0: 0000

    1: 0001

    2: 0010

    3: 0011

    4: 0100

    5: 0101

    6: 0110

    7: 0111

  • 22

    8: 1000

    9: 1001

    +: 1010

    -: 1011

    *: 1100

    /: 1101

    The above show all the different genes required to encode the problem as described. The possible genes 1110 & 1111 will remain unused and will be ignored by the algorithm if encountered.

    So now you can see that the solution mentioned above for 23, ' 6+5*4/2+1' would be represented by nine genes like so:

    0110 1010 0101 1100 0100 1101 0010 1010 0001

    6 + 5 * 4 / 2 + 1

    These genes are all strung together to form the chromosome:

    011010100101110001001101001010100001

    6.6 A Quick Word about Decoding

  • 23

    Because the algorithm deals with random arrangements of bits it is often going to come across a string of bits like this:

    0010001010101110101101110010

    Decoded, these bits represent:

    0010 0010 1010 1110 1011 0111 0010

    2 2 + n/a - 7 2

    Which is meaningless in the context of this problem! Therefore, when decoding, the algorithm will just ignore any genes which dont conform to the expected pattern of: number -> operator -> number -> operator and so on. With this in mind the above nonsense chromosome is read (and tested) as:

    2 + 7

    Stage 2: Deciding on a Fitness Function

  • 24

    This can be the most difficult part of the algorithm to figure out. It really depends on what problem you are trying to solve but the general idea is to give a higher fitness score the closer a chromosome comes to solving the problem. With regards to the simple project I'm describing here, a fitness score can be assigned that's inversely proportional to the difference between the solution and the value a decoded chromosome represents.

    If we assume the target number for the remainder of the tutorial is 42, the chromosome mentioned above

    011010100101110001001101001010100001

    has a fitness score of 1/(42-23) or 1/19.

    As it stands, if a solution is found, a divide by zero error would occur as the fitness would be 1/(42-42). This is not a problem however as we have found what we were looking for... a solution. Therefore a test can be made for this occurrence and the algorithm halted accordingly.

    Stage 3: Getting down to business

    First, please read this tutorial again.

  • 25

    If you now feel you understand enough to solve this problem I would recommend trying to code the genetic algorithm yourself. There is no better way of learning. If, however, you are still confused, I have already prepared some simple code which you can find here. Please tinker around with the mutation rate, crossover rate, size of chromosome etc to get a feel for how each parameter effects the algorithm. Hopefully the code should be documented well enough for you to follow what is going on! If not please email me and Ill try to improve the commenting.

    Note: The code given will parse a chromosome bit string into the values we have discussed and it will attempt to find a solution which uses all the valid symbols it has found. Therefore if the target is 42, + 6 * 7 / 2 would not give a positive result even though the first four symbols("+ 6 * 7") do give a valid solution.

    (Delphi code submitted by Asbjrn can be found here and Java code submitted by Tim Roberts can be found here)

    Last Words

    I hope this tutorial has helped you get to grips with the basics of genetic algorithms. Please note that I have only covered the very basics here. If you have found genetic algorithms interesting then there is much more for you to learn. There are different selection techniques to use, different crossover and mutation operators to try and more esoteric stuff like fitness sharing and speciation to fool around with. All or some of these techniques will improve the performance of your genetic algorithms considerably.

  • 26

    Stuff to Try

    If you have succeeded in coding a genetic algorithm to solve the problem given in the tutorial, try having a go at the following more difficult problem:

    Given an area that has a number of non overlapping disks scattered about its surface as shown in Screenshot 1,

    Screenshot 1

    Use a genetic algorithm to find the disk of largest radius which may be placed amongst these disks without overlapping any of them. See Screenshot 2.

  • 27

    Screenshot 2

    7 Example: Minimize Rastrigin's Function

    7.1 Rastrigin's FunctionThis section presents an example that shows how to find the minimum of Rastrigin's function, a function that is often used to test the genetic algorithm.

    For two independent variables, Rastrigin's function is defined as

    Ras(x)=20+x21+x2210(cos2x1+cos2x2).Global Optimization Toolbox software contains the rastriginsfcn.m file, which computes the values of Rastrigin's function. The following figure shows a plot of Rastrigin's function.

  • 28

    As the plot shows, Rastrigin's function has many local minimathe "valleys" in the plot. However, the function has just one global minimum, which occurs at the point [0 0] in the x-y plane, as indicated by the vertical line in the plot, where the value of the function is 0. At any local minimum other than [0 0], the value of Rastrigin's function is greater than 0. The farther the local minimum is from the origin, the larger the value of the function is at that point.Rastrigin's function is often used to test the genetic algorithm, because its many local minima make it difficult for standard, gradient-based methods to find the global minimum.

    The following contour plot of Rastrigin's function shows the alternating maxima and minima.

  • 7.2 Finding the Minimum of Rastrigin's FunctionThis section explains how to find the minimum of Rastrigigenetic algorithm.

    Note Because the genetic algorithm uses random number generators, the algorithm returns slightly different results each time you run it.

    To find the minimum, do the following steps:

    1. Enter optimtool('ga')2. Enter the following in the Optimization app: In the Fitness function field, enter In the Number of variables

    for Rastrigin's function.The Fitness function andin the following figure.

    3. Click the Start button in thethe following figure.

    Finding the Minimum of Rastrigin's FunctionThis section explains how to find the minimum of Rastrigin's function using the

    Because the genetic algorithm uses random number generators, the algorithm returns slightly different results each time you run it.

    To find the minimum, do the following steps:

    optimtool('ga') at the command line to open the Optimization app.Enter the following in the Optimization app:

    field, enter @rastriginsfcn.variables field, enter 2, the number of independent variables

    and Number of variables fields should appear as shown

    button in the Run solver and view results pane, as shown in

    29

    n's function using the

    Because the genetic algorithm uses random number generators, the

    command line to open the Optimization app.

    , the number of independent variables

    fields should appear as shown

    pane, as shown in

  • While the algorithm is running, theof the current generation. You can temporarily pause the algorithm by clicking the Pause button. When you do so, the button name changes toresume the algorithm from the point at which you paused it, clickWhen the algorithm is finished, theas shown in the following figure. Your numerical results might differ from those in the figure, since

    The display shows:

    The final value of the fitness function when

    Objective function value: 5.550533778020394E

    Note that the value shown is very close to the actual minimum value of Rastrigin's function, which isMutation, and Set Maximuma result that is closer to the actual minimum.

    The reason the algorithm terminated.

    Optimization terminated: maximum number of generations exceeded.

    The final point, which in this example is7.3 Finding the Minimum from the Command LineTo find the minimum of Rastrigin's function from the command line, enter

    rng(1,'twister') % for rep

    [x fval exitflag] = ga(@rastriginsfcn, 2)

    While the algorithm is running, the Current iteration field displays the number of the current generation. You can temporarily pause the algorithm by clicking

    button. When you do so, the button name changes toresume the algorithm from the point at which you paused it, click

    he algorithm is finished, the Run solver and view resultsas shown in the following figure. Your numerical results might differ from

    ga is stochastic.

    The final value of the fitness function when the algorithm terminated:

    Objective function value: 5.550533778020394E-4

    Note that the value shown is very close to the actual minimum value of Rastrigin's function, which is 0. Setting the Initial Range, Setting

    Maximum Number of Generations describe some ways to get a result that is closer to the actual minimum.The reason the algorithm terminated.

    terminated: maximum number of generations exceeded.

    The final point, which in this example is [-0.002 -0.001].Finding the Minimum from the Command Line

    To find the minimum of Rastrigin's function from the command line, enter

    rng(1,'twister') % for reproducibility

    [x fval exitflag] = ga(@rastriginsfcn, 2)

    30

    field displays the number of the current generation. You can temporarily pause the algorithm by clicking

    button. When you do so, the button name changes toResume. To Resume.

    results pane appears as shown in the following figure. Your numerical results might differ from

    the algorithm terminated:

    Note that the value shown is very close to the actual minimum value of the Amount of

    describe some ways to get

    terminated: maximum number of generations exceeded.

    To find the minimum of Rastrigin's function from the command line, enter

  • 31

    This returns

    Optimization terminated:

    average change in the fitness value less than options.TolFun.

    x =

    -0.0017 -0.0185

    fval =

    0.0682

    exitflag =

    1

    x is the final point returned by the algorithm. fval is the fitness function value at the final point. exitflag is integer value corresponding to the reason that the algorithm

    terminated.

    Note: Because the genetic algorithm uses random number generators, the algorithm returns slightly different results each time you run it.

    7.4 Displaying PlotsThe Optimization app Plot functions pane enables you to display various plots that provide information about the genetic algorithm while it is running. This information can help you change options to improve the performance of the algorithm. For example, to plot the best and mean values of the fitness function at each generation, select the box next to Best fitness, as shown in the following figure.

  • When you click Start, the Optimization app displayvalues of the fitness function at each generation.Try this on Minimize Rastrigin's

    When the algorithm stops, the plot appears as sho

    The points at the bottom of the plot denote the best fitness values, while the points above them denote the averages of the fitness values in each generation. The plot

    , the Optimization app displays a plot of the best and mean values of the fitness function at each generation.

    Rastrigin's Function:

    When the algorithm stops, the plot appears as shown in the following figure.

    The points at the bottom of the plot denote the best fitness values, while the points above them denote the averages of the fitness values in each generation. The plot

    32

    s a plot of the best and mean

    wn in the following figure.

    The points at the bottom of the plot denote the best fitness values, while the points above them denote the averages of the fitness values in each generation. The plot

  • 33

    also displays the best and mean values in the current generation numerically at the top.

    To get a better picture of how much the best fitness values are decreasing, you can change the scaling of the y-axis in the plot to logarithmic scaling. To do so,1. Select Axes Properties from the Edit menu in the plot window to open the

    Property Editor attached to your figure window as shown below.

    2. Click the Y Axis tab.3. In the Y Scale pane, select Log.

    The plot now appears as shown in the following figure.

    Typically, the best fitness value improves rapidly in the early generations, when the individuals are farther from the optimum. The best fitness value improves more slowly in later generations, whose populations are closer to the optimal point.

  • 34

    REFERENBCE

    1. Deb, K., Gupta, S., Daum, D., Branke, J., Mall, A., and Padmanabhan, D. (2009). Reliability-Based Optimization Using Evolutionary Algorithms. IEEE Transacations on Evolutionary Computation.

    2. Mittal, S. and Deb, K. (2009). Optimal strategies of the iterated prisoners dilemma problem for multiple conflicting objectives. IEEE Transactions on Evolutionary Computation, 13(3), 554565.

    3. Pettersson, F., Saxen, H. and Deb, K. (2009). Genetic algorthm based multicriteria optimization of ironmaking in the blast furnace. Journal of Materials and Manufacturing Processes, 24, 17.

    4. Deb, K. (2008). Scope of stationary multi-objective evolutionary optimization: A case study on a hydro-thermal power dispatch problem. Journal of Global Optimization, 41(4), 479515.

    5. Dyer, J. S., Fishburn, P. C., Steuer, R. E., Wallenius, J., Zionts, S. and Deb, K. (2008). Multiple criteria decision making, multiattribute utility theory: Recent accomplishments and what lies ahead.Management Science, 54(7), 13361349.

    6. http://in.mathworks.com/help/gads/example-rastrigins-function.html