scipy.optimize with SLSQP. 'Singular matrix C in LSQ subproblem'

﹥>﹥吖頭↗ 提交于 2019-12-08 07:12:46

问题


I'm trying to minimize a dot product of 2 vectors but it doesn't work and I have no idea why. Can someone please help me?

I have a matrix c of this form:

c =  [[c11, c12, c13, c14, c15],
      [c21, c22, c23, c24, c25]]

I want to get a matrix p of this form:

 p =  [[p11, p12, p13, p14, p15],
      [p21, p22, p23, p24, p25]]

I want to maximize this value :

c11*p11 + c12*p12 +c13*p13 + c14*p14 + c15*p15 + c21*p21 + c22*p22 +c23*p23 + c24*p24 + c25*p25

To get that I convert the c and p to 1-D vector and do the dot product so that my function to maximize is:

f(p) = c.dot(p)

The constraints are:

c11 + c12 + c13 + c14 + c15 = 1
c21 + c22 + c23 + c24 + c25 = 1

every element in p must be between 0.01 and 0.99.

I have tried scipy.optimize.linprog and it works:

from scipy.optimize import linprog

c = np.array([0. , 0. , 0. , 0. , 0. , 0. , 20094.21019108, 4624.08079143, 6625.51724138, 3834.81081081])

A_eq = np.array([[1,1,1,1,1,0,0,0,0,0],
                 [0,0,0,0,0,1,1,1,1,1]])

b_eq = np.array([1, 1])

res = linprog(-c, A_eq=A_eq, b_eq=b_eq, bounds=(0.01, 0.99))


res
Out[561]: 
 fun: -19441.285871873002
 message: 'Optimization terminated successfully.'
 nit: 13
 slack: array([0.03, 0.98, 0.98, 0.98, 0.98, 0.98, 0.03, 0.98, 0.98, 0.98, 0.  ,
   0.  , 0.95, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ])
 status: 0
 success: True
   x: array([0.96, 0.01, 0.01, 0.01, 0.01, 0.01, 0.96, 0.01, 0.01, 0.0

But I'm trying to use scipy.optimize.minimize with SLSQP instead and that's where I get this 'Singular matrix C in LSQ subproblem' . Here is what I've done:

from scipy.optimize import minimize

def build_objective(ck, sign = -1.00):
    """
    Builds the objective fuction for matrix ck
    """
    # Here I turn my c matrix to a 1-D matrix
    ck = np.concatenate(ck)
    def objective(P):
         return sign*(ck.dot(P))
    return objective

def build_constraint_rows(ck):
    """
    Builds the constraint functions that specify that the sum of the proportions for
    each bin equals 1
    """

    ncol = ck.shape[1]
    nrow = ck.shape[0]
    constrain_dict = []
    for i in range(nrow):

        vector = np.zeros((nrow,ncol))
        vector[i, :] =  1
        vector  = np.concatenate(vector)

        def row_constrain(P):
            return 1 - vector.dot(P) 

        constrain_dict.append({'type': 'eq', 'fun': row_constrain})

    return constrain_dict

# Matrix: Notice that this is not in vector form yet
c = np.array([[0. , 0. , 0. ,  0.,  0.], 
              [0. , 20094.21019108,  4624.08079143,  6625.51724138, 3834.81081081]])

# I need some initial p matrix for the function 'minimize'. I look for the value of the row that is the highest and assign it a proportion p of 0.96 and the rest 0.01 so the sum in 1 per row

P_initial = np.ones(c.shape)*0.01
nrow = test.shape[0]
for i in range(nrow):

    index= np.where(c[i,] == np.max(c[i,]))[0]

    if index.shape[0] > 1:
        index = int(np.random.choice(index, size = 1))
    else:
        index = int(index)
    P_initial[i,index] = 0.96

# I turn the P_initial to vector form
P_initial =  np.concatenate(P_initial)

# These are the bounds of each p value
b = (0.01,0.99)
bnds = (b,)*c.size


# I then use my previous functions

objective_fun = build_objective(c)
cons = build_constraint_rows(c)
res =  minimize(objective_fun,P_initial,method='SLSQP',\
                    bounds=bnds,constraints=cons)

This is my final result:

res
Out[546]: 
 fun: -19434.501741138763
 jac: array([0. , 0.,0. , 0. ,0. , 0., -20094.21020508, -4624.08056641, -6625.51708984,  -3834.81079102])
 message: 'Singular matrix C in LSQ subproblem'
 nfev: 24
 nit: 2
 njev: 2
 status: 6
 success: False
 x: array([0.96      , 0.01      , 0.01      , 0.01      , 0.01      ,
   0.01020202, 0.95962502, 0.01006926, 0.01001178, 0.01009192])

Please help me understand what I'm doing wrong.

Thank you in advanced,

Karol

来源:https://stackoverflow.com/questions/50340056/scipy-optimize-with-slsqp-singular-matrix-c-in-lsq-subproblem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!