Ask Your Question

How can I programmatically define constraints for minimize_constrained?

asked 2015-11-29 06:20:53 -0500

A.P. gravatar image

updated 2015-11-29 06:38:45 -0500

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 flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2015-11-29 08:50:51 -0500

A.P. gravatar image

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))))
edit flag offensive delete link more


I will wait to accept this answer, in case someone has a better solution.

A.P. gravatar imageA.P. ( 2015-11-29 08:51:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-11-29 06:20:53 -0500

Seen: 311 times

Last updated: Nov 29 '15