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, creator

creator.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 tools

IND_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 population

fitnesses = map(toolbox.evaluate, pop)

for ind, fit in zip(pop, fitnesses):

ind.fitness.values = fit

print(" 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 individuals

offspring = toolbox.select(pop, len(pop))

# Clone the selected individuals

offspring = list(map(toolbox.clone, offspring))

# Change map to list,The documentation on the official website is wrong

# Apply crossover and mutation on the offspring

for child1, child2 in zip(offspring[::2], offspring[1::2]):

if random.random() < CXPB:

toolbox.mate(child1, child2)

del child1.fitness.values

del child2.fitness.values

for mutant in offspring:

if random.random() < MUTPB:

toolbox.mutate(mutant)

del mutant.fitness.values

# Evaluate the individuals with an invalid fitness

invalid_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 offspring

pop[:] = offspring

print("-- 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, creator

creator.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 tools

IND_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 population

fitnesses = map(toolbox.evaluate, pop)

for ind, fit in zip(pop, fitnesses):

ind.fitness.values = fit

print(" 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 individuals

offspring = toolbox.select(pop, len(pop))

# Clone the selected individuals

offspring = list(map(toolbox.clone, offspring))

# Change map to list,The documentation on the official website is wrong

# Apply crossover and mutation on the offspring

for child1, child2 in zip(offspring[::2], offspring[1::2]):

if random.random() < CXPB:

toolbox.mate(child1, child2)

del child1.fitness.values

del child2.fitness.values

for mutant in offspring:

if random.random() < MUTPB:

toolbox.mutate(mutant)

del mutant.fitness.values

# Evaluate the individuals with an invalid fitness

invalid_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 offspring

pop[:] = offspring

print("-- 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

if __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)

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

  1. 协同进化遗传算法 代码_遗传算法在组卷中的应用

    最近看到之前做的项目,是关于使用遗传算法实现智能组卷的.所以这次为大家分享遗传算法的基本原理,以及在实际场景中的应用. "揭开算法神秘的面纱" 在学习计算机相关知识时,我们必定会遇 ...

  2. 数学建模——智能优化之遗传算法详解Python代码

    数学建模--智能优化之遗传算法详解Python代码 import numpy as np import matplotlib.pyplot as plt from matplotlib import ...

  3. python遗传算法_基于Python的遗传算法特征约简(附代码)

    导言 在某些情况下,使用原始数据训练机器学习算法可能不是合适的选择.该算法在接受原始数据训练时,必须进行特征挖掘,以检测不同组之间的差异.但这需要大量的数据来自动执行特征挖掘.对于小数据集,数据科学家 ...

  4. k图着色 遗传算法的简单python伪代码

    文章目录 概述 python伪代码 概述 该问题中所使用到的部分函数与知识与局部搜索.模拟退火中的相同,参照k图着色 局部搜索算法与模拟退火算法的python实现 遗传算法的整体思路比较简单,在解决图 ...

  5. python遗传算法_带有Python的AI –遗传算法

    python遗传算法 带有Python的AI –遗传算法 (AI with Python – Genetic Algorithms) This chapter discusses Genetic Al ...

  6. Python自用工具包PyTls

    我们搞了个python的工具包PyTls. 做这件事的初衷是发生了一个星期要用python同时开发3个项目的情况,我发现了两个现象:1.有很多定制化的需求是极度高频反复重写的:2.有很多功能之前写过, ...

  7. python计算平方面积_python中求平方

    python学习(2)--变量与表达式 python学习(2)--变量与表达式 1.与java和c语言相通,python中也分为四种运算符: (1)算数运算符(如:+.-.*./); 学过java或者 ...

  8. 2转单通道 python_机器学习用Python—Python集成工具包Anaconda安装步骤

    近几年来,机器学习以及深度学习的研究异常火热,机器学习和深度学习也逐渐渗透到各个领域,当然,脑科学领域也不例外.利用机器学习和深度学习技术解决脑科学领域中的问题,成为目前最为火热的研究方向之一.而神经 ...

  9. python可视化界面工具_8个流行的 Python可视化工具包,你喜欢哪个?

    点击上方"Python编程开发",选择"星标或者置顶" 一起高效学习Python编程开发! 编译:机器之心,作者:Aaron Frederick 喜欢用 Pyt ...

最新文章

  1. 机器人操作学习系列分享:模仿学习
  2. 刚进入win7系统就提示检测到一个硬盘问题的解决方法
  3. (chap1 web网络基础) HTTP协议相关的各个协议(2)
  4. 网易云信亮相 LiveVideoStackCon 2021,解构自研大规模传输网 WE-CAN
  5. python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...
  6. 10个调试Java的技巧
  7. ASP.NET 文件上传于下载
  8. ibm台式计算机不能自动关机,IBM X3650 M3 不定时自动关机求大神
  9. Eclipse Memory Analysis分析Java运行内存情况
  10. 2020最新直播源地址下载txt_TXT追书免费小说app安卓版下载-TXT追书免费小说最新版下载v5.0.0...
  11. Python简记--函数
  12. php mysql json 转义字符_PHP转义Json里的特殊字符的函数
  13. 3534 helpmsg mysql net_MySQL出现NET HELPMSG 3534
  14. Ajax学习总结(2)——Ajax参数详解及使用场景介绍
  15. 在PyCharm命令行中使用conda数学库的方法
  16. 公司终于把我变成了一颗忠诚的螺丝钉,我再也不能离开它
  17. Ps学习(色彩范围工具使用和多边形抠图案例)
  18. Mysql从入门到入魔——9. 游标、高级SQL特性
  19. android 车载app怎么开发,Android开发智能车载App(1)---android控件及属性介绍
  20. 记一次阿里java实习生面试(失败)

热门文章

  1. 魅蓝5s的android系统版本,魅族魅蓝5S的手机系统是什么
  2. python计算输入的两个数字,try...except...判断是否输入的是数字,如果是则相加
  3. jittor和pytorch生成网络对比之dragan
  4. jittor和pytorch生成网络对比之bicyclegan
  5. React项目实践系列一
  6. 杨剑勇:物联网是一个未来概念?其实就在身边
  7. (转)程序员新人怎样在复杂代码中找 bug?
  8. Oracle 好书 02 ( 安装oracle 10g软件及创建数据库 )
  9. IP别名与多网卡绑定(RHEL5/RHEL6)
  10. 基于SSM实现保健院管理系统