Python genetic algorithm framework DEAP-Creating Types

Time:2019-9-10

DEAP is a python genetic algorithm framework, here is its introduction. DEAP documentation
Today, I’ll sort out an overview of DEAP and get a general idea of its process. Initial study, not rigorous, only as their own memorandum learning notes.

This tutorial shows how types are created using the creator and initialized using the toolbox.
This tutorial demonstrates the use ofcreatorCreate Types and UsetoolboxInitialization.

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.
FitnessClass providesweightAttributes. Minimizing problems using negative valuesweightMaximize the problem with positive values. For example, use the following examplecreatorA single-objective minimal problem is created, named as: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:
As described in library, thisweightAttributes must betupleForm given, if the creation of a multi-objective problem, you can refer to the following examples:

creator.create("FitnessMulti", base.Fitness, weights=(-1.0, 1.0))

Obviously, it is a multi-objective, a minimum, a maximum.
An example of where the weights can be useful is in the crowding distance sort made in the NSGA-II selection algorithm.
weightThe use of attributes can be referred to in the example of congestion distance sorting using NSGA-II selection algorithm.

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
The first individual is a simple list of floating-point numbers. To create such an individual, we need to create oneIndividualClass, usingcreatorThis will be the standard of inheritance.listType, and have onefitnessAttributes.

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.
Newly introducedregisterThe method requires at least two parameters. An alias, a function assigned to that alias. When called, subsequent parameters are passed into the function. Therefore, the preceding code istoolboxTwo nickname functions are created inattr_floatandindividual

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.
The first function points torandom.randomFunction. The second pointinitRepeatFunction, DirectioncreatorFixed its container parameters.IndividualThe function parameter of the class isattr_floatFunction, and its repetition parameterIND_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.
Now calltoolbox.individual()Functions will be called with fixed parametersinitRepeat()To return to fullcreator.Individual(byIND_SIZEIt consists of floating-point numbers and has a maximized single target fitness attribute.

slightly

Population

Populations are much like individuals. Instead of being initialized with attributes, they are filled with individuals, strategies or particles.
Crossline individuals of the population. It’s not like an individual. There are many.attributePopulation is composed of many individuals, strategies and particles.

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.
The backpack population is the most commonly used type. It does not have a specific order, although lists are usually used. Because the knapsack has no specific properties, it does not need any special classes. This population is used directly.toolboxandinitRepeat()Function to initialize.

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.
calltoolbox.population()A complete list of populations will be returned, providing that the number of repetitions is a parameter of the population function. The following example produces 100 individuals:

toolbox.population(n=100)

Grid (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.
Grid population is an example of a special structured population. Neighboring individuals have a direct impact on each other. Individuals are distributed in a grid (each grid contains one individual). However, its implementation is only different from the list of knapsack populations — it consists 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.
calltoolbox.populationA population will be returned, and the individual is available using two indices. For example:pop[r][c]

Slightly.

Recommended Today

Hadoop MapReduce Spark Configuration Item

Scope of application The configuration items covered in this article are mainly for Hadoop 2.x and Spark 2.x. MapReduce Official documents https://hadoop.apache.org/doc…Lower left corner: mapred-default.xml Examples of configuration items name value description mapreduce.job.reduce.slowstart.completedmaps 0.05 Resource requests for Reduce Task will not be made until the percentage of Map Task completed reaches that value. mapreduce.output.fileoutputformat.compress false […]