-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AttributeError: 'list' object has no attribute 'fitness' #728
Comments
Verify that your crossover and mutation return Individuals? Often a small
mistake will make them return lists.
Le mar. 17 oct. 2023, 11 h 30, Darkestofdayss ***@***.***> a
écrit :
… Hello, the individual format I'm working with is ['TGCCG', 'ATGTT']. I
successfully created this format using toolbox.register("attr_int",
init_individual, seq_length=5) and toolbox.register("individual",
tools.initRepeat, creator.Individual, toolbox.attr_int, n=2). However, the
issue arises when handling the offspring. The current format of the
offspring is [['TGCCG', 'ATGTT'], ['CCTCC', 'GCATG'], ['CATAA', 'TCGGT'],
['CATAA', 'TCGGT'], ['CACGA', 'CGTGT']]. The problem occurs with the line
offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb), resulting
in the error AttributeError: 'list' object has no attribute 'fitness'. I
have cross-checked my code with the Deap framework guidelines, and
everything seems to be properly aligned. However, I am unable to discern
why this error is occurring. Thanks for your help.
—
Reply to this email directly, view it on GitHub
<#728>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHKXQSNRAHR62BSI3643JTX72QDFAVCNFSM6AAAAAA6EBUTF2VHI2DSMVQWIX3LMV43ASLTON2WKOZRHE2DONZQG4ZDMMY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Here are my mutation and crossover function that returns individuals Also in this format ['TGCCG', 'ATGTT']:
|
Your mutation returns a list. Try to print its type just before returning.
Le mar. 17 oct. 2023, 13 h 00, Darkestofdayss ***@***.***> a
écrit :
… Here are my mutation and crossover function that returns individuals Also
in this format ['TGCCG', 'ATGTT']:
def custom_mutate_individual(individual, indpb):
"""
Mutate sequences within an individual with a probability of indpb.
"""
mutated_individual = []
for sequence in individual:
mutated_sequence = ""
for nucleotide in sequence:
if random.random() < indpb:
# Perform a mutation
possible_mutations = ['A', 'T', 'G', 'C']
possible_mutations.remove(nucleotide) # Exclude the current nucleotide
mutated_nucleotide = random.choice(possible_mutations)
mutated_sequence += mutated_nucleotide
else:
# No mutation
mutated_sequence += nucleotide
mutated_individual.append(mutated_sequence)
return mutated_individual
def one_point_crossover(individual1, individual2):
"""
Perform one-point crossover on two individuals.
"""
# Choose a random crossover point within the range of the shortest sequence
min_length = min(len(individual1), len(individual2))
crossover_point = random.randint(1, min_length - 1)
# Perform the crossover
new_individual1 = individual1[:crossover_point] + individual2[crossover_point:]
new_individual2 = individual2[:crossover_point] + individual1[crossover_point:]
return new_individual1, new_individual2
—
Reply to this email directly, view it on GitHub
<#728 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHKXQRWGMNGSFQSA6N2BF3X722SDAVCNFSM6AAAAAA6EBUTF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRWHAYTSMRZHE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Plus it does not return a tuple of one individual.
Le mar. 17 oct. 2023, 13 h 02, François-Michel De Rainville <
***@***.***> a écrit :
… Your mutation returns a list. Try to print its type just before returning.
Le mar. 17 oct. 2023, 13 h 00, Darkestofdayss ***@***.***>
a écrit :
> Here are my mutation and crossover function that returns individuals Also
> in this format ['TGCCG', 'ATGTT']:
> def custom_mutate_individual(individual, indpb):
> """
> Mutate sequences within an individual with a probability of indpb.
> """
> mutated_individual = []
> for sequence in individual:
> mutated_sequence = ""
> for nucleotide in sequence:
> if random.random() < indpb:
> # Perform a mutation
> possible_mutations = ['A', 'T', 'G', 'C']
> possible_mutations.remove(nucleotide) # Exclude the current nucleotide
> mutated_nucleotide = random.choice(possible_mutations)
> mutated_sequence += mutated_nucleotide
> else:
> # No mutation
> mutated_sequence += nucleotide
> mutated_individual.append(mutated_sequence)
> return mutated_individual
>
> def one_point_crossover(individual1, individual2):
> """
> Perform one-point crossover on two individuals.
> """
> # Choose a random crossover point within the range of the shortest
> sequence
> min_length = min(len(individual1), len(individual2))
> crossover_point = random.randint(1, min_length - 1)
>
> # Perform the crossover
> new_individual1 = individual1[:crossover_point] + individual2[crossover_point:]
> new_individual2 = individual2[:crossover_point] + individual1[crossover_point:]
>
> return new_individual1, new_individual2
>
> —
> Reply to this email directly, view it on GitHub
> <#728 (comment)>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAHKXQRWGMNGSFQSA6N2BF3X722SDAVCNFSM6AAAAAA6EBUTF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRWHAYTSMRZHE>
> .
> You are receiving this because you commented.Message ID:
> ***@***.***>
>
|
it is because my individual itself is a list so mutate of it is going to be a list as well. |
Your individual should be something inheriting from a list containing a
fitness attribute. See how the individual class is created using the
creator.
Le mar. 17 oct. 2023, 13 h 04, Darkestofdayss ***@***.***> a
écrit :
… it is because my individual itself is a list.
—
Reply to this email directly, view it on GitHub
<#728 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHKXQSC6ZQAIC5MUFMJIYDX723CJAVCNFSM6AAAAAA6EBUTF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRWHAZDMMBWGU>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Try to revise the last 1 lines of mutation to: or simply something like: Same thing for crossover: Hopefully it should work. |
Great! it works. Thanks a lot. |
Hello, the individual format I'm working with is ['TGCCG', 'ATGTT']. I successfully created this format using toolbox.register("attr_int", init_individual, seq_length=5) and toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_int, n=2). However, the issue arises when handling the offspring. The current format of the offspring is [['TGCCG', 'ATGTT'], ['CCTCC', 'GCATG'], ['CATAA', 'TCGGT'], ['CATAA', 'TCGGT'], ['CACGA', 'CGTGT']]. The problem occurs with the line offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb), resulting in the error AttributeError: 'list' object has no attribute 'fitness'. I have cross-checked my code with the Deap framework guidelines, and everything seems to be properly aligned. However, I am unable to discern why this error is occurring. Thanks for your help.
The text was updated successfully, but these errors were encountered: