我有一个带非线性约束的有界优化问题,我正试图解决这个问题。非线性约束函数需要参数,这就是我无法使它工作的地方。下面是我遵循的结构。如何将参数arg3, arg4, arg5
发送到约束函数cons
from scipy.optimize import (BFGS, SR1, Bounds, NonlinearConstraint, minimize)
def objfcn(x, arg1, arg2):
...
return out1
def cons(x, arg3, arg4, arg5):
...
return out2
bounds = Bounds([-d, -d, -d, -d], [d, d, d, d])
nonlinear_constraint = NonlinearConstraint(cons, 0.0, 0.0, jac='2-point', hess=BFGS())
res = minimize(objfcn,
x0,
args=(arg1, arg2),
method='trust-constr',
jac="2-point",
hess=SR1(),
constraints=[nonlinear_constraint]
options={'verbose': 1},
bounds=bounds)
编辑:当前不太好的解决方案是通过全局变量将参数传递给约束函数cons()
trust-constr
与其他约束解算器有一点不同的方法。我没有看到将参数传递给约束的直接方法。当然,我们可以试着在课堂上打包东西相关问题 更多 >
编程相关推荐