成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

python遺傳算法(GA)DEAP-Overview學習摘要

draveness / 3615人閱讀

摘要:是一個遺傳算法框架,這里是它的簡介。就是這個評價功能函數(shù)得自己寫,其實,也就是適應度函數(shù)了就是注意評價函數(shù)返回值必須是可迭代的。完整性的目的我們將開發(fā)完整的分代算法。對于同一個種子值的輸入,之后產(chǎn)生的隨機數(shù)序列也一樣。

DEAP-Overview

DEAP是一個python遺傳算法框架,這里是它的簡介。DEAP documentation
今天整理一下DEAP的概覽,大體了解一下它的流程。初學,不嚴謹,僅作為自己的備忘學習筆記。

一. Types

The first thing to do is to think of the appropriate type for your problem.This is done with the creator module.
第一件事就是思考你問題的合適類型(是什么樣的優(yōu)化問題,最小值?最大值?單目標?多目標?)。在creator模塊中可以實現(xiàn)。

For example, the following creates a FitnessMin class for a minimization problem and an Individual class that is derived from a list with a fitness attribute set to the just created fitness.
例如,下面為一個最小化問題創(chuàng)建一個FitnessMin類和個體類,它來自于剛剛創(chuàng)建的適應度帶有fitness屬性值集合的列表。

from deap import base, creator
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)

That’s it. More on creating types can be found in the Creating Types tutorial.
更多關(guān)于創(chuàng)建類型的問題可以在Creating Types tutorial查看。

二. Initialization

Once the types are created you need to fill them with sometimes random values, sometime guessed ones. Again, DEAP provides an easy mechanism to do just that.
一旦這些類型被創(chuàng)建,你就需要用一些隨機的值來填充它們,有時是猜測值。同樣的, DEAP極大的提供了這樣的一個簡單的機制來做這件事兒。

The Toolbox is a container for tools of all sorts including initializers that can do what is needed of them. The following takes on the last lines of code to create the initializers for individuals containing random floating point numbers and for a population that contains them.
toolbox是一個工具容器,可用來包含各種“初始化體”(可以做它們需要的事兒)。以下是以代碼來創(chuàng)建包含隨機浮點數(shù)的初始化和個人人口包含他們最后的線。下面最后幾行代碼用來創(chuàng)建“初始化體”,為包含那些隨機值的“個體”和包含它們的種群。

import random
from deap import tools

IND_SIZE = 10

toolbox = base.Toolbox()
toolbox.register("attribute", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
                 toolbox.attribute, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)    #是吧,創(chuàng)建的individual放入了population中

This creates functions to initialize populations from individuals that are themselves initialized with random float numbers. The functions are registered in the toolbox with their default arguments under the given name.
這將創(chuàng)建函數(shù)來初始化種群(來自那些用隨機浮點數(shù)初始化而成的個體們)。這些函數(shù)在給定名字的默認參數(shù)下被“注冊到”toolbox中,

register感覺這個單詞在這兒的理解可以看做是“創(chuàng)建”的意思,然后toolbox中就有了toolbox.individual。具體理解,還得看后面的詳細介紹。
toolbox貌似和function有關(guān),里邊的第一個參數(shù)就是函數(shù)名。因為在Algorithm步驟里時,出現(xiàn)了這樣的用法toolbox.population()。
More initialization methods are found in the Creating Types tutorial and the various Examples.
更多初始化的方法可以在 Creating Types tutorial和Examples中查看。

三. Operator
toolbox.register("mate", tools.cxTwoPoint)

Operators都在toolbox模塊里,要給每個算子選擇合適算法。在Operator里邊可以給每個步驟的算子分別選擇合適的算法。

In addition you must create your evaluation function. This is how it is done in DEAP.
就是這個評價功能函數(shù)得自己寫,其實,也就是適應度函數(shù)了!

Note also that fitness values must be iterable, that is why we return a tuple in the evaluate function.
就是注意評價函數(shù)返回值必須是可迭代的。

def evaluate(individual):
    return sum(individual),

toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", evaluate)
四. Algorithms

