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

BinarySelection

Bases: Selection

Source code in digneapy/operators/selection/binary.py
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
50
51
52
53
class BinarySelection(Selection):
    def __init__(
        self, attr: str = "fitness", seed: Optional[int | np.random.SeedSequence] = None
    ):
        super().__init__(seed)
        self._attr = attr

    def __call__(
        self,
        population: Sequence[IndType] | np.ndarray,
    ) -> IndType:
        """Binary Tournament Selection Operator

        Args:
            population (Sequence): Population of individuals to select a parent from

        Raises:
            RuntimeError: If the population is empty

        Returns:
            Instance or Solution: New parent
        """
        if not population:
            msg = "Trying to selection individuals in an empty population."
            raise ValueError(msg)
        elif len(population) == 1:
            return population[0]
        else:
            idx1, idx2 = self._rng.integers(low=0, high=len(population), size=2)
            return max(population[idx1], population[idx2], key=attrgetter(self._attr))

__call__(population)

Binary Tournament Selection Operator

Parameters:
  • population (Sequence) –

    Population of individuals to select a parent from

Raises:
  • RuntimeError

    If the population is empty

Returns:
  • IndType

    Instance or Solution: New parent

Source code in digneapy/operators/selection/binary.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __call__(
    self,
    population: Sequence[IndType] | np.ndarray,
) -> IndType:
    """Binary Tournament Selection Operator

    Args:
        population (Sequence): Population of individuals to select a parent from

    Raises:
        RuntimeError: If the population is empty

    Returns:
        Instance or Solution: New parent
    """
    if not population:
        msg = "Trying to selection individuals in an empty population."
        raise ValueError(msg)
    elif len(population) == 1:
        return population[0]
    else:
        idx1, idx2 = self._rng.integers(low=0, high=len(population), size=2)
        return max(population[idx1], population[idx2], key=attrgetter(self._attr))