org.jgap
Class Genotype
- java.lang.Object
-
- 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.RunnableGenotypes 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, used by all Genotype instances.
- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description 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) insteadGenotype(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
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description voidapplyGeneticOperators()Applies all GeneticOperators registered with the Configuration.booleanequals(java.lang.Object a_other)Compares this Genotype against the specified object.voidevolve()Evolves the population of Chromosomes within this Genotype.java.util.List<java.lang.String>evolve(IEvolutionMonitor a_monitor)Evolves this genotype until the given monitor asks to quit the evolution cycle.voidevolve(int a_numberOfEvolutions)Evolves this Genotype the specified number of times.voidfillPopulation(int a_num)Fills up the population with random chromosomes if necessary.IChromosome[]getChromosomes()Deprecated.uses getPopulation() insteadConfigurationgetConfiguration()java.util.List<IEvolveJob>getEvolves(IPopulationSplitter a_splitter)Splits a population into pieces that can be evolved independently.IChromosomegetFittestChromosome()Retrieves the Chromosome in the Population with the highest fitness value.IChromosomegetFittestChromosome(int a_startIndex, int a_endIndex)Retrieves the Chromosome in the Population with the highest fitness value within the given indices.java.util.ListgetFittestChromosomes(int a_numberOfChromosomes)Retrieves the top n Chromsomes in the population (the ones with the best fitness values).PopulationgetPopulation()static ConfigurationgetStaticConfiguration()inthashCode()Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population.voidmergeResults(IPopulationMerger a_merger, EvolveResult[] a_results)static GenotyperandomInitialGenotype(Configuration a_configuration)Convenience method that returns a newly constructed Genotype instance configured according to the given Configuration instance.voidrun()If used in a Thread: runs the evolution forever.static voidsetStaticConfiguration(Configuration a_configuration)Sets the configuration to use with the Genetic Algorithm.java.lang.StringtoString()
-
-
-
Constructor Detail
-
Genotype
public Genotype(Configuration a_configuration, IChromosome[] a_initialChromosomes) throws InvalidConfigurationException
Deprecated. use Genotype(Configuration, Population) insteadConstructs 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 usea_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 to usea_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- not used here!- Throws:
InvalidConfigurationException- Since:
- 3.0
-
-
Method Detail
-
getChromosomes
public IChromosome[] getChromosomes()
Deprecated. uses getPopulation() insteadRetrieves 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 witha_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
-
evolve
public java.util.List<java.lang.String> evolve(IEvolutionMonitor a_monitor)
Evolves this genotype until the given monitor asks to quit the evolution cycle.- Parameters:
a_monitor- the monitor used to decide when to stop evolution- Returns:
- messages of the registered evolution monitor. May indicate why the evolution was asked to be stopped. May be empty, depending on the implementation of the used monitor
- Since:
- 3.4.4
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- string representation of this Genotype instance, useful for display or debug purposes
- 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 nullInvalidConfigurationException- if the given Configuration instance is not in a valid state- Since:
- 2.3
-
fillPopulation
public void fillPopulation(int a_num) throws InvalidConfigurationExceptionFills up the population with random chromosomes if necessary.- Parameters:
a_num- the number of chromosomes to add- Throws:
InvalidConfigurationException- Since:
- 3.2
-
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 population.- Overrides:
equalsin classjava.lang.Object- Parameters:
a_other- the object to compare against- Returns:
- true if the objects are the same, false otherwise
- Since:
- 1.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)- Overrides:
hashCodein classjava.lang.Object- Returns:
- the computed hashcode
- Since:
- 2.1
-
run
public void run()
If used in 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:
runin interfacejava.lang.Runnable- Since:
- 3.01
-
getEvolves
public java.util.List<IEvolveJob> getEvolves(IPopulationSplitter a_splitter) throws java.lang.Exception
Splits a population into pieces that can be evolved independently.- Parameters:
a_splitter- splits the population- Returns:
- list of IEvolveJob objects
- Throws:
java.lang.Exception
-
mergeResults
public void mergeResults(IPopulationMerger a_merger, EvolveResult[] a_results) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-
DMelt 3.0 © DataMelt by jWork.ORG