org.apache.commons.math3.genetics

## Class ListPopulation

- java.lang.Object
- - org.apache.commons.math3.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 a `List`.

Since:
  2.0
Version:
  $Id: ListPopulation.java 1422195 2012-12-15 06:45:18Z psteitz $

### Field Summary

Fields
`private List<Chromosome>` `chromosomes` List of chromosomes
`private int` `populationLimit` maximal size of the population

### Constructor Summary

Constructors
Constructor and 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

Methods
`void` `addChromosome(Chromosome chromosome)` Add the given chromosome to the population.
`void` `addChromosomes(Collection<Chromosome> chromosomeColl)` Add a `Collection` of chromosomes to this `Population`.
`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` `setChromosomes(List<Chromosome> chromosomes)` Deprecated. use `addChromosomes(Collection)` instead
`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 org.apache.commons.math3.genetics.Population

`nextGeneration`

### Field Detail

#### chromosomes

    private 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(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 population
  `populationLimit` - maximal size of the population
Throws:
  `NullArgumentException` - if the list of chromosomes is `null`
  `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(List<Chromosome> chromosomes)
                        throws NullArgumentException,
                               NumberIsTooLargeException

Deprecated. use `addChromosomes(Collection)` instead
Sets the list of chromosomes.

Note: this method removed 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 is `null`
  `NumberIsTooLargeException` - if the list of chromosomes exceeds the population limit

#### addChromosomes

    public void addChromosomes(Collection<Chromosome> chromosomeColl)
                        throws NumberIsTooLargeException

Add a `Collection` of chromosomes to this `Population`.

Parameters:
  `chromosomeColl` - a `Collection` 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 interface `Population`
Parameters:
  `chromosome` - the chromosome to add.
Throws:
  `NumberIsTooLargeException` - if the population would exceed the `populationLimit` after adding this chromosome

#### getFittestChromosome

    public Chromosome getFittestChromosome()

Access the fittest chromosome in this population.

Specified by:
  `getFittestChromosome` in interface `Population`
Returns:
  the fittest chromosome.

#### getPopulationLimit

    public int getPopulationLimit()

Access the maximum population size.

Specified by:
  `getPopulationLimit` in interface `Population`
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 interface `Population`
Returns:
  the current population size.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

#### iterator

    public Iterator<Chromosome> iterator()

Returns an iterator over the unmodifiable list of chromosomes.

Any call to `Iterator.remove()` will result in a `UnsupportedOperationException`.

Specified by:
  `iterator` in interface `Iterable<Chromosome>`
Returns:
  chromosome iterator