org.jgap
Class Population
- java.lang.Object
-
- org.jgap.Population
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, IPersistentRepresentation, ICloneable
public class Population extends java.lang.Object implements java.io.Serializable, ICloneable, IPersistentRepresentation
Makes up the population of a generation during evolution. Represented by a list of chromosomes held in the Genotype.- Since:
- 2.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.StringCHROM_DELIMITERstatic java.lang.StringCHROM_DELIMITER_CLOSINGRepresents the closing delimiter that is used to separate chromosomes in the persistent representation.static java.lang.StringCHROM_DELIMITER_HEADINGRepresents the heading delimiter that is used to separate chromosomes in the persistent representation.
-
Constructor Summary
Constructors Constructor and Description Population()Population(Configuration a_config)Population(Configuration a_config, IChromosome a_chromosome)Population(Configuration a_config, IChromosome[] a_chromosomes)Population(Configuration a_config, int a_size)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddChromosome(IChromosome a_toAdd)Adds a Chromosome to this Population.voidaddChromosomes(Population a_population)Adds all the Chromosomes in the given Population.voidclear()Clears the list of chromosomes.java.lang.Objectclone()intcompareTo(java.lang.Object a_pop)This method is not producing symmetric results as -1 is more often returned than 1 (see description of return value).booleancontains(IChromosome a_chromosome)Determines whether the given chromosome is contained within the population.IChromosomedetermineFittestChromosome()Determines the fittest Chromosome in the Population (the one with the highest fitness value) and memorizes it.IChromosomedetermineFittestChromosome(int a_startIndex, int a_endIndex)Determines the fittest Chromosome in the population (the one with the highest fitness value) within the given indices and memorizes it.java.util.ListdetermineFittestChromosomes(int a_numberOfChromosomes)Sorts the Chromosome list and returns the fittest n Chromosomes in the population.booleanequals(java.lang.Object a_pop)The equals-method.IChromosomegetChromosome(int a_index)java.util.List<IChromosome>getChromosomes()ConfigurationgetConfiguration()java.util.ListgetGenome(boolean a_resolveCompositeGenes)Returns the genotype of the population, i.e.java.lang.StringgetPersistentRepresentation()Returns a persistent representation of this chromosome, see interface Gene for description.inthashCode()Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population.booleanisChanged()booleanisSorted()java.util.Iteratoriterator()voidkeepPopSizeConstant()Cares that the population size does not exceed the maximum size given in the configuration.voidsetChromosome(int a_index, IChromosome a_chromosome)Sets in the given Chromosome on the given index in the list of chromosomes.voidsetChromosomes(java.util.List a_chromosomes)Replaces all chromosomes in the population with the give list of chromosomes.voidsetValueFromPersistentRepresentation(java.lang.String a_representation)Counterpart of getPersistentRepresentation.intsize()voidsortByFitness()Sorts the chromosomes within the population according to their fitness value using ChromosomFitnessComparator.IChromosome[]toChromosomes()
-
-
-
Field Detail
-
CHROM_DELIMITER
public static final java.lang.String CHROM_DELIMITER
- See Also:
- Constant Field Values
-
CHROM_DELIMITER_HEADING
public static final java.lang.String CHROM_DELIMITER_HEADING
Represents the heading delimiter that is used to separate chromosomes in the persistent representation.- See Also:
- Constant Field Values
-
CHROM_DELIMITER_CLOSING
public static final java.lang.String CHROM_DELIMITER_CLOSING
Represents the closing delimiter that is used to separate chromosomes in the persistent representation.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Population
public Population(Configuration a_config) throws InvalidConfigurationException
- Throws:
InvalidConfigurationException
-
Population
public Population(Configuration a_config, IChromosome[] a_chromosomes) throws InvalidConfigurationException
- Throws:
InvalidConfigurationException
-
Population
public Population(Configuration a_config, IChromosome a_chromosome) throws InvalidConfigurationException
- Throws:
InvalidConfigurationException
-
Population
public Population(Configuration a_config, int a_size) throws InvalidConfigurationException
- Throws:
InvalidConfigurationException
-
Population
public Population() throws InvalidConfigurationException- Throws:
InvalidConfigurationException
-
-
Method Detail
-
getConfiguration
public Configuration getConfiguration()
-
addChromosome
public void addChromosome(IChromosome a_toAdd)
Adds a Chromosome to this Population. Does nothing when given null.- Parameters:
a_toAdd- the Chromosome to add- Since:
- 2.0
-
addChromosomes
public void addChromosomes(Population a_population)
Adds all the Chromosomes in the given Population. Does nothing on null or an empty Population.- Parameters:
a_population- the Population to add- Since:
- 2.0
-
setChromosomes
public void setChromosomes(java.util.List a_chromosomes)
Replaces all chromosomes in the population with the give list of chromosomes.- Parameters:
a_chromosomes- the chromosomes to make the population up from
-
setChromosome
public void setChromosome(int a_index, IChromosome a_chromosome)Sets in the given Chromosome on the given index in the list of chromosomes. If the given index is exceeding the list by one, the chromosome is appended.- Parameters:
a_index- the index to set the Chromosome ina_chromosome- the Chromosome to be set- Since:
- 2.0
-
getChromosomes
public java.util.List<IChromosome> getChromosomes()
- Returns:
- the list of Chromosome's in the Population. Don't modify the retrieved list by using clear(), remove(int) etc. If you do so, you need to call setChanged(true)
- Since:
- 2.0
-
getChromosome
public IChromosome getChromosome(int a_index)
- Parameters:
a_index- the index of the Chromosome to be returned- Returns:
- Chromosome at given index in the Population
- Since:
- 2.0
-
size
public int size()
- Returns:
- number of Chromosome's in the Population
- Since:
- 2.0
-
iterator
public java.util.Iterator iterator()
- Returns:
- Iterator for the Chromosome list in the Population. Please be aware that using remove() forces you to call setChanged(true)
- Since:
- 2.0
-
toChromosomes
public IChromosome[] toChromosomes()
- Returns:
- the Population converted into a list of Chromosome's
- Since:
- 2.0
-
determineFittestChromosome
public IChromosome determineFittestChromosome()
Determines the fittest Chromosome in the Population (the one with the highest fitness value) and memorizes it. This is an optimized version compared to calling determineFittesChromosomes(1).- Returns:
- the fittest Chromosome of the Population
- Since:
- 2.0
-
determineFittestChromosome
public IChromosome determineFittestChromosome(int a_startIndex, int a_endIndex)
Determines the fittest Chromosome in the population (the one with the highest fitness value) within the given indices and memorizes it. This is an optimized version compared to calling determineFittesChromosomes(1).- Parameters:
a_startIndex- index to begin the evaluation witha_endIndex- index to end the evaluation with- Returns:
- the fittest Chromosome of the population within the given indices
- Since:
- 3.0
-
isChanged
public boolean isChanged()
- Returns:
- true: population's chromosomes (maybe) were changed, false: not changed for sure
- Since:
- 2.6
-
contains
public boolean contains(IChromosome a_chromosome)
Determines whether the given chromosome is contained within the population.- Parameters:
a_chromosome- the chromosome to check- Returns:
- true: chromosome contained within population
- Since:
- 2.1
-
keepPopSizeConstant
public void keepPopSizeConstant() throws InvalidConfigurationExceptionCares that the population size does not exceed the maximum size given in the configuration.- Throws:
InvalidConfigurationException- Since:
- 3.2
-
determineFittestChromosomes
public java.util.List determineFittestChromosomes(int a_numberOfChromosomes)
Sorts the Chromosome list and returns the fittest n Chromosomes in the population.- Parameters:
a_numberOfChromosomes- number of top performer chromosomes to be returned- Returns:
- list of the fittest n Chromosomes of the population, or the fittest x Chromosomes with x = number of chromosomes in case n > x.
- Since:
- 2.4
-
sortByFitness
public void sortByFitness()
Sorts the chromosomes within the population according to their fitness value using ChromosomFitnessComparator. The fittest chromosome is then at index 0.- Since:
- 2.6
-
getGenome
public java.util.List getGenome(boolean a_resolveCompositeGenes)
Returns the genotype of the population, i.e. the list of genes in the Population.- Parameters:
a_resolveCompositeGenes- true: split encountered CompositeGenes into their single (atomic) genes- Returns:
- genotype of the population
- Since:
- 2.3
-
isSorted
public boolean isSorted()
-
equals
public boolean equals(java.lang.Object a_pop)
The equals-method.- Overrides:
equalsin classjava.lang.Object- Parameters:
a_pop- the population instance to compare with- Returns:
- true: given object equal to comparing one
- Since:
- 2.6
-
compareTo
public int compareTo(java.lang.Object a_pop)
This method is not producing symmetric results as -1 is more often returned than 1 (see description of return value).- Parameters:
a_pop- the other population to compare- Returns:
- 1: a_pop is null or having fewer chromosomes or equal number of chromosomes but at least one not contained. 0: both populations containing exactly the same chromosomes. -1: this population contains fewer chromosomes than a_pop
- Since:
- 2.6
-
clone
public java.lang.Object clone()
- Specified by:
clonein interfaceICloneable- Overrides:
clonein classjava.lang.Object- Returns:
- deeply cloned population instance
- Since:
- 3.2
-
clear
public void clear()
Clears the list of chromosomes. Normally, this should not be necessary. But especially in distributed computing, a fresh population has to be provided sometimes.- Since:
- 3.2
-
getPersistentRepresentation
public java.lang.String getPersistentRepresentation()
Returns a persistent representation of this chromosome, see interface Gene for description. Similar to CompositeGene's routine. But does not include all information of the chromosome (yet).- Specified by:
getPersistentRepresentationin interfaceIPersistentRepresentation- Returns:
- string representation of this Chromosome's relevant parts of its current state
- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.2
-
setValueFromPersistentRepresentation
public void setValueFromPersistentRepresentation(java.lang.String a_representation) throws UnsupportedRepresentationExceptionCounterpart of getPersistentRepresentation.- Specified by:
setValueFromPersistentRepresentationin interfaceIPersistentRepresentation- Parameters:
a_representation- the string representation retrieved from a prior call to the getPersistentRepresentation() method- Throws:
UnsupportedRepresentationException- Since:
- 3.2
-
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:
- 3.5
-
-
DMelt 3.0 © DataMelt by jWork.ORG