对新网站做seo大概需要多久,线上推广网络公司,wordpress 文章 字体,贵阳网站推广本节将将在初始化的基础上#xff0c;进一步说明操作与算法。
1、Using the Toolbox
toolbox(base.Toolbox())是包含所有进化操作的工具箱#xff0c;从目标初始化到适应度计算。它允许在每个算法中实现简单的构造。toolbox基本上由两种方法组成#xff0c;register()和un…本节将将在初始化的基础上进一步说明操作与算法。
1、Using the Toolbox
toolbox(base.Toolbox())是包含所有进化操作的工具箱从目标初始化到适应度计算。它允许在每个算法中实现简单的构造。toolbox基本上由两种方法组成register()和unregister()这两种函数是用来添加和移除工具的。
2、Using the Tools
需要区别Toolbox和Tools前者包含后者关系Tools更加纯粹的指向进化工具如Initialization、Crossover、Mutation、Selection、Migration等等具体查看官方教程。
fitnesses list(map(toolbox.evaluate, pop))
for ind, fit in zip(pop, fitnesses):ind.fitness.values fittoolbox.register(population, tools.initRepeat,list, toolbox.individual)
pop toolbox.population(n 30)
NGEN 100 #ITERATION NUMBER
CXPB 0.1 #CROSSOVER RATE
MUPB 0.1 #MUTATION RATE
for i in range(NGEN):#selectionoffspring toolbox.select(pop, len(pop)) #select len(pop) individuals from pop#clone the selected individualsoffspring list(map(toolbox.clone, offspring))#crossoverfor child1, child2 in zip(offspring[::2], offspring[1::2]):if random.random() CXPB:toolbox.mate(child1, child2)del child1.fitness.valuesdel child2.fitness.values#mutationfor mutant in offspring:if random.random() MUPB:toolbox.mutate(mutant)del mutant.fitness.values#evaluate the individuals with an invalid fitnessinvalid_ind [ind for ind in offspring if not ind.fitness.valid]fitness toolbox.map(toolbox.evaluate, invalid_ind)for ind, fit in zip(invalid_ind, fitness):ind.fitness.values fit#the population is entirely replaced by offspringpop[:] offspring3、Tool Decoration
工具装饰是一个很强力的特征它可以帮助你在演化过程中去精确地对算法和操作进行控制。一个装饰是指将函数进行包装这叫做在真实的函数被调用前后做一些初始化和终止工作。例如在一个有限的区域内一种应用装饰符的方法是在变异和交叉的过程中保证个体不会越界。下面的定义就是检查是否有个体越界装饰符是通过三个函数定义的为了接收最小值和最大值。当变异或者交叉被调用时边界将会检查它们的运算结果。
def checkBounds(min, max):def decorator(func):def wrapper(*args, **kargs):offspring func(*args, **kargs)for child in offspring:for i in xrange(len(child)):if child[i] max:child[i] maxelif child[i] min:child[i] minreturn offspringreturn wrapperreturn decoratortoolbox.register(mate, tools.cxBlend, alpha0.2)
toolbox.register(mutate, tools.mutGaussian, mu0, sigma2)toolbox.decorate(mate, checkBounds(MIN, MAX))
toolbox.decorate(mutate, checkBounds(MIN, MAX))4、Variations
变化允许建立简单的算法使用预定义好的小模块。为了使用变化toolbox一定会建立一个包含所需运算符的集合。例如在最近展示的完整算法案例中交叉和变异运算在varAnd()中被重新整合。这个函数要求toolbox包含mate()和mutate()函数这种变化可以用来简化算法就像下面这样
from deap import algorithmsfor g in range(NGEN):# Select and clone the next generation individualsoffspring map(toolbox.clone, toolbox.select(pop, len(pop)))# Apply crossover and mutation on the offspringoffspring algorithms.varAnd(offspring, toolbox, CXPB, MUPB)# Evaluate the individuals with an invalid fitnessinvalid_ind [ind for ind in offspring if not ind.fitness.valid]fitnesses toolbox.map(toolbox.evaluate, invalid_ind)for ind, fit in zip(invalid_ind, fitnesses):ind.fitness.values fit# The population is entirely replaced by the offspringpop[:] offspring穿插讲解Variations
Variations变体是算法的较小部分可以单独用于构建更复杂的算法。
主要包含varAnd、varOr
algorithms.varAnd(population, toolbox, cxpb, mutpb)---函数的参数被写死了
进化算法的一部分只应用变异部分交叉和变异。修改后的个体的适应度无效。个体被克隆因此返回的种群独立于输入种群。
Parameters: population – A list of individuals to vary.toolbox – A Toolbox that contains the evolution operators.cxpb – The probability of mating two individuals.mutpb – The probability of mutating an individual.Returns: A list of varied individuals that are independent of their parents. 这种变异被命名为And因为它需要在个体上同时应用交叉和突变。注意这两种算子都不是系统应用的产生的个体可以根据给定的概率从仅交叉、仅突变、交叉和突变以及繁殖中生成。两种概率都应在[0,1]中。 algorithms.varOr(population, toolbox, lambda_, cxpb, mutpb)
Parameters: population – A list of individuals to vary.toolbox – A Toolbox that contains the evolution operators.lambda_ – The number of children to producecxpb – The probability of mating two individuals.mutpb – The probability of mutating an individual.Returns: The final population.
变化如下。在每次lambda_迭代中它从三个操作中选择一个交叉、突变或繁殖。在交叉的情况下从亲本群体Pp中随机选择两个个体使用toolbox.clone方法克隆这些个体然后使用toolbox.mate方法交配。只有第一个孩子被附加到后代群体Po第二个孩子被丢弃。在突变的情况下从Pp中随机选择一个个体然后使用toolbox.mutate方法对其进行克隆和突变。产生的突变体附加在Po上。在繁殖的情况下从Pp中随机选择一个个体克隆并附加到Po中。
这种变异被命名为Or因为后代永远不会同时来自交叉和突变操作。两种概率之和应为[0,1]再现概率为1-cxpb-mutpb。 5、Algorithms
有许多种可以在algorithm模块中使用的算法它们十分简单并且可以反映演化算法的基本类型。这些算法使用Toolbox作为定义好的内容。一旦toolbox被设定完成它们将会去调用这些算法。简单的演化算法需要五个参数种群、toolbox、交叉率、变异率和种群迭代次数。
举例如下
from deap import algorithmsalgorithms.eaSimple(pop, toolbox, cxpb0.5, mutpb0.2, ngen50)算法库中列举的算法包含
1、deap.algorithms.eaSimple(population, toolbox, cxpb, mutpb, ngen[, stats, halloffame, verbose]) 2、deap.algorithms.eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen[, stats, halloffame, verbose])
............///具体的内容可以参考官方文档不做展开了。