使用workers 关键字的能力可以在每次迭代中保持试验总体,以接受类似于地图的callable,该callable 被发送整个试验总体并预期返回一个包含评估函数值的数组对于整个试验人群:
from scipy.optimize import rosen, differential_evolution
bounds=[(0, 10), (0, 10)]
# pop will retain the trial population at each iteration of the minimisation
pop = []
energies = []
def maplike_fun(func, x):
# x.shape == (S, N), where S is the size of the population and N
# is the number of parameters. The rosen function is vectorised,
# so calling rosen with x.T will return an array of shape (S,)
# you could also do:
# v = np.array(list(map(func, x)))
pop.append(np.copy(x))
e = func(x.T)
energies.append(e)
return e
res = differential_evolution(rosen, bounds, workers=maplike_fun, polish=False, updating='deferred')
# the initial evaluation of the population is not counted in the number
# of iterations, so pop[0] is the initial population.
assert res.nit == len(pop) - 1
要在每次迭代中获得实际总体,您需要遍历试验总体列表并连续更新pop[0]:
pop_up = [pop[0]]
energies_up = [energies[0]]
for i in range(1, len(pop)):
new_energies = np.copy(energies_up[-1])
new_pop = np.copy(pop_up[-1])
pos = energies[i] < new_energies
new_energies[pos] = energies[i][pos]
new_pop[pos] = pop[i][pos]
pop_up.append(new_pop)
energies_up.append(new_energies)
然后由pop_up 描述实际的种群进化。
从 scipy 1.9 开始,还有一个 vectorized 关键字,它将在每次迭代时将整个试验群体发送到目标函数。