Chromosome class

These are the internal components of the GA and are used to come up with random ideas, solutions to problems, creatures, etc. You define a Chromosome by first creating an instance of GeneticAlgorithm (or its extended class if you are doing it that way) and then using the following format:

Chromosome myChromosome = myGeneticAlgorithm.makeChromosome();

This allows your Chromosome to use all of the utilities in myGeneticAlgorithm.

Property Summary
int [] dna
The GeneticAlgorithm that the Chromosome is registered with determines the length and maximum values initialised for this array acording to dnaLength and traitSize.
GeneticAlgorithm ga
The GeneticAlgorithm that the Chromosome is registered with.
float score
The fitness score of this Chromosome

Constructor Summary
Chromosome (GeneticAlgorithm ga)
Creates a Chromosome object registered with GeneticAlgorithm ga. Do not use this method, instead create a Chromosome with an existing GeneticAlgorithm's createChromosome method.
Chromosome (GeneticAlgorithm ga, int [] dna)
Creates a Chromosome object registered with GeneticAlgorithm ga and using a reference to an array dna (the array is not copied). Do not use this method, instead create a Chromosome with an existing GeneticAlgorithm's createChromosome method.

Method Summary
void splice (Chromosome mate)
Depending on the spliceRate property in the registered GA, effects an exchange of dna between this Chromosome and Chromosome mate.
void splice (Chromosome mate, int dnaPos)
Splices the dna between this Chromosome and Chromosome mate at the position dnaPos.
void splice (Chromosome mate, int dnaPos, int bitPos)
Splices the dna between this Chromosome and Chromosome mate at the position dnaPos in the dna array and at the binary position bitPos within that number.
void spliceBinary (Chromosome mate, int bitPos)
Splices the dna between this Chromosome and Chromosome mate at the position bitPos, a position along the the dna as a concatenated binary string.
void mutate ()
Depending on the mutationRate property in the registered GA, effects a randomization of dna in this Chromosome.
Chromosome copy ()
Creates a copy of the Chromosome. The Chromosome is not added to the genePool.
String toString ()
Returns a string representation of the dna as binary code.

Constructor Detail

Chromosome

public Chromosome (GeneticAlgorithm ga)

Creates an Chromosome registered with GeneticAlgorithm ga. Users are discouraged from using this method as it does not automatically populate the genePool as the method createChromosome() does. However it is detailed for advance use and understanding of the operation of the Chromosome class.

Parameters:

ga - a GeneticAlgorithm that provides methods and parameters for the functioning of the Chromosome


Chromosome

public Chromosome (GeneticAlgorithm ga, int [] dna)

Creates an Chromosome registered with GeneticAlgorithm ga with a reference to an array dna. The array is not copied. Users are discouraged from using this method as it does not automatically populate the genePool as the method makeChromosome() or createChromosome(int [] dna) does. However it is detailed for advance use and understanding of the operation of the Chromosome class.

Parameters:

ga - a GeneticAlgorithm that provides methods and parameters for the functioning of the Chromosome
dna - a reference to an integer array that will be used as the Chromosome's dna()

Method Detail

splice

public void splice(Chromosome mate)

This method, depending on the value of the registered GA's spliceRate property, effects an exchange of dna. Depending on whether the GeneticAlgorithm is set to operate at sub-integer level or not determines how detailed the splice is. Cross over occurs at a random given point in the dna and all values (binary or otherwise) are swapped over past that point from left to right.

Parameters:

mate - a Chromosome whose dna is to be partially swapped with this Chromosome.


splice

public void splice(Chromosome mate, int dnaPos)

This method splices the dna of two Chromosomes at the point defined by dnaPos. At dnaPos and beyond to the end of the dna of both Chromosomes, the values will be swapped. This method is supplied to allow the user to perform finite operations on dna.

Parameters:

mate - a Chromosome whose dna is to be partially swapped with this Chromosome.
dnaPos - the point in the dna arrays beyond which the swap of values occurs.


splice

public void splice(Chromosome mate, int dnaPos, int bitPos)

This method splices the dna of two Chromosomes at the point defined by dnaPos and bitPos. At the point in the binary value of the trait at dnaPos defined by bitPos and beyond to the end of the dna of both Chromosomes, the values will be swapped. This method is supplied to allow the user to perform finite operations on dna.

Users who wish to swap binary values over in integers themselves can use a binary masking operation with the aid of the spiceMask array. The code for this is as follows:

int temp = value1;
value1 = value1 ^ ((value1 ^ value2) & GeneticAlgorithm.spliceMask[bitPos]);
value2 = value2 ^ ((value2 ^ temp) & GeneticAlgorithm.spliceMask[bitPos]);

Parameters:

mate - a Chromosome whose dna is to be partially swapped with this Chromosome.
dnaPos - the point in the dna arrays beyond which the swap of values occurs.


spliceBinary

public void spliceBinary(Chromosome mate, int bitPos)

This method splices the dna of two Chromosomes at the point defined by bitPos. This method differs to spice in that it considers the dna as a concatenated string of binary numbers, the length defined by the GeneticAlgorithm's property binaryLength. At the point bitPos and beyond to the end of the dna of both Chromosomes, the values will be swapped. This method is supplied to allow the user to perform finite operations on dna. This method is slower than splice because it calls splice internally and must extrapolate a dna position and bit position from bitPos. For extra speed use splice().

Users who wish to swap binary values over in integers themselves can use a binary masking operation with the aid of the spiceMask array. The code for this is as follows:

int temp = value1;
value1 = value1 ^ ((value1 ^ value2) & GeneticAlgorithm.spliceMask[bitPos]);
value2 = value2 ^ ((value2 ^ temp) & GeneticAlgorithm.spliceMask[bitPos]);

Parameters:

mate - a Chromosome whose dna is to be partially swapped with this Chromosome.
dnaPos - the point in the dna arrays beyond which the swap of values occurs.


mutate

public void mutate ()

This method, depending on the registered GA's mutationRate property effects a randomisation upon the values in the dna of the Chromosome. This rate is tested for each value in the dna. Depending on whether the GA is set to operate at sub-integer level or not determines whether that value is an integer or a binary bit.

Users who wish to flip a single binary bit on an integer can use the singleBitMask array. The code to do this is as follows:

value ^= GeneticAlgorithm.singleBitMask[bitPos];

copy

public Chromosome copy ()

This method returns a copy of the Chromosome that it is called from. It does not load the new Chromosome into the genePool(), any changes to parameters of the GA may not reach the cloned Chromosome.


toString

public String toString ()

This method returns a string representing the dna as binary code. It takes each value in the dna array and then converts it into a binary number with as many digits as myGeneticAlgorithm.valueBits and then concatenates the whole. The binary value of any individual dna position or int can be found with Integer.toBinaryString(value).