3

I'm having trouble specifying constraints using basinhopping with method='COBYLA'. Here is a test case where things go wrong. Essentially, the constraints are ignored and there are function trials outside the specified range. I specify a simple quadratic with minimum at [0,0], searching for -3<x[0], but as you can see from the output, there are lots of searches outside that range (I increased the stepsize to make it obvious)

import numpy as np
from scipy.optimize import basinhopping

def f(x):
    if x[0]<-3 : 
        print('outside range ',x[0])
    return x[0]**2+x[1]**2

cons = [{'type':'ineq','fun': lambda x: x[0]+3}]
kwargs = {'method':'COBYLA','constraints':cons}

ret=basinhopping(f, [5,1],T=1,stepsize=1000,niter=1,minimizer_kwargs=kwargs)
print(ret)

runfile('py/cobyla_test', wdir='/py', post_mortem=True)
outside range  -446.14581341127945
outside range  -445.14581341127945
outside range  -445.14581341127945
outside range  -444.14581341127945
[etc... lots of output deleted]
[-4.81217825e-05 -5.23242054e-05] 5.0535284302996725e-09
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
andrea m.
  • 668
  • 7
  • 15

1 Answers1

1

As written at scipy.optimize.basinhopping — SciPy v1.1.0 Reference Guide, Basin-hopping is a two-step method:

  • first, a random jump is done (take_step callback)
  • then a local minimum is found from that point using the specificed minimization method
  • finally, it's decided if the step is accepted (accept_test callback)

The constraints you've specified are for the minimization method, they don't affect the jump step. For the jump step, either adjust stepsize (the maximum displacement for the random jump), or define your own take_step.


"I thought the point of the constraint is that it would never try an x outside the constraint" -- constraints in mathematical problems, including a constrained optimization problem, don't work that way. They only specify what conditions the solution itself must satisfy. They don't limit what points can be used while obtaining that solution, it's completely up to the algorithm to choose these.

The approach to limit the area in which a numerical method searches is to tweak method parameters in some way specific to the nature of the function and the method, to "guide" the method into the right direction.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • I thought the point of the constraint is that it would never try an x outside the constraint. That's what I want and it's part of the minimization method (COBYLA). – andrea m. Sep 17 '18 at 01:26
  • to clarify. I expected COBYLA, the minimization method, to never try values that violate the constraint – andrea m. Sep 17 '18 at 02:54
  • 1
    @andy contraints don't work that way, see the update. Since the solution to that is function- and method-dependent, ask a separate question about that and make sure to say why you need that (my guess is your real function is probably undefined somewhere). – ivan_pozdeev Sep 17 '18 at 16:48
  • no the function is defined outside the range, it's a common problem in economics where you may have multiple solutions but only some solutions have "economic" interpretation (e.g. interest rates must be positive, wages, etc...). I don't know how COBYLA works and I trust you that an efficient algorithm may want to approach the problem by violating the constraints as well. – andrea m. Sep 17 '18 at 22:45