本文整理匯總了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