pyswarm
pyswarm copied to clipboard
equal constraints
Hi there, I've tried to find a way to define some equal constraints but didn't find one, is there a way to do that?
Not directly, no.
Equality constraints are inherently difficult to satisfy in optimization routines because the basically force your solution to lie on an infinitely thin line, which, especially for algorithms like PSO, is really hard to land on and stay on by simply guessing values for the input.
A better approach, if possible, is to refactor your objective code to use the equality constraint as a driving calculation that helps determine the objective value. That way, you are guaranteed to satisfy the constraint at all times.
In other words, if you have the constraint ab = c-d and the objective function uses d as an input, then I would change it so that d is calculated, rather than guessed: d = c-(ab)
Another option, if you aren't able to refactor your code, would be to use a tolerance band around the equality constraint g(x)
, changing it to two inequality constraints. So, instead of
g(x) = b
you would have
g(x)<= b+tol
and
g(x)>=b-tol
where tol
is some small value, relative to the constraint value b
.
thanks tisimst , very detailed and clear answer. Since My parameter constrainted are state variables which comes from a black box function, there may not be any hope to refractor that(a iterator may be a solution), then I may choose the latter approach in my case.
That makes sense. In that case, I'd start with a generous tolerance and see if you can fine-tune it to something more narrow.