-
Notifications
You must be signed in to change notification settings - Fork 3
/
individual.py
97 lines (80 loc) · 2.98 KB
/
individual.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from parameters import *
from compare_parsed import compare_criterias
from random import randint, choice
from primitives import str_primitives, re_primitives, clean_primitives
from baseparser import BaseParser
from operator import attrgetter
class Individual(BaseParser):
expected_result = {}
mutation_rate = 0
max_str = MAX_STR
max_s2l = MAX_S2L
max_clean = MAX_CLEAN
def __init__(self, mother=None, father=None):
BaseParser.__init__(self)
if mother and father:
self.crossover(mother, father)
else:
self.fitness = 0
def constraints(self):
todel = []
for n, i in enumerate(zip(self._s2l_ops, self._s2l_ops[1:])):
if i[0][1] == 0 and i[1][1] == 0:
todel.append(n)
for n, i in enumerate(todel):
del self._s2l_ops[i - n]
def randomize(self):
for i in range(randint(0, self.max_str)):
self._str_ops.append(choice(str_primitives))
for i in range(randint(1, self.max_s2l)):
self._s2l_ops.append(choice(re_primitives))
for i in range(randint(0, self.max_clean)):
self._clean_ops.append(choice(clean_primitives))
self.constraints()
def crossover_list(self, l1, l2):
crossover_point = randint(0, min(len(l1), len(l2)))
return l1[crossover_point:] + l2[:crossover_point]
def apply_max(self):
self._str_ops = self._str_ops[: self.max_str]
self._s2l_ops = self._s2l_ops[: self.max_s2l]
self._clean_ops = self._clean_ops[: self.max_clean]
def crossover(self, mother, father):
self._str_ops = self.crossover_list(mother._str_ops, father._str_ops)
self._s2l_ops = self.crossover_list(mother._s2l_ops, father._s2l_ops)
self._clean_ops = self.crossover_list(mother._clean_ops, father._clean_ops)
self.constraints()
def mutate_list(self, l, all):
r = randint(0, 100)
if len(l) == 0:
i, r = 0, 0
else:
i = randint(0, len(l) - 1)
if r < 10: # add
l.insert(i, choice(all))
elif r > 90: # delete
del l[i]
else: # modify
l[i] = choice(all)
def mutate(self, force=False):
test = randint(0, 100)
if test < self.mutation_rate or force:
r = randint(0, 2)
if r == 0:
self.mutate_list(self._str_ops, str_primitives)
elif r == 1:
self.mutate_list(self._s2l_ops, re_primitives)
else:
self.mutate_list(self._clean_ops, clean_primitives)
self.constraints()
def evaluate(self):
self.run_all()
c = self.criterias()
self.fitness = compare_criterias(self, self.expected_result)
def __hash__(self):
return hash(self.st())
def __str__(self):
return str(
round(self.fitness, 5)
) # + ":" + self.st() # + ":" + str(self.parsed)
if __name__ == "__main__":
pass