Using the Toolbox class
The second mechanism offered by the DEAP framework is the base.Toolbox
class. Toolbox
is used as a container for functions (or operators) and enables us to create new operators by aliasing and customizing existing functions.
For example, suppose we have a function, sumOfTwo()
, defined as follows:
def sumOfTwo(a, b): return a + b
Using toolbox
, we can now create a new operator, incrementByFive()
, which
customizes the sumOfTwo()
function, as follows:
from deap import base toolbox= base.Toolbox() toolbox.register("incrementByFive", sumOfTwo, b=5)
The first argument that’s passed to the register()
toolbox function is the desired name (or alias) for the new operator. The second argument is the existing function to be customized. Then, each additional (optional) argument is automatically passed to the customized function whenever we call the new operator. For example, look at this definition:
toolbox.incrementByFive...