Python遗传算法工具箱DEAP框架分析
本文主要介绍python遗传算法工具箱DEAP的实现。先介绍deap的如何使用,再深入介绍deap的框架实现,以及遗传算法的各种实现算法。
代码可以参考 https://github.com/sumatrae/deap
下面是使用deap求解TSP的实现:
import array
import random
import json import numpy from deap import algorithms
from deap import base
from deap import creator
from deap import tools # gr*.json contains the distance map in list of list style in JSON format
# Optimal solutions are : gr17 = 2085, gr24 = 1272, gr120 = 6942
with open("tsp/gr17.json", "r") as tsp_data:
tsp = json.load(tsp_data) distance_map = tsp["DistanceMatrix"]
IND_SIZE = tsp["TourSize"] creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", array.array, typecode='i', fitness=creator.FitnessMin) toolbox = base.Toolbox() # Attribute generator
toolbox.register("indices", random.sample, range(IND_SIZE), IND_SIZE) # Structure initializers
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.indices)
toolbox.register("population", tools.initRepeat, list, toolbox.individual) def evalTSP(individual):
distance = distance_map[individual[-1]][individual[0]]
for gene1, gene2 in zip(individual[0:-1], individual[1:]):
distance += distance_map[gene1][gene2]
return distance, toolbox.register("mate", tools.cxPartialyMatched)
toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", evalTSP) def main():
random.seed(169) pop = toolbox.population(n=300) hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max) algorithms.eaSimple(pop, toolbox, 0.7, 0.2, 40, stats=stats,
halloffame=hof) return pop, stats, hofn'g'x
该例子中使用SGA实现求解TSP问题,可以看到deap提供了灵活的插件化算法解决方案。deap的思想实现就是通过灵活的插件化思想,同时再框架中提供了丰富的算法实现,你可以用堆积木一样的方式,轻松的实现你的遗传算法处理程序。同时你也可以使用自己实现的算法模块,只需要注册框架就可以。框架通过函数导入的方法,通过register可以将你的算法函数注册到运行环境中。
未完待续。
Python遗传算法工具箱DEAP框架分析的更多相关文章
- Python实例---抽屉后台框架分析
1.1. 抽屉框架分析 --登陆注册分析 1.2. 前台获取form表单补充知识: <!DOCTYPE html> <html lang="en"> < ...
- python三大web框架Django,Flask,Flask,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Python几种主流框架 从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python We ...
- Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...
- Android/Linux下CGroup框架分析及其使用
1 cgroup介绍 CGroup是control group的简称,它为Linux kernel提供一种任务聚集和划分的机制,可以限制.记录.隔离进程组(process groups)所使用的资源( ...
- Python几种主流框架
从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Djang ...
- selenium-webdriver(python) (十六) --unittest 框架
学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本.相信不少新手学习selenium 也是从IED 开始的. IDE学习参考: 菜鸟学自动化测试(一)----selen ...
- [python 译] 基于面向对象的分析和设计
[python 译] 基于面向对象的分析和设计 // */ // ]]> [python 译] 基于面向对象的分析和设计 Table of Contents 1 原文地址 2 引言 2.1 ...
- 【Python】【Web.py】详细解读Python的web.py框架下的application.py模块
详细解读Python的web.py框架下的application.py模块 这篇文章主要介绍了Python的web.py框架下的application.py模块,作者深入分析了web.py的源码, ...
- matlab遗传算法工具箱
转自http://blog.sina.com.cn/s/blog_5ebcc0240101pnrj.html matlab遗传算法工具箱函数及实例讲解 (2014-01-10 13:03:57) ...
随机推荐
- Javascript (ECMAScript5) 的细节和违反直觉的地方
记录在学习Javascript (ECMAScript5) 中的一些与其他语言的不同之处,本文会不断更新. 里面的知识可能并不太适合有一定经验的Javascript程序员,仅仅但不限于给初学者阅读. ...
- vagrant之道(即其工作流程)
原文http://mitchellh.com/the-tao-of-vagrant The Tao of Vagrant 在安装vagrant或了解它如何工作之前,了解vagrant在实际工作环境中的 ...
- VC++中的__super::
在学习别人的代码时,发现了一个__super,第一感觉很像java中的super,或者C#中的base网上查了一下发现作用差不多,都是指父类.C++本身没有__super,这是visual c++的扩 ...
- 论文笔记 Beyond Part Models: Person Retrieval with Refined Part Pooling_ECCV_2018
1. 摘要 使用part-feature 能够起到更好的效果,不过这个需要我们很好地定位part的位置. 本文中作者集中考虑part内部的一致性,提出了 part-based convolutiona ...
- (转)Jmeter http请求之content-type
原文传送门:http://www.cnblogs.com/dinghanhua/p/5646435.html 第一部分:目前工作中涉及到的content-type 有三种: content-type: ...
- Linux 内存使用方法详细解析
我是一名程序员,那么我在这里以一个程序员的角度来讲解Linux内存的使用. 一提到内存管理,我们头脑中闪出的两个概念,就是虚拟内存,与物理内存.这两个概念主要来自于linux内核的支持. Linux在 ...
- “C++动态绑定”相关问题探讨
一.相关问题: 1. 基类.派生类的构造和析构顺序 2. 基类.派生类中virtual的取舍 二.测试代码: #include <iostream> class A { public: A ...
- 求助:将以下ES5格式代码转换为ES6格式!!!
function Slider(id){ //属性 // 1. 通过id获取元素对象(大盒子) this.bigBox = document.getElementById(i ...
- 在Qtlabel中显示数字十六进制和十进制都可以
ui->label_6->setText(QString::number(table_test[0]<<8 | table_test[1]));这样子就可以把十六进制的数转换为 ...
- 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别
这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...