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
map_fit_ind[ind.fitness].append(ind)
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):
random.seed(64)
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