@File : init.py
@Time : 2026/05/21 14:20:33
@Author : Alejandro Marrero (amarrerd@ull.edu.es)
@Version : 1.0
@Contact : amarrerd@ull.edu.es
@License : (C)Copyright 2026, Alejandro Marrero
@Desc : None
OnePointCrossover
Bases: Crossover
Source code in digneapy/operators/crossover/opoint.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 | class OnePointCrossover(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:
"""One point crossover
Args:
individual Instance or Solution: First individual to apply crossover. Returned object
other Instance or Solution: Second individual to apply crossover
cxpb (float64, optional): Crossover probability. Not used in this operator.
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:
Instance or Solution: 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)
offspring = individual.clone()
cross_point = self._rng.integers(low=0, high=len(individual))
offspring[cross_point:] = other[cross_point:]
return offspring
|
__call__(individual, other)
One point crossover
| Parameters: |
-
individual Instance or Solution
–
First individual to apply crossover. Returned object
-
other Instance or Solution
–
Second individual to apply crossover
-
cxpb
(float64)
–
Crossover probability. Not used in this operator.
-
seed
(Optional[int | SeedSequence])
–
Seed for the random number generator. Defaults to None.
|
| Raises: |
-
ValueError
–
When the len(ind_1) != len(ind_2)
|
| Returns: |
-
IndType
–
Instance or Solution: New individual
|
Source code in digneapy/operators/crossover/opoint.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 | def __call__(self, individual: IndType, other: IndType) -> IndType:
"""One point crossover
Args:
individual Instance or Solution: First individual to apply crossover. Returned object
other Instance or Solution: Second individual to apply crossover
cxpb (float64, optional): Crossover probability. Not used in this operator.
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:
Instance or Solution: 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)
offspring = individual.clone()
cross_point = self._rng.integers(low=0, high=len(individual))
offspring[cross_point:] = other[cross_point:]
return offspring
|
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
|
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)
|
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
|