#740·deap

cxSimulatedBinaryBounded 为不同 eta 类型抛出异常

作者: eirirlar创建于 2024年2月16日更新于 2024年8月7日

The following code fails with the Exception: TypeError: '<=' not supported between instances of 'float' and 'complex' The Exception occurs in the mate function 'cxSimulatedBinaryBounded' where on some occasions beta becomes negative causing alpha to become complex and fail division. I would have expected either proper documentation of the acceptable eta range (maybe in relation to low and up), or a repair of this bug. import random from deap import base, creator, tools, algorithms # Define the problem as a maximization problem creator.create("FitnessMax", base.Fitness, weights=(1., 1.)) creator.create("Individual", list, fitness=creator.FitnessMax) # Define the problem-specific functions def eval_individual(individual): a, b, c, d, e, f = individual return (a + b) ** 2, (c + d) ** 2 # Define genetic operators toolbox = base.Toolbox() toolbox.register("attr_float", random.uniform, 0., 1.) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=6) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("evaluate", eval_individual) toolbox.register("mate", tools.cxSimulatedBinaryBounded, eta=0.9, low=0., up=1.) toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2) toolbox.register("select", tools.selTournament, tournsize=3) def main(): population_size = 50 generations = 20 population = toolbox.population(n=population_size) CXPB, MUTPB = 0.7, 0.2 print("Start of Evolution") # Evaluate the entire population fitnesses = list(map(toolbox.evaluate, population)) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit print(" Evaluated %i individuals" % len(population)) for gen in range(generations): print("-- Generation %i --" % gen) # Select the next generation individuals offspring = toolbox.select(population, len(population)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: # TODO mutate takes data out of range 0-1 toolbox.mutate(mutant, sigma=0.1) del mutant.fitness.values # Evaluate offspring individuals fitnesses = list(map(toolbox.evaluate, offspring)) for ind, fit in zip(offspring, fitnesses): ind.fitness.values = fit # Replace the old population by the offspring population[:] = offspring # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in population] length = len(fits) mean = sum(fits) / length min = min(fits) max = max(fits) print(" Min: %f, Max: %f, Mean: %f" % (min, max, mean))