Class ListPopulation
- java.lang.Object
-
- org.apache.commons.math3.genetics.ListPopulation
-
- All Implemented Interfaces:
java.lang.Iterable<Chromosome>
,Population
- Direct Known Subclasses:
ElitisticListPopulation
public abstract class ListPopulation extends java.lang.Object implements Population
Population of chromosomes represented by aList
.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<Chromosome>
chromosomes
List of chromosomesprivate int
populationLimit
maximal size of the population
-
Constructor Summary
Constructors Constructor Description ListPopulation(int populationLimit)
Creates a new ListPopulation instance and initializes its inner chromosome list.ListPopulation(java.util.List<Chromosome> chromosomes, int populationLimit)
Creates a new ListPopulation instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addChromosome(Chromosome chromosome)
Add the given chromosome to the population.void
addChromosomes(java.util.Collection<Chromosome> chromosomeColl)
Add aCollection
of chromosomes to thisPopulation
.protected java.util.List<Chromosome>
getChromosomeList()
Access the list of chromosomes.java.util.List<Chromosome>
getChromosomes()
Returns an unmodifiable list of the chromosomes in this population.Chromosome
getFittestChromosome()
Access the fittest chromosome in this population.int
getPopulationLimit()
Access the maximum population size.int
getPopulationSize()
Access the current population size.java.util.Iterator<Chromosome>
iterator()
Returns an iterator over the unmodifiable list of chromosomes.void
setChromosomes(java.util.List<Chromosome> chromosomes)
Deprecated.useaddChromosomes(Collection)
insteadvoid
setPopulationLimit(int populationLimit)
Sets the maximal population size.java.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.math3.genetics.Population
nextGeneration
-
-
-
-
Field Detail
-
chromosomes
private java.util.List<Chromosome> chromosomes
List of chromosomes
-
populationLimit
private int populationLimit
maximal size of the population
-
-
Constructor Detail
-
ListPopulation
public ListPopulation(int populationLimit) throws NotPositiveException
Creates a new ListPopulation instance and initializes its inner chromosome list.- Parameters:
populationLimit
- maximal size of the population- Throws:
NotPositiveException
- if the population limit is not a positive number (< 1)
-
ListPopulation
public ListPopulation(java.util.List<Chromosome> chromosomes, int populationLimit) throws NullArgumentException, NotPositiveException, NumberIsTooLargeException
Creates a new ListPopulation instance.Note: the chromosomes of the specified list are added to the population.
- Parameters:
chromosomes
- list of chromosomes to be added to the populationpopulationLimit
- maximal size of the population- Throws:
NullArgumentException
- if the list of chromosomes isnull
NotPositiveException
- if the population limit is not a positive number (< 1)NumberIsTooLargeException
- if the list of chromosomes exceeds the population limit
-
-
Method Detail
-
setChromosomes
@Deprecated public void setChromosomes(java.util.List<Chromosome> chromosomes) throws NullArgumentException, NumberIsTooLargeException
Deprecated.useaddChromosomes(Collection)
insteadSets the list of chromosomes.Note: this method removes all existing chromosomes in the population and adds all chromosomes of the specified list to the population.
- Parameters:
chromosomes
- the list of chromosomes- Throws:
NullArgumentException
- if the list of chromosomes isnull
NumberIsTooLargeException
- if the list of chromosomes exceeds the population limit
-
addChromosomes
public void addChromosomes(java.util.Collection<Chromosome> chromosomeColl) throws NumberIsTooLargeException
Add aCollection
of chromosomes to thisPopulation
.- Parameters:
chromosomeColl
- aCollection
of chromosomes- Throws:
NumberIsTooLargeException
- if the population would exceed the population limit when adding this chromosome- Since:
- 3.1
-
getChromosomes
public java.util.List<Chromosome> getChromosomes()
Returns an unmodifiable list of the chromosomes in this population.- Returns:
- the unmodifiable list of chromosomes
-
getChromosomeList
protected java.util.List<Chromosome> getChromosomeList()
Access the list of chromosomes.- Returns:
- the list of chromosomes
- Since:
- 3.1
-
addChromosome
public void addChromosome(Chromosome chromosome) throws NumberIsTooLargeException
Add the given chromosome to the population.- Specified by:
addChromosome
in interfacePopulation
- Parameters:
chromosome
- the chromosome to add.- Throws:
NumberIsTooLargeException
- if the population would exceed thepopulationLimit
after adding this chromosome
-
getFittestChromosome
public Chromosome getFittestChromosome()
Access the fittest chromosome in this population.- Specified by:
getFittestChromosome
in interfacePopulation
- Returns:
- the fittest chromosome.
-
getPopulationLimit
public int getPopulationLimit()
Access the maximum population size.- Specified by:
getPopulationLimit
in interfacePopulation
- Returns:
- the maximum population size.
-
setPopulationLimit
public void setPopulationLimit(int populationLimit) throws NotPositiveException, NumberIsTooSmallException
Sets the maximal population size.- Parameters:
populationLimit
- maximal population size.- Throws:
NotPositiveException
- if the population limit is not a positive number (< 1)NumberIsTooSmallException
- if the new population size is smaller than the current number of chromosomes in the population
-
getPopulationSize
public int getPopulationSize()
Access the current population size.- Specified by:
getPopulationSize
in interfacePopulation
- Returns:
- the current population size.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
iterator
public java.util.Iterator<Chromosome> iterator()
Returns an iterator over the unmodifiable list of chromosomes.Any call to
Iterator.remove()
will result in aUnsupportedOperationException
.- Specified by:
iterator
in interfacejava.lang.Iterable<Chromosome>
- Returns:
- chromosome iterator
-
-