1

I can't seem to figure out how to have variable length genomes in DEAP.

I have gone through the DEAP documentation and found nothing related to varibable length genomes.

1 Answers1

0

If you define your individual without specifying the n parameter, then you can create many individuals with various lengths. For instance

from deap import base, tools, creator
import random

toolbox = base.Toolbox()

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox.register("individual", tools.initRepeat, creator.Individual, random.random)

print(toolbox.individual(n=2))
print(toolbox.individual(n=5))

If you want to create a population where each individual has a different length, you can take a look at the Knapsack problem from the DEAP documentation. The main idea boils down to defining your population this way:

from deap import creator, base, tools
import random

creator.create("Fitness", base.Fitness, weights=(-1.0, 1.0))
creator.create("Individual", set, fitness=creator.Fitness)

toolbox = base.Toolbox()
toolbox.register("attr_item", random.randrange, 5)
toolbox.register("individual", tools.initRepeat, creator.Individual, 
    toolbox.attr_item, 3)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.population(n=5)
usernumber
  • 1,958
  • 1
  • 21
  • 58