@File : generational.py @Time : 2026/05/21 15:24:28 @Author : Alejandro Marrero (amarrerd@ull.edu.es) @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2026, Alejandro Marrero @Desc : None

Generational

Bases: Replacement

Source code in digneapy/operators/replacement/generational.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Generational(Replacement):
    def __init__(self, seed: Optional[int | np.random.SeedSequence] = None):
        super().__init__(seed)

    def __call__(
        self, population: Sequence[IndType], offspring: Sequence[IndType]
    ) -> Sequence[IndType]:
        """Returns the offspring population as the new current population

        Args:
            population (Sequence[IndType]): Current population in the algorithm
            offspring (Sequence[IndType]): Offspring population

        Raises:
            ValueError: Raises if the sizes of the population are different

        Returns:
            Sequence[IndType]: New population
        """
        if len(population) != len(offspring):
            msg = f"The size of the current population ({len(population)}) != size of the offspring ({len(offspring)}) in generational replacement"
            raise ValueError(msg)

        return offspring[:]

__call__(population, offspring)

Returns the offspring population as the new current population

Parameters:
  • population (Sequence[IndType]) –

    Current population in the algorithm

  • offspring (Sequence[IndType]) –

    Offspring population

Raises:
  • ValueError

    Raises if the sizes of the population are different

Returns:
  • Sequence[IndType]

    Sequence[IndType]: New population

Source code in digneapy/operators/replacement/generational.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def __call__(
    self, population: Sequence[IndType], offspring: Sequence[IndType]
) -> Sequence[IndType]:
    """Returns the offspring population as the new current population

    Args:
        population (Sequence[IndType]): Current population in the algorithm
        offspring (Sequence[IndType]): Offspring population

    Raises:
        ValueError: Raises if the sizes of the population are different

    Returns:
        Sequence[IndType]: New population
    """
    if len(population) != len(offspring):
        msg = f"The size of the current population ({len(population)}) != size of the offspring ({len(offspring)}) in generational replacement"
        raise ValueError(msg)

    return offspring[:]