Package net.bmahe.genetics4j.core
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>>
Represents a population of individuals in an evolutionary algorithm.
A population is a collection of Individual
s, 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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty population.Population
(List<Genotype> _genotype, List<T> _fitnesses) Creates a population with the specified genotypes and fitnesses. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds an individual to the population by specifying its genotype and fitness separately.void
add
(Individual<T> individual) Adds an individual to the population.void
addAll
(Population<T> population) Adds all individuals from another population to this population.static <U extends Comparable<U>>
Population<U> empty()
Creates an empty population.boolean
Returns all fitness values in this population.Returns all genotypes in this population.getFitness
(int index) Returns the fitness value at the specified index.getGenotype
(int index) Returns the genotype at the specified index.getIndividual
(int index) Returns the individual at the specified index.int
hashCode()
boolean
isEmpty()
Checks if this population is empty.iterator()
static <U extends Comparable<U>>
Population<U> Creates a new population with the specified genotypes and fitnesses.static <U extends Comparable<U>>
Population<U> of
(List<Individual<U>> individuals) Creates a new population from a list of individuals.int
size()
Returns the number of individuals in this population.toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
genotypes
-
fitnesses
-
-
Constructor Details
-
Population
public Population()Creates an empty population. -
Population
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
Adds an individual to the population by specifying its genotype and fitness separately.- Parameters:
genotype
- the genotype of the individual to addfitness
- the fitness value of the individual to add- Throws:
IllegalArgumentException
- if genotype or fitness is null
-
add
Adds an individual to the population.- Parameters:
individual
- the individual to add to the population- Throws:
IllegalArgumentException
- if individual is null
-
addAll
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
- Specified by:
iterator
in interfaceIterable<T extends Comparable<T>>
-
getGenotype
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
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
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
Returns all genotypes in this population.- Returns:
- a list containing all genotypes in this population
-
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() -
equals
-
toString
-
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
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
Creates an empty population.- Type Parameters:
U
- the type of the fitness values- Returns:
- a new empty population
-