Artturi Björk
Artturi Björk

Reputation: 3893

How to keep optimization search within bounds in Scipy?

I'm solving a (constrained) nonlinear minimization problem with three variables (w,V,m). Given w the minimization problem is (constrained) linear for (V,m). The linear minimization problem is defined for w in [w_0, w_1].

The way I've set up the problem is that given w, I'm solving a constrained linear program and then I'm minimizing it over w with bounds = ((w_0, w_1),) the range of w as bounds. I'm running into problems when the minimization over w searches outside it's bounds i.e. to a region where the linear program is not defined.

Is there a way to restrict the search to not go outside the provided bounds? If not are there alternatives? Pass tighter bounds? Make the objective function return a large value if it is passed a value outside the bounds?

I'm sorry for not being able to provide a minimal working example.

Here is some pseudo-code:

from scipy.optimize import linprog,minimize

def objective(w):
A_ub,b_ub = constraints(w)
results = linprog(c,A_ub = A_ub,b_ub=b_ub)
return results.fun

bounds = ((w_0,w_1),)
x0 = (w_0+w_1)/2
minimize(objective,x0,bounds)

Upvotes: 0

Views: 2770

Answers (2)

cd98
cd98

Reputation: 3532

Something that might work is reparametrizing/redifining the variable w, so that it never leaves the bounds. If x goes between -infinity and infinity, then

w = a + (b-a)/(1 + exp(-x))

will be in the (a, b) interval.

Just to be clear, you should set x as the argument to optimize in the minimize function, and include w with this formula. Whatever x you get, you're guaranteed that the w will be within bounds.

When is this a bad idea? Definitely if you think you might have a corner solution at either a or b. Other than that, I think it usually works, but please report back to see if it worked for you.

Upvotes: 2

sascha
sascha

Reputation: 33532

One can classify the approaches to solve constrained optimization problems into two classes:

  • (1) Feasible methods: every iterate will satisfy the constraints
  • (2) Infeasible methods: iterates do not need to satisfy the constraints until convergence

So using a feasible method is natural for your case. Sadly scipy's docs are a bit unclear about this characteristic in regards to the available algorithms.

I'm pretty sure, that L-BFGS-B is the way to go here (as you only need bound-constraints!)

Upvotes: 0

Related Questions