Class Population<T extends Comparable<T>>

java.lang.Object
net.bmahe.genetics4j.core.Population<T>
Type Parameters:
T - the type of the fitness values, must be comparable for selection operations
All Implemented Interfaces:
Iterable<Individual<T>>

public class Population<T extends Comparable<T>> extends Object implements Iterable<Individual<T>>
Represents a population of individuals in an evolutionary algorithm.

A population is a collection of Individuals, each consisting of a genotype and its associated fitness. This class provides methods to manage, access, and iterate over the individuals in the population.

Populations are mutable and support adding individuals, either individually or in bulk from other populations. The class maintains parallel lists of genotypes and fitnesses for efficient access.

See Also:
  • Field Details

  • Constructor Details

    • Population

      public Population()
      Creates an empty population.
    • Population

      public Population(List<Genotype> _genotype, List<T> _fitnesses)
      Creates a population with the specified genotypes and fitnesses.
      Parameters:
      _genotype - the list of genotypes for the population
      _fitnesses - the list of fitness values corresponding to the genotypes
      Throws:
      IllegalArgumentException - if genotypes or fitnesses are null, or if their sizes don't match
  • Method Details

    • add

      public void add(Genotype genotype, T fitness)
      Adds an individual to the population by specifying its genotype and fitness separately.
      Parameters:
      genotype - the genotype of the individual to add
      fitness - the fitness value of the individual to add
      Throws:
      IllegalArgumentException - if genotype or fitness is null
    • add

      public void add(Individual<T> individual)
      Adds an individual to the population.
      Parameters:
      individual - the individual to add to the population
      Throws:
      IllegalArgumentException - if individual is null
    • addAll

      public void addAll(Population<T> population)
      Adds all individuals from another population to this population.
      Parameters:
      population - the population whose individuals should be added to this population
      Throws:
      IllegalArgumentException - if population is null
    • iterator

      public Iterator<Individual<T>> iterator()
      Specified by:
      iterator in interface Iterable<T extends Comparable<T>>
    • getGenotype

      public Genotype getGenotype(int index)
      Returns the genotype at the specified index.
      Parameters:
      index - the index of the genotype to retrieve (0-based)
      Returns:
      the genotype at the specified index
      Throws:
      IllegalArgumentException - if index is out of bounds
    • getFitness

      public T getFitness(int index)
      Returns the fitness value at the specified index.
      Parameters:
      index - the index of the fitness value to retrieve (0-based)
      Returns:
      the fitness value at the specified index
      Throws:
      IllegalArgumentException - if index is out of bounds
    • getIndividual

      public Individual<T> getIndividual(int index)
      Returns the individual at the specified index.
      Parameters:
      index - the index of the individual to retrieve (0-based)
      Returns:
      the individual at the specified index, combining its genotype and fitness
      Throws:
      IllegalArgumentException - if index is out of bounds
    • getAllGenotypes

      public List<Genotype> getAllGenotypes()
      Returns all genotypes in this population.
      Returns:
      a list containing all genotypes in this population
    • getAllFitnesses

      public List<T> getAllFitnesses()
      Returns all fitness values in this population.
      Returns:
      a list containing all fitness values in this population
    • size

      public int size()
      Returns the number of individuals in this population.
      Returns:
      the size of the population
    • isEmpty

      public boolean isEmpty()
      Checks if this population is empty.
      Returns:
      true if the population contains no individuals, false otherwise
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • of

      public static <U extends Comparable<U>> Population<U> of(List<Genotype> _genotype, List<U> _fitnesses)
      Creates a new population with the specified genotypes and fitnesses.
      Type Parameters:
      U - the type of the fitness values
      Parameters:
      _genotype - the list of genotypes for the population
      _fitnesses - the list of fitness values corresponding to the genotypes
      Returns:
      a new population containing the specified genotypes and fitnesses
      Throws:
      IllegalArgumentException - if genotypes or fitnesses are null, or if their sizes don't match
    • of

      public static <U extends Comparable<U>> Population<U> of(List<Individual<U>> individuals)
      Creates a new population from a list of individuals.
      Type Parameters:
      U - the type of the fitness values
      Parameters:
      individuals - the list of individuals to include in the population
      Returns:
      a new population containing the specified individuals
      Throws:
      IllegalArgumentException - if individuals list is null
    • empty

      public static <U extends Comparable<U>> Population<U> empty()
      Creates an empty population.
      Type Parameters:
      U - the type of the fitness values
      Returns:
      a new empty population