亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

python遺傳算法(GA)DEAP-Overview學(xué)習(xí)摘要

draveness / 3747人閱讀

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

DEAP-Overview

DEAP是一個(gè)python遺傳算法框架,這里是它的簡(jiǎn)介。DEAP documentation
今天整理一下DEAP的概覽,大體了解一下它的流程。初學(xué),不嚴(yán)謹(jǐn),僅作為自己的備忘學(xué)習(xí)筆記。

一. Types

The first thing to do is to think of the appropriate type for your problem.This is done with the creator module.
第一件事就是思考你問(wèn)題的合適類型(是什么樣的優(yōu)化問(wèn)題,最小值?最大值?單目標(biāo)?多目標(biāo)?)。在creator模塊中可以實(shí)現(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.
例如,下面為一個(gè)最小化問(wèn)題創(chuàng)建一個(gè)FitnessMin類和個(gè)體類,它來(lái)自于剛剛創(chuàng)建的適應(yī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)建類型的問(wèn)題可以在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)建,你就需要用一些隨機(jī)的值來(lái)填充它們,有時(shí)是猜測(cè)值。同樣的, DEAP極大的提供了這樣的一個(gè)簡(jiǎn)單的機(jī)制來(lái)做這件事兒。

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是一個(gè)工具容器,可用來(lái)包含各種“初始化體”(可以做它們需要的事兒)。以下是以代碼來(lái)創(chuàng)建包含隨機(jī)浮點(diǎn)數(shù)的初始化和個(gè)人人口包含他們最后的線。下面最后幾行代碼用來(lái)創(chuàng)建“初始化體”,為包含那些隨機(jī)值的“個(gè)體”和包含它們的種群。

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ù)來(lái)初始化種群(來(lái)自那些用隨機(jī)浮點(diǎn)數(shù)初始化而成的個(gè)體們)。這些函數(shù)在給定名字的默認(rèn)參數(shù)下被“注冊(cè)到”toolbox中,

register感覺這個(gè)單詞在這兒的理解可以看做是“創(chuàng)建”的意思,然后toolbox中就有了toolbox.individual。具體理解,還得看后面的詳細(xì)介紹。
toolbox貌似和function有關(guān),里邊的第一個(gè)參數(shù)就是函數(shù)名。因?yàn)樵?b>Algorithm步驟里時(shí),出現(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模塊里,要給每個(gè)算子選擇合適算法。在Operator里邊可以給每個(gè)步驟的算子分別選擇合適的算法。

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

Note also that fitness values must be iterable, that is why we return a tuple in the evaluate function.
就是注意評(píng)價(jià)函數(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

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

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

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

Operator--算子選擇,交叉,變異,選擇,進(jìn)化等等。每個(gè)算子都有不同的算法,可以選擇的!

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

例子
#    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,))   #這里這個(gè)base.Fitness是干嘛的???
creator.create("Individual", list, fitness=creator.FitnessMax)    #這里的list,fitness是參數(shù),干嘛的???

toolbox = base.Toolbox()    #base是個(gè)很基本的類啊?。?!看來(lái)很重要

# 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的隨機(jī)整數(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    注意?。?!這里就定義了我們的適應(yīng)度f(wàn)itness函數(shù)啦?。。∫?yàn)槲覀円鉀Q的就是求和問(wèn)題
# 只要返回一個(gè)值給我們的這個(gè)適應(yīng)度函數(shù)??!利用自帶的sum函數(shù)!
# 這里取名為evalOneMax是因?yàn)檫@里的適應(yīng)度函數(shù)就是我們后面要用來(lái)評(píng)價(jià)的依據(jù),evaluate

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

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

# 瞧瞧,這里的交叉函數(shù),尼瑪,crossover不用,非得用個(gè)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方法的作用是給隨機(jī)數(shù)對(duì)象一個(gè)種子值,用于產(chǎn)生隨機(jī)序列。
    # 對(duì)于同一個(gè)種子值的輸入,之后產(chǎn)生的隨機(jī)數(shù)序列也一樣。
    # 通常是把時(shí)間秒數(shù)等變化值作為種子值,達(dá)到每次運(yùn)行產(chǎn)生的隨機(jī)系列都不一樣
    
    # create an initial population of 300 individuals (where
    # each individual is a list of integers)
    pop = toolbox.population(n=300)    #定義了300個(gè)個(gè)體的種群?。?!

    # 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   進(jìn)化運(yùn)行的代數(shù)!果然,運(yùn)行40代之后,就停止計(jì)算了
    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))    #這時(shí)候,pop的長(zhǎng)度還是300呢
    
    # Begin the evolution      開始進(jìn)化了哈!??!注意注意注意!就是一個(gè)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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/37740.html

相關(guān)文章

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

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

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

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

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

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

    tunny 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<