-
Notifications
You must be signed in to change notification settings - Fork 3
/
base.py
81 lines (67 loc) · 2.33 KB
/
base.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import abc
"""
Base class for all classes of generic types with
abstract mutation, crossover, and generate methods.
"""
class BaseData(abc.ABC):
"""
The method 'crossover' for a generic class T takes another instance of T
as an argument and returns *new instance of T*
NOTE: the instance (self) and the argument (other) are NOT modified
"""
@abc.abstractmethod
def crossover(self, other):
raise NotImplementedError()
"""
The method 'mutation' for a generic class T returns *new instance of T*
which was mutated by some pre-defined rules.
NOTE: original instance is NOT modified.
"""
@abc.abstractmethod
def mutation(self):
raise NotImplementedError()
"""
The method 'generate' returns the value of the generic class T. So for example
if T is of class 'generic.Int' then some integer value is returned. Usually the
generate method has the following structure
if not self._value:
... generate the value with the given constraints ...
return self._value
"""
@abc.abstractmethod
def generate(self):
raise NotImplementedError()
"""
Base class for classes under test with
implemented mutation, crossover, and generate methods.
"""
class BaseInput(abc.ABC):
def crossover(self, other):
"""
Returns the new instance of the class, where
new instance has crossovered fields.
"""
instance = self.__class__()
for k, v in self.__dict__.items():
if issubclass(v, BaseInput) or issubclass(v, BaseData):
setattr(instance, k, v.crossover(getattr(other, k)))
return instance
def mutation(self):
"""
Returns the new modified instance of the class whose
fields were mutated.
"""
instance = self.__class__()
for k, v in self.__dict__.items():
if issubclass(v, BaseInput) or issubclass(v, BaseData):
setattr(instance, k, v.mutation())
return instance
def generate(self):
"""
Returns the instance with generated value fields.
"""
instance = self.__class__()
for k, v in self.__dict__.items():
if issubclass(v, BaseInput) or issubclass(v, BaseData):
setattr(instance, k, v.generate())
return instance