scipy minimize with constraints

前端 未结 1 1914
名媛妹妹
名媛妹妹 2020-12-05 02:48

I know that this question should be handled in the manual of scipy.optimize, but I don\'t understand it well enough. Maybe you can help

I have a function (this is ju

相关标签:
1条回答
  • 2020-12-05 03:11

    This constraint

    t[0] + t[1] = 1
    

    would be an equality (type='eq') constraint, where you make a function that must equal zero:

    def con(t):
        return t[0] + t[1] - 1
    

    Then you make a dict of your constraint (list of dicts if more than one):

    cons = {'type':'eq', 'fun': con}
    

    I've never tried it, but I believe that to keep t real, you could use:

    con_real(t):
        return np.sum(np.iscomplex(t))
    

    And make your cons include both constraints:

    cons = [{'type':'eq', 'fun': con},
            {'type':'eq', 'fun': con_real}]
    

    Then you feed cons into minimize as:

    scipy.optimize.minimize(func, x0, constraints=cons)
    
    0 讨论(0)
提交回复
热议问题