org.jgap
Class Chromosome

java.lang.Object
  extended by org.jgap.Chromosome
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable

public class Chromosome
extends java.lang.Object
implements java.lang.Comparable, 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 with the same innovation ID must share the same concrete implementation across Chromosomes within a single population (genotype).

See Also:
Serialized Form

Field Summary
static java.lang.Long DEFAULT_ID
          default ID
protected  int m_fitnessValue
          Stores the fitness value of this Chromosome as determined by the active fitness function.
protected  boolean m_isSelectedForNextGeneration
          Keeps track of whether or not this Chromosome has been selected by the natural selector to move on to the next generation.
 
Constructor Summary
Chromosome(ChromosomeMaterial a_material, java.lang.Long an_id)
          this should only be called when a chromosome is being created from persistence; otherwise, the ID should be generated by a_activeConfiguration.
 
Method Summary
 ChromosomeMaterial cloneMaterial()
           
 int compareTo(java.lang.Object o)
          Compares the given Chromosome to this Chromosome.
 double distance(Chromosome target, SpeciationParms parms)
          Calculates compatibility distance between this and target according to NEAT speciation methodology.
 boolean equals(java.lang.Object other)
          Compares this Chromosome against the specified object.
 Allele findMatchingGene(Allele alleleToMatch)
           
 java.util.SortedSet getAlleles()
           
 int getFitnessValue()
          Retrieves the fitness value of this Chromosome, as determined by the active fitness function.
 java.lang.Long getId()
           
 java.lang.Long getPrimaryParentId()
           
 java.lang.Long getSecondaryParentId()
           
 int getSpeciatedFitnessValue()
           
 Specie getSpecie()
           
 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.
 void setFitnessValue(int a_newFitnessValue)
          Sets the fitness value of this Chromosome.
 void setIsSelectedForNextGeneration(boolean a_isSelected)
          Sets whether this Chromosome has been selected by the natural selector to continue to the next generation.
 int size()
          Returns the size of this Chromosome (the number of alleles 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
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_ID

public static final java.lang.Long DEFAULT_ID
default ID


m_isSelectedForNextGeneration

protected boolean m_isSelectedForNextGeneration
Keeps track of whether or not this Chromosome has been selected by the natural selector to move on to the next generation.


m_fitnessValue

protected int m_fitnessValue
Stores the fitness value of this Chromosome as determined by the active fitness function. A value of -1 indicates that this field has not yet been set with this Chromosome's fitness values (valid fitness values are always positive).

Constructor Detail

Chromosome

public Chromosome(ChromosomeMaterial a_material,
                  java.lang.Long an_id)
this should only be called when a chromosome is being created from persistence; otherwise, the ID should be generated by a_activeConfiguration.

Parameters:
a_material - Genetic material to be contained within this Chromosome instance.
an_id - unique ID of new chromosome
Method Detail

distance

public double distance(Chromosome target,
                       SpeciationParms parms)
Calculates compatibility distance between this and target according to NEAT speciation methodology. It is generic enough that the alleles do not have to be nodes and connections.

Parameters:
target -
parms -
Returns:
distance between this object and target
See Also:
ChromosomeMaterial.distance(ChromosomeMaterial, SpeciationParms)

getId

public java.lang.Long getId()
Returns:
Long unique identifier for chromosome; useful for hashCode() and persistence

size

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

Returns:
The number of alleles contained within this Chromosome instance.

cloneMaterial

public ChromosomeMaterial cloneMaterial()
Returns:
clone with primary parent ID of this chromosome and the same genetic material.

getAlleles

public java.util.SortedSet getAlleles()
Returns:
SortedSet alleles, sorted by innovation ID

findMatchingGene

public Allele findMatchingGene(Allele alleleToMatch)
Parameters:
alleleToMatch -
Returns:
Gene gene with same innovation ID as geneToMatchnull if none match

getFitnessValue

public int getFitnessValue()
Retrieves the fitness value of this Chromosome, as determined by the active fitness function.

Returns:
a positive integer value representing the fitness of this Chromosome, or -1 if fitness function has not yet assigned a fitness value to this Chromosome.

getSpeciatedFitnessValue

public int getSpeciatedFitnessValue()
Returns:
int fitness value adjusted for fitness sharing according to

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 invoked from anything else. This is the raw fitness value, before species fitness sharing.

Parameters:
a_newFitnessValue - a positive integer representing the fitness of this Chromosome. if 0, fitness is set as 1.

toString

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

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

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.

Overrides:
equals in class java.lang.Object
Parameters:
other - The object to compare against.
Returns:
true if the objects are the same, false otherwise.

hashCode

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

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code of this Chromosome.

compareTo

public int compareTo(java.lang.Object o)
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:
o - 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.

setIsSelectedForNextGeneration

public void setIsSelectedForNextGeneration(boolean a_isSelected)
Sets whether this Chromosome has been selected by the natural selector to continue to the next generation.

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

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.

getSpecie

public Specie getSpecie()
Returns:
this chromosome's specie

getPrimaryParentId

public java.lang.Long getPrimaryParentId()
Returns:
primary parent ID; this is the dominant parent for chromosomes spawned by crossover, and the only parent for chromosomes spawned by cloning

getSecondaryParentId

public java.lang.Long getSecondaryParentId()
Returns:
secondary parent ID; this is the recessive parent for chromosomes spawned by crossover, and null for chromosomes spawned by cloning