首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DEAP可以用于多模式优化吗?

DEAP可以用于多模式优化吗?
EN

Stack Overflow用户
提问于 2019-10-28 22:01:28
回答 1查看 289关注 0票数 1

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

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

EN

回答 1

Stack Overflow用户

发布于 2019-11-12 17:07:33

DEAP没有内置的多模式优化支持。然而,它可以用来解决这样的问题,只需指定正确的适应度函数。

代码语言:javascript
复制
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')

这样,种群就可以分布在小环境中,并且可以识别每个局部最大值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58592325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档