org.jgap
Class Chromosome

java.lang.Object
  |
  +--org.jgap.Chromosome

public class Chromosome
extends java.lang.Object

Chromosomes represent fixed-length collections of genes. In the current implementation, this is the lowest level class since genes are, themselves, represented as individual bits. Each gene can have two values (true or false). This should be suitable for most applications, but it's always possible for a fitness function to consider multiple genes as a single unit if more than two possible values are desired. In the future, a Gene class and Allele interface may be created that will allow more flexibility in this regard.

Chromosomes support reproduction, crossover, and mutation. Additional mechanisms for chromosome evolution are planned for future versions.

Author:
Neil Rotstan (neil at bluesock.org)

Constructor Summary
Chromosome(java.util.BitSet initialGenes, int chromosomeLength)
          Constructs this Chromosome instance with the given set of genes.
Chromosome(java.util.BitSet initialGenes, int chromosomeLength, int desiredMutationRate)
          Constructs this Chromosome instance with the given set of genes and the given mutation rate.
 
Method Summary
 void crossover(org.jgap.Chromosome mate)
          Performs basic crossover between this Chromosome instance and the given instance.
 boolean getAllele(int locus)
          Returns the allele (value) of the gene at the given locus (index) within the Chromosome.
 void mutate()
          Runs through the genes of this Chromosome, possibly mutating some in the process.
static org.jgap.Chromosome randomInitialChromosome(int size)
          Convenience method that returns a newly constructed Chromosome instance of the given size with a random population of genes.
static org.jgap.Chromosome randomInitialChromosome(int size, int mutationRate)
          Convenience method that returns a newly constructed Chromosome instance of the given size with a random population of genes and instantiated with the given mutation rate.
 org.jgap.Chromosome reproduce()
          Returns a copy of this Chromosome.
 int size()
          Returns the size of this Chromosome (the number of genes).
 java.lang.String toString()
          Returns a string representation of this Chromosome, useful for debugging purposes.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Chromosome

public Chromosome(java.util.BitSet initialGenes,
                  int chromosomeLength)
Constructs this Chromosome instance with the given set of genes. Each gene is represented by a single bit in the BitSet. For convenience, the randomInitialChromosome method is provided that will take care of generating a Chromosome instance of a specified size. This constructor exists in case it's desirable to initialize Chromosomes with specific gene values.

Parameters:
initialGenes - The set of genes with which to initialize this Chromosome instance. Each bit in the BitSet represents a single gene.

Chromosome

public Chromosome(java.util.BitSet initialGenes,
                  int chromosomeLength,
                  int desiredMutationRate)
Constructs this Chromosome instance with the given set of genes and the given mutation rate. Each gene is represented by a single bit in the BitSet. For convenience, the randomInitialChromosome method is provided that will take care of generating a Chromosome instance of a specified size. This constructor exists in case it's desirable to initialize Chromosomes with specific gene values.

Parameters:
initialGenes - The set of genes with which to initialize this Chromosome instance. Each bit in the BitSet represents a single gene.
desiredMutationRate - The desired chances of mutation expressed as the fraction 1/n where n is this parameter. For example, if 1000 were given, then statistically one out of every thousand genes processed would incur mutation during evolution.
Method Detail

reproduce

public org.jgap.Chromosome reproduce()
Returns a copy of this Chromosome. The returned instance can evolve independently of this instance.

Returns:
A copy of this Chromosome.

crossover

public void crossover(org.jgap.Chromosome mate)
Performs basic crossover between this Chromosome instance and the given instance. A locus (element index) is randomly selected and all genes with a locus greater or equal to that randomly selected are then swapped between the two Chromosome instances. For example, suppose this Chromosome had the genes 1010 and the given Chromosome had the genes 0011. This method would choose a random index from 0-3. Let's say it chose 2. Then it would swap the genes with indexes >= 2 between the two chromosomes. This chromosome would then be 1011 and the given Chromosome would be 0010. Note that the given Chromosome must have the same number of genes as this Chromosome or an IllegalArgumentException will be thrown.

Throws:
java.lang.IllegalArgumentException - if the size of the given Chromosome is not the same as the size of this Chromosome.

mutate

public void mutate()
Runs through the genes of this Chromosome, possibly mutating some in the process. For each gene, a random number is chosen between zero and the mutation rate provided at construction (or the default rate if none was provided). If the value of the random number is 0, then the gene's bit will be flipped.


getAllele

public boolean getAllele(int locus)
Returns the allele (value) of the gene at the given locus (index) within the Chromosome. The first gene is at locus zero and the last gene is at the locus equal to the size of this Chromosome - 1.

Returns:
The boolean value of the indicated gene.

size

public int size()
Returns the size of this Chromosome (the number of genes). A Chromosome's size is constant and will never change.

Returns:
The number of genes in this Chromosome instance.

toString

public java.lang.String toString()
Returns a string representation of this Chromosome, useful for debugging purposes.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this Chromosome.

randomInitialChromosome

public static org.jgap.Chromosome randomInitialChromosome(int size)
Convenience method that returns a newly constructed Chromosome instance of the given size with a random population of genes. This method will instantiate the Chromosome with the default mutation rate.


randomInitialChromosome

public static org.jgap.Chromosome randomInitialChromosome(int size,
                                                          int mutationRate)
Convenience method that returns a newly constructed Chromosome instance of the given size with a random population of genes and instantiated with the given mutation rate.