本文主要介绍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框架分析的更多相关文章

  1. Python实例---抽屉后台框架分析

    1.1. 抽屉框架分析 --登陆注册分析 1.2. 前台获取form表单补充知识: <!DOCTYPE html> <html lang="en"> < ...

  2. python三大web框架Django,Flask,Flask,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架

    Python几种主流框架 从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python We ...

  3. Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架

    Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...

  4. Android/Linux下CGroup框架分析及其使用

    1 cgroup介绍 CGroup是control group的简称,它为Linux kernel提供一种任务聚集和划分的机制,可以限制.记录.隔离进程组(process groups)所使用的资源( ...

  5. Python几种主流框架

    从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Djang ...

  6. selenium-webdriver(python) (十六) --unittest 框架

    学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本.相信不少新手学习selenium 也是从IED 开始的. IDE学习参考: 菜鸟学自动化测试(一)----selen ...

  7. [python 译] 基于面向对象的分析和设计

    [python 译] 基于面向对象的分析和设计 // */ // ]]>   [python 译] 基于面向对象的分析和设计 Table of Contents 1 原文地址 2 引言 2.1 ...

  8. 【Python】【Web.py】详细解读Python的web.py框架下的application.py模块

    详细解读Python的web.py框架下的application.py模块   这篇文章主要介绍了Python的web.py框架下的application.py模块,作者深入分析了web.py的源码, ...

  9. matlab遗传算法工具箱

    转自http://blog.sina.com.cn/s/blog_5ebcc0240101pnrj.html matlab遗传算法工具箱函数及实例讲解 (2014-01-10 13:03:57)   ...

随机推荐

  1. KVOController原理解析

    1.使用类似动态代理的模式和消息派发中枢模式实现整个架构: 2.使用NSMapTable和NSHashTable进行切面信息的增删查维护:主要用于去重和查看是否存在. 实现方式 消息流 KVOCont ...

  2. ssh无密码登陆远程机,pssh轻批量工具

    #B(client)--------A(g_server)#A:   ssh-keygen -t rsa (g_server)#B:    scp -P 58422 root@g_server_ip: ...

  3. BZOJ2435:[NOI2011]道路修建 (差分)

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...

  4. 安卓预览报错 Failed to load AppCompat ActionBar with unknown error

    报错信息 : Render ProblemFailed to load AppCompat ActionBar with unknown error. Failed to instantiate on ...

  5. AE-----合成

    AE-----合成 大纲:

  6. 【Agile123】Automated Test in Agile

    https://www.infoq.com/articles/thoughts-on-test-automation-in-agile Start Small Balance the cost vs. ...

  7. Elasticsearch之如何合理分配索引分片

    大多数ElasticSearch用户在创建索引时通用会问的一个重要问题是:我需要创建多少个分片? 在本文中, 我将介绍在分片分配时的一些权衡以及不同设置带来的性能影响. 如果想搞清晰你的分片策略以及如 ...

  8. SRcnn:神经网络重建图片的开山之作

    % ========================================================================= % Test code for Super-Re ...

  9. Flume性能测试报告(翻译Flume官方wiki报告)

    因使用flume的时候总是会对其性能有所调研,网上找的要么就是自测的这里找到一份官方wiki的测试报告供大家参考 https://cwiki.apache.org/confluence/display ...

  10. Python学习笔记系列——高阶函数(map/reduce)

    一.map #变量可以指向函数,函数的参数能接受变量,那么一个函数就可以接受另一个函数作为参数,这种函数被称之为高阶函数 def add(x,y,f): return f(x)+f(y) print( ...