简体   繁体   中英

SCIPY multi-dimensional minimization

currently I have a lot of variables as 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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