簡體   English   中英

SCIPY多維最小化

[英]SCIPY multi-dimensional minimization

目前,我有很多2D np.arrays變量。 但是我會用它來解決線性系統,例如:

min W
st:
w >= U - S
W >= 0

我已經嘗試將np.optimize.minimize用作:

>>> W0=np.zeros((2,4))
>>> U = np.array([[18, 0, 0, 10],[210,0,0,20]])
>>> S = np.array([[16, 8, 12, 8],[80, 160, 80, 80]])
>>> cons = ({'type':'ineq', 'fun': lambda x: x - U + S})
>>> res = minimize(lambda x: x, W0, constraints=cons)

最后一步導致錯誤:

ValueError: operands could not be broadcast together with shapes (8,) (2,4) 

預期的解決方案是:

 array([[   2, 0, 0, 2],
        [ 130, 0, 0, 0]])

謝謝

編輯

我可以解決使用:

 res=np.array([[minimize( fun, (0), bounds=[(0,None)], constraints = ({'type':'ineq', 'fun': lambda x: x -U[r,m] + S[r,m]})).x[0] for m in range(4)] for r in range(2)])

使用以下方法解決:

res=np.array([
               [
                 minimize( fun, (0), bounds=[(0,None)], 
                   constraints = ({'type':'ineq', 
                     'fun': lambda x: x -U[r,m] + S[r,m]}
                   )
                 ).x[0] 
                f or m in range(4)] 
              for r in range(2)]
            )

所以我稱RM最小化問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM