1

I have a DEAP toolbox setup

import random
from deap import base, tools, creator, algorithms

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, n=2)

toolbox.register("population", tools.initRepeat, list, toolbox.individual)

I want to force the value of certain individuals in the population. For this, I would like to create individuals that have a given genome and add them to the population

pop = toolbox.population(n=10)
new_individual = ?
pop.append(new_individual)

How do I define the new individual? Doing new_individual = toolbox.individual([.1, .2]) doesn't work. Nor does new_individual = toolbox.individual(); new_individual.values = [.1, .2]. If I do new_individual = [.1, .2], then it no longer has the right type (list rather than deap.creator.Individual.

usernumber
  • 1,958
  • 1
  • 21
  • 58

1 Answers1

0

One way is the following: new_individual = creator.Individual(individual_values)

Boilerplate example (for context):

from deap import base, creator, tools
import numpy as np


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

creator.create("Individual", list, fitness=creator.FitnessMax)

toolbox = base.Toolbox()

toolbox.register('evaluate', lambda x: fitness_fn(x, **fitness_fn_kwargs))

toolbox.register("attr_int", np.random.randint, low = 0, high = nvals)

toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_int, n = npars)

toolbox.register("population", tools.initRepeat, list, toolbox.individual)

pop = toolbox.population(n=pop_size)

replace the values of one individual:

pop[0] = creator.Individual(10 * [0])