-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgaussian_mutator.py
More file actions
98 lines (73 loc) · 3.32 KB
/
gaussian_mutator.py
File metadata and controls
98 lines (73 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
87
88
89
90
91
92
93
94
95
96
97
98
from pygenalgo.utils.utilities import clamp
from pygenalgo.genome.chromosome import Chromosome
from pygenalgo.operators.mutation.mutate_operator import MutationOperator
class GaussianMutator(MutationOperator):
"""
Description:
Gaussian mutator, mutates the chromosome by selecting randomly a position
and perturbing it with a Gaussian random value to the current gene value.
"""
def __init__(self, mutate_probability: float = 0.1, sigma: float = 1.0,
lower_val: float = None, upper_val: float = None) -> None:
"""
Construct a 'GaussianMutator' object with a given probability value.
:param mutate_probability: (float).
:param sigma: (float) standard deviation of the Gaussian N(0, sigma).
:param lower_val: (float) lower limit value for the gene mutation.
:param upper_val: (float) upper limit value for the gene mutation.
"""
# Call the super constructor with the provided
# probability value.
super().__init__(mutate_probability)
# Ensure sigma parameter is float.
sigma = float(sigma)
# Ensure standard deviation is positive.
if sigma <= 0.0:
raise ValueError(f"{self.__class__.__name__}: "
f"Standard deviation must be positive.")
# _end_if_
# Ensure that both lower and upper limits are provided.
if lower_val is None or upper_val is None:
raise ValueError(f"{self.__class__.__name__}: "
f"Lower or Upper limits are missing.")
# _end_if_
# Ensure lower_val parameter is float.
lower_val = float(lower_val)
# Ensure upper_val parameter is float.
upper_val = float(upper_val)
# Ensure the order is correct.
if upper_val <= lower_val:
raise ValueError(f"{self.__class__.__name__}: "
f"The limit values are incorrect.")
# _end_if_
# Assign variables to the _items placeholder.
self._items = [sigma, lower_val, upper_val]
# _end_def_
def mutate(self, individual: Chromosome) -> None:
"""
Perform the mutation operation by randomly adding the
Gaussian value to a randomly selected gene position.
:param individual: (Chromosome).
:return: None.
"""
# If the mutation probability is higher than
# a uniformly random value, make the changes.
if self.is_operator_applicable():
# Get the size of the chromosome.
n_genes: int = len(individual)
# Extract the values from the placeholder variable.
sigma, xl, xu = self._items
# Select a random position in the genome.
i = self.rng.integers(n_genes, dtype=int)
# Get the old value of the Gene.
old_value = individual[i].value
# Calculate the new Gene value by sampling from N(value, sigma),
# ensuring it stays within limits.
individual[i].value = clamp(self.rng.normal(loc=old_value, scale=sigma),
xl, xu)
# Set the fitness to NaN.
individual.invalidate_fitness()
# Increase the mutator counter.
self.inc_counter()
# _end_def_
# _end_class_