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

GreedyReplacement

Bases: Replacement

Source code in digneapy/operators/replacement/greedy.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
47
48
49
class GreedyReplacement(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 a new population produced by a greedy operator.
        Each individual in the current population is compared with its analogous in the offspring population
        and the best survives

        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 first_improve_replacement"
            raise ValueError(msg)
        return [a if a > b else b for a, b in zip(population, offspring)]

__call__(population, offspring)

Returns a new population produced by a greedy operator. Each individual in the current population is compared with its analogous in the offspring population and the best survives

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/greedy.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __call__(
    self,
    population: Sequence[IndType],
    offspring: Sequence[IndType],
) -> Sequence[IndType]:
    """Returns a new population produced by a greedy operator.
    Each individual in the current population is compared with its analogous in the offspring population
    and the best survives

    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 first_improve_replacement"
        raise ValueError(msg)
    return [a if a > b else b for a, b in zip(population, offspring)]