-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolyExpression.py
85 lines (68 loc) · 2.69 KB
/
PolyExpression.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
from random import uniform, randint
from PolyTerm import PolyTerm
from copy import deepcopy
class PolyExpression:
def __init__(self, min_length=1, max_length=6, max_exponent=6.0, max_coefficient=9.0, mutation_rate=10,
max_mutation_size=5.0):
self.min_length = min_length
self.max_length = max_length
self.max_exponent = max_exponent
self.max_coefficient = max_coefficient
self.mutation_rate = mutation_rate
self.max_mutation_size = max_mutation_size
self.terms = []
self.length = randint(min_length, max_length)
self.chi_square = 0
for i in range(self.length):
self.terms.append(deepcopy(self.create_term()))
def mutate(self):
roll = randint(0, 100)
if roll <= self.mutation_rate:
size_roll = randint(0, 1)
size_change = False
if size_roll and self.length < self.max_length:
self.length += 1
self.terms.append(deepcopy(self.create_term()))
size_change = True
size_roll = randint(0, 1)
if size_roll and self.length > self.min_length and not size_change:
index = randint(0, self.length - 1)
self.terms.remove(self.terms[index])
self.length -= 1
for i in range(self.length):
exp_roll = randint(0, 1)
coeff_roll = randint(0, 1)
sign_roll = randint(0, 1)
if exp_roll:
self.terms[i].exponent_change(uniform(-self.max_mutation_size, self.max_mutation_size))
if coeff_roll:
self.terms[i].coefficient_change(uniform(-self.max_mutation_size, self.max_mutation_size))
if sign_roll:
self.terms[i].flip_sign()
return True
else:
return False
def create_term(self):
coefficient = uniform(0, self.max_coefficient)
exponent = uniform(0, self.max_exponent)
sign = randint(0, 1)
return PolyTerm(coefficient, exponent, sign)
def get_equation(self, x):
equation = ""
for i in range(self.length):
if equation != "":
equation += "+"
equation += self.terms[i].build_term(x)
return equation
def evaluate(self, x):
value = 0
for i in range(self.length):
value += self.terms[i].get_value(x)
return value
def set_chi_squared(self, value):
self.chi_square = value
def get_chi_squared(self):
return self.chi_square
def set_terms(self, terms):
self.terms = terms
self.length = len(terms)