'AttributeError: 'list' object has no attribute 'fitness'' Maybe offspring Doesn't have fitness

339 views
Skip to first unread message

신민철

unread,
May 30, 2022, 12:06:01 PM5/30/22
to deap-users
Thank you in advance.

I'm not familiar with both English and Deap.

I want to perform multi-purpose optimization through the NSGA-2 algorithm with 3 objective functions.

My individual is a list of lists.

And, I made an offspring using algorithms.varAnd.

'AttributeError: 'list' object has no attribute 'fitness''
I checked the Error Message.

Presumably, the initial individual and population have fitness, but
The offspring created by varAnd doesn't seem to have a fitness attribute.
It doesn't seem to be recognized as an individual, but just as a list.

I am making code by modifying deap's nsga-2 example.
Here is a part of my code and the result is:

Any help would be greatly appreciated.


creator.create("FitnessMax", base.Fitness, weights=(1.0, 1.0, -1.0))
creator.create("Individual", list , fitness=creator.FitnessMax)

toolbox.register("Sensors_IND", Sensors_IND)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.Sensors_IND)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.individual()
Out
[[4, 290, 616], [1, 270, 378], [0, 300, 363], [1, 210, 31], [1, 40, 366]]
toolbox.population(2)
Out
[[[0, 140, 802], [0, 90, 464], [3, 280, 518], [3, 70, 16], [2, 170, 356]],
 [[4, 140, 330], [1, 50, 579], [3, 60, 889], [0, 20, 988], [4, 40, 978]]]

offspring = algorithms.varAnd(pop, toolbox, CXPB, MUTPB)


I also added a mutation function to see if there might be a problem with crossbreeding or mutation.
def mutSet(individual):
    empty = []
    for item in individual:
        List_item = list(item)
        if random.random() < 0.5:
            mut_place = item[2] + random.randint(-100,100)
            if mut_place < 0 :
                mut_place = mut_place + len(items)
            elif mut_place > len(items)-1 :
                mut_place = mut_place - len(items)
            else :
                pass
            item[2] = mut_place
        if random.random() < 0.5:
            mut_ang = item[1] + 10*random.randint(-9,9)
            if mut_ang < 0 :
                mut_ang = mut_ang + 360
            elif mut_ang > 360 :
                mut_ang = mut_ang - 360
            else :
                pass
            item[1] = mut_ang
            else :
            pass
       
        empty = empty + [item[0]]
    random.shuffle(empty)
    for k in range (len(individual)):
        individual[k][0] = empty[k]
   
    return individual
toolbox.register("mutate", mutSet)

Derek Tishler

unread,
May 30, 2022, 12:43:58 PM5/30/22
to deap-users
You will need to return a tuple from your operators, much like with the fitness evaluation function.

You can base your code on examples from the source code of DEAP:
https://github.com/DEAP/deap/blob/master/deap/tools/mutation.py

But focus on the the return line (note the comma):
return individual, 

신민철

unread,
Jun 29, 2022, 11:02:06 PM6/29/22
to deap-users
Thank you very much for your favor.

As you mentioned, I made my operators return tuples.

However, I still got an error message from the main function.
The error message is:

<ipython-input-194-167168659d53> in main()
     57
     58 # Select the next generation population from parents and offspring
---> 59 pop = toolbox.select(pop + offspring, MU)
     60
     61 # Compile statistics about the new population

~\Anaconda3\lib\site-packages\deap\tools\emo.py in selNSGA2(individuals, k, nd)
     32 """
     33 if nd == 'standard':
---> 34 pareto_fronts = sortNondominated(individuals, k)
     35 elif nd == 'log':
     36 pareto_fronts = sortLogNondominated(individuals, k)

~\Anaconda3\lib\site-packages\deap\tools\emo.py in sortNondominated(individuals, k, first_front_only)
     74 map_fit_ind = defaultdict(list)
     75 for ind in individuals:
---> 76 map_fit_ind[ind.fitness].append(ind)
     77 fits = list(map_fit_ind.keys())
     78

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

The returns of my operators have this form:


toolbox.individual()
=>tuple of list
([2, 90, 910],
 [4, 180, 1235],
 [1, 20, 537],
 [0, 10, 762],
 [1, 280, 568],
 [0, 170, 183])

toolbox.population(5)
=> list of tuples of list
  [([2, 180, 698],
  [0, 190, 1248],
  [4, 100, 1107],
  [3, 190, 807],
  [4, 130, 234],
  [2, 50, 835]),
 ([3, 190, 1002],
  [0, 140, 561],
  [1, 30, 665],
  [3, 190, 834],
  [3, 40, 759],
  [2, 100, 1130]),
 ([3, 250, 1015],
  [1, 90, 781],
  [3, 310, 428],
  [0, 210, 1151],
  [4, 50, 182],
  [1, 100, 92])]

I made the individual a tuple,
My population is a list.
creator.create("Individual", tuple , fitness=creator.FitnessMax)

creator.create("FitnessMax", base.Fitness, weights=(1.0, 1.0, -1.0))
creator.create("Individual", list , fitness=creator.FitnessMax)

  Again, any help would be greatly appreciated.
2022년 5월 31일 화요일 오전 1시 43분 58초 UTC+9에 lstr...@gmail.com님이 작성:
Reply all
Reply to author
Forward
0 new messages