/
gen_alg_module.py
87 lines (68 loc) · 2.59 KB
/
gen_alg_module.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
import random
import math
import time
import pandas as pd
def calc_scores(expressions, target):
"""
Calculates the fitness score based on it's proximity to the
target
NOTE: it also signals to end the program if it equals the target value
"""
scores = []
values = []
for expression in expressions:
value = eval(expression)
if round(value) == target:
return True, expression
else:
scores.append(1 / abs(target - value))
values.append(value)
return scores, values
def mutate(expression, double rate_cap, double rate_constant, double value, double max_exponent, operators, int target, out_data):
"""
Creates a list with len(list) <= len(expression) + 1 where a
different character has been randomized each time from the
original expression. There is a rate percent chance of any
one character to be changed
"""
cdef double rate
mutation = [expression]
mutated = False
alt_exp = expression
for i in range(len(expression)):
try:
# prevent OverflowError by
exponent = -(rate_constant / target) * (value)
if exponent > max_exponent:
exponent = max_exponent
elif exponent < -max_exponent:
exponent = -max_exponent
# Calculate the actual mutation rate based on the expression's proximity
# r = c - (c / (1 + c*e^(-(k/t)*x)))
# math.exp(x) is e**x
rate = rate_cap - (rate_cap / (1 + rate_cap *
math.exp(exponent)))
except OverflowError:
print("rate_cap:", rate_cap, "\nrate_constant:",
rate_constant, "\ntarget:", target, "\nvalue:", value)
raise OverflowError("math range error at gen_alg_module.pyx:50")
# decide whether to mutate or not
if random.random() <= rate:
mutated = True
# mutate
if i % 2 == 0:
alt_exp = (alt_exp[:i] +
str(random.randint(1, 9)) +
alt_exp[i + 1:])
else:
alt_exp = (alt_exp[:i] +
random.choice(operators) +
alt_exp[i + 1:])
if round(eval(alt_exp)) == target:
return [alt_exp]
if mutated:
mutation = [alt_exp]
mutation.append(expression + ' = ' +
str(round(eval(expression))) + ' -> ' +
alt_exp + ' = ' + str(round(eval(alt_exp))))
return mutation