-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussian_mutator.py
59 lines (42 loc) · 1.77 KB
/
gaussian_mutator.py
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
from numpy import nan as np_nan
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 add a Gaussian random value to the current gene value.
"""
def __init__(self, mutate_probability: float = 0.1, sigma: float = 1.0):
"""
Construct a 'GaussianMutator' object with a given probability value.
:param mutate_probability: (float).
:param sigma: (float) standard deviation of the Gaussian.
"""
# Call the super constructor with the provided
# probability value.
super().__init__(mutate_probability)
# Standard deviation (scale) of the Gaussian sample.
self._items = max(min(float(sigma), 1.0), 0.0)
# _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.probability > self.rng.random():
# Get the size of the chromosome.
M = len(individual)
# Select randomly the mutation point and update its value.
individual[self.rng.integers(M)].gaussian(sigma=self._items)
# Invalidate the fitness of the chromosome.
individual.fitness = np_nan
# Increase the mutator counter.
self.inc_counter()
# _end_if_
# _end_def_
# _end_class_