Now that everything is ready, we can start to write our own algorithm. It is usually done in a main function. For the purpose of completeness we will develop the complete generational algorithm.
現(xiàn)在一切具備,我們可以開始寫自己的算法了。通常在主函數(shù)里邊寫。完整性的目的,我們將開發(fā)完整的分代算法。

def main():
    pop = toolbox.population(n=50)
    CXPB, MUTPB, NGEN = 0.5, 0.2, 40

    # Evaluate the entire population
    fitnesses = map(toolbox.evaluate, pop)
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    for g in range(NGEN):
        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = map(toolbox.clone, offspring)

        # 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

    return pop

縱觀整個步驟,可以發(fā)現(xiàn)DEAP在實現(xiàn)GA過程中的思路一般是很清晰的。

確定Types--creator創(chuàng)建,選好解決問題的類型

初始化Initialization--toolbox注冊個體啊,種群啊等等函數(shù)。

Operator--算子選擇,交叉,變異,選擇,進化等等。每個算子都有不同的算法,可以選擇的!

Algorithms--算法就是具體將上面注冊的函數(shù)啊,等等應用結(jié)合起來,編寫流程。

例子
#    This file is part of DEAP.
#
#    DEAP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of
#    the License, or (at your option) any later version.
#
#    DEAP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with DEAP. If not, see .


#    example which maximizes the sum of a list of integers
#    each of which can be 0 or 1

import random

from deap import base
from deap import creator
from deap import tools

creator.create("FitnessMax", base.Fitness, weights=(1.0,))   #這里這個base.Fitness是干嘛的???
creator.create("Individual", list, fitness=creator.FitnessMax)    #這里的list,fitness是參數(shù),干嘛的???

toolbox = base.Toolbox()    #base是個很基本的類?。。?!看來很重要

# Attribute generator: define "attr_bool" to be an attribute ("gene")
#                      which corresponds to integers sampled uniformly
#                      from the range [0,1] (i.e. 0 or 1 with equal
#                      probability)
toolbox.register("attr_bool", random.randint, 0, 1)   #包含了0,1的隨機整數(shù)。不明白這里是干嘛的???

# Structure initializers: define "individual" to be an individual
#                         consisting of 100 "attr_bool" elements ("genes")
toolbox.register("individual", tools.initRepeat, creator.Individual,    #tools.initRepeat是干嘛的???
    toolbox.attr_bool, 100)

# define the population to be a list of "individual"s
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

# the goal ("fitness") function to be maximized    注意?。?!這里就定義了我們的適應度fitness函數(shù)啦?。。∫驗槲覀円鉀Q的就是求和問題
# 只要返回一個值給我們的這個適應度函數(shù)??!利用自帶的sum函數(shù)!
# 這里取名為evalOneMax是因為這里的適應度函數(shù)就是我們后面要用來評價的依據(jù),evaluate

def evalOneMax(individual):
    return sum(individual),

#----------
# Operator registration
#----------
# register the goal / fitness function
# 這里的toolbox register語句的理解:注冊了一個函數(shù)evaluae依據(jù)的是后面的evalOneMax 理通了!!!
toolbox.register("evaluate", evalOneMax)

# 瞧瞧,這里的交叉函數(shù),尼瑪,crossover不用,非得用個mate,還以為是華為mate呢!你看,這里的交叉算子采用的函數(shù),也是已經(jīng)停供的,可以選擇的
# register the crossover operator
toolbox.register("mate", tools.cxTwoPoint)

# register a mutation operator with a probability to
# flip each attribute/gene of 0.05
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)

# operator for selecting individuals for breeding the next
# generation: each individual of the current generation
# is replaced by the "fittest" (best) of three individuals
# drawn randomly from the current generation.
toolbox.register("select", tools.selTournament, tournsize=3)    #這里選擇的tournsize又是什么意思呢?

#----------

