DEAP是一個python遺傳演算法框架,這裡是它的簡介。DEAP documentation
今天整理一下DEAP的概覽,大體瞭解一下它的流程。初學,不嚴謹,僅作為自己的備忘學習筆記。
This tutorial shows how types are created using the creator and initialized using the toolbox.
這個教程展示的是使用creator
建立型別和使用toolbox
初始化。
Fitness(適應度)
The provided Fitness class is an abstract class that needs a weights attribute in order to be functional. A minimizing fitness is built using negatives weights, while a maximizing fitness has positive weights. For example, the following line creates, in the creator, a ready to use single objective minimizing fitness named FitnessMin.Fitness
類提供了weight
屬性。最小化問題使用負值的的weight
,最大化問題用正值。例如下面的例子,利用creator
,建立了一個單目標最小問題,命名為:FitnessMin
。
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
As specified in the Fitness documentation, the weights attribute must be a tuple so that multi-objective and single objective fitnesses can be treated the same way. A FitnessMulti would be created the same way but using:
正如在library中敘述,這個weight
屬性必須是以tuple
的形式給出,如果建立的是多目標問題,可以參考下面例子:
creator.create("FitnessMulti", base.Fitness, weights=(-1.0, 1.0))
可見,是一個多目標,一個最小值,一個最大值。
An example of where the weights can be useful is in the crowding distance sort made in the NSGA-II selection algorithm.weight
屬性的使用可以參考使用NSGA-II選擇演算法進行擁擠距離排序的例子中。
Individual(個體)
List of Floats
The first individual created will be a simple list containing floats. In order to produce this kind of individual, we need to create an Individual class, using the creator, that will inherit from the standard list type and have a fitness attribute
第一個個體是個包含浮點數的簡單列表。為了創造這樣的個體,我們需要建立一個Individual
類,使用creator
,這個將會繼承標準list
型別,並擁有一個fitness
屬性。
import random
from deap import base
from deap import creator
from deap import tools
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
IND_SIZE=10
toolbox = base.Toolbox()
toolbox.register("attr_float", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_float, n=IND_SIZE)
The newly introduced register() method takes at least two arguments; an alias and a function assigned to this alias. Any subsequent argument is passed to the function when called (à la functools.partial()). Thus, the preceding code creates two aliases in the toolbox; attr_float and individual.
新引進的register
方法需要至少兩個引數。一個別名,一個賦予到這個別名的函式。當被呼叫時,隨後的引數都被傳進給這個函式。因此,前面的程式碼在toolbox
中建立了兩個別名函式attr_float
和individual
。
The first one redirects to the random.random() function. The second one is a shortcut to the initRepeat() function, fixing its container argument to the creator.Individual class, its func argument to the toolbox.attr_float() function, and its number of repetitions argument to IND_SIZE.
第一個函式指向random.random
函式。第二個指向initRepeat
函式,向creator
固定了它的容器引數。Individual
類的函式引數是attr_float
函式,和它的重複數引數IND_SIZE
。
Now, calling toolbox.individual() will call initRepeat() with the fixed arguments and return a complete creator.Individual composed of IND_SIZE floating point numbers with a maximizing single objective fitness attribute.
現在呼叫toolbox.individual()
函式將會使用固定引數呼叫initRepeat()
,返回完整的creator.Individual
(由IND_SIZE
個浮點陣列成,有一個最大化單目標fitness attribute)。
略
Population(種群)
Populations are much like individuals. Instead of being initialized with attributes, they are filled with individuals, strategies or particles.
種群橫線個體。它不像個體一樣,有很多attribute
,種群是由很多個體、策略、粒子組成的。
Bag
A bag population is the most commonly used type. It has no particular ordering although it is generally implemented using a list. Since the bag has no particular attribute, it does not need any special class. The population is initialized using the toolbox and the initRepeat() function directly.
揹包種群是最常使用的型別。它沒有特定的順序雖然通常使用列表來實現。因為揹包沒特定的屬性,它不需要任何特殊的類。這個種群是直接使用toolbox
和initRepeat()
函式來初始化的。
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
Calling toolbox.population() will readily return a complete population in a list, providing a number of times the repeat helper must be repeated as an argument of the population function. The following example produces a population with 100 individuals.
呼叫toolbox.population()
將會返回一個完整的列表形式的種群,提供重複次數是種群函式的引數。下面例子生產了100個個體:
toolbox.population(n=100)
Grid(網格)
A grid population is a special case of structured population where neighbouring individuals have a direct effect on each other. The individuals are distributed in the grid where each cell contains a single individual. However, its implementation only differs from the list of the bag population, in that it is composed of lists of individuals.
網格種群是一個特殊結構種群的例子,相鄰的個體對彼此有直接的影響。個體之間分佈在網格中(每個格子包含一個個體)。然而,它的實現只和和揹包種群的列表不同–它是由個體們的列表組成的。
toolbox.register("row", tools.initRepeat, list, toolbox.individual, n=N_COL)
toolbox.register("population", tools.initRepeat, list, toolbox.row, n=N_ROW)
Calling toolbox.population() will readily return a complete population where the individuals are accessible using two indices, for example popr.
呼叫toolbox.population
將會返回一個種群,個體是使用兩個索引可獲得的。例如:pop[r][c]
。
略。