Constraints on parameters using differential evolution in python

ⅰ亾dé卋堺 提交于 2019-12-23 23:11:11

问题


I am trying to use differential evolution to optimize availability based on cost. However, I have three unknown parameters (a, b, c) here and I can define the range using bounds. However, I want to define additional constraint as a+b+c <= 10000. I am using python to do this and I tried to use an option "args" within differential evolution but it did not work. Any information will be appreciated.


回答1:


Here is a hack. I used the last example from the documentation and constrained the sum(x) > 4.1 (without this constraint the optimized solution is (0,0)):

from scipy.optimize import differential_evolution
import numpy as np
def ackley(x):
    arg1 = -0.2 * np.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2))
    arg2 = 0.5 * (np.cos(2. * np.pi * x[0]) + np.cos(2. * np.pi * x[1]))

    if x[0]+x[1] > 4.1: #this is the constraint, where you would say a+b+c <=1000
        return -20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e
    else:
        return 1000 #some high value

bounds = [(-5, 5), (-5, 5)]
result = differential_evolution(ackley, bounds)
result.x, result.fun



回答2:


Defining the constraint using differential evolution is not an appropriate solution for the problem I have described above. For this purpose, we can use Nminimize command which has dedicated option to define constraints.

scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)


来源:https://stackoverflow.com/questions/43284991/constraints-on-parameters-using-differential-evolution-in-python

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