日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Deap: python中的遗传算法工具箱

發(fā)布時間:2025/3/21 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Deap: python中的遗传算法工具箱 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Overview 程序概覽

官方文檔:http://deap.readthedocs.io/en/master/index.html
1. Types : 選擇你要解決的問題類型,確定要求解的問題個數(shù),最大值還是最小值
2. Initialization : 初始化基因編碼位數(shù),初始值,等基本信息
3. Operators : 操作,設(shè)計(jì)evaluate函數(shù),在工具箱中注冊參數(shù)信息:交叉,變異,保留個體,評價函數(shù)
4. Algorithm : 設(shè)計(jì)main函數(shù),確定參數(shù)并運(yùn)行得到結(jié)果

Types

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

Initialization

import random from deap import toolsIND_SIZE = 10 # 種群數(shù)toolbox = base.Toolbox() toolbox.register("attribute", random.random) # 調(diào)用randon.random為每一個基因編碼編碼創(chuàng)建 隨機(jī)初始值 也就是范圍[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

高斯變異:

這種變異的方法就是,產(chǎn)生一個服從高斯分布的隨機(jī)數(shù),取代原先基因中的實(shí)數(shù)數(shù)值。這個算法產(chǎn)生的隨機(jī)數(shù),數(shù)學(xué)期望當(dāng)為當(dāng)前基因的實(shí)數(shù)數(shù)值。
一個模擬產(chǎn)生的算法是,產(chǎn)生6個服從U(0,1)的隨機(jī)數(shù),以他們的數(shù)學(xué)期望作為高斯分布隨機(jī)數(shù)的近似。

mutate方法

  • 這個函數(shù)適用于輸入個體的平均值和標(biāo)準(zhǔn)差的高斯突變

  • mu:python中基于平均值的高斯變異

  • sigma:python中基于標(biāo)準(zhǔn)差的高斯變異

  • indpb:每個屬性的獨(dú)立變異概率

mate : 交叉

select : 選擇保留的最佳個體

evaluate : 選擇評價函數(shù),要注意返回值的地方最后面要多加一個逗號

Algorithms 計(jì)算程序

也就是設(shè)計(jì)主程序的地方,按照官網(wǎng)給的模式,我們要早此處設(shè)計(jì)其他參數(shù),并設(shè)計(jì)迭代和取值的代碼部分,并返回我們所需要的值.

# 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

要注意的地方就是,官網(wǎng)中給出的Overview代碼中有一行代碼是錯誤的,需要把一個數(shù)據(jù)類型(map)轉(zhuǎn)換為list.

輸出結(jié)果

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 : 最佳個體在經(jīng)過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,)求第一個參數(shù)的最大值,求第二個參數(shù)的最小值 creator.create("Individual", list, fitness=creator.FitnessMin)# Initialization import random from deap import toolsIND_SIZE = 10 # 種群數(shù)toolbox = base.Toolbox() toolbox.register("attribute", random.random) # 調(diào)用randon.random為每一個基因編碼編碼創(chuàng)建 隨機(jī)初始值 也就是范圍[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)

總結(jié)

以上是生活随笔為你收集整理的Deap: python中的遗传算法工具箱的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。