【问题标题】:Can DEAP be used for multimodal optimization?DEAP 可以用于多模式优化吗?
【发布时间】:2020-02-23 19:14:25
【问题描述】:

通过浏览 DEAP 的 documentation 和示例(hereherehere),我发现了一些使用 DEAP 进行多目标优化的实例,但没有关于多模态优化的内容。

是否可以使用 DEAP 框架进行进化多模式优化,类似于this article 中描述的内容?有这样做的例子吗?

【问题讨论】:

  • 是和不是。遗传算法具有逃避局部解决方案(突变)的设施。但由于它们只是启发式方法,它们通常不会找到全局最优解。当然,在许多实际情况下,一个好的解决方案可能是完全可以接受的。
  • @ErwinKalvelagen 感谢您抽出宝贵时间发表评论。但是,我不明白这与我的问题有什么关系。通过使用利基/拥挤策略,遗传算法可用于多模式优化。我想知道是否有使用 DEAP 执行此操作的示例。

标签: python-3.x mathematical-optimization genetic-algorithm evolutionary-algorithm deap


【解决方案1】:

DEAP 没有对多模式优化的内置支持。但是,它可以用来解决此类问题,只需指定正确的适应度函数即可。

import numpy as np
import random
import math
import matplotlib.pyplot as plt
from scipy.spatial import distance_matrix
from deap import base, tools, creator, algorithms

npop = 1000
sigma = 0.3
alpha = 2

# Here is a function with many local maxima
def objective(x, y):
    return np.exp(-9*abs(x*y)) * np.sin(3*math.pi*x)**2 * np.sin(3*math.pi*y)**2

def fitness(individual):
    return objective(individual[0], individual[1]),

xrange = np.arange(0., 1., 0.01)
X, Y = np.meshgrid(xrange, xrange)
zs = np.array(objective(np.ravel(X), np.ravel(Y)))
Z = zs.reshape(X.shape)

# Setup the DEAP toolbox
toolbox = base.Toolbox()

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox.register("individual", tools.initRepeat, creator.Individual, random.random, n=2)

toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register('mutate', tools.mutPolynomialBounded, eta=.6, low=[0,0], up=[1,1], indpb=0.1)
toolbox.register('mate', tools.cxUniform, indpb=0.5)
toolbox.register('select', tools.selBest)

# Register a shared fitness function
def sharing(distance, sigma, alpha):
    res = 0
    if distance<sigma:
        res += 1 - (distance/sigma)**alpha
    return res

def shared_fitness(individual, population, sigma, alpha):
    num = fitness(individual)[0]

    dists = distance_matrix([individual], population)[0]
    tmp = [sharing(d, sigma, alpha) for d in dists]
    den = sum(tmp)

    return num/den,

pop = toolbox.population(n=npop)

toolbox.register('evaluate', shared_fitness, population=pop, sigma=sigma, alpha=alpha)

# Run the optimization algorithm
mu = int(len(pop)*0.5)
lambda_ = int(len(pop)*0.5)
cxpb = 0.4
mutpb = 0.5
ngen = 10

pop, logbook = algorithms.eaMuPlusLambda(pop, toolbox, mu, lambda_, cxpb, mutpb, ngen)

fig = plt.figure()
ax = fig.add_subplot(111)
sc = ax.scatter(X, Y, c=Z)
plt.colorbar(sc) 
ax.scatter(*zip(*pop), color='red')

这样,种群分布在生态位内,并且可以识别每个局部最大值。

【讨论】:

  • 按广告宣传,谢谢。次要建议:在 shared_fitness() 函数中,您可能会收到除零 NumPy 警告。考虑诱捕它们。
  • @LaryxDecidua Trapping?
  • if abs(den) &lt; 1.0e-7: ...意义上的“trapping”,即检测分母何时接近0.0,然后处理。 NumPy 发出警告并继续。标准 Python 会引发异常。
  • @LaryxDecidua 一个点到自己的距离为0,sharing(0,...) 等于1。所以分母是 1 加上一些正值的总和。这意味着分母应始终大于 1,因此绝不会引发除以零错误。
  • @LaryxDecidua 感谢您指出这一点。它对算法的内部工作提出了有趣的问题。
猜你喜欢
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
相关资源
最近更新 更多