You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently trying to do some mutations over a list of str. The way I set up deap is with the following code:
creator.create("Fitness", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.Fitness)
toolbox=base.Toolbox()
toolbox.register("attr_str", fuzzer.random_individual)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_str, n=len(types)) # len(types) can be any int from 1 to 3toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", fitness.get_fitness_of_f(globals()[func]))
toolbox.register("mate", deap_crossover)
toolbox.register("mutate", deap_mutate)
toolbox.register("select", tools.selTournament, tournsize=TOURNSIZE)
foriinrange(REPS):
population=toolbox.population(n=NPOP)
algorithms.eaSimple(population, toolbox, CXPROB, MUPROB, NGEN, verbose=False)
As you can notice, my Individual is a list, that can contain any number (from 1 to 3) of strings.
Additionally, let me give you the deap_mutate function, as it will be useful for later:
fuzzer.mutate takes in a list of strings and returns a copy of that list, with a one element mutated.
For some reason, my code kept crashing after a mutation was triggered. I was really confused why so I started digging in the code of the library, and found this: inside the varAnd function, with this piece of code that handles the mutations:
My question is related to that little , in the third line, namely the offspring[i],. Why is it there? I am very confused by it's existence and am probably missing something.
The issue it gave me was the following, given that my individuals are a lists, when the list has one element, that element would get unpacked (because of that ,) and cause an error saying
del offspring[i].fitness.values
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'fitness'
If however my list contains more than one element, then the error would come from the line where the mutation happen, saying
offspring[i], = toolbox.mutate(offspring[i])
^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 1)
Now, there are two fixes I found for this issue. One on my side and one on the side of the library. The one on my side is simply to wrap the individual returned in a list, in order for the mutated individual to be unpacked correctly inside the library:
I'm not sure. But I guess the mutate function is to align with the mate function or crossover function, which returns a tuple of two variables, i.e two child individuals. Mutate only has one mutated individual, so the second returned variable is left empty, But still takes a position. I think the simplest way to solve the issue is, you make the deap_mutate function also return a tuple of two variables, and leave the second empty.
Hello,
I'm currently trying to do some mutations over a list of
str
. The way I set up deap is with the following code:As you can notice, my Individual is a list, that can contain any number (from 1 to 3) of strings.
Additionally, let me give you the
deap_mutate
function, as it will be useful for later:fuzzer.mutate
takes in a list of strings and returns a copy of that list, with a one element mutated.For some reason, my code kept crashing after a mutation was triggered. I was really confused why so I started digging in the code of the library, and found this: inside the
varAnd
function, with this piece of code that handles the mutations:My question is related to that little
,
in the third line, namely theoffspring[i],
. Why is it there? I am very confused by it's existence and am probably missing something.The issue it gave me was the following, given that my individuals are a lists, when the list has one element, that element would get unpacked (because of that
,
) and cause an error sayingIf however my list contains more than one element, then the error would come from the line where the mutation happen, saying
Now, there are two fixes I found for this issue. One on my side and one on the side of the library. The one on my side is simply to wrap the individual returned in a list, in order for the mutated individual to be unpacked correctly inside the library:
The one on the side of the library is to remove that
,
in thevarAnd
function:Hence my question, why are you unpacking the result of the mutation?
The text was updated successfully, but these errors were encountered: