0

Here it was suggested to use mathematical transforms to impose hard constraints on search variables in the SLSQP algorithm in scipy.optimize.minimize. I assume more algorithms that allow constraints take them more as suggestions than hard facts, and it is not even just a python question at this point.

So in my case, I have a hard constraint where the individual search variables x0, x1, x2, x3 ... and their sum in particular combinations must be constrained to an interval, say 0..1. More specifically, the constraint is that the sum 1 - k*x0 +k*x1 -k*x2 -c0 +k*x3 -k*x4 -c1 ... should be in the interval 0 .. 1 for every step of the way. You can use sigmoid functions to transform the search space into an interval. But with many such functions in combinations, that becomes infeasible - right? What is the proper way to impose hard limitations on simple summation combinations of search variables?

Scipy.optimize.minimize method='SLSQP' ignores constraint

Andreas Schuldei
  • 343
  • 1
  • 15
  • "I assume more algorithms that allow constraints take them more as suggestions than hard facts [...]" No, this assumption is wrong from a mathematical point of view. "What is the proper way to impose hard limitations on combinations of search variables?" You can formulate your "hard limitations" just as variable bounds and linear constraints and solve it as a constrained optimization problem. That said, your linked answer is outdated and the part regarding "hard constraints" is no longer true. – joni Jan 24 '23 at 17:35
  • I am using scipy.optimize.minimize currently and face the described problem (where the constraints are largely ignored). Could you please suggest a replacement algorithm accepting boundaries and constraints and actually respecting them? I plan to switch to linear programming in the long term, but i need to reformulate my problem for that. Until then I would like to try an alternative. – Andreas Schuldei Jan 24 '23 at 19:32
  • You could use [`NonlinearConstraint`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.NonlinearConstraint.html) objects (see the `keep_feasible` option) and the trust-constr algorithm instead of SLSQP. Depending on the size of your problem, you could also try some proper large-scale solver like Ipopt instead of scipy.optimize's algorithms. However, if your objective **and** your constraints are linear (your above constraints are linear), it's **highly** recommended to solve your problem by means of an LP solver that uses some simplex-algorithm (way faster and more robust). – joni Jan 25 '23 at 09:44
  • @joni The constraint is linear, but finding the optimal solution requires integrating over time-series data and finding maximal and minimal cost intervals of variable length. i described my problem here, and I would love your input https://stackoverflow.com/questions/75158376/scipy-optimize-minimize-slsqp-indirect-constraints-problem/75171451#75171451 – Andreas Schuldei Jan 25 '23 at 12:53
  • @joni i found your earlier answer https://stackoverflow.com/questions/69839367/nonlinearconstraints-in-scipy-optimize using trust-constr and NonlinearConstraint and think about how to transfer that to my problem at hand. I would not know how to give derivatives of my constraints. I could supply numerical derivatives to my price function - but so could the algorithm itself. I don't know how to calculate a jacobian matrix for my problem either. Can you help? – Andreas Schuldei Jan 25 '23 at 16:36

0 Answers0