JGAP

org.jgap
Class Chromosome

java.lang.Object
  extended by org.jgap.BaseChromosome
      extended by org.jgap.Chromosome
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, IChromosome, IHandler, IInitializer
Direct Known Subclasses:
MyChromosome

public class Chromosome
extends BaseChromosome

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
See Also:
Serialized Form

Field Summary
protected  double m_fitnessValue
          Stores the fitness value of this Chromosome as determined by the active fitness function.
 
Fields inherited from interface org.jgap.IChromosome
S_ALLELES, S_APPLICATION_DATA, S_FITNESS_VALUE, S_SIZE
 
Constructor Summary
Chromosome()
          Default constructor, provided for dynamic instantiation.
Chromosome(Configuration a_configuration)
          Default constructor, provided for dynamic instantiation.
Chromosome(Configuration a_configuration, Gene[] a_initialGenes)
          Constructs a Chromosome separate from any specific Configuration.
Chromosome(Configuration a_configuration, Gene[] a_initialGenes, IGeneConstraintChecker a_constraintChecker)
          Constructs a Chromosome separate from any specific Configuration.
Chromosome(Configuration a_configuration, Gene a_sampleGene, int a_desiredSize)
          Constructs a Chromosome of the given size separate from any specific Configuration.
Chromosome(Configuration a_configuration, Gene a_sampleGene, int a_desiredSize, IGeneConstraintChecker a_constraintChecker)
           
Chromosome(Configuration a_configuration, int a_desiredSize)
          Constructor for specifying the number of genes.
 
Method Summary
protected  double calcFitnessValue()
           
protected  void checkGenes(Gene[] a_initialGenes)
          Helper: called by constructors only to verify the initial genes.
 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.
protected  java.lang.Object cloneObject(java.lang.Object a_object)
          Clones an object by using clone handlers.
 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.
 IGeneConstraintChecker getConstraintChecker()
           
 double getFitnessValue()
          Retrieves the fitness value of this Chromosome, as determined by the active fitness function.
 double getFitnessValueDirectly()
           
 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.
 java.util.List getMultiObjectives()
           
 int hashCode()
          Retrieve a hash code for this Chromosome.
protected  void initFromGene(Gene a_sampleGene)
           
 boolean isCompareApplicationData()
           
 boolean isHandlerFor(java.lang.Object a_obj, java.lang.Class a_class)
          Determines whether the handler is suitable for the given object instance or class.
 boolean isSelectedForNextGeneration()
          Retrieves whether this Chromosome has been selected by the natural selector to continue to the next generation.
 java.lang.Object perform(java.lang.Object a_obj, java.lang.Class a_class, java.lang.Object a_params)
          Performs a task for the given object or class.
static IChromosome randomInitialChromosome(Configuration a_configuration)
          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 setCompareApplicationData(boolean a_doCompare)
          Should we also consider the application data when comparing?
 void setConstraintChecker(IGeneConstraintChecker a_constraintChecker)
          Sets the constraint checker to be used for this gene whenever method setAllele(Object) is called.
 void setFitnessValue(double a_newFitnessValue)
          Sets the fitness value of this Chromosome.
 void setFitnessValueDirectly(double a_newFitnessValue)
          Sets the fitness value of this Chromosome directly without any constraint checks, conversions or checks.
 void setGenes(Gene[] a_genes)
          Sets the genes for the chromosome.
 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).
 void setMultiObjectives(java.util.List a_values)
           
 int size()
          Returns the size of this Chromosome (the number of genes it contains).
 java.lang.String toString()
           
protected  void verify(IGeneConstraintChecker a_constraintChecker)
          Verifies the state of the chromosome.
 
Methods inherited from class org.jgap.BaseChromosome
getConfiguration
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

m_fitnessValue

protected double 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).

Since:
2.0 (until 1.1: type int)
Constructor Detail

Chromosome

public Chromosome()
           throws InvalidConfigurationException
Default constructor, provided for dynamic instantiation.

Attention: The configuration used is the one set with the static method Genotype.setConfiguration.

Throws:
InvalidConfigurationException
Since:
2.4

Chromosome

public Chromosome(Configuration a_configuration)
           throws InvalidConfigurationException
Default constructor, provided for dynamic instantiation.

Parameters:
a_configuration - the configuration to use
Throws:
InvalidConfigurationException
Since:
3.0

Chromosome

public Chromosome(Configuration a_configuration,
                  int a_desiredSize)
           throws InvalidConfigurationException
Constructor for specifying the number of genes.

Parameters:
a_configuration - the configuration to use
a_desiredSize - number of genes the chromosome contains of
Throws:
InvalidConfigurationException
Since:
2.2

Chromosome

public Chromosome(Configuration a_configuration,
                  Gene a_sampleGene,
                  int a_desiredSize)
           throws InvalidConfigurationException
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_configuration - the configuration to use
a_sampleGene - a concrete sampleGene 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
Throws:
InvalidConfigurationException
Since:
1.0

Chromosome

public Chromosome(Configuration a_configuration,
                  Gene a_sampleGene,
                  int a_desiredSize,
                  IGeneConstraintChecker a_constraintChecker)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

Chromosome

public Chromosome(Configuration a_configuration,
                  Gene[] a_initialGenes)
           throws InvalidConfigurationException
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_configuration - the configuration to use
a_initialGenes - the initial genes of this Chromosome
Throws:
InvalidConfigurationException
Since:
1.0

