pear
pear

Reputation:

Specifying constraints for fmin_cobyla in scipy

I use Python 2.5.

I am passing bounds to the cobyla optimisation:

import numpy 
from numpy import asarray

Initial = numpy.asarray [2, 4, 5, 3]       # Initial values to start with

#bounding limits (lower,upper) - for visualizing

#bounds = [(1, 5000), (1, 6000), (2, 100000), (1, 50000)] 

# actual passed bounds

b1 = lambda  x: 5000 - x[0]      # lambda x: bounds[0][1] - Initial[0]

b2 = lambda  x: x[0] - 2.0       # lambda x: Initial[0] - bounds[0][0]

b3 = lambda  x: 6000 - x[1]      # same as above

b4 = lambda  x: x[1] - 4.0

b5 = lambda  x: 100000 - x[2]

b6 = lambda  x: x[2] - 5.0

b7 = lambda  x: 50000 - x[3]

b8 = lambda  x: x[3] - 3.0

b9 = lambda  x: x[2] >  x[3]  # very important condition for my problem!


opt= optimize.fmin_cobyla(func,Initial,cons=[b1,b2,b3,b4,b5,b6,b7,b8,b9,b10],maxfun=1500000)

Based on the initial values Initial and as per/within the bounds b1 to b10 the values are passed to opt(). But the values are deviating, especially with b9. This is a very important bounding condition for my problem!

"The value of x[2] passed to my function opt() at every iteration must be always greater than x[3]" -- How is it possible to achieve this?

Is there anything wrong in my bounds (b1 to b9) definition ?

Or is there a better way of defining of my bounds?

Please help me.

Upvotes: 1

Views: 3295

Answers (2)

fortran
fortran

Reputation: 76077

for b10, a possible option could be:

b10 = lambda x: min(abs(i-j)-d for i,j in itertools.combinations(x,2))

where d is a delta greater than the minimum difference you want between your variables (e.g 0.001)

Upvotes: 2

Robert Kern
Robert Kern

Reputation: 13430

fmin_cobyla() is not an interior point method. That is, it will pass points that are outside of the bounds ("infeasible points") to the function during the course of the optmization run.

On thing that you will need to fix is that b9 and b10 are not in the form that fmin_cobyla() expects. The bound functions need to return a positive number if they are within the bound, 0.0 if they are exactly on the bound, and a negative number if they are out of bounds. Ideally, these functions should be smooth. fmin_cobyla() will try to take numerical derivatives of these functions in order to let it know how to get back to the feasible region.

b9 = lambda x: x[2] - x[3]

I'm not sure how to implement b10 in a way that fmin_cobyla() will be able to use, though.

Upvotes: 3

Related Questions