Overview 程序概览

官方文档:http://deap.readthedocs.io/en/master/index.html
1. Types : 选择你要解决的问题类型,确定要求解的问题个数,最大值还是最小值
2. Initialization : 初始化基因编码位数,初始值,等基本信息
3. Operators : 操作,设计evaluate函数,在工具箱中注册参数信息:交叉,变异,保留个体,评价函数
4. Algorithm : 设计main函数,确定参数并运行得到结果

Types

# Types
from deap import base, creatorcreator.create("FitnessMin", base.Fitness, weights=(-1.0,))  # weights 1.0, 求最大值,-1.0 求最小值
# (1.0,-1.0,)求第一个参数的最大值,求第二个参数的最小值
creator.create("Individual", list, fitness=creator.FitnessMin)

Initialization

import random
from deap import toolsIND_SIZE = 10  # 种群数toolbox = base.Toolbox()
toolbox.register("attribute", random.random)
# 调用randon.random为每一个基因编码编码创建 随机初始值 也就是范围[0,1]
toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attribute, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

Operators

# Operators
# difine evaluate function
# Note that a comma is a must
def evaluate(individual):return sum(individual),# use tools in deap to creat our application
toolbox.register("mate", tools.cxTwoPoint) # mate:交叉
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1) # mutate : 变异
toolbox.register("select", tools.selTournament, tournsize=3) # select : 选择保留的最佳个体
toolbox.register("evaluate", evaluate)  # commit our evaluate

高斯变异:

这种变异的方法就是,产生一个服从高斯分布的随机数,取代原先基因中的实数数值。这个算法产生的随机数,数学期望当为当前基因的实数数值。
一个模拟产生的算法是,产生6个服从U(0,1)的随机数,以他们的数学期望作为高斯分布随机数的近似。

mutate方法

  • 这个函数适用于输入个体的平均值和标准差的高斯突变

  • mu:python中基于平均值的高斯变异

  • sigma:python中基于标准差的高斯变异

  • indpb:每个属性的独立变异概率

mate : 交叉

select : 选择保留的最佳个体

evaluate : 选择评价函数,要注意返回值的地方最后面要多加一个逗号

Algorithms 计算程序

也就是设计主程序的地方,按照官网给的模式,我们要早此处设计其他参数,并设计迭代和取值的代码部分,并返回我们所需要的值.


# Algorithms
def main():# create an initial population of 300 individuals (where# each individual is a list of integers)pop = toolbox.population(n=50)CXPB, MUTPB, NGEN = 0.5, 0.2, 40'''# CXPB  is the probability with which two individuals#       are crossed## MUTPB is the probability for mutating an individual## NGEN  is the number of generations for which the#       evolution runs'''# Evaluate the entire populationfitnesses = map(toolbox.evaluate, pop)for ind, fit in zip(pop, fitnesses):ind.fitness.values = fitprint("  Evaluated %i individuals" % len(pop))  # 这时候,pop的长度还是300呢print("-- Iterative %i times --" % NGEN)for g in range(NGEN):if g % 10 == 0:print("-- Generation %i --" % g)# Select the next generation individualsoffspring = toolbox.select(pop, len(pop))# Clone the selected individualsoffspring = list(map(toolbox.clone, offspring))# Change map to list,The documentation on the official website is wrong# 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# 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# The population is entirely replaced by the offspringpop[:] = offspringprint("-- End of (successful) evolution --")best_ind = tools.selBest(pop, 1)[0]return best_ind, best_ind.fitness.values  # return the result:Last individual,The Return of Evaluate function

要注意的地方就是,官网中给出的Overview代码中有一行代码是错误的,需要把一个数据类型(map)转换为list.

输出结果

  Evaluated 50 individuals
