scipy.optimize.differential_evolution 计算要最小化的函数多少次?
How many times does scipy.optimize.differential_evolution evaluate the function to be minimized?
我正在尝试应用 this answer to my code to display a progress bar for the scipy.optimize.differential_evolution 方法。
我以为differential_evolution
会计算func
(被调用的函数被最小化)popsize * maxiter
次,但显然不是这样。
下面的代码应该显示一个进度条,该进度条会增加到 100%
:
[####################] 100%
但实际上这会继续进行,因为 DEdist()
函数的计算次数比 popsize * maxiter
多得多(我将其用作 updt()
的 total
参数功能)。
如何计算 differential_evolution
执行的函数求值总数?这完全可以做到吗?
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:
"""
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},))
来自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)``
默认也polish=True
:
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.
所以你需要改变两件事:
1 此处使用正确的公式:
updt(popsize * (maxiter + 1) * len(model), info['Nfeval'] + 1)
2 传递 polish=False
参数:
result = DE(
DEdist, bounds, popsize=popsize, maxiter=maxiter, polish=False,
args=({'Nfeval': 0},))
之后你会看到进度条正好在达到 100% 时停止。
我正在尝试应用 this answer to my code to display a progress bar for the scipy.optimize.differential_evolution 方法。
我以为differential_evolution
会计算func
(被调用的函数被最小化)popsize * maxiter
次,但显然不是这样。
下面的代码应该显示一个进度条,该进度条会增加到 100%
:
[####################] 100%
但实际上这会继续进行,因为 DEdist()
函数的计算次数比 popsize * maxiter
多得多(我将其用作 updt()
的 total
参数功能)。
如何计算 differential_evolution
执行的函数求值总数?这完全可以做到吗?
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:
"""
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},))
来自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)``
默认也polish=True
:
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.
所以你需要改变两件事:
1 此处使用正确的公式:
updt(popsize * (maxiter + 1) * len(model), info['Nfeval'] + 1)
2 传递 polish=False
参数:
result = DE(
DEdist, bounds, popsize=popsize, maxiter=maxiter, polish=False,
args=({'Nfeval': 0},))
之后你会看到进度条正好在达到 100% 时停止。