@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(ind, other)
One point crossover
Parameters: |
-
ind
(Instance or Solution )
–
First individual to apply crossover. Returned object
-
ind_2
(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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 | def one_point_crossover(ind: IndType, other: IndType) -> IndType:
"""One point crossover
Args:
ind Instance or Solution: First individual to apply crossover. Returned object
ind_2 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(ind) != len(other):
msg = f"Individual of different length in uniform_crossover. len(ind) = {len(ind)} != len(other) = {len(other)}"
raise ValueError(msg)
offspring = ind.clone()
cross_point = np.random.default_rng().integers(low=0, high=len(ind))
offspring[cross_point:] = other[cross_point:]
return offspring
|
Uniform Crossover Operator for Instances and Solutions
Parameters: |
-
ind
(Instance or Solution )
–
First individual to apply crossover. Returned object.
-
ind_2
(Instance or Solution )
–
Second individual to apply crossover
-
cxpb
(float , default:
0.5
)
–
description. Defaults to 0.5.
|
Raises: |
-
ValueError
–
When the len(ind_1) != len(ind_2)
|
Returns: |
-
IndType
–
Instance or Solution: New individual
|
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 | def uniform_crossover(ind: IndType, other: IndType, cxpb: float = 0.5) -> IndType:
"""Uniform Crossover Operator for Instances and Solutions
Args:
ind Instance or Solution: First individual to apply crossover. Returned object.
ind_2 Instance or Solution: Second individual to apply crossover
cxpb (float, optional): _description_. Defaults to 0.5.
Raises:
ValueError: When the len(ind_1) != len(ind_2)
Returns:
Instance or Solution: New individual
"""
if len(ind) != len(other):
msg = f"Individual of different length in uniform_crossover. len(ind) = {len(ind)} != len(other) = {len(other)}"
raise ValueError(msg)
probs = np.random.default_rng().random(size=len(ind))
chromosome = [i if pb <= cxpb else j for pb, i, j in zip(probs, ind, other)]
offspring = ind.clone()
offspring[:] = chromosome
return offspring
|