Getting the error: AttributeError: 'list' object has no attribute 'fitness' when I try to initialize an individual

69 views
Skip to first unread message

Akhilnandh Ramesh

unread,
Jan 19, 2024, 7:12:56 AMJan 19
to deap-users
Hi there,

I am using DEAP to perform NSGA-3 for scheduling. The individuals are an array of the form:
ind_1=[ind_sub_part1, ind_sub_part2], where ind_sub_part1=[1,2,3,4,5,2,...] (ie a list by itself)
and ind_sub_part2=[2,3,4,5,6,...,...] (ie a list by itself.
However I get the following error while I am performing VarAnd procedure:

del offspring[i - 1].fitness.values, offspring[i].fitness.values
AttributeError: 'list' object has no attribute 'fitness'


The pop is initialized as follows:


creator.create("FitnessMin",base.Fitness,weights=(-1.0,-1.0)) 
creator.create("Individual", list,fitness=creator.FitnessMin) 
creator.create("Individual", list,fitness=creator.FitnessMin) 
toolbox=base.Toolbox() #initiating the toolbox.
ref_points=tools.uniform_reference_points(nobj,p) 
ind_size=1
toolbox.register("individual",generate_individuals , creator.Individual)
toolbox.register("individual", tools.initIterate, creator.Individual,generate_individuals) 
toolbox.register("population", tools.initRepeat, list, toolbox.individual) 

toolbox.register("evaluate", decode_op1) #This is the fitness function
toolbox.register("mate", crossover) #Registering the function for mating
toolbox.register("mutate",mutation) #Registering the function for mutation
toolbox.register("select",select_nsga3, ref_points=ref_points) #Registering the function for selection
pop=toolbox.population(n=mu) #Creating a parent population, which is based on the individual defined in 'population' and 'individual'



Each individual is initialized through a generate_individuals function.
which is defined with the header def generate_individuals ()
                     
Any suggestions on how to overcome this error will be great.

Akhilnandh Ramesh

unread,
Jan 19, 2024, 2:44:21 PMJan 19
to deap-users
An  update on this error:

It turns out that when offspring is created through toolbox.clone, the fitness attribute is not copied over.
offspring = [toolbox.clone(ind) for ind in population]

François-Michel De Rainville

unread,
Jan 19, 2024, 2:51:14 PMJan 19
to deap-users
It is copied over, clone is the standard 
copy.deepcopy.

--
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 on the web visit https://groups.google.com/d/msgid/deap-users/c8b1d329-d1da-4e87-b7bd-072b11ff5104n%40googlegroups.com.

Akhilnandh Ramesh

unread,
Jan 19, 2024, 3:02:58 PMJan 19
to deap-users
Hi.

Thank you so much for your prompt response. I figured out where the error is. It is with my crossover function.
Apparently after toolbox.mate is called, fitness functions are not getting carried out.
This is the error that I seem to get
Error:
 del offspring[i - 1].fitness.values, offspring[i].fitness.values
AttributeError: 'list' object has no attribute 'fitness'


and this is after:

offspring[i - 1], offspring[i] = toolbox.mate(offspring[i - 1],
offspring[i])

François-Michel De Rainville

unread,
Jan 19, 2024, 3:31:37 PMJan 19
to deap-users
It happens, ensure that nothing is creating a new type. For example, if you have something like list(ind) in your crossover or seq = ind[a:b], these things will create lists, not individuals.

Cheers,

Akhilnandh Ramesh

unread,
Jan 19, 2024, 5:07:39 PMJan 19
to deap-users

Yes, seems like this is what has exactly happened to me. Would you have any idea on how to overcome this problem? I tried to do icls(offspring1) and icls(offspring2) while returning the offsprings.

François-Michel De Rainville

unread,
Jan 19, 2024, 5:12:47 PMJan 19
to deap-users
Reusing the original individuals is the trick. Something like ind[a:b] = attributes should do.

Akhilnandh Ramesh

unread,
Jan 19, 2024, 5:37:04 PMJan 19
to deap-users
Thanks much.
I have tried to do something like what you have suggested, but the issue continues:
Any help with the code can be greatly useful:
The individual consists of two parts: ms1 and os1 of same length (len_chromosome). Hence, the part 0: len_chromosome is used for ms and part from len_chromosome to 2*len_chromosome is used for os.

ms1=[]
os1=[]
for i in range(0,len_chromosome):
ms1.append(ind1[i])
for i in range(len_chromosome,2*len_chromosome):
os1.append(ind1[i])
ms2 = []
os2 = []
for i in range(0, len_chromosome):
ms2.append(ind1[i])
for i in range(len_chromosome, 2 * len_chromosome):
os2.append(ind1[i])

mschild1=[]
mschild2=[]

cutting_point = random.randint(0, len(ms1) - 1)
# crossover for ms part
mschild1 = ms1[:cutting_point] + ms2[cutting_point:]
mschild2 = ms2[:cutting_point] + ms1[cutting_point:]


#Crossover for the second half: OS part
parent1=copy.deepcopy(os1)
parent2=copy.deepcopy(os2)
cutting_point1 = random.randint(0, len(parent1) - 1)
cutting_point2 = random.randint(0, len(parent1) - 1)

# Ensure cutting_point1 is smaller than cutting_point2
if cutting_point1 > cutting_point2:
cutting_point1, cutting_point2 = cutting_point2, cutting_point1
print(cutting_point1, cutting_point2)
# Initialize child1 and child2 as copies of parent1 and parent2
oschild1 = copy.deepcopy(parent1)
oschild2 = copy.deepcopy(parent2)

# Remove genes between cutting_point1 and cutting_point2 from child1 and child2
del oschild1[cutting_point1:cutting_point2]
del oschild2[cutting_point1:cutting_point2]


child1=mschild1+oschild1
child2=mschild2+oschild2

for i in range(len(ind1)):
print("Ind1,",ind1[i],"Child1,",child1[i])
print("Ind2,", ind2[i], "Child1,", child2[i])
ind1[i]=child1[i]
ind2[i]=child2[i]


return ind1,ind2

Akhilnandh Ramesh

unread,
Jan 19, 2024, 8:46:47 PMJan 19
to deap-users
And in particular,

this seems to affect varAnd procedure and not VaroR procedure

François-Michel De Rainville

unread,
Jan 20, 2024, 12:20:07 PMJan 20
to deap-users
I suggest you debug checking types and presence of fitness attribute all along your crossover.

Reply all
Reply to author
Forward
0 new messages