JGAP

org.jgap
Class Genotype

java.lang.Object
  extended by org.jgap.Genotype
All Implemented Interfaces:
java.io.Serializable, java.lang.Runnable
Direct Known Subclasses:
DistributedGenotype

public class Genotype
extends java.lang.Object
implements java.io.Serializable, java.lang.Runnable

Genotypes are fixed-length populations of chromosomes. As an instance of a Genotype is evolved, all of its Chromosomes are also evolved. A Genotype may be constructed normally via constructor, or the static randomInitialGenotype() method can be used to generate a Genotype with a randomized Chromosome population.

Please note that among all created Genotype instances there may only be one configuration (singleton!), used by all Genotype instances

Since:
1.0
See Also:
Serialized Form

Constructor Summary
Genotype(Configuration a_configuration)
          Don't use this constructor, it's only for internal use.
Genotype(Configuration a_configuration, IChromosome[] a_initialChromosomes)
          Deprecated. use Genotype(Configuration, Population) instead
Genotype(Configuration a_configuration, Population a_population)
          Constructs a new Genotype instance with the given array of Chromosomes and the given active Configuration instance.
 
Method Summary
protected  void applyGeneticOperator(GeneticOperator a_operator, Population a_population, java.util.List a_chromosomes)
          Overwritable method that calls a GeneticOperator to operate on a given population and asks him to store the result in the list of chromosomes.
 void applyGeneticOperators()
          Applies all GeneticOperators registered with the Configuration.
protected  void applyNaturalSelectors(boolean a_processBeforeGeneticOperators)
          Applies all NaturalSelectors registered with the Configuration.
 boolean equals(java.lang.Object a_other)
          Compares this Genotype against the specified object.
 void evolve()
          Evolves the population of Chromosomes within this Genotype.
 void evolve(int a_numberOfEvolutions)
          Evolves this Genotype the specified number of times.
 IChromosome[] getChromosomes()
          Deprecated. uses getPopulation() instead
 Configuration getConfiguration()
           
 IChromosome getFittestChromosome()
          Retrieves the Chromosome in the population with the highest fitness value.
 IChromosome getFittestChromosome(int a_startIndex, int a_endIndex)
          Retrieves the Chromosome in the population with the highest fitness value within the given indices.
 java.util.List getFittestChromosomes(int a_numberOfChromosomes)
          Retrieves the top n Chromsomes in the population (the ones with the best fitness values).
 Population getPopulation()
           
static Configuration getStaticConfiguration()
           
 int hashCode()
          Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population.
protected  void keepPopSizeConstant(Population a_pop, int a_maxSize)
          Cares that the population size does not exceed the given maximum size.
static Genotype randomInitialGenotype(Configuration a_configuration)
          Convenience method that returns a newly constructed Genotype instance configured according to the given Configuration instance.
 void run()
          If used with a Thread: runs the evolution forever.
protected  void setPopulation(Population a_pop)
           
static void setStaticConfiguration(Configuration a_configuration)
          Sets the configuration to use with the Genetic Algorithm.
 java.lang.String toString()
          Return a string representation of this Genotype instance, useful for display purposes.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Genotype

public Genotype(Configuration a_configuration,
                IChromosome[] a_initialChromosomes)
         throws InvalidConfigurationException
Deprecated. use Genotype(Configuration, Population) instead

Constructs a new Genotype instance with the given array of Chromosomes and the given active Configuration instance. Note that the Configuration object must be in a valid state when this method is invoked, or a InvalidConfigurationException will be thrown.

Parameters:
a_configuration - the Configuration object to use
a_initialChromosomes - the Chromosome population to be managed by this Genotype instance
Throws:
InvalidConfigurationException - if the given Configuration object is in an invalid state
Since:
1.0

Genotype

public Genotype(Configuration a_configuration,
                Population a_population)
         throws InvalidConfigurationException
Constructs a new Genotype instance with the given array of Chromosomes and the given active Configuration instance. Note that the Configuration object must be in a valid state when this method is invoked, or a InvalidconfigurationException will be thrown.

Parameters:
a_configuration - the Configuration object t use
a_population - the Chromosome population to be managed by this Genotype instance
Throws:
InvalidConfigurationException
Since:
2.0

Genotype

public Genotype(Configuration a_configuration)
         throws InvalidConfigurationException
Don't use this constructor, it's only for internal use.

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

getChromosomes

public IChromosome[] getChromosomes()
Deprecated. uses getPopulation() instead

Retrieves the array of Chromosomes that make up the population of this Genotype instance.

Returns:
the population of Chromosomes
Since:
1.0

getPopulation

public Population getPopulation()
Returns:
the current population of chromosomes
Since:
2.1 (?)

getFittestChromosome

public IChromosome getFittestChromosome()
Retrieves the Chromosome in the population with the highest fitness value.

Returns:
the Chromosome with the highest fitness value, or null if there are no chromosomes in this Genotype
Since:
1.0

getFittestChromosome

