@File : crossover.py
@Time : 2023/11/03 10:33:32
@Author : Alejandro Marrero
@Version : 1.0
@Contact : amarrerd@ull.edu.es
@License : (C)Copyright 2023, Alejandro Marrero
@Desc : None
one_point_crossover(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
|
| Raises: |
-
ValueError
–
When the len(ind_1) != len(ind_2)
|
| Returns: |
-
IndType
–
Instance or Solution: New individual
|
Source code in digneapy/operators/_crossover.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 | def one_point_crossover(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
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 = np.random.default_rng().integers(low=0, high=len(individual))
offspring[cross_point:] = other[cross_point:]
return offspring
|
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, default:
float64(0.5)
)
–
Crossover probability. Defaults to 0.5.
|
| Raises: |
-
ValueError
–
When the len(ind_1) != len(ind_2)
|
Source code in digneapy/operators/_crossover.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 | def uniform_crossover(
individual: IndType, other: IndType, cxpb: np.float64 = np.float64(0.5)
) -> 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.
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)
probs = np.random.default_rng().random(size=len(individual))
genotype = np.empty_like(individual)
genotype = np.where(probs <= cxpb, individual, other)
return individual.clone_with(variables=genotype)
|