Maybe an issue in the overview part of the documentation

15 views
Skip to first unread message

Alan Teesdale

unread,
Nov 13, 2024, 11:03:56 AM11/13/24
to deap-users
Hi all,

I've just started investigating using DEAP for some university work I'm doing and so I thought I'd go through the overview in the documentation (https://deap.readthedocs.io/en/master/overview.html)

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

François-Michel De Rainville

unread,
Nov 13, 2024, 11:11:16 AM11/13/24
to deap-users

I did a fresh install and did not get the same error. However there is error further in the code, where the offspring should be list(map(...)) and not just map. You can copy the original code from https://github.com/DEAP/deap/blob/master/doc/code/tutorials/part_1/1_where_to_start.py.

I corrected the code


--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/deap-users/618a96ea-5181-4e8e-8c99-5990e0e57c5an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages