-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutate_operator.py
46 lines (35 loc) · 1.3 KB
/
mutate_operator.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
from pygenalgo.genome.chromosome import Chromosome
from pygenalgo.operators.genetic_operator import GeneticOperator
class MutationOperator(GeneticOperator):
"""
Description:
Provides the base class (interface) for a Mutation Operator.
"""
def __init__(self, mutation_probability: float):
"""
Construct a 'MutationOperator' object with a
given probability value.
:param mutation_probability: (float).
"""
# Call the super constructor with the provided
# probability value.
super().__init__(mutation_probability)
# _end_def_
def mutate(self, individual: Chromosome) -> None:
"""
Abstract method that "reminds" the user that if they want to
create a Mutation Class that inherits from here they should
implement a mutate method.
:param individual: the chromosome to be mutated.
:return: Nothing but raising an error.
"""
raise NotImplementedError(f"{self.__class__.__name__}: "
f"You should implement this method!")
# _end_def_
def __call__(self, *args, **kwargs):
"""
This is only a wrapper of the "mutate" method.
"""
return self.mutate(*args, **kwargs)
# _end_def_
# _end_class_