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?

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

1 Answer 1


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

  • $\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$ Commented Feb 2, 2021 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$ Commented Feb 2, 2021 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$ Commented Feb 2, 2021 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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