I'm trying to apply this answer to my code to display a progress bar for the scipy.optimize.differential_evolution method.
I thought that differential_evolution
would evaluate func
(the function that is called to be minimized) popsize * maxiter
times, but apparently this is not the case.
The code below should display a progress bar that increases up to 100%
:
[####################] 100%
but in reality this keeps going as the DEdist()
function is evaluated a lot more times than popsize * maxiter
(which I use as the total
argument of the updt()
function).
How can I calculate the total number of function evaluations performed by the differential_evolution
? Can this be done at all?
from scipy.optimize import differential_evolution as DE
import sys
popsize, maxiter = 10, 50
def updt(total, progress, extra=""):
"""
Displays or updates a console progress bar.
Original source: https://stackoverflow.com/a/15860757/1391441
"""
barLength, status = 20, ""
progress = float(progress) / float(total)
if progress >= 1.:
progress, status = 1, "\r\n"
block = int(round(barLength * progress))
text = "\r[{}] {:.0f}% {}{}".format(
"#" * block + "-" * (barLength - block),
round(progress * 100, 0), extra, status)
sys.stdout.write(text)
sys.stdout.flush()
def DEdist(model, info):
updt(popsize * maxiter, info['Nfeval'] + 1)
info['Nfeval'] += 1
res = (1. - model[0])**2 + 100.0 * (model[1] - model[0]**2)**2 + \
(1. - model[1])**2 + 100.0 * (model[2] - model[1]**2)**2
return res
bounds = [[0., 10.], [0., 10.], [0., 10.], [0., 10.]]
result = DE(
DEdist, bounds, popsize=popsize, maxiter=maxiter,
args=({'Nfeval': 0},))