добавил возможность задать ограничения

This commit is contained in:
2025-04-27 12:57:30 +04:00
parent 7ceb16a8b8
commit 1160656c3d

View File

@@ -64,10 +64,13 @@ def exploratory_search(f, x, delta, r=lambda x: True):
f_x_up = f(x_up)
f_x_down = f(x_down)
if f_x_up < f_x_new and f_x_up < f_x_down:
if not r(x_up) or not r(x_down):
print("Ограничение ОДЗ")
if f_x_up < f_x_new and f_x_up < f_x_down and r(x_up):
x_new = x_up
f_x_new = f_x_up
elif f_x_down < f_x_new:
elif f_x_down < f_x_new and r(x_down):
x_new = x_down
f_x_new = f_x_down
@@ -80,20 +83,24 @@ def sample_search(f, x1, x2, r=lambda x: True):
print()
print("Выполняем поиск по образцу")
while f(x2) < f(x1):
if not r(x2 + (x2 - x1)):
print("Ограничение ОДЗ")
break
x2, x1 = x2 + (x2 - x1), x2
print("Найдена точка", x2, f(x2))
return x2
def f1():
x0 = [0, 0]
delta0 = [1e6, 1e6, 1e6]
def example():
x0 = [100, 100]
delta0 = [10, 10]
alpha = 2
epsilon = 1e-4
c = [[1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]]
r = [[-1000, 1000], [-100, 100]]
def r(x):
return -10000 < x[0] < 10000 and -10000 < x[1] < 10000
def f(x):
return sum(
@@ -107,51 +114,9 @@ def f1():
return x, val
def f2():
x0 = [1, 1]
delta0 = [1, 1]
alpha = 2
epsilon = 1e-6
def f(x):
return x[0] ** 4 + x[1] ** 2 - 4 * x[0] * x[1]
x, val = hooke_jeeves(f, x0, delta0, epsilon, alpha)
return x, val
def f3():
x0 = [1, 0]
delta0 = [1, 1]
alpha = 2
epsilon = 1e-5
def f(x):
return x[0] * np.exp(x[0]) - (1 + np.exp(x[0])) * np.sin(x[1])
x, val = hooke_jeeves(f, x0, delta0, epsilon, alpha)
return x, val
def example():
x0 = [-4, -4]
delta0 = [1, 1]
alpha = 2
epsilon = 1e-4
def f(x):
return 8 * (x[0] ** 2) + 4 * x[0] * x[1] + 5 * (x[1] ** 2)
x, val = hooke_jeeves(f, x0, delta0, epsilon, alpha)
return x, val
if __name__ == "__main__":
x, val = f1()
# x, val = f2()
# x, val = f3()
# x, val = example()
x, val = example()
print()
print("=" * 40)