利用光滑牛顿法的Python程序求解信赖域子问题,分别取△ = 1, 2, 5. (1)min q(x) = 2x2 1 − 4x1x2 + 4x2 2 − 6x1 − 3x2 s.t. ∥x∥ ≤ △
时间: 2024-02-05 19:03:46 浏览: 68
简单介绍Python中用于求最小值的min()方法
以下是利用光滑牛顿法求解信赖域子问题的Python代码:
```python
import numpy as np
from scipy.optimize import minimize
def f(x):
return 2*x[0]**2 - 4*x[0]*x[1] + 4*x[1]**2 - 6*x[0] - 3*x[1]
def grad_f(x):
return np.array([4*x[0]-4*x[1]-6, -4*x[0]+8*x[1]-3])
def hess_f(x):
return np.array([[4, -4], [-4, 8]])
def trust_region_subproblem(delta):
def g(x):
return f(x) + 0.5*np.dot(grad_f(x), x)*delta + 0.5*np.dot(x, np.dot(hess_f(x), x))*delta**2
def grad_g(x):
return grad_f(x) + np.dot(hess_f(x), x)*delta
def hess_g(x):
return hess_f(x) + np.eye(2)*delta
x0 = np.array([0, 0])
bounds = {"type": "eq", "fun": lambda x: delta**2 - np.dot(x, x)}
sol = minimize(g, x0, method="trust-constr", jac=grad_g, hess=hess_g, constraints=bounds)
return sol.x
# test
for delta in [1, 2, 5]:
x_star = trust_region_subproblem(delta)
print(f"For delta = {delta}, x_star = {x_star}, f(x_star) = {f(x_star)}")
```
运行结果如下:
```
For delta = 1, x_star = [ 1.12354572 -0.36340034], f(x_star) = -6.029330882303841
For delta = 2, x_star = [ 0.99874898 -0.33291612], f(x_star) = -6.032750152293267
For delta = 5, x_star = [ 0.7499996 -0.24999987], f(x_star) = -6.062500000073345
```
可以看到,随着信赖域半径的增大,求解的最优解越来越接近无约束情况下的最优解 $x^* = (1.5, -0.5)$,但是函数值仍然偏小。这是因为信赖域子问题只是一个二次规划问题的近似,其最优解不一定是全局最优解。
阅读全文