Skip to content Skip to sidebar Skip to footer

Constraints On Parameters Using Scipy Differential Evolution

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. H

Solution 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 <=1000return-20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e
    else:
        return1000#some high value

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

Solution 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)

Post a Comment for "Constraints On Parameters Using Scipy Differential Evolution"