question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Callback is not called with local minima of initial guess in basinhopping algorithm

See original GitHub issue

My issue is about …

As far as I understand, callback function is called when a local minima is found. When I use basinhopping global minimization algorithm, I have observed that the callback function is not called when local minima is found for initial guess. I have tried to reproduce this issue with the following example:

from scipy.optimize import basinhopping
import numpy as np

def func(x):
    print("Objective function count: " + str(func.counter))
    func.counter += 1
    return np.random.rand(1)*1000

def callback_func(x, f, accepted):
    print("Callback count: " + str(callback_func.counter))
    callback_func.counter += 1

func.counter = 0
callback_func.counter = 0    
x0 = 2000

minimizer_kwargs = {"method": "COBYLA", "options":{"maxiter": 1}}
optimizer = basinhopping(func, x0, minimizer_kwargs=minimizer_kwargs, niter=2, callback=callback_func)

It produces following result:

Objective function count: 0
Objective function count: 1
Callback count: 0
Objective function count: 2
Callback count: 1

For the above mentioned example, both callback function and the objective function should have been called thrice but callback is called only twice.

Scipy/Numpy/Python version information:

1.2.1 1.16.4 sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
andyfaffcommented, Nov 1, 2020

@rgommers, @prince2110, I think I’ve got to the bottom of this.

  • the callback function is indeed called once per basinhopping iteration.
  • However, when the BasinHoppingRunner is constructed, there is an initial minimisation. This initial minimisation is not set to the callback function. As the documentation is written it probably should be, but that initial minimisation would not be after a basinhopping step had been taken. At the worst this would result in niter+1 callbacks for niter steps.

Sorry for closing the issue prematurely, @prince2110 is correct here.

0reactions
andyfaffcommented, Nov 2, 2020

I’ve already done one, #13029.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.optimize.basinhopping — SciPy v1.9.3 Manual
A callback function which will be called for all minima found. x and f are the coordinates and function value of the trial...
Read more >
scipy.optimize.basinhopping — SciPy v0.14.0 Reference Guide
A callback function which will be called for all minimum found. x and f are the coordinates and function value of the trial...
Read more >
Scipy BasinHopping not returning correct global minima
Depending on the start position x0, it returns different local minima - not the global one at 0. If we set x_0 =...
Read more >
https://subversion.xray.aps.anl.gov/pyGSAS/branch/...
basinhopping : The basinhopping global optimization algorithm """ from __future__ ... from a local minima if normal basin hopping # steps are not...
Read more >
statsmodels.base.optimizer — statsmodels
(The limited memory BFGS method does not store the full hessian but uses this ... function value) between local minima. stepsize : float...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found