TypeError: unhashable type: 'Individual' when using np.ndarray as an Indiviual

Skip to first unread message

Abhijeet Pise

Oct 7, 2018, 12:11:03 PM10/7/18
to deap-users
My code works for one generation gives o/p:
gen    nevals    avg                                std                                min                                max                           
0      50        [1.10038469e+05 8.96000000e+00]    [4.76634927e+04 1.09471457e+00]    [3.74872295e+04 6.00000000e+00]    [2.46606563e+05 1.00000000e+01]

however afterwards fails with following exception:
Traceback (most recent call last):
   File "/usr/local/lib/python2.7/dist-packages/deap/algorithms.py", line 331, in eaMuPlusLambda
    population[:] = toolbox.select(population + offspring, mu)
  File "/usr/local/lib/python2.7/dist-packages/deap/tools/emo.py", line 33, in selNSGA2
    pareto_fronts = sortNondominated(individuals, k)
  File "/usr/local/lib/python2.7/dist-packages/deap/tools/emo.py", line 74, in sortNondominated
  File "/usr/local/lib/python2.7/dist-packages/deap/base.py", line 227, in __hash__
    return hash(self.wvalues)
TypeError: unhashable type: 'Individual'
here is my code :
creator.create("Fitness", base.Fitness, weights=(-1.0, -1.0))
creator.create("Individual", np.ndarray, fitness=creator.Fitness)

self.toolbox = base.Toolbox()

self.toolbox.register("migrate", self.migration)
self.toolbox.register("individual", tools.initIterate, creator.Individual, self.toolbox.migrate)

self.toolbox.register("population", tools.initRepeat, list, self.toolbox.individual)

self.toolbox.register("evaluate", self.evaluate)
self.toolbox.register("mate", self.crossover)
self.toolbox.register("mutate", self.mutate)
self.toolbox.register("select", tools.selNSGA2)

def migration(self):

temp_allocations = np.array(self.past_allocations, copy=True)
for i in range(self.edge_cnt):
temp_rand = random.randrange(self.edge_cnt)
temp_allocations[i] &= 0
temp_allocations[i][temp_rand] = 1

return temp_allocations

def balance(self):

GEN_CNT = 50
MU = 50
LAMBDA = 100
CXPB = 0.7
MUTPB = 0.2

pop = self.toolbox.population(n=MU)

hof = tools.HallOfFame(1, similar=np.array_equal)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)

algorithms.eaMuPlusLambda(pop, self.toolbox, MU, LAMBDA, CXPB, MUTPB, GEN_CNT, stats, halloffame=hof)

return pop, stats, hof

François-Michel De Rainville

Oct 7, 2018, 12:13:40 PM10/7/18
to deap-users
It looks like your evaluation function is somehow returning an individual.

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.
For more options, visit https://groups.google.com/d/optout.

Abhijeet Pise

Oct 7, 2018, 1:07:18 PM10/7/18
to deap-...@googlegroups.com
Thanks for the suggestion, but, my evaluation function is not returning individual, but just the 2 values, whose stats are calculated at the end of 1st generation, as seen in the o/p.
however, Can you provide me some pointers in what direction should I go ahead, debugging it may be.
Also, what does this error mean?
Can writing my own hash function help? If yes, what could be a good way to start?
And does this exception has to do something with np.ndarray, which is 2-dimensional?
Thank you.

François-Michel De Rainville

Oct 7, 2018, 2:20:17 PM10/7/18
to deap-users
The error says that the wvalues of the fitness are of type Individual. Your code is doing something similar to

ind.fitness.values = ind

That is the only explanation.

The first generation does only evaluation and statistics. Those two operations are valid even if computed on individuals because individuals are iterable. That is why the error occurs only at the second generation.

Abhijeet Pise

Oct 8, 2018, 1:06:09 AM10/8/18
to deap-...@googlegroups.com
Thank you for the clarification of problem. It works now. It was not very intuitive, however, I figured it out. And I was unknowingly returning something of type 'Individual'.
Great help, Thanks.

David Valera Laborda

Dec 9, 2023, 7:41:43 AM12/9/23
to deap-users

I have the same problem but I have not understand what the solution is. ¿Could you help me?

François-Michel De Rainville

Dec 9, 2023, 8:31:02 AM12/9/23
to deap-...@googlegroups.com
The fitness function was returning somehow something of type individual. Adding a break point in your application will confirm if it is the case for you.

Le 9 déc. 2023 à 07:41, David Valera Laborda <labordava...@gmail.com> a écrit :

Reply all
Reply to author
0 new messages