Class ListPopulation
- java.lang.Object
-
- org.apache.commons.math4.legacy.genetics.ListPopulation
-
- All Implemented Interfaces:
Iterable<Chromosome>
,Population
- Direct Known Subclasses:
ElitisticListPopulation
public abstract class ListPopulation extends Object implements Population
Population of chromosomes represented by aList
.- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description ListPopulation(int populationLimit)
Creates a new ListPopulation instance and initializes its inner chromosome list.ListPopulation(List<Chromosome> chromosomes, int populationLimit)
Creates a new ListPopulation instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addChromosome(Chromosome chromosome)
Add the given chromosome to the population.void
addChromosomes(Collection<Chromosome> chromosomeColl)
Add aCollection
of chromosomes to thisPopulation
.protected List<Chromosome>
getChromosomeList()
Access the list of chromosomes.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.Iterator<Chromosome>
iterator()
Returns an iterator over the unmodifiable list of chromosomes.void
setPopulationLimit(int populationLimit)
Sets the maximal population size.String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface org.apache.commons.math4.legacy.genetics.Population
nextGeneration
-
-
-
-
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(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
-
addChromosomes
public void addChromosomes(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 List<Chromosome> getChromosomes()
Returns an unmodifiable list of the chromosomes in this population.- Returns:
- the unmodifiable list of chromosomes
-
getChromosomeList
protected 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.
-
iterator
public 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 interfaceIterable<Chromosome>
- Returns:
- chromosome iterator
-
-