2. Short One Max Genetic Algorithm

The short one max genetic algorithm example is very similar to one max example. The only difference is that it makes use of the algorithms module that implements some basic evolutionary algorithms. The initialization are the same so we will skip this phase. The algorithms impemented use specific functions from the toolbox, in this case evaluate(), mate(), mutate() and select() must be registered.

toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoints)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)

The toolbox is then passed to the algorithm and the algorithm uses the registered function.

pop = toolbox.population()
hof = tools.HallOfFame(1)

algorithms.eaSimple(toolbox, pop, cxpb=0.5, mutpb=0.2, ngen=40, halloffame=hof)

The short GA One max example makes use of a HallOfFame in order to keep track of the best individual to appear in the evolution (it keeps it even in the case it extinguishes). All algorithms from the algorithms module do take a halloffame argument that gets updated after every evaluation section.======= The short GA One max example makes use of a HallOfFame in order to keep track of the best individual to appear in the evolution (it keeps them even in the case they extinguish). All algorithms from the eap.algorithms module do take a halloffame argument that gets updated after every evaluation section of the basic algorithms.

Previous topic

1. One Max Genetic Algorithm

Next topic

3. Individual Inheriting from Set

This Page