本文整理汇总了Python中deap.creator.FitnessMax方法的典型用法代码示例。如果您正苦于以下问题:Python creator.FitnessMax方法的具体用法?Python creator.FitnessMax怎么用?Python creator.FitnessMax使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类deap.creator
的用法示例。
在下文中一共展示了creator.FitnessMax方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: geneticAlgorithm
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMax [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: create_toolbox
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMax [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
示例3: create_toolbox
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMax [as 别名]
def create_toolbox():
global robot, pset
pset = gp.PrimitiveSet("MAIN", 0)
pset.addPrimitive(robot.if_target_ahead, 2)
pset.addPrimitive(Prog().prog2, 2)
pset.addPrimitive(Prog().prog3, 3)
pset.addTerminal(robot.move_forward)
pset.addTerminal(robot.turn_left)
pset.addTerminal(robot.turn_right)
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("expr_init", gp.genFull, pset=pset, min_=1, max_=2)
# Structure initializers
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr_init)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", eval_func)
toolbox.register("select", tools.selTournament, tournsize=7)
toolbox.register("mate", gp.cxOnePoint)
toolbox.register("expr_mut", gp.genFull, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
return toolbox
示例4: setUp
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMax [as 别名]
def setUp(self):
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("IndList", list, fitness=creator.FitnessMax)
creator.create("IndArray", array.array, typecode='f', fitness=creator.FitnessMax)
creator.create("IndNDArray", numpy.ndarray, typecode='f', fitness=creator.FitnessMax)
creator.create("IndTree", gp.PrimitiveTree, fitness=creator.FitnessMax)
self.toolbox = base.Toolbox()
self.toolbox.register("func", func)
self.toolbox.register("lambda_func", lambda: "True")
示例5: tearDown
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMax [as 别名]
def tearDown(self):
del creator.FitnessMax
del creator.IndList
del creator.IndArray
del creator.IndNDArray
del creator.IndTree
示例6: test_pickle_fitness
# 需要导入模块: from deap import creator [as 别名]
# 或者: from deap.creator import FitnessMax [as 别名]
def test_pickle_fitness(self):
fitness = creator.FitnessMax()
fitness.values = (1.0,)
fitness_s = pickle.dumps(fitness)
fitness_l = pickle.loads(fitness_s)
self.assertEqual(fitness, fitness_l, "Unpickled fitness != pickled fitness")