org.jgap
Class Chromosome

java.lang.Object
  extended byorg.jgap.Chromosome
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable, java.io.Serializable

public class Chromosome
extends java.lang.Object
implements java.lang.Comparable, java.lang.Cloneable, java.io.Serializable

Chromosomes represent potential solutions and consist of a fixed-length collection of genes. Each gene represents a discrete part of the solution. Each gene in the Chromosome may be backed by a different concrete implementation of the Gene interface, but all genes in a respective position (locus) must share the same concrete implementation across Chromosomes within a single population (genotype). In other words, gene 1 in a chromosome must share the same concrete implementation as gene 1 in all other chromosomes in the population.

Since:
1.0
Author:
Neil Rotstan, Klaus Meffert
See Also:
Serialized Form

Field Summary
static double DELTA
           
 
Constructor Summary
Chromosome(Gene[] a_initialGenes)
          Constructs a Chromosome separate from any specific Configuration.
Chromosome(Gene a_sampleGene, int a_desiredSize)
          Constructs a Chromosome of the given size separate from any specific Configuration.
 
Method Summary
 void cleanup()
          Invoked when this Chromosome is no longer needed and should perform any necessary cleanup.
 java.lang.Object clone()
          Returns a copy of this Chromosome.
 int compareTo(java.lang.Object other)
          Compares the given Chromosome to this Chromosome.
 boolean equals(java.lang.Object other)
          Compares this Chromosome against the specified object.
 java.lang.Object getApplicationData()
          Retrieves the application-specific data that is attached to this Chromosome.
 double getFitnessValue()
          Retrieves the fitness value of this Chromosome, as determined by the active fitness function.
 Gene getGene(int a_desiredLocus)
          Returns the Gene at the given index (locus) within the Chromosome.
 Gene[] getGenes()
          Retrieves the set of genes that make up this Chromosome.
 int hashCode()
          Retrieve a hash code for this Chromosome.
 boolean isSelectedForNextGeneration()
          Retrieves whether this Chromosome has been selected by the natural selector to continue to the next generation.
static Chromosome randomInitialChromosome()
          Convenience method that returns a new Chromosome instance with its genes values (alleles) randomized.
 void setApplicationData(java.lang.Object a_newData)
          This sets the application-specific data that is attached to this Chromosome.
 void setFitnessValue(int a_newFitnessValue)
          Sets the fitness value of this Chromosome.
 void setGenes(Gene[] a_genes)
           
 void setIsSelectedForNextGeneration(boolean a_isSelected)
          Sets whether this Chromosome has been selected by the natural selector to continue to the next generation or manually (e.g. via an add-method).
 int size()
          Returns the size of this Chromosome (the number of genes it contains).
 java.lang.String toString()
          Returns a string representation of this Chromosome, useful for some display purposes.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DELTA

public static final double DELTA
See Also:
Constant Field Values
Constructor Detail

Chromosome

public Chromosome(Gene a_sampleGene,
                  int a_desiredSize)
Constructs a Chromosome of the given size separate from any specific Configuration. This constructor will use the given sample Gene to construct a new Chromosome instance containing genes all of the same type as the sample Gene. This can be useful for constructing sample chromosomes that use the same Gene type for all of their genes and that are to be used to setup a Configuration object.

Parameters:
a_sampleGene - A sample concrete Gene instance that will be used as a template for all of the genes in this Chromosome.
a_desiredSize - The desired size (number of genes) of this Chromosome.
Since:
1.0

Chromosome

public Chromosome(Gene[] a_initialGenes)
Constructs a Chromosome separate from any specific Configuration. This can be useful for constructing sample chromosomes that are to be used to setup a Configuration object.

Parameters:
a_initialGenes - The genes of this Chromosome.
Since:
1.0
Method Detail

clone

public java.lang.Object clone()
Returns a copy of this Chromosome. The returned instance can evolve independently of this instance. Note that, if possible, this method will first attempt to acquire a Chromosome instance from the active ChromosomePool (if any) and set its value appropriately before returning it. If that is not possible, then a new Chromosome instance will be constructed and its value set appropriately before returning.

Returns:
A copy of this Chromosome.
Since:
1.0

getGene

public Gene getGene(int a_desiredLocus)
Returns the Gene at the given index (locus) within the Chromosome. The first gene is at index zero and the last gene is at the index equal to the size of this Chromosome - 1.

