@File : mutation.py
@Time : 2023/11/03 10:33:30
@Author : Alejandro Marrero
@Version : 1.0
@Contact : amarrerd@ull.edu.es
@License : (C)Copyright 2023, Alejandro Marrero
@Desc : None
Performs uniform one mutation in batches
| Parameters: |
-
population
(ndarray)
–
Batch of individuals to mutate
-
lb
(ndarray)
–
Lower bound for each dimension
-
up
(ndarray)
–
Upper bound for each dimension
|
| Raises: |
-
ValueError
–
If the dimension of the individuals do not match the bounds
|
| Returns: |
-
ndarray
–
np.ndarray: mutated population
|
Source code in digneapy/operators/_mutation.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 | def batch_uniform_one_mutation(
population: np.ndarray, lb: np.ndarray, ub: np.ndarray
) -> np.ndarray:
"""Performs uniform one mutation in batches
Args:
population (np.ndarray): Batch of individuals to mutate
lb (np.ndarray): Lower bound for each dimension
up (np.ndarray): Upper bound for each dimension
Raises:
ValueError: If the dimension of the individuals do not match the bounds
Returns:
np.ndarray: mutated population
"""
dimension = len(population[0])
n_individuals = len(population)
if len(lb) != len(ub) or dimension != len(lb):
msg = f"The size of individuals ({dimension}) and bounds {len(lb)} is different in uniform_one_mutation"
raise ValueError(msg)
rng = np.random.default_rng(84793258734753)
mutation_points = rng.integers(low=0, high=dimension, size=n_individuals)
new_values = rng.uniform(
low=lb[mutation_points], high=ub[mutation_points], size=n_individuals
)
population[np.arange(n_individuals), mutation_points] = new_values
return population
|