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

UniformCrossover

Bases: Crossover

Source code in digneapy/operators/crossover/uniform.py
22
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
50
51
52
53
class UniformCrossover(Crossover):
    def __init__(
        self, cxpb: float = 0.5, seed: Optional[int | np.random.SeedSequence] = None
    ):
        super().__init__(cxpb, seed)

    def __call__(self, individual: IndType, other: IndType) -> IndType:
        """Uniform Crossover Operator for Instances and Solutions

        Args:
            individual (IndType): First individual to apply crossover. Returned object.
            other (IndType): Second individual to apply crossover
            cxpb (float64, optional): Crossover probability. Defaults to 0.5.
            seed (Optional[int  |  np.random.SeedSequence], optional): Seed for the random number generator. Defaults to None.

        Raises:
            ValueError: When the len(ind_1) != len(ind_2)

        Returns:
            ndarray: New individual
        """

        if len(individual) != len(other):
            msg = f"Individual of different length in uniform_crossover. len(ind) = {len(individual)} != len(other) = {len(other)}"
            raise ValueError(msg)

        cloned = individual.clone()
        probs = self._rng.random(size=len(individual))
        genotype = np.empty_like(individual)
        genotype = np.where(probs <= self._cxpb, individual, other)
        cloned.variables = genotype
        return cloned

__call__(individual, other)

Uniform Crossover Operator for Instances and Solutions

Parameters:
  • individual (IndType) –

    First individual to apply crossover. Returned object.

  • other (IndType) –

    Second individual to apply crossover

  • cxpb (float64) –

    Crossover probability. Defaults to 0.5.

  • seed (Optional[int | SeedSequence]) –

    Seed for the random number generator. Defaults to None.

Raises:
  • ValueError

    When the len(ind_1) != len(ind_2)

Returns:
  • ndarray( IndType ) –

    New individual

Source code in digneapy/operators/crossover/uniform.py
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
def __call__(self, individual: IndType, other: IndType) -> IndType:
    """Uniform Crossover Operator for Instances and Solutions

    Args:
        individual (IndType): First individual to apply crossover. Returned object.
        other (IndType): Second individual to apply crossover
        cxpb (float64, optional): Crossover probability. Defaults to 0.5.
        seed (Optional[int  |  np.random.SeedSequence], optional): Seed for the random number generator. Defaults to None.

    Raises:
        ValueError: When the len(ind_1) != len(ind_2)

    Returns:
        ndarray: New individual
    """

    if len(individual) != len(other):
        msg = f"Individual of different length in uniform_crossover. len(ind) = {len(individual)} != len(other) = {len(other)}"
        raise ValueError(msg)

    cloned = individual.clone()
    probs = self._rng.random(size=len(individual))
    genotype = np.empty_like(individual)
    genotype = np.where(probs <= self._cxpb, individual, other)
    cloned.variables = genotype
    return cloned