Parameters:
a_desiredLocus - The index of the gene value to be returned.
Returns:
The Gene at the given index.
Since:
1.0

getGenes

public Gene[] getGenes()
Retrieves the set of genes that make up this Chromosome. This method exists primarily for the benefit of GeneticOperators that require the ability to manipulate Chromosomes at a low level.

Returns:
an array of the Genes contained within this Chromosome.
Since:
1.0

size

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

Returns:
The number of genes contained within this Chromosome instance.
Since:
1.0

getFitnessValue

public double getFitnessValue()
Retrieves the fitness value of this Chromosome, as determined by the active fitness function. If a bulk fitness function is in use and has not yet assigned a fitness value to this Chromosome, then -1 is returned.

Returns:
a positive double value representing the fitness of this Chromosome, or -1 if a bulk fitness function is in use and has not yet assigned a fitness value to this Chromosome.
Since:
2.0 (until 1.1: return type int)

setFitnessValue

public void setFitnessValue(int a_newFitnessValue)
Sets the fitness value of this Chromosome. This method is for use by bulk fitness functions and should not be invokved from anything else.

Parameters:
a_newFitnessValue - a positive integer representing the fitness of this Chromosome.
Since:
1.0

toString

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

Returns:
A string representation of this Chromosome.
Since:
1.0

randomInitialChromosome

public static Chromosome randomInitialChromosome()
                                          throws InvalidConfigurationException
Convenience method that returns a new Chromosome instance with its genes values (alleles) randomized. Note that, if possible, this method will acquire a Chromosome instance from the active ChromosomePool (if any) and then randomize its gene values before returning it. If a Chromosome cannot be acquired from the pool, then a new instance will be constructed and its gene values randomized before returning it.

Returns:
randomly initialized Chromosome
Throws:
InvalidConfigurationException - if the given Configuration instance is invalid.
java.lang.IllegalArgumentException - if the given Configuration instance is null.
Since:
1.0

equals

public boolean equals(java.lang.Object other)
Compares this Chromosome against the specified object. The result is true if and the argument is an instance of the Chromosome class and has a set of genes equal to this one.

Parameters:
other - The object to compare against.
Returns:
true if the objects are the same, false otherwise.
Since:
1.0

hashCode

public int hashCode()
Retrieve a hash code for this Chromosome.

Returns:
the hash code of this Chromosome.
Since:
1.0

compareTo

public int compareTo(java.lang.Object other)
Compares the given Chromosome to this Chromosome. This chromosome is considered to be "less than" the given chromosome if it has a fewer number of genes or if any of its gene values (alleles) are less than their corresponding gene values in the other chromosome.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - The Chromosome against which to compare this chromosome.
Returns:
a negative number if this chromosome is "less than" the given chromosome, zero if they are equal to each other, and a positive number if this chromosome is "greater than" the given chromosome.
Since:
1.0

setIsSelectedForNextGeneration

public void setIsSelectedForNextGeneration(boolean a_isSelected)
Sets whether this Chromosome has been selected by the natural selector to continue to the next generation or manually (e.g. via an add-method).

Parameters:
a_isSelected - true if this Chromosome has been selected, false otherwise.
Since:
1.0

isSelectedForNextGeneration

public boolean isSelectedForNextGeneration()
Retrieves whether this Chromosome has been selected by the natural selector to continue to the next generation.

Returns:
true if this Chromosome has been selected, false otherwise.
Since:
1.0

cleanup

public void cleanup()
Invoked when this Chromosome is no longer needed and should perform any necessary cleanup. Note that this method will attempt to release this Chromosome instance to the active ChromosomePool, if any.

Since:
1.0

setApplicationData

public void setApplicationData(java.lang.Object a_newData)
This sets the application-specific data that is attached to this Chromosome. Attaching application-specific data may be useful for some applications when it comes time to evaluate this Chromosome in the fitness function. JGAP ignores this data.

Parameters:
a_newData - The new application-specific data to attach to this Chromosome.
Since:
1.1

getApplicationData

public java.lang.Object getApplicationData()
Retrieves the application-specific data that is attached to this Chromosome. Attaching application-specific data may be useful for some applications when it comes time to evaluate this Chromosome in the fitness function. JGAP ignores this data.

Returns:
The application-specific data previously attached to this Chromosome, or null if there is no attached data.
Since:
1.1

setGenes

public void setGenes(Gene[] a_genes)