我一直在使用scipy.optimize.minimize (docs)當我定義一個無法滿足約束的問題時,發現一些奇怪的行為。這是一個例子:from scipy import optimize# minimize f(x) = x^2 - 4xdef f(x): return x**2 - 4*xdef x_constraint(x, sign, value): return sign*(x - value)# subject to x >= 5 and x<=0 (not possible)constraints = []constraints.append({'type': 'ineq', 'fun': x_constraint, 'args': [1, 5]})constraints.append({'type': 'ineq', 'fun': x_constraint, 'args': [-1, 0]})optimize.minimize(f, x0=3, constraints=constraints)結果輸出:fun: -3.0 jac: array([ 2.]) message: 'Optimization terminated successfully.' nfev: 3 nit: 5 njev: 1 status: 0 success: True x: array([ 3.])沒有滿足約束條件的解決方案,但是,使用初始條件作為最佳解決方案,minimum()成功返回。這種行為是故意的嗎?如果是這樣,如果最佳解決方案不滿足約束條件,是否有辦法強制失?。?
1 回答

千巷貓影
TA貢獻1829條經驗 獲得超7個贊
這似乎是一個錯誤。我在github上的問題上添加了帶有您的示例變體的評論。
如果您使用其他方法(例如COBYLA),則該函數將無法正確找到解決方案:
In [10]: optimize.minimize(f, x0=3, constraints=constraints, method='COBYLA')
Out[10]:
fun: -3.75
maxcv: 2.5
message: 'Did not converge to a solution satisfying the constraints. See `maxcv` for magnitude of violation.'
nfev: 7
status: 4
success: False
x: array(2.5)
添加回答
舉報
0/150
提交
取消