Skip to content
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

Closed
Mehdiasr opened this issue Oct 17, 2023 · 8 comments
Closed

AttributeError: 'list' object has no attribute 'fitness' #728

Mehdiasr opened this issue Oct 17, 2023 · 8 comments

Comments

@Mehdiasr
Copy link

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.

@fmder
Copy link
Member

fmder commented Oct 17, 2023 via email

@Mehdiasr
Copy link
Author

Mehdiasr commented Oct 17, 2023

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

@fmder
Copy link
Member

fmder commented Oct 17, 2023 via email

@fmder
Copy link
Member

fmder commented Oct 17, 2023 via email

@Mehdiasr
Copy link
Author

Mehdiasr commented Oct 17, 2023

it is because my individual itself is a list so mutate of it is going to be a list as well.

@fmder
Copy link
Member

fmder commented Oct 17, 2023 via email

@fengyangsun
Copy link

fengyangsun commented Oct 17, 2023

Try to revise the last 1 lines of mutation to:
individual[:] = mutated_individual[:]
del individual.fitness.values
return individual

or simply something like:
mutated_individual = creator.Individual(mutated_individual)
I prefer the first one and it should work.

Same thing for crossover:
new_individual1 = deepcopy(individual1)
new_individual2=deepcopy(individual2)
:
.
new_individual1[:] = individual1[:crossover_point] + individual2[crossover_point:]
new_individual2[:] = individual2[:crossover_point] + individual1[crossover_point:]

Hopefully it should work.
If not, you may try to something like
new_individual1 = creator.Individual(new_individual1)

@Mehdiasr
Copy link
Author

Great! it works. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants