我正在尝试将这个答案应用到我的代码中,以显示演化方法的进度条。
我认为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: 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},))发布于 2019-05-16 19:47:07
来自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%时停止。
https://stackoverflow.com/questions/56175378
复制相似问题