摘要:打印出個(gè)體檢查它的適應(yīng)度是否有效這個(gè)個(gè)體打印出來(lái)了。這個(gè)適應(yīng)度值是通過(guò)設(shè)置值和元祖關(guān)聯(lián)。適應(yīng)度值被刪除了,因?yàn)樗鼈儾辉俸瓦@個(gè)個(gè)體相關(guān)了因?yàn)樽儺惲寺?。如上面所述,這個(gè)變異算子只是變異并且只是變異一個(gè)個(gè)體,它也不對(duì)適應(yīng)度的無(wú)效負(fù)責(zé)或者其它。
Before starting with complex algorithms, we will see some basics of DEAP. First, we will start by creating simple individuals (as seen in the Creating Types tutorial) and make them interact with each other using different operators. Afterwards, we will learn how to use the algorithms and other tools.
在開(kāi)始復(fù)雜的算法之前,我們將會(huì)看到一些基礎(chǔ)的東西。首先,我們開(kāi)始創(chuàng)建簡(jiǎn)單的個(gè)體(之前的教程中介紹了)和讓它們利用操作算子互相作用。然后,我們將學(xué)習(xí)怎么使用算法和其他工具。
First import the required modules and register the different functions required to create individuals that are lists of floats with a minimizing two objectives fitness.
一開(kāi)始導(dǎo)入需要模塊和注冊(cè)不同的函數(shù)去創(chuàng)建個(gè)體(浮點(diǎn)數(shù)列表),求解的是一個(gè)最小化二目標(biāo)適應(yīng)度問(wèn)題。
import random from deap import base from deap import creator from deap import tools IND_SIZE = 5 creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0)) creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() toolbox.register("attr_float", random.random) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=IND_SIZE)
The first individual can now be built by adding the appropriate line to the script.
第一個(gè)個(gè)體現(xiàn)在能用合適一行代碼去創(chuàng)建添加了。
ind1 = toolbox.individual()
Printing the individual ind1 and checking if its fitness is valid will give something like this
打印出個(gè)體ind1,檢查它的適應(yīng)度是否有效:
print ind1 # [0.86..., 0.27..., 0.70..., 0.03..., 0.87...] print ind1.fitness.valid # False
The individual is printed as its base class representation (here a list) and the fitness is invalid because it contains no values.
這個(gè)個(gè)體打印出來(lái)了。這個(gè)適應(yīng)度是無(wú)效的,因?yàn)闆](méi)有值。
The evaluation is the most personal part of an evolutionary algorithm, it is the only part of the library that you must write yourself. A typical evaluation function takes one individual as argument and returns its fitness as a tuple. As shown in the in the core section, a fitness is a list of floating point values and has a property valid to know if this individual shall be re-evaluated. The fitness is set by setting the values to the associated tuple. For example, the following evaluates the previously created individual ind1 and assigns its fitness to the corresponding values.
評(píng)價(jià)函數(shù)部分是評(píng)價(jià)算法中最“個(gè)人”的部分,它是庫(kù)中你必須自己寫(xiě)的一部分。一個(gè)典型的評(píng)價(jià)函數(shù)需要一個(gè)個(gè)體作為參數(shù),然后返回這個(gè)個(gè)體的fitness(以tuple格式)。在核心部分所示,一個(gè)適應(yīng)度值是一個(gè)浮點(diǎn)值的列表并有一個(gè)有效的屬性來(lái)知道這個(gè)個(gè)體是否應(yīng)當(dāng)重新評(píng)估。這個(gè)適應(yīng)度值是通過(guò)設(shè)置值和元祖關(guān)聯(lián)。舉個(gè)例子,下面的選擇評(píng)價(jià)之前創(chuàng)建的個(gè)體ind1,同時(shí),分配它的fitness給相應(yīng)的值。
(⊙o⊙)…讓我回憶起專(zhuān)業(yè)英語(yǔ)翻譯文章的感覺(jué)了,難以讀懂。硬著頭皮往下看吧。
def evaluate(individual): # Do some hard computing on the individual a = sum(individual) b = len(individual) return a, 1. / b ind1.fitness.values = evaluate(ind1) print ind1.fitness.valid # True 原來(lái)有`fitness`是True!搜嘎 print ind1.fitness # (2.73, 0.2)上面創(chuàng)建的個(gè)體IND_SIZE=5,所以這里是1/5=0.2
Dealing with single objective fitness is not different, the evaluation function must return a tuple because single-objective is treated as a special case of multi-objective.
與處理單目標(biāo)適應(yīng)度不同,這里評(píng)價(jià)函數(shù)必須返回一個(gè)元祖因?yàn)閱文繕?biāo)是作為多目標(biāo)的一個(gè)特例而已。
There is a variety of mutation operators in the deap.tools module. Each mutation has its own characteristics and may be applied to different types of individuals. Be careful to read the documentation of the selected operator in order to avoid undesirable behaviour.
在deap.tools module模塊那兒有很多變異操作算子。每個(gè)變異算子有它自己的特征,可以應(yīng)用到不同類(lèi)型的個(gè)體上。仔細(xì)閱讀選擇算子的文檔,從而避免無(wú)法理解的行為。
The general rule for mutation operators is that they only mutate, this means that an independent copy must be made prior to mutating the individual if the original individual has to be kept or is a reference to another individual (see the selection operator).
變異算子的總體規(guī)則就是他們只是變異,這意味著一個(gè)獨(dú)立的副本必須在變異個(gè)體操作之前,如果原始個(gè)體必須要保存或者是另外個(gè)體的參考(看選擇算子)。
In order to apply a mutation (here a gaussian mutation) on the individual ind1, simply apply the desired function.
為了在個(gè)體ind1上施行變異(這里是高斯變異),簡(jiǎn)單應(yīng)用了所需的函數(shù)。
mutant = toolbox.clone(ind1) ind2, = tools.mutGaussian(mutant, mu=0.0, sigma=0.2, indpb=0.2) del mutant.fitness.values
The fitness’ values are deleted because they’re not related to the individual anymore. As stated above, the mutation does mutate and only mutate an individual it is neither responsible of invalidating the fitness nor anything else. The following shows that ind2 and mutant are in fact the same individual.
適應(yīng)度值被刪除了,因?yàn)樗鼈儾辉俸瓦@個(gè)個(gè)體相關(guān)了(因?yàn)樽儺惲寺铮。。。?。如上面所述,這個(gè)變異算子只是變異并且只是變異一個(gè)個(gè)體,它也不對(duì)適應(yīng)度的無(wú)效負(fù)責(zé)或者其它。下面展示了ind2,變異算子事實(shí)上同樣的個(gè)體。(不是很明白這里)
print ind2 is mutant # True print mutant is ind1 # FalseCrossover(交叉)
The second kind of operator that we will present is the crossover operator. There is a variety of crossover operators in the deap.tools module. Each crossover has its own characteristics and may be applied to different types of individuals. Be careful to read the documentation of the selected operator in order to avoid undesirable behaviour.
我們將要展示的第二種操作算子是交叉算子。在deap.tools module模塊中有很多交叉算子。每種交叉算子有它自己的特性,可能適用于不同類(lèi)型的個(gè)體。仔細(xì)閱讀選擇算子的文檔從而避免無(wú)法理解的的行為。
The general rule for crossover operators is that they only mate individuals, this means that an independent copies must be made prior to mating the individuals if the original individuals have to be kept or are references to other individuals (see the selection operator).
交叉算子的總體規(guī)則是它們值負(fù)責(zé)交叉?zhèn)€體,這意味著在交叉?zhèn)€體之前需獨(dú)立的副本,如果原始個(gè)體不得不被保存或者作為其他個(gè)體的參考(看選擇算子)。
Lets apply a crossover operation to produce the two children that are cloned beforehand.
讓我們施行交叉算子來(lái)產(chǎn)生兩個(gè)子孫(事先已經(jīng)被“克隆”了)。
child1, child2 = [toolbox.clone(ind) for ind in (ind1, ind2)] tools.cxBlend(child1, child2, 0.5) del child1.fitness.values del child2.fitness.valuesSelection(選擇)
Selection is made among a population by the selection operators that are available in the deap.tools module. The selection operator usually takes as first argument an iterable container of individuals and the number of individuals to select. It returns a list containing the references to the selected individuals. The selection is made as follow.
選擇是通過(guò)選擇算子在種群中操作的,它可以在deap.tools module模塊中獲得。這個(gè)選擇算子操作通常第一個(gè)參數(shù)是個(gè)體們的可迭代容器,以及要選擇個(gè)體的個(gè)數(shù)。它返回的是選擇個(gè)體們的應(yīng)用列表。選擇算子操作示例如下:
selected = tools.selBest([child1, child2], 2) print child1 in selected # True
Usually duplication of the entire population will be made after selection or before variation.
通常在選擇或者變異之后,會(huì)做整個(gè)種群的副本。
selected = toolbox.select(population, LAMBDA) offspring = [toolbox.clone(ind) for ind in selected]Using the Toolbox(使用工具箱)
The toolbox is intended to contain all the evolutionary tools, from the object initializers to the evaluation operator. It allows easy configuration of each algorithm. The toolbox has basically two methods, register() and unregister(), that are used to add or remove tools from the toolbox.
工具箱是來(lái)包含所有進(jìn)化工具的,從對(duì)象的初始化到評(píng)價(jià)操作算子。它允許每種算法的配置。工具箱有兩個(gè)基本的方法,register()和unregister(),這個(gè)用來(lái)從工具箱中增加或者移除工具的。
This part of the tutorial will focus on registration of the evolutionary tools in the toolbox rather than the initialization tools. The usual names for the evolutionary tools are mate(), mutate(), evaluate() and select(), however, any name can be registered as long as it is unique. Here is how they are registered in the toolbox.
教程的這部分將會(huì)專(zhuān)注于工具箱中進(jìn)化工具的注冊(cè)而不是初始化工具。進(jìn)化工具通常的名字是mate(),mutate(),evaluate(),select(),然而,任何名字都是可以被注冊(cè)的,只要它是獨(dú)一無(wú)二的。這里展示了它們是怎么被注冊(cè)進(jìn)工具箱的:
from deap import base from deap import tools toolbox = base.Toolbox() def evaluateInd(individual): # Do some computation return result, toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2) toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("evaluate", evaluateInd)
Using the toolbox for registering tools helps keeping the rest of the algorithms independent from the operator set. Using this scheme makes it very easy to locate and change any tool in the toolbox if needed.
使用工具箱注冊(cè)工具有助于保持其余算法獨(dú)立于操作符的集合。使用這種方案使得它很容易定位和改變工具箱中的任何工具。
When building evolutionary algorithms the toolbox is used to contain the operators, which are called using their generic name. For example, here is a very simple generational evolutionary algorithm.
當(dāng)建立進(jìn)化算法時(shí),工具箱是用來(lái)容納這個(gè)操作算子的,就是被稱為使用它們的通用名字。例如,這里是一個(gè)非常簡(jiǎn)單的進(jìn)化算法。
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 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 # Apply mutation on the offspring 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 = toolbox.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
This is a complete algorithm. It is generic enough to accept any kind of individual and any operator, as long as the operators are suitable for the chosen individual type. As shown in the last example, the usage of the toolbox allows to write algorithms that are as close as possible to pseudo code. Now it is up to you to write and experiment on your own.
這是一個(gè)完整的算法。它是足夠通用的來(lái)接收各種個(gè)體和任何操作算子,只要操作算子是是適合選擇的個(gè)體類(lèi)型。正如最后一個(gè)例子所示,工具箱的使用允許寫(xiě)算法(和偽代碼盡可能相近)?,F(xiàn)在取決于你自己寫(xiě)、實(shí)驗(yàn)?zāi)愕乃惴ā?/p>
Tool Decoration(工具裝飾)
Tool decoration is a very powerful feature that helps to control very precise things during an evolution without changing anything in the algorithm or operators. A decorator is a wrapper that is called instead of a function. It is asked to make some initialization and termination work before and after the actual function is called.
工具裝飾是一個(gè)非常強(qiáng)大的特性,在進(jìn)化過(guò)程中它有助于控制非常精確的東西而不需要改變算法或者算子的其他東西。一個(gè)裝飾器被稱為一個(gè)包裝器而不是一個(gè)函數(shù)。它是在一些初始化和終止工作的時(shí)候被訪問(wèn),在實(shí)際函數(shù)調(diào)用之前或者之后。
For example, in the case of a constrained domain, one can apply a decorator to the mutation and crossover in order to keep any individual from being out-of-bound. The following defines a decorator that checks if any attribute in the list is out-of-bound and clips it if this is the case. The decorator is defined using three functions in order to receive the min and max arguments. Whenever the mutation or crossover is called, bounds will be checked on the resulting individuals.
例如,在約束域的例子里,一個(gè)能對(duì)變異和交叉引用裝飾器為了避免個(gè)體出界,如果出界就去除它。裝飾器使用三個(gè)函數(shù)來(lái)定義為了接受最小和最大的參數(shù)。變異或交叉操作無(wú)論什么時(shí)候被調(diào)用,都會(huì)對(duì)結(jié)果個(gè)體們進(jìn)行范圍檢查。
╮(╯▽╰)╭無(wú)奈,不明白啊,加油
def checkBounds(min, max): def decorator(func): def wrapper(*args, **kargs): offspring = func(*args, **kargs) for child in offspring: for i in xrange(len(child)): if child[i] > max: child[i] = max elif child[i] < min: child[i] = min return offspring return wrapper return decorator toolbox.register("mate", tools.cxBlend, alpha=0.2) toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=2) toolbox.decorate("mate", checkBounds(MIN, MAX)) toolbox.decorate("mutate", checkBounds(MIN, MAX))
This will work on crossover and mutation because both return a tuple of individuals. The mutation is often considered to return a single individual but again like for the evaluation, the single individual case is a special case of the multiple individual case.
這將會(huì)作用在交叉和變異算子上因?yàn)閮烧叨挤祷匾粋€(gè)個(gè)體們的元祖。變異通常被認(rèn)為返回一個(gè)單一個(gè)體但是卻像評(píng)估算子一樣,單一個(gè)體的例子是多目標(biāo)的一個(gè)特殊例子。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/37734.html
摘要:是一個(gè)遺傳算法框架,這里是它的簡(jiǎn)介。就是這個(gè)評(píng)價(jià)功能函數(shù)得自己寫(xiě),其實(shí),也就是適應(yīng)度函數(shù)了就是注意評(píng)價(jià)函數(shù)返回值必須是可迭代的。完整性的目的我們將開(kāi)發(fā)完整的分代算法。對(duì)于同一個(gè)種子值的輸入,之后產(chǎn)生的隨機(jī)數(shù)序列也一樣。 DEAP-Overview DEAP是一個(gè)python遺傳算法框架,這里是它的簡(jiǎn)介。DEAP documentation今天整理一下DEAP的概覽,大體了解一下它的流程...
摘要:本文介紹了包括等在內(nèi)的一系列編程語(yǔ)言的深度學(xué)習(xí)庫(kù)。是一個(gè)部署在編程語(yǔ)言中的深度學(xué)習(xí)工具包,用于通過(guò)高效的算法處理大型文本集。是公司基于開(kāi)發(fā)的深度學(xué)習(xí)框架。是第一個(gè)為和編寫(xiě)的消費(fèi)級(jí)開(kāi)元分布式深度學(xué)習(xí)庫(kù)。 本文介紹了包括 Python、Java、Haskell等在內(nèi)的一系列編程語(yǔ)言的深度學(xué)習(xí)庫(kù)。PythonTheano 是一種用于使用數(shù)列來(lái)定義和評(píng)估數(shù)學(xué)表達(dá)的 Python 庫(kù)。它可以讓 Pyt...
摘要:本文介紹了包括等在內(nèi)的一系列編程語(yǔ)言的深度學(xué)習(xí)庫(kù)。是一個(gè)在中用于帶有神經(jīng)網(wǎng)絡(luò)的深度學(xué)習(xí)的庫(kù),它通過(guò)使用帶有的加速。是一個(gè)用和開(kāi)發(fā)的深度學(xué)習(xí)庫(kù)。是第一個(gè)為和編寫(xiě)的消費(fèi)級(jí)開(kāi)元分布式深度學(xué)習(xí)庫(kù)。它帶有豐富的作為機(jī)器學(xué)習(xí)庫(kù)一部分的深度學(xué)習(xí)庫(kù)。 本文介紹了包括 Python、Java、Haskell等在內(nèi)的一系列編程語(yǔ)言的深度學(xué)習(xí)庫(kù)。PythonTheano 是一種用于使用數(shù)列來(lái)定義和評(píng)估數(shù)學(xué)表達(dá)的 ...
閱讀 3057·2021-11-19 11:31
閱讀 3148·2021-09-02 15:15
閱讀 1001·2019-08-29 17:22
閱讀 1072·2019-08-29 16:38
閱讀 2475·2019-08-26 13:56
閱讀 844·2019-08-26 12:16
閱讀 1448·2019-08-26 11:29
閱讀 941·2019-08-26 10:12