Scipy.optimize: how to restrict argument values

后端 未结 4 818
星月不相逢
星月不相逢 2020-12-13 18:40

I\'m trying to use scipy.optimize functions to find a global minimum of a complicated function with several arguments. scipy.optimize.minimize seem

4条回答
  •  自闭症患者
    2020-12-13 19:09

    The argument you are looking for is: constraints which is one of the arguments passed to scipy.minimize. Roll your own lambda function that receives the parameters to constrain like this:

    #A function to define the space where scipy.minimize should 
    #confine its search:
    def apply_sum_constraint(inputs):
        #return value must come back as 0 to be accepted
        #if return value is anything other than 0 it's rejected
        #as not a valid answer.
        total = 50.0 - np.sum(inputs)
        return total
    
    my_constraints = ({'type': 'eq', "fun": apply_sum_constraint })
    result = spo.minimize(f, 
                          guess, 
                          method='SLSQP', 
                          args=(a, b, c),
                          bounds=((-1.0, 1.0), (-1.0, 1.0)),
                          options={'disp': True},
                          constraints=my_constraints)
    

    The above example asserts that all the new candidates in the neighborhood of the last searched item better add up to 50. Change that method to define the permissible search space and the scipy.minimize function will waste no energy considering those answers.

提交回复
热议问题