Scipy.optimize.minimize SLSQP with linear constraints fails

后端 未结 1 752
走了就别回头了
走了就别回头了 2020-12-03 12:46

Consider the following (convex) optimization problem:

minimize 0.5 * y.T * y
s.t.     A*x - b == y

where the optimization (vector) variable

相关标签:
1条回答
  • 2020-12-03 13:29

    You've run into the "late binding closures" gotcha. All the calls to cons_i are being made with the second argument equal to 19.

    A fix is to use the args dictionary element in the dictionary that defines the constraints instead of the lambda function closures:

    cons_per_i = [{'type':'eq', 'fun': cons_i, 'args': (i,)} for i in np.arange(m)]
    

    With this, the minimization works:

    In [417]: sol2 = minimize(obj, x0 = z0, constraints = cons_per_i, method = 'SLSQP', options={'disp': True})
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 2.1223622086
                Iterations: 6
                Function evaluations: 192
                Gradient evaluations: 6
    

    You could also use the the suggestion made in the linked article, which is to use a lambda expression with a second argument that has the desired default value:

    cons_per_i = [{'type':'eq', 'fun': lambda z, i=i: cons_i(z, i)} for i in np.arange(m)]
    
    0 讨论(0)
提交回复
热议问题