本文整理汇总了Python中deap.tools.mutFlipBit方法的典型用法代码示例。如果您正苦于以下问题:Python tools.mutFlipBit方法的具体用法?Python tools.mutFlipBit怎么用?Python tools.mutFlipBit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deap.tools
的用法示例。
在下文中一共展示了tools.mutFlipBit方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: geneticAlgorithm
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutFlipBit [as 别名]
def geneticAlgorithm(X, y, n_population, n_generation):
"""
Deap global variables
Initialize variables to use eaSimple
"""
# create individual
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# create toolbox
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat,
creator.Individual, toolbox.attr_bool, len(X.columns))
toolbox.register("population", tools.initRepeat, list,
toolbox.individual)
toolbox.register("evaluate", getFitness, X=X, y=y)
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
# initialize parameters
pop = toolbox.population(n=n_population)
hof = tools.HallOfFame(n_population * n_generation)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("min", np.min)
stats.register("max", np.max)
# genetic algorithm
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2,
ngen=n_generation, stats=stats, halloffame=hof,
verbose=True)
# return hall of fame
return hof
示例2: mutate
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutFlipBit [as 别名]
def mutate(self, individual, probability):
if self.method == 'Shuffle':
return tools.mutShuffleIndexes(individual, probability)[0]
elif self.method == 'Flipbit':
return tools.mutFlipBit(individual, probability)[0]
示例3: create_toolbox
# 需要导入模块: from deap import tools [as 别名]
# 或者: from deap.tools import mutFlipBit [as 别名]
def create_toolbox(num_bits):
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
# Initialize the toolbox
toolbox = base.Toolbox()
# Generate attributes
toolbox.register("attr_bool", random.randint, 0, 1)
# Initialize structures
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_bool, num_bits)
# Define the population to be a list of individuals
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# Register the evaluation operator
toolbox.register("evaluate", eval_func)
# Register the crossover operator
toolbox.register("mate", tools.cxTwoPoint)
# Register a mutation operator
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
# Operator for selecting individuals for breeding
toolbox.register("select", tools.selTournament, tournsize=3)
return toolbox