tags:

views:

101

answers:

2

Hi,

I am trying to optimize a function using l_bfgs constraint optimization routine in scipy. But the optimization routine passes values to the function, which are not with in the Bounds.

my full code looks like,

def humpy(aParams):
 aParams = numpy.asarray(aParams)
 print aParams
 ####
 # connect to some other software for simulation
 # data[1] & data[2] are read
 ##### objective function
 val = sum(0.5*(data[1] - data[2])**2)
 print val
 return val

 ####

def approx_fprime():
 ####
 Initial = numpy.asarray([10.0, 15.0, 50.0, 10.0])
 interval = [(5.0, 60000.0),(10.0, 50000.0),(26.0, 100000.0),(8.0, 50000.0)]

 opt = optimize.fmin_l_bfgs(humpy,Initial,fprime=approx_fprime, bounds=interval ,pgtol=1.0000000000001e-05,iprint=1, maxfun=50000)

 print 'optimized parameters',opt[0]
 print 'Optimized function value', opt[1]

####### the end ####

based on the initial values(Initial) and bounds(interval) opt = optimize.fmin_l_bfgs() will pass values to my software for simulation, but the values passed should be with in 'bounds'. Thats not the case..see below the values passed at various iterations

iter 1  = [ 10.23534209  15.1717302   50.5117245   10.28731118]

iter 2  = [ 10.23534209  15.1717302   50.01160842  10.39018429]

          [ 11.17671043  15.85865102  50.05804208  11.43655591]

          [ 11.17671043  15.85865102  50.05804208  11.43655591]

          [ 11.28847754  15.85865102  50.05804208  11.43655591]

          [ 11.17671043  16.01723753  50.05804208  11.43655591]

          [ 11.17671043  15.85865102  50.5586225   11.43655591]
          ...............
          ...............
          ...............
         [  49.84670071 -4.4139714 62.2536381 23.3155698847]

at this iteration -4.4139714 is passed to my 2nd parameter but it should vary from (10.0, 50000.0), from where come -4.4139714 i don't know?

where should i change in the code? so that it passed values which should be with in bounds

+1  A: 

You are trying to do bitwise exclusive or (the ^ operator) on floats, which makes no sense, so I don't think your code is actually the code you have problems with. However, I changed the ^ to ** assuming that was what you meant, and had no problems. The code worked fine for me with that change. The parameters are restricted exactly as defined.

Python 2.5.

Lennart Regebro
yes you are right i used ** , i this case its fine but some cases its going beyond the bounds, what to do? how to restrict with in bounds?
Can you give an example of when it actually goes beyond bounds, because for me it doesn't.
Lennart Regebro
Thank you for your patient, please see my edited code.
A: 

Are you asking about doing something like this?

def humpy(aParams):
  aParams = numpy.asarray(aParams)
  x = aParams[0]
  y = aParams[1]
  z = aParams[2]
  u = aParams[3]
  v = aParams[4]
  assert 2 <= x <= 50000
  assert 1 <= y <= 35000
  assert 1 <= z <= 45000
  assert 2 <= u <= 50000
  assert 2 <= v <= 60000
  val=100.0*((y-x**2.0)^2.0+(z-y**2.0)^2.0+(u-z**2.0)^2.0+(v-u**2.0)^2.0)+(1-x)^2.0+(1-y)^2.0+(1-z)^2.0+(1-u)^2.0
  return val
S.Lott
Thanks S.Lott, but value deviates the interval.please see me edited code, you may get some idea