def main():
    random.seed(64)
    # hash(64)is used
    
    # random.seed方法的作用是給隨機數(shù)對象一個種子值,用于產(chǎn)生隨機序列。
    # 對于同一個種子值的輸入,之后產(chǎn)生的隨機數(shù)序列也一樣。
    # 通常是把時間秒數(shù)等變化值作為種子值,達到每次運行產(chǎn)生的隨機系列都不一樣
    
    # create an initial population of 300 individuals (where
    # each individual is a list of integers)
    pop = toolbox.population(n=300)    #定義了300個個體的種群?。?!

    # 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   進化運行的代數(shù)!果然,運行40代之后,就停止計算了
    CXPB, MUTPB, NGEN = 0.5, 0.2, 40
    
    print("Start of evolution")
    
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    print("  Evaluated %i individuals" % len(pop))    #這時候,pop的長度還是300呢
    
    # Begin the evolution      開始進化了哈?。。∽⒁庾⒁庾⒁?!就是一個for循環(huán)里了!40次--代數(shù)
    for g in range(NGEN):
        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))
    
        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):

            # cross two individuals with probability CXPB
            if random.random() < CXPB:
                toolbox.mate(child1, child2)

                # fitness values of the children
                # must be recalculated later
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:

            # mutate an individual with probability MUTPB
            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
        
        print("  Evaluated %i individuals" % len(invalid_ind))
        
        # The population is entirely replaced by the offspring
        pop[:] = offspring
        
        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5
        
        print("  Min %s" % min(fits))
        print("  Max %s" % max(fits))
        print("  Avg %s" % mean)
        print("  Std %s" % std)
    
    print("-- End of (successful) evolution --")
    
    best_ind = tools.selBest(pop, 1)[0]
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))

if __name__ == "__main__":
    main()

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/37740.html

相關(guān)文章

  • 利用遺傳算法優(yōu)化神經(jīng)網(wǎng)絡(luò):Uber提出深度學習訓練新方式

    摘要:和的得分均未超過右遺傳算法在也表現(xiàn)得很好。深度遺傳算法成功演化了有著萬自由參數(shù)的網(wǎng)絡(luò),這是通過一個傳統(tǒng)的進化算法演化的較大的神經(jīng)網(wǎng)絡(luò)。 Uber 涉及領(lǐng)域廣泛,其中許多領(lǐng)域都可以利用機器學習改進其運作。開發(fā)包括神經(jīng)進化在內(nèi)的各種有力的學習方法將幫助 Uber 發(fā)展更安全、更可靠的運輸方案。遺傳算法——訓練深度學習網(wǎng)絡(luò)的有力競爭者我們驚訝地發(fā)現(xiàn),通過使用我們發(fā)明的一種新技術(shù)來高效演化 DNN,...

    AlienZHOU 評論0 收藏0
  • 遺傳算法GA(Genetic Algorithm)入門知識梳理

    摘要:編碼需要將問題的解編碼成字符串的形式才能使用遺傳算法。遺傳算子遺傳算法有個最基本的操作選擇,交叉,變異。實驗發(fā)現(xiàn),比較大的種群的規(guī)模并不能優(yōu)化遺傳算法的結(jié)果。災變遺傳算法的局部搜索能力較強,但是很容易陷入局部極值。 一、遺傳算法進化論背景知識 作為遺傳算法生物背景的介紹,下面內(nèi)容了解即可: 種群(Population):生物的進化以群體的形式進行,這樣的一個群體稱為種群。 個體:組成...

    gxyz 評論0 收藏0
  • 使用JavaScript實現(xiàn)機器學習和神經(jīng)學網(wǎng)絡(luò)

    摘要:我會使用一個先進的神經(jīng)網(wǎng)絡(luò)和機器學習框架這個框架,并向你們展示如何用這個框架來實現(xiàn)光學字符辨識,模擬退火法,遺傳算法和神經(jīng)網(wǎng)絡(luò)。歐氏距離我們從歐氏距離開始談起,歐氏距離是一個非常簡單的概念,適用于不同的機器學習技術(shù)。 歡迎大家前往云+社區(qū),獲取更多騰訊海量技術(shù)實踐干貨哦~ 下載 heaton-javascript-ml.zip - 45.1 KB 基本介紹 在本文中,你會對如何使用Ja...

    tunny 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<