public IChromosome getFittestChromosome(int a_startIndex,
                                        int a_endIndex)
Retrieves the Chromosome in the population with the highest fitness value within the given indices.

Parameters:
a_startIndex - the index to start the determination with
a_endIndex - the index to end the determination with
Returns:
the Chromosome with the highest fitness value within the given indices, or null if there are no chromosomes in this Genotype
Since:
3.0

getFittestChromosomes

public java.util.List getFittestChromosomes(int a_numberOfChromosomes)
Retrieves the top n Chromsomes in the population (the ones with the best fitness values).

Parameters:
a_numberOfChromosomes - the number of chromosomes desired
Returns:
the list of Chromosomes with the highest fitness values, or null if there are no chromosomes in this Genotype
Since:
2.4

evolve

public void evolve()
Evolves the population of Chromosomes within this Genotype. This will execute all of the genetic operators added to the present active configuration and then invoke the natural selector to choose which chromosomes will be included in the next generation population. Note that the population size not always remains constant (dependent on the NaturalSelectors used!).

To consecutively call this method, use evolve(int)!!!

Since:
1.0

evolve

public void evolve(int a_numberOfEvolutions)
Evolves this Genotype the specified number of times. This is equivalent to invoking the standard evolve() method the given number of times in a row.

Parameters:
a_numberOfEvolutions - the number of times to evolve this Genotype before returning
Since:
1.1

toString

public java.lang.String toString()
Return a string representation of this Genotype instance, useful for display purposes.

Overrides:
toString in class java.lang.Object
Returns:
string representation of this Genotype instance
Since:
1.0

randomInitialGenotype

public static Genotype randomInitialGenotype(Configuration a_configuration)
                                      throws InvalidConfigurationException
Convenience method that returns a newly constructed Genotype instance configured according to the given Configuration instance. The population of Chromosomes will be created according to the setup of the sample Chromosome in the Configuration object, but the gene values (alleles) will be set to random legal values.

Parameters:
a_configuration - the current active Configuration object
Returns:
a newly constructed Genotype instance
Throws:
java.lang.IllegalArgumentException - if the given Configuration object is null
InvalidConfigurationException - if the given Configuration instance is not in a valid state
Since:
2.3

equals

public boolean equals(java.lang.Object a_other)
Compares this Genotype against the specified object. The result is true if the argument is an instance of the Genotype class, has exactly the same number of chromosomes as the given Genotype, and, for each Chromosome in this Genotype, there is an equal chromosome in the given Genotype. The chromosomes do not need to appear in the same order within the populations.

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

applyNaturalSelectors

protected void applyNaturalSelectors(boolean a_processBeforeGeneticOperators)
Applies all NaturalSelectors registered with the Configuration.

Parameters:
a_processBeforeGeneticOperators - true apply NaturalSelectors applicable before GeneticOperators, false: apply the ones applicable after GeneticOperators
Since:
2.0

applyGeneticOperators

public void applyGeneticOperators()
Applies all GeneticOperators registered with the Configuration.

Since:
3.0

getStaticConfiguration

public static Configuration getStaticConfiguration()
Returns:
the configuration to use with the Genetic Algorithm
Since:
2.0 (?)

setStaticConfiguration

public static void setStaticConfiguration(Configuration a_configuration)
Sets the configuration to use with the Genetic Algorithm.

Parameters:
a_configuration - the configuration to use
Since:
2.0 (?)

getConfiguration

public Configuration getConfiguration()

hashCode

public int hashCode()
Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population. The logic for the hashcode is Step Result ---- ------ 1 31*0 + hashcode_0 = y(1) 2 31*y(1) + hashcode_1 = y(2) 3 31*y(2) + hashcode_2 = y(3) n 31*y(n-1) + hashcode_n-1 = y(n) Each hashcode is a number and the binary equivalent is computed and returned.

Overrides:
hashCode in class java.lang.Object
Returns:
the computed hashcode
Since:
2.1

setPopulation

protected void setPopulation(Population a_pop)

applyGeneticOperator

protected void applyGeneticOperator(GeneticOperator a_operator,
                                    Population a_population,
                                    java.util.List a_chromosomes)
Overwritable method that calls a GeneticOperator to operate on a given population and asks him to store the result in the list of chromosomes. Override this method if you want to ensure that a_chromosomes is not part of a_population resp. if you want to use a different list.

Parameters:
a_operator - the GeneticOperator to call
a_population - the population to use
a_chromosomes - the list of Chromosome objects to return
Since:
2.4

keepPopSizeConstant

protected void keepPopSizeConstant(Population a_pop,
                                   int a_maxSize)
Cares that the population size does not exceed the given maximum size.

Parameters:
a_pop - the population to keep constant in size
a_maxSize - the maximum size allowed for the population
Since:
2.5

run

public void run()
If used with a Thread: runs the evolution forever. You have to implement a listener to stop computation sometime. See examples.simpleBooleanThreaded for a possible implementation of such a listener.

Specified by:
run in interface java.lang.Runnable
Since:
3.01

JGAP