2
$\begingroup$

I've set up and successfully executed an evolutionary optimization in Excel, and now have a need to convert the problem to Python. differential_evolution() in the optimize package would seem to get me there, but doesn't appear to allow for the setting of a constraint such as x1 < x2 (where x1 and x2 are elements of the decision variable vector).

Any suggestions?

$\endgroup$
1
  • $\begingroup$ Can you detail your evolutionary optimization ? $\endgroup$ – Kuifje Feb 1 at 15:45
6
$\begingroup$

You could apply the following trick inside your objective function:

x1 = min(x[1],x[2])
x2 = max(x[1],x[2])

Now x1 <= x2 automatically holds and you don't need the constraint. (Assuming you can live with <= instead of <).

$\endgroup$
3
  • $\begingroup$ Thanks much, Erwin! Follow-up question: do I only want this adjustment to hold within the function, or do I want to ensure the current decision vector maintains this "correction" in between objective function calls? $\endgroup$ – Zain Khandwala Feb 2 at 2:59
  • $\begingroup$ You can just do it inside the evaluation function. You also may need to repeat it when reporting the solution to the user. $\endgroup$ – Erwin Kalvelagen Feb 2 at 7:20
  • $\begingroup$ Makes sense. Really clever solution; being relatively new to the study of optimization, I likely wouldn't have gotten there myself anytime soon! $\endgroup$ – Zain Khandwala Feb 2 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.