@File : uniform.py
@Time : 2026/05/21 14:52:09
@Author : Alejandro Marrero (amarrerd@ull.edu.es)
@Version : 1.0
@Contact : amarrerd@ull.edu.es
@License : (C)Copyright 2026, Alejandro Marrero
@Desc : None
Bases: Mutation
Source code in digneapy/operators/mutation/uniform.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 | class BatchUniformMutation(Mutation):
def __init__(self, seed: Optional[int | np.random.SeedSequence] = None):
super().__init__(seed)
def __call__(
self, 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
ub (np.ndarray): Upper bound for each dimension
seed (Optional[int | np.random.SeedSequence], optional): Seed for the random number generator. Defaults to None.
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"len of individuals ({dimension}) and bounds {len(lb)} differs in uniform_one_mutation"
raise ValueError(msg)
mutation_points = self._rng.integers(low=0, high=dimension, size=n_individuals)
new_values = self._rng.uniform(
low=lb[mutation_points], high=ub[mutation_points], size=n_individuals
)
population[np.arange(n_individuals), mutation_points] = new_values
return population
|
Performs uniform one mutation in batches
| Parameters: |
-
population
(ndarray)
–
Batch of individuals to mutate
-
lb
(ndarray)
–
Lower bound for each dimension
-
ub
(ndarray)
–
Upper bound for each dimension
-
seed
(Optional[int | SeedSequence])
–
Seed for the random number generator. Defaults to None.
|
| 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/uniform.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 | def __call__(
self, 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
ub (np.ndarray): Upper bound for each dimension
seed (Optional[int | np.random.SeedSequence], optional): Seed for the random number generator. Defaults to None.
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"len of individuals ({dimension}) and bounds {len(lb)} differs in uniform_one_mutation"
raise ValueError(msg)
mutation_points = self._rng.integers(low=0, high=dimension, size=n_individuals)
new_values = self._rng.uniform(
low=lb[mutation_points], high=ub[mutation_points], size=n_individuals
)
population[np.arange(n_individuals), mutation_points] = new_values
return population
|
Bases: Mutation
Source code in digneapy/operators/mutation/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 | class UniformMutation(Mutation):
def __init__(self, seed: Optional[int | np.random.SeedSequence] = None):
super().__init__(seed)
def __call__(self, individual: IndType, lb: np.ndarray, ub: np.ndarray) -> IndType:
"""Performs Uniform One Mutation on Instances and Solution objects.
Args:
individual (IndType): Instance or Solution to mutate.
lb (np.ndarray): Lower bound for each dimension
ub (np.ndarray): Upper bound for each dimension
seed (Optional[int | np.random.SeedSequence], optional): Seed for the random number generator. Defaults to None.
Raises:
ValueError: If bouns != dimension
Returns:
IndType: Newly mutated individual
"""
if len(lb) != len(ub) or len(individual) != len(lb):
msg = f"The size of individual ({len(individual)}) and bounds {len(lb)} is different in uniform_one_mutation."
raise ValueError(msg)
mutation_point = self._rng.integers(low=0, high=len(individual))
new_value = self._rng.uniform(low=lb[mutation_point], high=ub[mutation_point])
individual[mutation_point] = new_value
return individual
|
Performs Uniform One Mutation on Instances and Solution objects.
| Parameters: |
-
individual
(IndType)
–
Instance or Solution to mutate.
-
lb
(ndarray)
–
Lower bound for each dimension
-
ub
(ndarray)
–
Upper bound for each dimension
-
seed
(Optional[int | SeedSequence])
–
Seed for the random number generator. Defaults to None.
|
Source code in digneapy/operators/mutation/uniform.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 | def __call__(self, individual: IndType, lb: np.ndarray, ub: np.ndarray) -> IndType:
"""Performs Uniform One Mutation on Instances and Solution objects.
Args:
individual (IndType): Instance or Solution to mutate.
lb (np.ndarray): Lower bound for each dimension
ub (np.ndarray): Upper bound for each dimension
seed (Optional[int | np.random.SeedSequence], optional): Seed for the random number generator. Defaults to None.
Raises:
ValueError: If bouns != dimension
Returns:
IndType: Newly mutated individual
"""
if len(lb) != len(ub) or len(individual) != len(lb):
msg = f"The size of individual ({len(individual)}) and bounds {len(lb)} is different in uniform_one_mutation."
raise ValueError(msg)
mutation_point = self._rng.integers(low=0, high=len(individual))
new_value = self._rng.uniform(low=lb[mutation_point], high=ub[mutation_point])
individual[mutation_point] = new_value
return individual
|