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.
Asked
Active
Viewed 6,154 times
3
-
2Can you please provide a code sample. – SudoKid Apr 07 '17 at 18:37
-
def ahs_Ca(): for budg in range(7000000,100000000,10000000): xaxis.append(budg) f = (0,budg) bounds =[f]*component resultEta = differential_evolution(eta, bounds, maxiter = 4000) This is the sample code with bounds. I need to define some constraints here. Hope this information is helpful. – Lucky Apr 10 '17 at 18:10
2 Answers
3
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

qbzenker
- 4,412
- 3
- 16
- 23
-
Thank you for sharing this. I had to use a different optimization due to the complexity of my optimization problem. – Lucky Apr 22 '17 at 20:33
-
More generically, `np.inf` can be returned if a constraint is violated, and it works. – Asclepius Dec 12 '21 at 03:34
-
I posted a proper and detailed solution, using `scipy.optimize.NonlinearConstraint` here https://stackoverflow.com/a/71341247/3753826 – divenex Mar 03 '22 at 18:22
-3
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)

Lucky
- 347
- 6
- 15