'list' object has no attribute 'fitness'

2,225 views
Skip to first unread message

Liz Wachs

unread,
Dec 7, 2016, 3:06:30 PM12/7/16
to deap-users
Hello,
I am still a relative novice and hope you can help me. I am trying to make a ga with a custom individual that I create in a user-defined function. The individual I create is a binary list. Unless necessary I don't post the whole function. The function does return the kind of list I need. I know that I need to integrate this with the creator function. Right now I have tried to do this as follows:

creator.create("FitnessMin", base.Fitness, weights=(-1,-1))
creator.create("Individual", list, fitness=creator.FitnessMin)

def CreateInd(icls)#modified from original version to just add icls in parens
    ...
    return individual

toolbox.register("individual", CreateInd, icls=creator.Individual)

What am I missing? I thought I had succeeded after reading much of the documentation on the site and in the questions. I am getting the above error, however when I try to run my ga. It refers me to the varOr function in the algorithms page:

  File "C:\Anaconda3\lib\site-packages\deap\algorithms.py", line 236, in varOr
    del ind.fitness.values

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

I am using Python 3.5 in case that is relevant.  

Many thanks!



Marc-André Gardner

unread,
Dec 7, 2016, 3:11:51 PM12/7/16
to deap-users
Hi Liz,

Since I don't exactly know what CreateInd is doing, I can just speculate, be here is the issue: this function returns a list, when it should return an individual. Granted, an individual is a list (by inheritance) but it also has other attributes that a list does not (in this case, the fitness attribute). You may take a look at the provided initialization functions in DEAP : https://github.com/DEAP/deap/blob/master/deap/tools/init.py

Usually, you just need to pass your list through your Individual class, like :

return icls(individual)

instead of just

return individual

But again, since I do not have all of your code, I can just assume that this will work :)

Have fun with DEAP,

Marc-André

Liz Wachs

unread,
Dec 7, 2016, 4:37:40 PM12/7/16
to deap-users
Dear Marc-Andre,

Thank you so much for responding so quickly. Unfortunately I am still a little unclear about it and having some problems. 

I am still struggling to understand your response. What does "by inheritance" mean? Is there a certain syntax for the toolbox.register() function for the individual? So the first argument in quotes is the name of a standard function for the toolbox that the main algorithms expect to find. But in the case of the individual, which I define in creator to be of type list, how can I populate the toolbox to bring in a list from a custom function and include the fitness? I am not sure how I can use the tools that I see to use inside the toolbox.register function to do what I am doing in my function.  

The reason that I made a custom function is because I want the list to have a certain structure. This is a bounded knapsack problem, so the first n entries all correspond to a particular knapsack item. Then the next k entries correspond to the next item, etc. I am using a binary representation, so it will be all zeroes and ones.

When I made the change you suggested that error stopped. Now, however, I get another error (below). Do you think this is caused by the same problem? I just get the sense that I am not using the toolbox function correctly. Any help is really appreciated.

Liz

  File "W:/My Documents/WDC.py", line 237, in GA
    pop[:] = toolbox.select(pop+offspring, 100)

  File "C:\Anaconda3\lib\site-packages\deap\tools\emo.py", line 33, in selNSGA2
    assignCrowdingDist(front)

  File "C:\Anaconda3\lib\site-packages\deap\tools\emo.py", line 123, in assignCrowdingDist
    crowd.sort(key=lambda element: element[0][i])

  File "C:\Anaconda3\lib\site-packages\deap\tools\emo.py", line 123, in <lambda>
    crowd.sort(key=lambda element: element[0][i])

IndexError: tuple index out of range

Marc-André Gardner

unread,
Dec 7, 2016, 4:49:00 PM12/7/16
to deap-users
Hi again,

Sorry, I may have not made myself really clear. Basically, DEAP creator is just a fancy (and shorter) way to do this :

class Individual(list):
    def __init__(self, ...):
        super().__init__(...)
        self.fitness = creator.FitnessMin

As you can see, Individual is inheriting from list. DEAP creator simply allows you to do this as a one-liner :


creator.create("Individual", list, fitness=creator.FitnessMin)

However, if you want to use an individual, you cannot just pass a list, you must actually pass an Individual object. In other words, if you have a "Rectangle" class inheriting from "Shape", you may pass a Rectangle object to a function accepting a Shape, but not the other way around. That was the reason for your first error.
 
Now, for the next issue, what is your evaluation function? Does it return a tuple of 2 values (your two objectives)? That might explain why you're getting this error.

Marc-André

Liz Wachs

unread,
Dec 8, 2016, 11:13:49 AM12/8/16
to deap-users
Dear Marc-Andre,
Thank you again. I was not familiar with classes and now have been reading and have a somewhat better understanding. 
My evaluation function does give a tuple of the two values I need for fitness, so am not sure why I get the index out of range. I show you the code for the evaluation function in case it is immediately obvious to you.
Liz
def evalKnapsack(individual):
    Capweight = sum(individual[0:b[0]])*items[0][0]
    EROIweight = sum(individual[0:b[0]])*items[0][1]
    valueEmis = sum(individual[0:b[0]])*items[0][2]
    valueCost = sum(individual[0:b[0]])*items[0][3]
    indexval = b[0]
    for item in range(1,SourceLength-1):
        newval=indexval+b[item]
        Capweight += sum(individual[indexval:newval]) * items[item][0]
        EROIweight += sum(individual[indexval:newval]) * items[item][1]
        valueEmis += sum(individual[indexval:newval]) * items[item][2]
        valueCost += sum(individual[indexval:newval]) * items[item][3]
        indexval=newval
#for now I am not using the EROI weight constraint in case problem is already too difficult
    if Capweight > PowerDemandtoUse:
        return 1000000, #1000000           # Ensure overweighted bags are dominated
    return  valueCost,#valueEmis   

Marc-André Gardner

unread,
Dec 8, 2016, 11:17:22 AM12/8/16
to deap-users
Hi Liz,

Well, if this is the actual code you're using, it explains the error. In Python, # is a comment starter : Python will ignore everything after a #. So when you write :

return  valueCost,#valueEmis
it is the same as writing :
return  valueCost,

And you can see that you're actually returning only one value :)
If you're not sure, just print the result of your evaluation function on a dummy individual, you'll see it right away.

If this is not the problem, let us know it, we will look for another issue.

Marc-André

Liz Wachs

unread,
Dec 8, 2016, 11:20:08 AM12/8/16
to deap-...@googlegroups.com
Sorry, I do know that. I changed it trying to make it just single-objective to see if that would solve the problem. It didn't.

--
You received this message because you are subscribed to a topic in the Google Groups "deap-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/deap-users/22g1kyrpKy8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to deap-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Liz Wachs

unread,
Dec 8, 2016, 3:31:20 PM12/8/16
to deap-users
Hi Marc-Andre,
Thank you again. I had been concerned that my individual had been created wrong. Now with your explanation I have finally understood why your correction worked and why the individual is created correctly. The error I was getting was from my algorithm, not from my functions, so I have found some ways to make it work now. I really appreciate your help.
Liz

Akhilnandh Ramesh

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

I am facing the same error. I am not sure how to correct this error. Some help will be good.

Reply all
Reply to author
Forward
0 new messages