虽然程序不可行,但解决下面描述的信任区域子问题minimize
会成功返回。
from numpy import array
from numpy import dot
from numpy import reshape
from numpy import zeros
from numpy.linalg import norm
from scipy.optimize import minimize
cIneq = array([ 9.])
AIneq = array([[ 1., -1.]])
cEq = array([ 0.09070257])
AEq = array([[ 1.00427046, 0.35586232]])
radius = 1
cons = [
{'type': 'ineq',
'fun': lambda n: radius**2 - dot(n, n),
'jac': lambda n: reshape(-2*n, (1,2))},
{'type': 'ineq',
'fun': lambda n: -cIneq - dot(AIneq, n),
'jac': lambda n: -AIneq},
{'type': 'eq',
'fun': lambda n: cEq + dot(AEq, n),
'jac': lambda n: AEq}]
res = minimize(lambda n: dot(n, n), jac=lambda n: reshape(2 * n, (1,2)), x0=zeros(2),
constraints=cons, method='SLSQP', options={"disp": False, "maxiter": 1000}, tol=1e-8)
print(radius**2 - dot(res.x, res.x)) # -11.5640205848
print(res.success) # True
目前,我所知道的唯一解决方法是在运行minimize
后添加一次双重检查:
def dbl_check_sol(cons, res):
if not res.success:
return True
for c in cons:
if c['type'] == 'ineq':
if (c['fun'](res.x) < -1e-8).any():
return False
elif c['type'] == 'eq':
if norm(c['fun'](res.x)) > 1e-8:
return False
else:
raise Exception('unknown type of constraint')
return True
print(dbl_check_sol(cons, res)) # False
print(res) # I see no other errors, and the message is 'Optimization terminated successfully.'
我在这里做错了什么?起初,我认为它可能是维度的一个问题,但如果我不重塑信任区域问题的jacobian,只会说'jac': lambda n: return -2*n},
,就会出现相同的结果。