-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic_operations.py
52 lines (45 loc) · 1.82 KB
/
genetic_operations.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
class GeneticOperations:
def __init__(self, evaluate, generators, individual_creator, target):
self.evaluate = evaluate
self.generators = generators
self.generators_len = len(generators)
self.individual_creator = individual_creator
self.target = target
def generate(self):
return self.__create_individual__(self.__generate_values__)
def mutate(self, values, index):
values_creator = lambda : self.__mutate_values__(values, index)
return self.__create_individual__(values_creator)
def is_in_range(self, value, index):
return self.generators[index].is_in_range(value)
def change(self, values, index, value):
new_values = values.copy()
new_values[index] = value
try:
result = self.evaluate(new_values)
return self.individual_creator(new_values, result, self.target)
except:
return self.mutate(values, index)
def __generate_values__(self):
values = []
for i in range(self.generators_len):
value = self.generators[i].generate()
values.append(value)
return values
def __mutate_values__(self, values, index):
new_values = values.copy()
new_values[index] = self.generators[index].generate()
return new_values
def __create_individual__(self, values_creator):
while True:
try:
values = values_creator()
result = self.evaluate(values)
return self.individual_creator(values, result, self.target)
except:
pass
#except Exception as inst:
# print(type(inst)) # the exception instance
# print(inst.args) # arguments stored in .args
# print(inst)
# raise inst