-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask7optimize.py
More file actions
321 lines (240 loc) · 10.9 KB
/
Copy pathtask7optimize.py
File metadata and controls
321 lines (240 loc) · 10.9 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import numpy as np
import numpy.linalg as ln
import numdifftools as nd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from scipy.optimize import minimize
from PIL import Image, ImageTk
from matplotlib import cm
calls = 0
class NotEnteredValueError(Exception):
def __init__(self, value_name):
self.value_name = value_name
def golden_section_search(f, a, b, tol=1e-5):
gr = (np.sqrt(5) + 1) / 2 - 1
c = b - gr * (b - a)
d = a + gr * (b - a)
while abs(c - d) > tol:
if f(c) < f(d):
b = d
else:
a = c
c = b - gr * (b - a)
d = a + gr * (b - a)
return (b + a) / 2
def bfgs_method(x0, epsilon, K, f):
global calls
x = x0
xk = [x]
k = 0
N = len(x0)
I = np.eye(N, dtype=int)
Hk = I
f_x = None
pk = None
#################
calls += 2
gradient = nd.Gradient(f)(x)
while ln.norm(gradient) > epsilon and k < K:
print('gradient', gradient, ln.norm(gradient))
print('x', x)
f_x = f(x)
pk = -np.dot(Hk, gradient)
objective_function = lambda alpha: f(x + alpha * pk)
alpha_k = golden_section_search(objective_function, 0, 1, tol=1e-6)
xkp1 = x + alpha_k * pk
sk = xkp1 - x
x = xkp1
calls += 2
fderxp1 = nd.Gradient(f)(xkp1)
yk = fderxp1 - gradient
gradient = fderxp1
ro = 1.0 / (np.dot(yk, sk))
A1 = I - ro * sk[:, np.newaxis] * yk[np.newaxis, :]
A2 = I - ro * yk[:, np.newaxis] * sk[np.newaxis, :]
Hk = np.dot(A1, np.dot(Hk, A2)) + (ro * sk[:, np.newaxis] * sk[np.newaxis, :])
xk.append(x)
k += 1
if f_x is not None:
return x, f_x, np.linalg.norm(pk), k, xk
else: return (x, )
def newton_method(epsilon, K, x0, f):
global calls
x = x0
xk = [x]
k = 0
while k < K:
calls += 2
gradient = nd.Gradient(f)(x)
calls += 3
hessian = np.array([[2, 0], [0, 2]])
p = -np.linalg.solve(hessian, gradient)
if np.linalg.norm(p) < epsilon:
break
objective_function = lambda alpha: f(x + alpha * p)
alpha_k = golden_section_search(objective_function, 0, 1, tol=1e-6)
x = x + alpha_k * p
xk.append(x)
k += 1
return x, f(x), np.linalg.norm(p), k, xk
def steepest_descent(f, x0, epsilon, K):
global calls
x = x0
xk = [x]
k = 0
while True:
calls += 2
gradient = -nd.Gradient(f)(x)
if np.linalg.norm(gradient) < epsilon or k >= K:
break
alpha = golden_section_search(lambda a: f(x + a * gradient), 0, 1, tol=1e-6)
x = x + alpha * gradient
xk.append(x)
k += 1
return x, f(x), np.linalg.norm(gradient), k, xk
def penalty_function(x, c, f, g, p):
global calls
calls += 1
penalty = f(x) + c / 2 * sum([max(0, gi(x))**p for gi in g])
return penalty
def constrained_optimization(f, g, x0, c0, p, epsilon, optim_method, K):
x = x0
c = c0
k = 0
k_penalty = 0
prev = float('inf')
xk = []
while True:
print('c =', c)
penalty_func = lambda x: penalty_function(x, c, f, g, p)
result = optim_method(x0=x, epsilon=epsilon, K=K, f=penalty_func)
x = result[0]
if len(result) == 5:
k += result[3]
xk += result[4]
if (all([gi(x) <= 0 for gi in g]) and (abs(prev - penalty_func(x))) < epsilon) or (k >= K) :
break
c *= 10
prev = penalty_func(x)
k_penalty += 1
return x, xk, f(x), penalty_func(x), k, k_penalty
class Optimization:
def __init__(self, master):
self.master = master
master.title("Task 7 desktop app Anastasiia Vynnychuk")
# Variables
self.function_str= tk.StringVar(master, value='(x[0] - 2 )**2 + (x[1] - 4)**2')
self.initial_point = tk.StringVar(master, value='[100, 100]')
self.precision = tk.StringVar(master, value='1e-6')
self.max_iterations = tk.IntVar(master, value=1000)
self.method_var = tk.StringVar(master)
self.result_label = tk.StringVar(master, value='')
self.figure = plt.figure(figsize=(7, 7))
self.ax = self.figure.add_subplot(111, projection='3d')
self.canvas = FigureCanvasTkAgg(self.figure, master=root)
self.canvas_widget = self.canvas.get_tk_widget()
self.constraint1_str = tk.StringVar(master, value='2*x[0] + x[1] - 8')
self.constraint2_str = tk.StringVar(master, value='2*x[0] + 5*x[1] - 30')
# GUI Elements
self.create_widgets()
def create_widgets(self):
ttk.Label(self.master, text="Функція:").grid(row=1, column=0, padx=10, pady=5)
ttk.Entry(self.master, textvariable=self.function_str, width=30).grid(row=1, column=1, padx=10, pady=5)
ttk.Label(self.master, text="Початкова точка:").grid(row=2, column=0, padx=10, pady=5)
ttk.Entry(self.master, textvariable=self.initial_point, width=30).grid(row=2, column=1, padx=10, pady=5)
ttk.Label(self.master, text="Точність:").grid(row=3, column=0, padx=10, pady=5)
ttk.Entry(self.master, textvariable=self.precision, width=30).grid(row=3, column=1, padx=10, pady=5)
ttk.Label(self.master, text="Максимальна кількість ітерацій:").grid(row=4, column=0, padx=10, pady=5)
ttk.Entry(self.master, textvariable=self.max_iterations, width=30).grid(row=4, column=1, padx=10, pady=5)
ttk.Label(self.master, text="Метод оптимізації:").grid(row=5, column=0, padx=10, pady=5)
ttk.OptionMenu(self.master, self.method_var, "Обрати", "Градієнтний", "Ньютонівський", "Квазі-Ньютонівський").grid(row=5, column=1, padx=10, pady=5)
ttk.Label(self.master, text="Обмеження:").grid(row=6, column=0, pady=10, padx=5)
ttk.Entry(self.master, textvariable=self.constraint1_str, width=40).grid(row=6, column=1, columnspan=2, pady=5, padx=5, sticky="ew")
ttk.Entry(self.master, textvariable=self.constraint2_str, width=40).grid(row=7, column=1, columnspan=2, pady=5, padx=5, sticky="ew")
ttk.Button(self.master, text="Запустити оптимізацію", command=self.run_optimization).grid(row=8, columnspan=2, column=0, pady=10)
ttk.Label(self.master, text="Результати:").grid(row=9, column=0, columnspan=2, pady=5)
ttk.Label(self.master, textvariable=self.result_label).grid(row=10, column=0, columnspan=2, pady=5)
self.canvas_widget.grid(row=0, column=3, rowspan=10, pady=5)
ttk.Button(self.master, text='Про автора', command=self.show_about).grid(row=1, column=3)
def show_about(self):
avatar_path = "C:\\Users\\Admin\\Optimization\\photo_2023-11-28_20-50-01.jpg"
newWindow = tk.Toplevel(self.master)
# sets the title of the
newWindow.title("About author")
# A Label widget to show in toplevel
ttk.Label(newWindow, text ="Author: Anastasiia Vynnychuk\nstudent 4-th grade of Applied Mathematics and Informatics\nLviv national university of Ivan Franko").pack()
i = Image.open(avatar_path).resize((200, 250))
img = ImageTk.PhotoImage(i)
canvas = tk.Canvas(newWindow, width=i.width, height=i.height, borderwidth=0,highlightthickness=0)
canvas.pack()
canvas.create_image(0, 0, image=img, anchor=tk.NW)
newWindow.mainloop()
def run_optimization(self):
global calls
try:
# Отримання введених даних
function_str = self.function_str.get()
if function_str == '':
raise NotEnteredValueError('функція')
initial_point = self.initial_point.get()
precision = self.precision.get()
max_iterations = self.max_iterations.get()
method = self.method_var.get()
constraint1_str = self.constraint1_str.get()
constraint2_str = self.constraint2_str.get()
g = [eval(f'lambda x: {constraint1_str}'), eval(f'lambda x: {constraint2_str}'), lambda x: -x[0], lambda x: -x[1]]
f = eval(f'lambda x: {function_str}')
initial_point = eval(initial_point)
epsilon = eval(precision)
calls = 0
if method == 'Градієнтний':
optim_method = steepest_descent
elif method == 'Ньютонівський':
optim_method = newton_method
elif method == 'Квазі-Ньютонівський':
optim_method = bfgs_method
else:
raise NotEnteredValueError('метод')
except NotEnteredValueError as e:
messagebox.showerror("Бракує вхідних даних", f"Вибери {e.value_name}")
return
x, xk, f_x, penalty_x, k, k_penalty = constrained_optimization(f=f, g=g, x0=initial_point, c0=1, p=2, epsilon=epsilon, optim_method=optim_method, K=max_iterations)
# Приклад виводу результатів
result_str = f"Точка мінімуму: {x[0]:.3f}, {x[1]:.3f}\n"
result_str += f"Значення функції: {f_x}\n"
result_str += f"Значення функції пенальті: {penalty_x}\n"
result_str += f"Кількість ітерацій: {k}\n"
result_str += f"Кількість ітерацій пенальті: {k_penalty}\n"
result_str += f"Кількість обчислень цільової функції: {calls}\n"
self.result_label.set(result_str)
self.plot_graph(f, x, xk)
def plot_graph(self, f, x_min, xk):
plt.clf()
self.ax = self.figure.add_subplot(111, projection='3d')
# plot a 3D surface like in the example mplot3d/surface3d_demo
f2 = lambda x, y: f([x, y])
X = np.linspace(-10, 10, 100)
Y = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(X, Y)
Z = f2(X, Y)
self.ax.scatter(x_min[0], x_min[1], f(x_min), c='red', marker='*')
for xki in xk[:-1]:
self.ax.scatter(xki[0], xki[1], f(xki), c='green', marker='.')
xk_points = [[xki[0], xki[1], f(xki)] for xki in xk[:-1]]
xk_points = np.array(xk_points)
self.ax.plot(xk_points[:,0], xk_points[:,1], xk_points[:,2])
surf = self.ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False, alpha=.3)
self.figure.colorbar(surf, shrink=0.5, aspect=10)
self.canvas.draw()
if __name__ == "__main__":
print("Початок програми")
try:
root = tk.Tk()
app = Optimization(root)
root.mainloop()
except Exception as e:
print(e)