SciPy 使用 SLSQP 的优化给出了不满足约束的解决方案
SciPy Optimization using SLSQP gives solution that does not satisfy constraints
我正在尝试最小化函数
B1=0.00299540627439527*x[0]**2 + 0.00701825276534463*x[0]*x[1] + 0.0672877782113971*x[0] + 0.00456646480250456*x[1]**2 + 0.054080834634827*x[1] + 0.298938755491431
在 x1=[-10,10]
和 x2=[-10,10]
的状态 space
给定以下不等式约束
EqX0=[[(x[0]+5)**2+x[1]**2-2.5]]<=0
其中 x[0]
和 x[1]
已被符号定义为 x1
和 x2
ga=1
是一个参数
然而,当我使用SLSQP解决非线性约束优化问题时,答案导致不满足不等式约束的x=[-10,10]。
这是一段代码:
def Objective2(ax):
B2=B1.copy()
B2=B2.subs((x[i],ax[i]) for i in range(len(ax)))
return ga-B2
def Constraint1(ax):
EqX0c=EqX0.copy()
cc=[]
for pp in range(len(EqX0c)):
cc.append(-EqX0c[0][pp].subs((x[i],ax[i]) for i in range(len(ax))))
return cc
con1= {'type': 'ineq','fun': Constraint1}
bounds=Bounds([-10,10],[-10,10])
sol2=minimize(Objective2,x0,method="SLSQP",bounds=bounds,constraints=con1)
这是生成的输出。程序成功终止但给出了错误的结果。
fun: -0.12123115088124994
jac: array([-0.07756218, -0.0752276 ])
message: 'Optimization terminated successfully.'
nfev: 4
nit: 5
njev: 1
status: 0
success: True
x: array([-10., 10.])
知道为什么会这样吗?我该如何解决?
我认为您错误地定义了边界。试试 Bounds([-10,-10],[10,10])
.
参见docs。
我正在尝试最小化函数
B1=0.00299540627439527*x[0]**2 + 0.00701825276534463*x[0]*x[1] + 0.0672877782113971*x[0] + 0.00456646480250456*x[1]**2 + 0.054080834634827*x[1] + 0.298938755491431
在 x1=[-10,10]
和 x2=[-10,10]
的状态 space
给定以下不等式约束
EqX0=[[(x[0]+5)**2+x[1]**2-2.5]]<=0
其中 x[0]
和 x[1]
已被符号定义为 x1
和 x2
ga=1
是一个参数
然而,当我使用SLSQP解决非线性约束优化问题时,答案导致不满足不等式约束的x=[-10,10]。
这是一段代码:
def Objective2(ax):
B2=B1.copy()
B2=B2.subs((x[i],ax[i]) for i in range(len(ax)))
return ga-B2
def Constraint1(ax):
EqX0c=EqX0.copy()
cc=[]
for pp in range(len(EqX0c)):
cc.append(-EqX0c[0][pp].subs((x[i],ax[i]) for i in range(len(ax))))
return cc
con1= {'type': 'ineq','fun': Constraint1}
bounds=Bounds([-10,10],[-10,10])
sol2=minimize(Objective2,x0,method="SLSQP",bounds=bounds,constraints=con1)
这是生成的输出。程序成功终止但给出了错误的结果。
fun: -0.12123115088124994
jac: array([-0.07756218, -0.0752276 ])
message: 'Optimization terminated successfully.'
nfev: 4
nit: 5
njev: 1
status: 0
success: True
x: array([-10., 10.])
知道为什么会这样吗?我该如何解决?
我认为您错误地定义了边界。试试 Bounds([-10,-10],[10,10])
.
参见docs。