2

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},))
Gabriel
  • 40,504
  • 73
  • 230
  • 404

1 Answers1

1

From help(scipy.optimize.differential_evolution):

maxiter : int, optional
    The maximum number of generations over which the entire population is
    evolved. The maximum number of function evaluations (with no polishing)
    is: ``(maxiter + 1) * popsize * len(x)``

Also polish=True by default:

polish : bool, optional
    If True (default), then `scipy.optimize.minimize` with the `L-BFGS-B`
    method is used to polish the best population member at the end, which
    can improve the minimization slightly.

So you need to change two things:

1 Use correct folmula here:

updt(popsize * (maxiter + 1) * len(model), info['Nfeval'] + 1)

2 Pass polish=False argument:

result = DE(
    DEdist, bounds, popsize=popsize, maxiter=maxiter, polish=False,
    args=({'Nfeval': 0},))

After that you will see progress bar stopping exactly when it reaches 100%.

sanyassh
  • 8,100
  • 13
  • 36
  • 70