-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplex.py
136 lines (121 loc) · 4.52 KB
/
simplex.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class Simplex:
def __init__(self, x1, y1, z1, x2, y2, z2, x3, y3, z3, operation):
self.x1 = x1
self.y1 = y1
self.z1 = z1
self.x2 = x2
self.y2 = y2
self.z2 = z2
self.x3 = x3
self.y3 = y3
self.z3 = z3
self.operation = operation
def all_xyz(self):
return [self.x1, self.y1, self.z1, self.x2, self.y2, self.z2, self.x3, self.y3, self.z3]
def which_p_h(self):
if self.z1 >= self.z2 and self.z1 >= self.z3:
return 1
elif self.z2 >= self.z1 and self.z2 >= self.z3:
return 2
elif self.z3 >= self.z1 and self.z3 >= self.z2:
return 3
def p_h(self):
which_p_h = self.which_p_h()
if which_p_h == 1:
return self.x1, self.y1
elif which_p_h == 2:
return self.x2, self.y2
elif which_p_h == 3:
return self.x3, self.y3
def z_without_zh(self):
which_p_h = self.which_p_h()
if which_p_h == 1:
return self.z2, self.z3
elif which_p_h == 2:
return self.z1, self.z3
elif which_p_h == 3:
return self.z1, self.z2
def f_p_h(self):
which_p_h = self.which_p_h()
if which_p_h == 1:
return self.z1
elif which_p_h == 2:
return self.z2
elif which_p_h == 3:
return self.z3
def which_p_l(self):
if self.z1 <= self.z2 and self.z1 <= self.z3:
return 1
elif self.z2 <= self.z1 and self.z2 <= self.z3:
return 2
elif self.z3 <= self.z1 and self.z3 <= self.z2:
return 3
def p_l(self):
which_p_l = self.which_p_l()
if which_p_l == 1:
return self.x1, self.y1
elif which_p_l == 2:
return self.x2, self.y2
elif which_p_l == 3:
return self.x3, self.y3
def f_p_l(self):
which_p_l = self.which_p_l()
if which_p_l == 1:
return self.z1
elif which_p_l == 2:
return self.z2
elif which_p_l == 3:
return self.z3
def p_cen(self):
if self.z1 >= self.z2 and self.z1 >= self.z3:
return (self.x2 + self.x3) / 2, (self.y2 + self.y3) / 2
elif self.z2 >= self.z1 and self.z2 >= self.z3:
return (self.x1 + self.x3) / 2, (self.y1 + self.y3) / 2
elif self.z3 >= self.z1 and self.z3 >= self.z2:
return (self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2
def draw(self, surface):
if self.operation == "start":
surface.draw_simplex(self.all_xyz() + ["y"])
elif self.operation == "reflection":
surface.draw_simplex(self.all_xyz() + ["r"])
elif self.operation == "contraction":
surface.draw_simplex(self.all_xyz() + ["black"])
elif self.operation == "expansion":
surface.draw_simplex(self.all_xyz() + ["black"])
def reflection_point(self, alpha):
x_h, y_h = self.p_h()
cen_x, cen_y = self.p_cen()
new_x = (1 + alpha) * cen_x - alpha * x_h
new_y = (1 + alpha) * cen_y - alpha * y_h
return new_x, new_y
def expansion_point(self, gamma, alpha):
ref_x, ref_y = self.reflection_point(alpha)
cen_x, cen_y = self.p_cen()
new_x = (1 - gamma) * ref_x - gamma * cen_x
new_y = (1 - gamma) * ref_y - gamma * cen_y
return new_x, new_y
def contraction_point(self, beta):
x_h, y_h = self.p_h()
cen_x, cen_y = self.p_cen()
new_x = beta * x_h + (1 - beta) * cen_x
new_y = beta * y_h + (1 - beta) * cen_y
return new_x, new_y
def new_point_instead_p_h(self, new_x, new_y, new_z, action):
which_p_h = self.which_p_h()
if which_p_h == 1:
return Simplex(new_x, new_y, new_z, self.x2, self.y2, self.z2, self.x3, self.y3, self.z3, action)
elif which_p_h == 2:
return Simplex(self.x1, self.y1, self.z1, new_x, new_y, new_z, self.x3, self.y3, self.z3, action)
elif which_p_h == 3:
return Simplex(self.x1, self.y1, self.z1, self.x2, self.y2, self.z2, new_x, new_y, new_z, action)
def reduction(self, f):
x_l, y_l = self.p_l()
self.x1 = (self.x1 + x_l) / 2
self.x2 = (self.x2 + x_l) / 2
self.x3 = (self.x3 + x_l) / 2
self.y1 = (self.y1 + x_l) / 2
self.y2 = (self.y2 + x_l) / 2
self.y3 = (self.y3 + x_l) / 2
self.z1 = f(self.x1, self.y1)
self.z2 = f(self.x2, self.y2)
self.z3 = f(self.x3, self.y3)