scipy.optimize.differential

您所在的位置:网站首页 evolution有几代 scipy.optimize.differential

scipy.optimize.differential

2023-12-16 11:55| 来源: 网络整理| 查看: 265

查找多元函数的全局最小值。

差分进化本质上是随机的(不使用梯度方法)来寻找最小值,可以搜索大范围的候选空间,但与传统的基于梯度的技术相比,通常需要更多的函数计算。

该算法归因于Storn和Price [1].

参数 func可调用

要最小化的目标函数。必须在表格中 f(x, *args) ,在哪里 x 是一维数组形式的参数,并且 args 是完全指定函数所需的任何附加固定参数的元组。

边界 :序列或 Bounds序列或

变量的界限。有两种方式可以指定边界:1. Bounds 班级。2. (min, max) 中每个元素的对 x 的优化参数的有限上下界。 func 。它被要求有 len(bounds) == len(x) 。 len(bounds) 用于确定 x 。

args元组,可选

完全指定目标函数所需的任何附加固定参数。

strategy字符串,可选

要使用的差异进化策略。应为以下之一:

“Best 1bin”

“Best 1exp”

“rand1exp”

“RANDOBEST 1EXP”

‘CURRENT TO BEST 1EXP’

“Best 2exp”

“rand2exp”

“RANDOBEST 1BIN”

‘当前到最佳1bin’

“Best 2bin”

“随机2bin”

“随机1bin”

默认值为‘Best 1bin’。

maxiter整型,可选

整个种群进化的最大世代数。函数求值的最大数量(无抛光)为: (maxiter + 1) * popsize * len(x)

popsize整型,可选

用于设置总人口规模的乘数。人口中有 popsize * len(x) 个人。如果通过 init 关键字。在使用时 init='sobol' 人口大小计算为2的下一个幂 popsize * len(x) 。

tol浮动,可选

收敛的相对容差,在以下情况下停止求解 np.std(pop) >> from scipy.optimize import rosen, differential_evolution >>> bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)] >>> result = differential_evolution(rosen, bounds) >>> result.x, result.fun (array([1., 1., 1., 1., 1.]), 1.9216496320061384e-19)

现在重复,但要并行化。

>>> bounds = [(0,2), (0, 2), (0, 2), (0, 2), (0, 2)] >>> result = differential_evolution(rosen, bounds, updating='deferred', ... workers=2) >>> result.x, result.fun (array([1., 1., 1., 1., 1.]), 1.9216496320061384e-19)

让我们试着做一个有约束的最小化

>>> from scipy.optimize import NonlinearConstraint, Bounds >>> def constr_f(x): ... return np.array(x[0] + x[1]) >>> >>> # the sum of x[0] and x[1] must be less than 1.9 >>> nlc = NonlinearConstraint(constr_f, -np.inf, 1.9) >>> # specify limits using a `Bounds` object. >>> bounds = Bounds([0., 0.], [2., 2.]) >>> result = differential_evolution(rosen, bounds, constraints=(nlc), ... seed=1) >>> result.x, result.fun (array([0.96633867, 0.93363577]), 0.0011361355854792312)

接下来,找出Ackley函数(https://en.wikipedia.org/wiki/Test_functions_for_optimization).的最小值

>>> from scipy.optimize import differential_evolution >>> import numpy as np >>> def ackley(x): ... arg1 = -0.2 * np.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2)) ... arg2 = 0.5 * (np.cos(2. * np.pi * x[0]) + np.cos(2. * np.pi * x[1])) ... return -20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e >>> bounds = [(-5, 5), (-5, 5)] >>> result = differential_evolution(ackley, bounds) >>> result.x, result.fun (array([ 0., 0.]), 4.4408920985006262e-16)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3