简体   繁体   English

SCIPY多维最小化

[英]SCIPY multi-dimensional minimization

currently I have a lot of variables as 2D np.arrays. 目前,我有很多2D np.arrays变量。 But I would to use then to solve linear systems like: 但是我会用它来解决线性系统,例如:

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

I alredy tried to use np.optimize.minimize as: 我已经尝试将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)

This last step result in error: 最后一步导致错误:

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

The expected solution is: 预期的解决方案是:

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

Thanks 谢谢

EDIT : 编辑

I could solve using: 我可以解决使用:

 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)])

Solved using: 使用以下方法解决:

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)]
            )

So I'm calling RM minimization problems 所以我称RM最小化问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM