Optimization with "add to 1" constraint in optimization variable
The NLP solvers I typically use will not ask for function and gradient evaluations when X is outside its bounds. That means we can protect things like sqrt(x) and log(x) by adding appropriate lower bounds.
Constraints can not be assumed to hold when we evaluate functions and gradients.
If your function assumes sum(X)=1, you could call F(X/sum(X)). The only complication is when X=0. In that case, return a large value for F so that the solver will move away from it.
I am trying to formulate an optimization problem using scipy minimize function. However, I am having the following problem that I can't work around:
I want X = [x1, x2, x3, x4, x5] that minimizes a cost function F(X). This X vector, however, are percentage values that must add to 1, i.e., np.sum(X) = 1.
The problem is: if I use, for instance, the "SLSQP" method with some initial values (such as X0 = [0.2, 0.2, 0.2, 0.2, 0.2]), it will try to increment each value to find some convergence direction. For example, the algorithm will make X0 -> [0.21, 0.2, 0.2, 0.2, 0.2]. But that cannot happen, because np.sum(X) = 1 is a requirement for me to compute the objective function.
Using constraints does not help either! I could make a constraint with np.sum(X) = 1. However, the minimize algorithm will only check the constraint after computing the objective function.
Anyone have an idea on how to deal with such a problem?
Thanks a lot!