views:

335

answers:

2

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.

+2  A: 

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.

Robert Kern
`max(x) - min(x)` might work -- it is reasonably smooth, > 0 the more "within bounds" x is, == 0 when "exactly on the bound" (I see no way to be "outside" a 1-D bound with a 4-D point;-).
Alex Martelli
x[2]-x[3] > 0 and x[2] > x[3] are equivalent conditions
fortran
+1  A: 

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)

fortran
Excellent idea ! unfortunately i use only python 2.5, the main problem is opt()should firmly stick to b9 = lambda x: x[2] > x[3] , x[2] must be always greater than x[3] this condition very important for my problem. How to code b9 condition?