0

差分进化是基于种群的算法。然而,scipy.optimize.differential_evolution返回结果OptimizeResult只给出了人口中最优秀的精英,而忽略了这一代人的其余部分。

有人如何保存有关最后一个人口的信息?

4

2 回答 2

1

我发现返回最后一个种群的一种方法是下载源代码并直接使用该类DifferentialEvolutionSolver

solver = DifferentialEvolutionSolver(fun, bounds, arg**)
solver.init_population_random()
solver.__next__() # for each generation

last_pop = solver.population
las_pop_costs = solver.population_energies
于 2021-11-16T10:28:20.427 回答
0

可以使用关键字的能力在每次迭代中保留试验总体,workers以接受一个类似地图的方式,该地图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关键字,它将在每次迭代时将整个试验群体发送到目标函数。

于 2022-02-02T03:40:00.217 回答