-- Iterative 40 times --
-- Generation 0 --
-- Generation 10 --
-- Generation 20 --
-- Generation 30 --
-- End of (successful) evolution --
best_ind [-2.402824207878805, -1.5920248739487302, -4.397332290574777, -0.7564815676249151, -3.3478264358788814, -5.900475519316307, -7.739284213710048, -4.469259215914226, 0.35793917907272843, -2.8594709616875256]
best_ind.fitness.values (-33.10704010746149,)
  • best_ind : 最佳个体
  • best_ind.fitness.values : 最佳个体在经过evaluate之后的输出
#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:fonttian
@file: Overview.py
@time: 2017/10/15
"""# Types
from deap import base, creatorcreator.create("FitnessMin", base.Fitness, weights=(-1.0,))
# weights 1.0, 求最大值,-1.0 求最小值
# (1.0,-1.0,)求第一个参数的最大值,求第二个参数的最小值
creator.create("Individual", list, fitness=creator.FitnessMin)# Initialization
import random
from deap import toolsIND_SIZE = 10  # 种群数toolbox = base.Toolbox()
toolbox.register("attribute", random.random)
# 调用randon.random为每一个基因编码编码创建 随机初始值 也就是范围[0,1]
toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attribute, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)# Operators
# difine evaluate function
# Note that a comma is a must
def evaluate(individual):return sum(individual),# use tools in deap to creat our application
toolbox.register("mate", tools.cxTwoPoint) # mate:交叉
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1) # mutate : 变异
toolbox.register("select", tools.selTournament, tournsize=3) # select : 选择保留的最佳个体
toolbox.register("evaluate", evaluate)  # commit our evaluate# Algorithms
def main():# create an initial population of 300 individuals (where# each individual is a list of integers)pop = toolbox.population(n=50)CXPB, MUTPB, NGEN = 0.5, 0.2, 40'''# CXPB  is the probability with which two individuals#       are crossed## MUTPB is the probability for mutating an individual## NGEN  is the number of generations for which the#       evolution runs'''# Evaluate the entire populationfitnesses = map(toolbox.evaluate, pop)for ind, fit in zip(pop, fitnesses):ind.fitness.values = fitprint("  Evaluated %i individuals" % len(pop))  # 这时候,pop的长度还是300呢print("-- Iterative %i times --" % NGEN)for g in range(NGEN):if g % 10 == 0:print("-- Generation %i --" % g)# Select the next generation individualsoffspring = toolbox.select(pop, len(pop))# Clone the selected individualsoffspring = list(map(toolbox.clone, offspring))# Change map to list,The documentation on the official website is wrong# 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# 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# The population is entirely replaced by the offspringpop[:] = offspringprint("-- End of (successful) evolution --")best_ind = tools.selBest(pop, 1)[0]return best_ind, best_ind.fitness.values  # return the result:Last individual,The Return of Evaluate functionif __name__ == "__main__":# t1 = time.clock()best_ind, best_ind.fitness.values = main()# print(pop, best_ind, best_ind.fitness.values)# print("pop",pop)print("best_ind",best_ind)print("best_ind.fitness.values",best_ind.fitness.values)# t2 = time.clock()# print(t2-t1)

Deap: python中的遗传算法工具箱相关推荐

  1. python遗传算法工具包_Deap: python中的遗传算法工具箱

    Overview 程序概览 官方文档:http://deap.readthedocs.io/en/master/index.html 1. Types : 选择你要解决的问题类型,确定要求解的问题个数 ...

  2. Python遗传算法工具箱的使用(一)求解带约束的单目标优化

    加了个小目录~方便定位查看~ 前言 正文 一. 基础术语: 二. 遗传算法基本算子: 三.完整实现遗传算法: 四.后记: 前言 网上有很多博客讲解遗传算法,但是大都只是"点到即止" ...

  3. matlab bs2rv.m,matlab遗传算法工具箱中的例子不能用?

    matlab遗传算法工具箱中的例子不能用,我也发现了,求助 figure(1); fplot('variable.*sin(10*pi*variable)+2.0',[-1,2]);   %画出函数曲 ...

  4. python中心性评价_centrality 计算复杂网络中的节点或边 数中心性,基于python的 工具箱 matlab 238万源代码下载- www.pudn.com...

    文件名称: centrality下载 收藏√  [ 5  4  3  2  1 ] 开发工具: Python 文件大小: 101 KB 上传时间: 2014-03-13 下载次数: 4 详细说明:计算 ...

  5. ArcGIS中ArcMap通过Python程序脚本新建工具箱与自定义工具的方法

      本文介绍如何在ArcMap中,通过已有的Python脚本程序,建立新的工具箱并在其中设置自定义工具的方法.通过本文介绍的操作,我们便可以实现将自己的Python代码封装,并像其他ArcGIS已有工 ...

  6. 遗传算法加入约束条件matlab,使用matlab遗传算法工具箱如何加入目标函数中变量的约束条件啊,可否在M文件中加?...

    1.首先打开2113matlab软件,在"APP(应用)"5261选项卡4102中选择"Optimization(优化)1653"工具箱版.2.在优化工具箱中选 ...

  7. matlab genfunction,Keras / Python相当于nn工具箱中的Matlab的genFunction

    我编写了一个带有3个隐藏层的神经网络(使用Python中的Keras模块) . 网的代码如下: # Create the model np.random.seed(1) # Just for repr ...

  8. MATLAB遗传算法工具箱Genetic Algorithm Toolbox的下载和安装

    2019独角兽企业重金招聘Python工程师标准>>> 1. 下载Genetic Algorithm Toolbox 打开网页http://codem.group.shef.ac.u ...

  9. MATLAB遗传算法工具箱的使用及实例(线性规划)

    一.引言 在使用遗传算法(Genetic Algorithm,GA)之前,你得了解遗传算法是干什么的.遗传算法一般用于求解优化问题.遗传算法最早是由美国的 John holland于20世纪70年代提 ...

最新文章

  1. 滴滴员工抱怨女朋友要求自己上进!工资必须比她高一半!决定分手却不直说!对女朋友冷暴力等她自己走!...
  2. Linq to xml 示例分析
  3. 阿里开发者们的第15个感悟:做一款优秀大数据引擎,要找准重点解决的业务场景...
  4. python文件操作:文件指针移动、修改
  5. Android 开发实战
  6. 無題(後改為總有那麼一句話)
  7. 【历史上的今天】8 月 24 日:Windows 95问世;乔布斯辞任苹果 CEO 库克上台
  8. 网易云音乐多账号全自动升级,彻底解放双手
  9. 域名系统几类服务器,域名服务器可分为什么类型
  10. 面试官问我:多个 List 如何取交集、并集、去重并集、差集?
  11. 基本算法总结,力扣题目整理
  12. python统计英文文章中单词出现的次数
  13. centos光盘修复引导_CentOs7 修复 引导启动
  14. 对称加密算法AES联合设备IMEI码设计的加密机制
  15. 计算机类普刊有哪些,基础数学类的容易发表的普刊有哪些
  16. 快手,存在的优势都有哪些???
  17. Cris 玩转大数据系列之任务流神器 Azkaban
  18. 小米10至尊纪念版参数配置
  19. 开发Unity3D空战类插件 战机飞行模拟模板
  20. Apache+Tomcat 动静分离

热门文章

  1. 提醒一下技术人,你是不是陷入局部最优了
  2. Nginx之windows下搭建
  3. 关于Spring Cloud Zuul网管上传文件乱码问题
  4. containerd容器命令
  5. js文件中定义全局配置文件
  6. Viewpager中改变PagerTabStrip的颜色(背景色,指示条颜色,文字颜色)
  7. UidGenerator
  8. 解决eclipse显示jar源代码中文乱码问题
  9. MySQL 优化原理(一)
  10. 语言相关系数显著性_ggplot2 绘制带星号和 Pvalue 值的相关系数热图