Вы находитесь на странице: 1из 6

Lecture 3 Genetic Algorithms

Page 1 of 6

Genetic algorithms are founded in Charles Darwins theory of evolution.


Some concepts:
The fundamental concept is that of a gene. The main purpose of a gene (in an organism) is to produce
an enzyme that contributes somehow towards the well being of that organism. Genes control amongst
other things, height, colour, the immune system, growth, sex and so on.
Genes are organised on chromosomes. Each gene has a fixed position on a given chromosome. An
organism usually has many chromosomes, for example we have 23 pairs of chromosomes, a goldfish
has 47 pairs whereas the poor cabbage has only 9 pairs.
Each Gene, although responsible for a particular function may express itself in different ways. In
particular the tomato plant has a gene that regulates its height; depending on the nature of the gene the
tomato plant will be either short or tall. We distinguish a gene and that of its value called its allele.
Thus the height gene will dictate whether the plant is short or tall depending on its allele.
Geneticists distinguish between what genes the organism has (its genotype) and what the organism
looks like (its phenotype). Of course the appearance of an organism depends on its genetic makeup
(the alleles) but some genes are recessive and may not be expressed in the presence of dominant genes
or in fact genes may not even be turned on.
We are now going to apply a genetic algorithm to produce beautiful sheep. We make the following
assumptions.
Each of our sheep will have three chromosomes

The head chromosome


The body chromosome and
The Legs chromosome

The head chromosome:


This consists of six genes depicted as follows:

The genes govern the shape of


The left ear
The right ear
The left eye
The right eye
The mouth
The skin covering.
Each gene has one of two values (its alleles)
For the ears, eyes and mouth 0, indicates that the shape is rectangular and 1 indicates that the shape is
round. For the skin covering, 0 indicates the head is bald and 1 indicates that the head is woolly.
Thus
1

is the chromosome that is expressed in the phenotype (sheeps head) as

A round left ear


A square right ear
A square left eye
A square right eye
A round mouth
A bald head

Lecture 3 Genetic Algorithms

Page 2 of 6

The body Chromosome


This has three genes that govern

The shape
The skin covering
The tail

In this case, 0 indicates that the sheep is round and 1 indicates it is rectangular; 0 indicates the sheep is
woolly whereas 1 indicates it is bald; 0 indicates the sheep has a tail whereas 1 indicates its absence.
Thus
0

is the chromosome for a round bald sheep having a tail


Finally the legs chromosome has eight genes (2 for each leg). Each pair governs the shape of the leg
and its skin covering; in a similar manner to the head genes, 1 means that the leg is round (0 is for
square legs); 0 indicates the leg is woolly and 1 indicates that it is bald.
Thus
1

is the gene for a sheep with two round legs, one woolly and one bald; and two square legs, one bald and
one woolly.
The genotype of my sheep consists of the three chromosomes given above represented as

We now want to breed beautiful sheep. These we have decided (in actual fact it is the sheep that
ultimately decide) are round and woolly and have a tail. Thus the genotype for the most beautiful sheep
is
1

How do we breed these sheep?


In activities of this kind, we choose from a population of sheep (the genome) a pair that we think most
beautiful, these then are allowed to breed producing an offspring. This offspring becomes a member of
a new population. This is continued for several generations of new populations.
Not everything is quite so simple; breeding from the same pair of sheep often produce defective sheep
in one or more of their genes called genetic drift. Thus we introduce an element of randomness into
our breeding program.
We select each sheep (do not ask awkward questions about their gender) with a probability that reflects
how beautiful they are relative to other such sheep.
These breed by exchanging genes (in a random manner) to produce an offspring (this process is called
crossover). The child genes also can mutate (with a small probability) thus ensuring that we avoid
genetic drift.
Finally we always include in our new breeding stock the prettiest sheep from the parent population; this
is its reward for being the most beautiful1, I am afraid to say that all the others are eaten for Sunday
lunch.

Let this be a warning to all those amongst you that do not wash behind their ears.

Lecture 3 Genetic Algorithms

Page 3 of 6

For example the two sheep with genotypes


1

May produce by crossover the sheep


1

And by mutation produce


1

Finally we have to decide how to measure beautiful sheep in a quantitative way. This is performed by a
fitness function (defined on chromosomes and genotypes). The following is the code for the fitness
function that I have adopted for each chromosome. The fitness function is found in the Environment
class.
fitness(i) returns the fitness of the ith chromosome in the genotype.
public int fitness(int i){
int result = 0;
switch (i){
case 0: for (int j = 0; j < this.genes; j++){
if (this.chromosome.charAt(j) == '1'){
result++;
} // if
}// for j
return result;
case 1: for (int j = 0; j < this.genes; j++){
if (this.chromosome.charAt(j) == '0'){
result++;
} // if
}// for j
return result;
case 2: for (int j = 0; j < this.genes-1; j++){
if (j%2 == 0){
if (this.chromosome.charAt(j) == '1'){
result++;
} // if
} else {
if (this.chromosome.charAt(j) == '0'){
result++;
} // if
} // if
} // for
return result;
default: return 1;
} // switch
} // fitness

// shape of the leg

// skin covering

Lecture 3 Genetic Algorithms

Page 4 of 6

The fitness of the genotype (found in class Genotype) is given as


public int fitness(){
int totalFitness = 1;
for (int i = 0; i < chromosomes; i++){
totalFitness *= genotype[i].fitness(i);
//System.out.println(totalFitness);
} // for i
return totalFitness;
} //
that is, the product of the fitness of each chromosome.
I have also assumed that the fitness for the population is simply the sum of the fitness of each
individual that make up the population this of course is an assumption that is only partially true. The
following diagram depicts the class dependencies for the sheep breeding GA.

Lecture 3 Genetic Algorithms

The following is the output for breeding 40 new populations


------Current Population ------101110X010X01011000
011000X101X01000100
011101X000X11010111
110001X001X10101011
110100X110X11010111
000000X010X01111111
010110X100X10001010
010111X100X00010111
Generations = 0
Genetic Mutation = 0.1
------Current Population ------111110X000X10000010
111110X110X10000000
111110X000X10001001
111110X010X00000001
111110X010X10010010
111110X000X10000000
111110X100X00000010
111110X000X10001001
Generations = 8
Genetic Mutation = 0.1
------Current Population ------111111X000X10000010
110100X000X10000011
111110X100X10000011
111100X101X10100011
111111X000X10000010
101110X000X10100011
111111X001X10010011
111111X000X10000011
Generations = 16
Genetic Mutation = 0.1
------Current Population ------111111X000X10000010
111111X100X10000010
010110X000X10000010
011111X000X10000010
111111X000X10000011
010111X000X01111110
111111X100X10010010
011111X001X11100010
Generations = 24
Genetic Mutation = 0.1
------Current Population ------111111X000X10100010
111111X001X10110000
111111X000X10100010
111111X000X10100010
111110X101X10100010
111011X000X11000010
111011X010X11001010
111111X000X10000010

Page 5 of 6

Lecture 3 Genetic Algorithms

Generations = 32
Genetic Mutation = 0.1

------Current Population ------111111X000X10101010


110111X000X10100010
111110X010X00001010
111110X000X10100000
011110X000X00110010
111111X000X00100010
111101X000X10000010
111111X000X10101010
Generations = 40
Genetic Mutation = 0.1
Resulting in some beautiful sheep; drop dead gorgeous you must agree!!

Page 6 of 6

Вам также может понравиться