DEAP Example: One Max Problem
DEAP Example: One Max Problem
DEAP原文链接
目录
- Setting Things Up
- Creator
- Toolbox
- The Evaluation Function
- The Genetic Operators
- Evolving the Population
- Creating the Population
- Performing the Evolution
- 完整代码
本文是一个十分简单的入门任务。这个任务是求一个元素为0或1构成的数组的和的最大值。显然当元素全部为1的时候,数组的和有最大值,即为其长度。
Setting Things Up
首先,导入一些模块
import randomfrom deap import base
from deap import creator
from deap import tools
Creator
creator是一个类工厂,可以在运行是构建新类。第一个参数是新类名称,第二个参数是所继承的基类,之后的参数是新类的属性。
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
首先,定义一个FitnessMax类。它继承于deap.base模块的Fitness类。其还包含一个额外的属性weights。weights是一个元组(1.0,),意思是我们将最大化单一的目标值。
然后,定义一个Individual类。它继承list类并且使fitness这个属性等于之前我们定义的fitnessMax。要注意的是,所有在creator容器创建的类都可以直接调用。
Toolbox
现在,我们自定义一些类来创建个体和种群。
我们将使用到的,如个体、种群、函数、操作符、参数等等,都是在Toolbox里面存储。添加和删除分别为register()和unregister()。
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("attr_bool", random.randint, 0, 1)
# Structure initializers
toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attr_bool, 100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
第一句创建一个toolbox类。
然后注册一个生成函数toolbox.attr_bool()。该函数被调用的时候,会随机产生为0或1。接下来的两个函数就是个体和种群的实例。
新函数在toolbox中注册只将别名与已经存在的函数相关联,并冻结部分参数。例如attr_bool(),它与randint()关联并设定里面两个参数a=0和b=1,意思是返回a≤n≤ba≤n≤ba\le n \le b中的整数n,也就是0,1。
个体是由函数initRepeat()产生。它的第一个参数是一个容器类,在我们的例子中 Individual是我们在前一部分中定义的。这个容器会用方法attr_bool()填充。调用的时候,individual()方法将用attr_bool()初始化100次,然后产生一个个体。最后,population()用相同方法生成。
The Evaluation Function
评价函数。
def evalOneMax(individual):return sum(individual),
简单地求和最大值。
The Genetic Operators
遗传算子。
在DEAP有两种方法使用算子。我们可以简单地调用tools模块中的函数,又或者,在toolbox里面注册。最方便的方法是注册,因为其可以允许我们容易地切换算子。
注册如下
toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
评价函数将采用别名来调用。稍后我们将需要在种群中每一个个体应用函数。突变要把一个参数固定下来。
Evolving the Population
进化。
选择好算子后,我们将定义一个进化算法来解决我们的问题。
Creating the Population
首先,我们实例化我们的种群。
def main():pop = toolbox.population(n=300)
pop是一个有300个体的list。由于我们没有固定种群的数量,所有可以自由创建任意数量。
下一步,评价新种群
# Evaluate the entire populationfitnesses = list(map(toolbox.evaluate, pop))for ind, fit in zip(pop, fitnesses):ind.fitness.values = fit
利用map()计算每个个体的评价函数。然后分配到各自的适合度中。
定义一些常量。
# CXPB is the probability with which two individuals# are crossed## MUTPB is the probability for mutating an individualCXPB, MUTPB = 0.5, 0.2
Performing the Evolution
执行进化。
要做的是获得个体的适应度。
# Extracting all the fitnesses offits = [ind.fitness.values[0] for ind in pop]
进化我们的种群直到其中一个个体达到100或者世代数到1000
# Variable keeping track of the number of generationsg = 0# Begin the evolutionwhile max(fits) < 100 and g < 1000:# A new generationg = g + 1print("-- Generation %i --" % g)
进化是由选择、交配、变异个体来进行的。
在这个例子中,第一步是选择下一代。
# Select the next generation individualsoffspring = toolbox.select(pop, len(pop))# Clone the selected individualsoffspring = list(map(toolbox.clone, offspring))
这创建了一组后代。toolbox.clone()方法确保我们将使用一个完全独立的实例引用。这是十分重要的,因为算子会直接修改提供的对象。
然后,根据概率CXPB和MUTPB来交配和变异产生的后代。
# Apply crossover and mutation on the offspringfor child1, child2 in zip(offspring[::2], offspring[1::2]):if random.random() < CXPB:toolbox.mate(child1, child2)del child1.fitness.valuesdel child2.fitness.valuesfor mutant in offspring:if random.random() < MUTPB:toolbox.mutate(mutant)del mutant.fitness.values
交配和变异的算子通常是2个或1个个体作为一个输入,返回2个或1个修改过的个体。除修改过的个体,我们不需要重新计算结果。
因为在上一步,一些后代改变了,我们需要重新评价他们的适应度。只需要评价那些适应度无效的后代。
# Evaluate the individuals with an invalid fitnessinvalid_ind = [ind for ind in offspring if not ind.fitness.valid]fitnesses = map(toolbox.evaluate, invalid_ind)for ind, fit in zip(invalid_ind, fitnesses):ind.fitness.values = fit
最后,使种群更新为我们计算出来的后代。
pop[:] = offspring
检查进化的性能,我们打印所有个体的适应度的最小,最大,平均值以及他们的标准差。
# Gather all the fitnesses in one list and print the statsfits = [ind.fitness.values[0] for ind in pop]length = len(pop)mean = sum(fits) / lengthsum2 = sum(x*x for x in fits)std = abs(sum2 / length - mean**2)**0.5print(" Min %s" % min(fits))print(" Max %s" % max(fits))print(" Avg %s" % mean)print(" Std %s" % std)
最终结果将是有一个个体全为1。
完整代码
# -*- coding: utf-8 -*-# This file is part of DEAP.
#
# DEAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DEAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.# example which maximizes the sum of a list of integers
# each of which can be 0 or 1import randomfrom deap import base
from deap import creator
from deap import toolscreator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)toolbox = base.Toolbox()# Attribute generator
# define 'attr_bool' to be an attribute ('gene')
# which corresponds to integers sampled uniformly
# from the range [0,1] (i.e. 0 or 1 with equal
# probability)
toolbox.register("attr_bool", random.randint, 0, 1)# Structure initializers
# define 'individual' to be an individual
# consisting of 100 'attr_bool' elements ('genes')
toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attr_bool, 100)# define the population to be a list of individuals
toolbox.register("population", tools.initRepeat, list, toolbox.individual)# the goal ('fitness') function to be maximized
def evalOneMax(individual):return sum(individual),#----------
# Operator registration
#----------
# register the goal / fitness function
toolbox.register("evaluate", evalOneMax)# register the crossover operator
toolbox.register("mate", tools.cxTwoPoint)# register a mutation operator with a probability to
# flip each attribute/gene of 0.05
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)# operator for selecting individuals for breeding the next
# generation: each individual of the current generation
# is replaced by the 'fittest' (best) of three individuals
# drawn randomly from the current generation.
toolbox.register("select", tools.selTournament, tournsize=3)#----------def main():random.seed(64)# create an initial population of 300 individuals (where# each individual is a list of integers)pop = toolbox.population(n=300)# CXPB is the probability with which two individuals# are crossed## MUTPB is the probability for mutating an individualCXPB, MUTPB = 0.5, 0.2print("Start of evolution")# Evaluate the entire populationfitnesses = list(map(toolbox.evaluate, pop))for ind, fit in zip(pop, fitnesses):ind.fitness.values = fitprint(" Evaluated %i individuals" % len(pop))# Extracting all the fitnesses offits = [ind.fitness.values[0] for ind in pop]# Variable keeping track of the number of generationsg = 0# Begin the evolutionwhile max(fits) < 100 and g < 1000:# A new generationg = g + 1print("-- Generation %i --" % g)# Select the next generation individualsoffspring = toolbox.select(pop, len(pop))# Clone the selected individualsoffspring = list(map(toolbox.clone, offspring))# Apply crossover and mutation on the offspringfor child1, child2 in zip(offspring[::2], offspring[1::2]):# cross two individuals with probability CXPBif random.random() < CXPB:toolbox.mate(child1, child2)# fitness values of the children# must be recalculated laterdel child1.fitness.valuesdel child2.fitness.valuesfor mutant in offspring:# mutate an individual with probability MUTPBif random.random() < MUTPB: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]fitnesses = map(toolbox.evaluate, invalid_ind)for ind, fit in zip(invalid_ind, fitnesses):ind.fitness.values = fitprint(" Evaluated %i individuals" % len(invalid_ind))# The population is entirely replaced by the offspringpop[:] = offspring# Gather all the fitnesses in one list and print the statsfits = [ind.fitness.values[0] for ind in pop]length = len(pop)mean = sum(fits) / lengthsum2 = sum([x*x for x in fits])std = abs(sum2 / length - mean**2)**0.5print(" Min %s" % min(fits))print(" Max %s" % max(fits))print(" Avg %s" % mean)print(" Std %s" % std)print("-- End of (successful) evolution --")best_ind = tools.selBest(pop, 1)[0]print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))if __name__ == "__main__":main()
DEAP Example: One Max Problem相关推荐
- 清北学堂 2017-10-07
********今天得了在清北的最高分,有点开心wwwww,mjy爱您! 树 [问题背景] zhx 是二叉查找树大师. [问题描述] 二叉查找树是一种特殊的二叉树(每个节点最多只有两个儿子的树 ...
- HDU 2993 MAX Average Problem(斜率优化DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...
- hdu 2993 MAX Average Problem 斜率优化DP
详见,算法合集之<浅谈数形结合思想在信息学竞赛中的应用>. #include<cstdio> #include<cstring> #include<algor ...
- 卡丹尔算法(max subarray problem)
问题介绍 Say you have an array for which the ith element is the price of a given stock on day i. If you ...
- 【斜率优化】HDU-2993——MAX Average Problem
前言 大约一年前,我在机房学"斜率优化" 一年后的今天,我在机房学"斜率优化" ... ... 题目 Consider a simple sequence wh ...
- 【遗传规划/计算智能】 彻底学会 DEAP 框架,从零搭建 GP
1. 应用方向 DEAP是一个新颖的进化计算框架,用于快速原型设计和测试.它旨在使算法清楚和数据结构透明. 它可以在并行机制之间完美协调,例如多处理和SCOOP(简单并发性面向对象程序设计). DEA ...
- 遗传算法(Python) #5 用DEAP框架解决OneMax问题
遗传算法(Python) #5 用DEAP框架解决OneMax问题 遗传算法系列的第三期介绍了如何不用任何框架从零开始解决OneMax问题,第四期介绍了DEAP框架的基本用法.若读者对下文中定义或术语 ...
- ADPRL - 近似动态规划和强化学习 - Note 3 - Stochastic Infinite Horizon Problem
Stochastic Infinite Horizon Problem 3.Stochastic Infinite Horizon Problem 定义3.1 无限范围的马尔可夫决策过程 (Marko ...
- POJ 1207 The 3n + 1 problem
题目链接:http://poj.org/problem?id=1207 题目大意:给你一个数x,规定一个函数F(x),如果x为1则F(x)==1,否则如果x是偶数,F(x)==F(x/2),x为奇数F ...
最新文章
- 三大国有银行同时入股 AI 公司第四范式
- signature=ae032dcfdc89568ef19ec0c956de36e7,来用百度密语吧!!!
- 常见的web应用服务器
- 图的长宽_华为P50 Pro渲染图曝光:单挖孔屏+超高屏占比
- 1970“变种”bug连WiFi热点iOS设备会变砖?
- 基于DirectShow的流媒体解码和回放
- c语言输出教学日历表 节假日突出,[蓝桥杯][算法提高VIP]任意年月日历输出 (C语言代码)...
- IDL | 实验四 IDL输入输出
- CSS外边距合并(塌陷/margin越界)
- Django使用orm创建数据表字段常用命令
- android 命名空间解析,Android Bluetooth、Android AdapterView等命名空间-Android中文API文档...
- educoder实训平台linux,educoder平台hadoop开发环境搭建[技术学习]
- 抖音检测注入框架分析
- 主引导记录mbr介绍
- 苹果8a1660是什么版本_苹果a1780是什么版本
- element-ui的文本框 input加㎡平方米符号
- P2615 [NOIP2015 提高组] 神奇的幻方
- PAMTRI:用于车辆重新识别的姿势感知多任务学习
- STM32单片机RGB红蓝调光植物补光系统红光蓝光PWM调色调节亮度
- linux amd显卡双屏,显示器的合理利用 ati显卡双屏详细设置【图文】