【发布时间】:2019-10-04 03:13:56
【问题描述】:
我正在尝试将this answer 应用于我的代码以显示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: 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},))
【问题讨论】:
标签: python scipy output mathematical-optimization