# How can I programmatically define constraints for minimize_constrained?

I'd like to run a minimization problem in several different dimensions, and then find a minimum over the results (for more information on the problem see here).

I can easily define both the objective function and the main constraint in a dimenson-independent way with a simple combination of map, sum, and prod:

d = 25
t = 0.75
f  = lambda p: sum(map(lambda x:  d^2 * tan(x * pi/2) + 8*d, p))
c1 = lambda p: 1 - prod(map(lambda q: 1 - q, p)) - t


On the other hand, I'm not sure on how to express the fact that every variable should be an element of the interval [a,b], where 0 < a < b < 1 are to be given beforehand. I thought I could use min like so:

a = 0.1
b = 0.95
lower_bound = lambda x: x - a
upper_bound = lambda x: b - x
c_lower = lambda p: min(map(lower_bound, p))
c_upper = lambda p: min(map(upper_bound, p))


but I'm worried that using those instead of a pair of constraints for every variable would have a negative effect on minimize_constrained.

edit retag close merge delete

Sort by » oldest newest most voted

With a bit of effort, due to how Python handles lambda functions, I found a way. Given the above set-up, the following code returns a list of constraints for n variables:

from itertools import chain

def constraints(n):
def lb(i):
return lambda p: lower_bound(p[i])
def ub(i):
return lambda p: upper_bound(p[i])

return list(chain([c1], *map(lambda i: [lb(i), ub(i)], range(n))))

more