Chromosome

public Chromosome(Configuration a_configuration,
                  Gene[] a_initialGenes,
                  IGeneConstraintChecker a_constraintChecker)
           throws InvalidConfigurationException
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. Additionally, a constraint checker can be specified. It is used right here to verify the validity of the gene types supplied.

Parameters:
a_configuration - the configuration to use
a_initialGenes - the initial genes of this Chromosome
a_constraintChecker - constraint checker to use
Throws:
InvalidConfigurationException - in case the constraint checker reports a configuration error
Since:
2.5
Method Detail

initFromGene

protected void initFromGene(Gene a_sampleGene)

checkGenes

protected void checkGenes(Gene[] a_initialGenes)
Helper: called by constructors only to verify the initial genes.

Parameters:
a_initialGenes - the initial genes of this Chromosome to verify
Since:
2.5

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.

Specified by:
clone in interface IChromosome
Specified by:
clone in class BaseChromosome
Returns:
copy of this Chromosome
Throws:
java.lang.IllegalStateException - instead of CloneNotSupportedException
Since:
1.0

cloneObject

protected java.lang.Object cloneObject(java.lang.Object a_object)
                                throws java.lang.Exception
Clones an object by using clone handlers. If no deep cloning possible, then return the reference.

Parameters:
a_object - the object to clone
Returns:
the cloned object, or the object itself if no coning supported
Throws:
java.lang.Exception
Since:
2.6

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 - index of the gene value to be returned
Returns:
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 not change, until setGenes(...) is used.

Returns:
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.

Attention: should not be called from toString() as the fitness value would be computed if it was initial!

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)

getFitnessValueDirectly

public double getFitnessValueDirectly()
Returns:
the lastly computed fitness value, or FitnessFunction.NO_FITNESS_VALUE in case no value has been computed yet.

calcFitnessValue

protected double calcFitnessValue()
Returns:
fitness value of this chromosome determined via the registered fitness function
Since:
2.4

setFitnessValue

public void setFitnessValue(double 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 (except test cases).

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

setFitnessValueDirectly

public void setFitnessValueDirectly(double a_newFitnessValue)
Sets the fitness value of this Chromosome directly without any constraint checks, conversions or checks. Only use if you know what you do.

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
a string representation of this Chromosome, useful for display purposes.
Since:
1.0

randomInitialChromosome

public static IChromosome randomInitialChromosome(Configuration a_configuration)
                                           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.

Parameters:
a_configuration - the configuration to use
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.

Overrides:
equals in class java.lang.Object
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. Does not considers the order of the Genes for all cases (especially when gene is empty).

Overrides:
hashCode in class java.lang.Object
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.

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, except for cloning and comparison (latter only if opted in via setCompareApplicationData(..))

Parameters:
a_newData - the new application-specific data to attach to this Chromosome. Should be an instance of IApplicationData
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 functionally.

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

setGenes

public void setGenes(Gene[] a_genes)
              throws InvalidConfigurationException
Sets the genes for the chromosome.

Parameters:
a_genes - the genes to set for the chromosome
Throws:
InvalidConfigurationException - in case constraint checker is provided

setCompareApplicationData

public void setCompareApplicationData(boolean a_doCompare)
Should we also consider the application data when comparing? Default is "false" as "true" means a Chromosome is losing its identity when application data is set differently!

Parameters:
a_doCompare - true: consider application data in method compareTo
Since:
2.2

isCompareApplicationData

public boolean isCompareApplicationData()

setConstraintChecker

public void setConstraintChecker(IGeneConstraintChecker a_constraintChecker)
                          throws InvalidConfigurationException
Sets the constraint checker to be used for this gene whenever method setAllele(Object) is called.

Parameters:
a_constraintChecker - the constraint checker to be set
Throws:
InvalidConfigurationException
Since:
2.5

getConstraintChecker

public IGeneConstraintChecker getConstraintChecker()
Returns:
IGeneConstraintChecker the constraint checker to be used whenever method setGenes(Gene[]) is called.
Since:
2.5

verify

protected void verify(IGeneConstraintChecker a_constraintChecker)
               throws InvalidConfigurationException
Verifies the state of the chromosome. Especially takes care of the given constraint checker.

Parameters:
a_constraintChecker - the constraint checker to verify
Throws:
InvalidConfigurationException
Since:
2.5

isHandlerFor

public boolean isHandlerFor(java.lang.Object a_obj,
                            java.lang.Class a_class)
Determines whether the handler is suitable for the given object instance or class.

Parameters:
a_obj - the object instance to check
a_class - the class to check, alternatively if no object instance is available (some handlers could need to get an object and would always return false if only a class is provided)
Returns:
true: handler suitable for given object or class

perform

public java.lang.Object perform(java.lang.Object a_obj,
                                java.lang.Class a_class,
                                java.lang.Object a_params)
                         throws java.lang.Exception
Performs a task for the given object or class. For some handlers, additional parameters are necessary, which could be provided.

Parameters:
a_obj - the object instance to perform the handler task for
a_class - the class to perform the handler task for, in case no object instance is available (some handlers could need to get an object and would always throw an exception if only a class is provided)
a_params - optional parameters needed for the handler to perform its task
Returns:
Object
Throws:
java.lang.Exception - in case of any error

setMultiObjectives

public void setMultiObjectives(java.util.List a_values)

getMultiObjectives

public java.util.List getMultiObjectives()

JGAP