Created
December 15, 2017 09:17
-
-
Save b0g3r/b94dd87270cf843f2d31221a2d737603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
def calculate(): | |
step = float(e1.get()) | |
x = float(e21.get()) | |
y = float(e22.get()) | |
prec = float(e3.get()) | |
dx = float(e41.get()) | |
dy = float(e42.get()) | |
x0 = x, y | |
x1 = research(x0, dx, dy, prec, step, False) | |
if x1: | |
while True: | |
x = x0[0] + 2.0 * (x1[0] - x0[0]) | |
y = x0[1] + 2.0 * (x1[1] - x0[1]) | |
x2 = x, y | |
x3 = research(x2, dx, dy, prec, step, False) | |
if x3 and x3 == x2: | |
x0 = x1[0], x1[1] | |
x1 = x3[0], x3[1] | |
else: | |
break | |
e5['state'] = 'normal' | |
e6['state'] = 'normal' | |
e7['state'] = 'normal' | |
e5.delete(0, END) | |
e6.delete(0, END) | |
e7.delete(0, END) | |
e5.insert(0, str(x0[0])) | |
e6.insert(0, str(x0[1])) | |
e7.insert(0, str(func(x0[0], x0[1]))) | |
e5['state'] = 'disabled' | |
e6['state'] = 'disabled' | |
e7['state'] = 'disabled' | |
def func(x1, x2): | |
# return pow(1 - x1, 2) + 100 * pow(x2 - pow(x1, 2), 2) | |
return 4 * pow(x1 - 5, 2) + pow(x2 - 6, 2) | |
def research(x0, dx, dy, prec, step, step_fixed=False): | |
step_x = dx | |
step_y = dy | |
x1 = x0 | |
while step_y >= prec or step_x >= prec: | |
if func(x0[0], x0[1]) > func(x1[0] + step_x, x1[1]): | |
return x1[0] + step_x, x1[1] | |
elif func(x0[0], x0[1]) > func(x1[0] - step_x, x1[1]): | |
return x1[0] - step_x, x1[1] | |
else: | |
if not step_fixed: | |
step_x = step_x / step | |
val1 = func(x1[0] + step_x, x1[1]) | |
val2 = func(x1[0] - step_x, x1[1]) | |
if val1 < val2: | |
x1 = x1[0] + step_x, x1[1] | |
else: | |
x1 = x1[0] - step_x, x1[1] | |
if func(x0[0], x0[1]) > func(x1[0], x1[1] + step_y): | |
return x1[0], x1[1] + step_y | |
elif func(x0[0], x0[1]) > func(x1[0], x1[1] - step_y): | |
return x1[0], x1[1] - step_y | |
else: | |
if not step_fixed: | |
step_y = step_y / step | |
val1 = func(x1[0], x1[1] + step_y) | |
val2 = func(x1[0], x1[1] - step_y) | |
if val1 < val2: | |
x1 = x1[0], x1[1] + step_y | |
else: | |
x1 = x1[0], x1[1] - step_y | |
master = Tk() | |
Label(master, text="Коэффициент уменьшения шага").grid(row=0) | |
e1 = Entry(master, width=40) | |
e1.grid(row=0, columnspan=2, column=1) | |
Label(master, text="Начальная точка X0(X1:X2)").grid(row=1) | |
e21 = Entry(master) | |
e21.grid(row=1, column=1) | |
e22 = Entry(master) | |
e22.grid(row=1, column=2) | |
Label(master, text="Точность").grid(row=3) | |
e3 = Entry(master, width=40) | |
e3.grid(row=3, columnspan=2, column=1) | |
Label(master, text="Приращение (dX1: dX2)").grid(row=4) | |
e41 = Entry(master) | |
e41.grid(row=4, column=1) | |
e42 = Entry(master) | |
e42.grid(row=4, column=2) | |
Button(master, text="Исследовать", command=calculate).grid( | |
row=5, columnspan=2, sticky='n' | |
) | |
Label(master, text="X1").grid(row=6) | |
e5 = Entry(master, state='disabled') | |
e5.grid(row=6, column=1) | |
Label(master, text="X2").grid(row=7) | |
e6 = Entry(master, state='disabled') | |
e6.grid(row=7, column=1) | |
Label(master, text="f(x)").grid(row=8) | |
e7 = Entry(master, state='disabled') | |
e7.grid(row=8, column=1) | |
master.title('Метод Хука-Дживса') | |
mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment