Hi all,
However, going through the overview, the code breaks if I run the main function with the error:
```
Traceback (most recent call last):
File "/home/loaf/Documents/SummerResearch/deap_practice/first.py", line 61, in <module>
print(main())
^^^^^^
File "/home/loaf/Documents/SummerResearch/deap_practice/first.py", line 28, in main
ind.fitness.values = fit
^^^^^^^^^^^^^^^^^^
File "/home/loaf/Documents/SummerResearch/deap_practice/.venv/lib/python3.12/site-packages/deap/base.py", line 188, in setValues
assert len(values) == len(self.weights), "Assigned values have not the same length than fitness weights"
^^^^^^^^^^^
TypeError: object of type 'float' has no len()
```
The code is below, it's directly copied block by block from the overview, with exception of calling main.
```
from deap import base, creator
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
import random
from deap import tools
IND_SIZE = 10
toolbox = base.Toolbox()
toolbox.register("attribute", random.random)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attribute, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evaluate(individual):
return sum(individual),
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", evaluate)
def main():
pop = toolbox.population(n=50)
CXPB, MUTPB, NGEN = 0.5, 0.2, 40
# Evaluate the entire population
fitnesses = map(toolbox.evaluate, pop)
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
for g in range(NGEN):
# Select the next generation individuals
offspring = toolbox.select(pop, len(pop))
# Clone the selected individuals
offspring = map(toolbox.clone, offspring)
# Apply crossover and mutation on the offspring
for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < CXPB:
toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# The population is entirely replaced by the offspring
pop[:] = offspring
return pop
main()```
Also python version 3.12.7 and DEAP version 1.4.1 if it's relevant